`

windchill交流第三篇:Windchill中的事件监听

阅读更多

前言:常年离线在线开发实施Windchill9.0和Windchill10.0。
带人经验极其丰富,可帮公司创建Windchill开发团队。
高效,务实,认真,负责!

所有文章全部原创,均经过测试,如有不对,欢迎留言指正。
以下是Windchill中可以监听的部分事件类型,一般是按功能划分:
wt.fc.PersistenceManagerEvent
wt.vc.VersionControlServiceEvent
wt.vc.wip.WorkInProgressServiceEvent
wt.events.summary.ModifyContentSummaryEvent
以PersistenceManagerEvent为例,所有可监听的事件在该类的静态常量中,定义如下:
    
    public static final String PRE_STORE = "PRE_STORE";
    public static final String POST_STORE = "POST_STORE";
    public static final String PRE_MODIFY = "PRE_MODIFY";
    public static final String POST_MODIFY = "POST_MODIFY";
    public static final String PREPARE_FOR_MODIFICATION = "PREPARE_FOR_MODIFICATION";
    public static final String PREPARE_FOR_VIEW = "PREPARE_FOR_VIEW";
    public static final String UPDATE = "UPDATE";
    public static final String PRE_REMOVE = "PRE_REMOVE";
    public static final String REMOVE = "REMOVE";
    public static final String PRE_DELETE = "PRE_DELETE";
    public static final String POST_DELETE = "POST_DELETE";
    public static final String CLEANUP_LINK = "CLEANUP_LINK";
    public static final String COPY_LINK = "COPY_LINK";


所有的事件监听原理相同,我们以删除事件为例(对应的事件类型为PersistenceManagerEvent.POST_DELETE),假定存在名字为postdelete的文档对象,如果对此文档进行删除操作,则在提交确认的时候阻止并进行信息提示,实现该监听的方法如下:

创建一个空的接口ListenerService,这个是必须的。

package com.wclistener; 

 /**
  * 空接口,什么也不做,但这是必须的
  * <p>Description:  </p>
  * @author:
  * @time: May 22, 2010 6:08:24 PM
  * @version 1.0
  */
public interface ListenerService {

}


创建具体的实现类,这个类需要继承wt.services.StandardManager类并实现我们定义的空接口ListenerService,其他解释见类中的注释。
package com.wclistener;

import java.io.Serializable;

import wt.doc.WTDocument;
import wt.events.KeyedEvent;
import wt.events.KeyedEventListener;
import wt.fc.PersistenceManagerEvent;
import wt.services.ManagerException;
import wt.services.ServiceEventListenerAdapter;
import wt.services.StandardManager;
import wt.util.WTException;

 
/**
 * 
 * <p>Description:  </p>
 * @author:
 * @time: May 22, 2010 6:09:20 PM
 * @version 1.0
 */
public class StandardListenerService extends StandardManager implements
		ListenerService, Serializable {
	
	private static final long serialVersionUID = 1L;

	private static final String CLASSNAME = StandardListenerService.class.getName();

	private KeyedEventListener listener;

	public String getConceptualClassname() {
		return CLASSNAME;
	}

	public static StandardListenerService newStandardListenerService()
			throws WTException {
		StandardListenerService instance = new StandardListenerService();
		instance.initialize();
		return instance;
	}
	
	/**
	 * 添加需要监听的事件
	 */
	protected void performStartupProcess() throws ManagerException {
		listener = new WCListenerEventListener(this.getConceptualClassname());
		//添加需要监听的事件,可以添加多个,这里添加的是删除事件POST_DELETE和修改事件	POST_MODIFY
		getManagerService().addEventListener(listener,PersistenceManagerEvent.generateEventKey(PersistenceManagerEvent.POST_DELETE));
		getManagerService().addEventListener(listener,PersistenceManagerEvent.generateEventKey(PersistenceManagerEvent.POST_MODIFY));
	}
 
	
	/**
	 * 定义内部类,用来处理相应的事件
	 * <p>Description:  </p>
	 * @author:
	 * @time: May 18, 2010 5:53:55 PM
	 * @version 1.0
	 */
	class WCListenerEventListener extends ServiceEventListenerAdapter {
		
		private String post_delete = PersistenceManagerEvent.POST_DELETE;
		private String post_modify = PersistenceManagerEvent.POST_MODIFY;
		
		public WCListenerEventListener(String manager_name) {
			super(manager_name);
		}
		public void notifyVetoableEvent(Object eve) throws Exception {
			if (!(eve instanceof KeyedEvent))
				return;
			//获取当前触发的事件对象
			KeyedEvent event = (KeyedEvent) eve;
			//获取当前被操作的持久化对象,如部件,文档,容器等
			Object target = event.getEventTarget(); 
                           String eventType = event.getEventType();
			/************************以下代码可根据实际业务进行修改******************************/
			if(eventType.equals(post_delete)){
				System.out.println("删除操作执行!");
			}else if(eventType.equals(post_modify)){
				System.out.println("修改操作执行!"); 
			}
			if (target instanceof WTDocument&& eventType.equals(post_delete)) {
				WTDocument document = (WTDocument) target; 
				if("postdelete".equals(document.getName())){
					//抛出WTException阻止删除操作的确认并提示必要的信息
					throw new WTException("名字为postdelete的文档是不可被删除的!");
				}
			} 
		}
	}
}


代码写完,同样需要在site.xconf中配置以便让其生效,添加如下内容:
<Property name="wt.services.service.5007" overridable="true"
             targetFile="codebase/wt.properties" 
             value="com.wclistener.ListenerService/com.wclistener.StandardListenerService"/>  


添加完毕,windchill shell中执行命令让其生效并重启methodserver:
xconfmanager -p&windchill stop&windchill start


全部配置完毕,请在任意容器中创建名字为postdelete的文档并对其执行删除操作,观察页面的提示信息和methodserver的输出信息。
整理好的代码在附件,直接拿来修改修改业务逻辑就可以用了。

前言:常年离线在线开发实施Windchill9.0和Windchill10.0。
带人经验极其丰富,可帮公司创建Windchill开发团队。
高效,务实,认真,负责!

0
0
分享到:
评论
1 楼 hm_240912 2010-11-17  
有沒有建立三種table 的全部代碼,請發到-----> hm_240912@163.com  的郵箱,本人非常感謝

相关推荐

Global site tag (gtag.js) - Google Analytics