View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2012-5-16 下午02:53:22
4    */
5   package gboat2.serviceflow.util;
6   
7   import gboat2.serviceflow.Activator;
8   import gboat2.serviceflow.service.IEntryActionHandler;
9   import gboat2.serviceflow.service.IExitActionHandler;
10  import gboat2.serviceflow.service.IHandlerRegister;
11  
12  import java.util.List;
13  import java.util.Map;
14  
15  import org.osgi.framework.BundleContext;
16  import org.osgi.framework.ServiceReference;
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  
20  /**
21   * 
22   * 工作流服务工具
23   * @author lysming
24   * @since jdk1.6
25   * @date 2012-5-16
26   *  
27   */
28  
29  public class WorkflowUtil {
30  
31  	private static Logger logger = LoggerFactory.getLogger(WorkflowUtil.class);
32  
33  	/** 设置流程进入节点状态
34  	 * @param processId
35  	 * 			流程id
36  	 * @param businessObjectId
37  	 * 			对象id
38  	 * @param nodeName
39  	 * 			节点名称
40  	 * @param params 动态参数
41  	 */
42  	public static void onEntryAction(String processId, String businessObjectId, String nodeName, Map<String, Object> params) {
43  		try {
44  			logger.info("on Entry Action! businessObjectId:" + businessObjectId + ",nodeName:" + nodeName);
45  			BundleContext context = Activator.LOCAL_BUNDLE.getBundleContext();
46  			ServiceReference sr = context.getServiceReference(IHandlerRegister.class.getName());
47  			if (sr != null) {
48  				IHandlerRegister register = (IHandlerRegister) context.getService(sr);
49  				if (register != null) {
50  					List<IEntryActionHandler> handlers = register.getEntryActionHandlers();
51  					if (handlers.size() == 0) {
52  						logger.warn("no IEntryActionHanlder is registed.");
53  					}
54  					for (IEntryActionHandler handler : handlers) {
55  						try {
56  							if(handler.accept(processId, businessObjectId, nodeName, params)){
57  								logger.info("trigger " + handler.getClass().getName() + " ... ");
58  								handler.action(processId, businessObjectId, nodeName, params);
59  							}
60  						} catch (Exception e) {
61  							logger.error("error while call hanlder{"+handler.getClass().getName()+"}", e);
62  						}
63  					}
64  				} else {
65  					logger.error("IHandlerRegister cann't find in gboat2.serviceflow.It must be there.");
66  				}
67  			} else {
68  				logger.error("IHandlerRegister cann't find in gboat2.serviceflow.It must be there.");
69  			}
70  		} catch (Exception e) {
71  			logger.error("WorkflowUtil onEntryAction Exception!!!", e);
72  		}
73  	}
74  
75  	/**设置流程退出节点状态
76  	 * @param processId
77  	 * 			流程id
78  	 * @param businessObjectId
79  	 * 			对象id
80  	 * @param nodeName
81  	 * 			节点名称
82  	 * @param params 动态参数
83  	 */
84  	public static void onExitAction(String processId, String businessObjectId, String nodeName, Map<String, Object> params) {
85  		logger.info("on Exit Action ! businessObjectId:" + businessObjectId + ",nodeName:" + nodeName);
86  		BundleContext context = Activator.LOCAL_BUNDLE.getBundleContext();
87  		ServiceReference sr = context.getServiceReference(IHandlerRegister.class.getName());
88  		if (sr != null) {
89  			IHandlerRegister register = (IHandlerRegister) context.getService(sr);
90  			if (register != null) {
91  				List<IExitActionHandler> handlers = register.getExitActionHandlers();
92  				if (handlers.size() == 0) {
93  					logger.warn("no IExitActionHanlder is registed.");
94  				}
95  				for (IExitActionHandler handler : handlers) {
96  					try {
97  						if(handler.accept(processId, businessObjectId, nodeName, params)){
98  							logger.info("trigger " + handler.getClass().getName() + " ... ");
99  							handler.action(processId, businessObjectId,nodeName,params);
100 						}
101 					} catch (Exception e) {
102 						logger.error("error while call hanlder{"+handler.getClass().getName()+"}", e);
103 					}
104 				}
105 			} else {
106 				logger.error("IHandlerRegister cann't find in gboat2.serviceflow.It must be there.");
107 			}
108 		} else {
109 			logger.error("IHandlerRegister cann't find in gboat2.serviceflow.It must be there.");
110 		}
111 	}
112 }