在windchill中如何监听workItem进行任务邮件定制

  • Post author:
  • Post category:其他


在windchill定制开发中,有一种场景,需要在定制接收的任务邮件, 但是workitem对象用poststore监听不到、可以通过监听的是workitemlink 达到workItem创建时,定制邮件。

package ext.vivo.common.service;

/**
 * @version 1.0 2017-11-6
 * @author weiting
 */
public interface WorkItemListenerService {

}



package ext.vivo.common.service;
/**
 * @version 1.0 2017-11-6
 * @author weiting
 */
import java.util.ArrayList;
import java.util.List;

import javax.mail.internet.InternetAddress;

import org.apache.log4j.Logger;

import com.elead.core.model.ExtWorkFlowPBO;
import com.elead.extwf.docWorkFlow.util.DocWorkflowUtil;
import com.elead.util.WfUICommonUtil;

import ext.vivo.common.util.MailUtil;
import wt.doc.WTDocument;
import wt.events.KeyedEvent;
import wt.events.KeyedEventListener;
import wt.fc.PersistenceManagerEvent;
import wt.httpgw.URLFactory;
import wt.log4j.LogR;
import wt.org.WTPrincipalReference;
import wt.services.ServiceEventListenerAdapter;
import wt.services.StandardManager;
import wt.session.SessionHelper;
import wt.session.sessionResource;
import wt.util.WTException;
import wt.workflow.engine.WfProcess;
import wt.workflow.work.WfAssignedActivity;
import wt.workflow.work.WorkItem;
import wt.workflow.work.WorkItemLink;
@SuppressWarnings("serial")
public class StandardWorkItemListenerService extends StandardManager implements WorkItemListenerService {
    private static final String CLASSNAME = StandardWorkItemListenerService.class.getName();
    private static Logger logger = LogR.getLogger(CLASSNAME);

    private KeyedEventListener listener = null;

    public String getConceptualClassname() {
        return this.CLASSNAME;
    }

    public static StandardWorkItemListenerService newStandardWorkItemListenerService() throws WTException {
        StandardWorkItemListenerService instance = new StandardWorkItemListenerService();
        instance.initialize();
        return instance;
    }

    class WorkItemListener extends ServiceEventListenerAdapter {
        public WorkItemListener(String manager_name) {
            super(manager_name);
        }

        public void notifyVetoableEvent(Object event) throws Exception {
            if (!(event instanceof KeyedEvent))
                return;
            KeyedEvent eventObject = (KeyedEvent) event;
            Object busObject = eventObject.getEventTarget();
            String strEventType = eventObject.getEventType();
            if ( strEventType.equals(PersistenceManagerEvent.POST_STORE)) {
                if(busObject instanceof WorkItemLink) {
                    WorkItemLink link=(WorkItemLink) busObject;
                    WorkItem wrkItem= (WorkItem) link.getWorkItem();
                    if(wrkItem.getPrimaryBusinessObject()!=null) {
                        Object primaryObj=wrkItem.getPrimaryBusinessObject().getObject();
                        if( primaryObj instanceof ExtWorkFlowPBO) {
                            ExtWorkFlowPBO pbo=(ExtWorkFlowPBO)primaryObj;
                            WTDocument doc=DocWorkflowUtil.getDocByPBO(pbo);
                            String name=doc.getName();
                            String pboOid=WfUICommonUtil.obj2Oid(pbo);
                            WfAssignedActivity wfactivity=(WfAssignedActivity) wrkItem.getSource().getObject();
                            WfProcess wfprocess=wfactivity.getParentProcess();
                            List<InternetAddress> list = new ArrayList<InternetAddress>();
                            InternetAddress emaiAdress = new InternetAddress(wrkItem.getOwnership().getOwner().getEMail(),wrkItem.getOwnership().getOwner().getEMail());
                            list.add(emaiAdress);
                            WTPrincipalReference user=SessionHelper.manager.getPrincipalReference();
                            String fromUserMail =user.getEMail();
                            String fromUserFullName=user.getFullName();
                            URLFactory factory = new URLFactory();
                            String host = factory.getBaseHREF();
                            String docURL = host
                                    + "netmarkets/ext/inner-index.html?docType="+pbo.getName()+"&wfprocessid="+
                                    WfUICommonUtil.getOidByPersistable(wfprocess)+"&pboid="+ pboOid+"&workItemid="+
                                    WfUICommonUtil.getOidByPersistable(wrkItem)+"&wfactivityid="+
                                    WfUICommonUtil.getOidByPersistable(wfactivity);
                            String mailUrl ="请您点击文档链接查看:"+"< a href='" + docURL + "'>"
                                    + name + "</ a>";
                            MailUtil.sendMail("待办通知:【"+pbo.getDescription()+"】"+name, mailUrl, list,fromUserMail,fromUserFullName);
                        }
                    }
                }
            }
        }
    }

    protected void performStartupProcess() {
        String speaker = CLASSNAME + ".performStartupProcess";
        logger.debug("=> IN " + speaker);
        listener = new WorkItemListener(getConceptualClassname());
        getManagerService().addEventListener(listener,
                PersistenceManagerEvent.generateEventKey(PersistenceManagerEvent.POST_STORE));
        logger.debug("=> OUT " + speaker);
    }
}



版权声明:本文为whw174660897原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。