/src/java/org/drools/gorm/processinstance/GormWorkItemManager.java

https://github.com/diega/drools-gorm · Java · 223 lines · 172 code · 27 blank · 24 comment · 38 complexity · cb7e8f766f832faf88924b5e6ca7a91e MD5 · raw file

  1. /**
  2. * Copyright 2010 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.drools.gorm.processinstance;
  17. import java.io.Externalizable;
  18. import java.io.IOException;
  19. import java.io.ObjectInput;
  20. import java.io.ObjectOutput;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import java.util.concurrent.ConcurrentHashMap;
  26. import org.drools.WorkItemHandlerNotFoundException;
  27. import org.drools.common.InternalKnowledgeRuntime;
  28. import org.drools.gorm.GrailsIntegration;
  29. import org.drools.gorm.session.WorkItemInfo;
  30. import org.drools.process.instance.WorkItem;
  31. import org.drools.process.instance.WorkItemManager;
  32. import org.drools.process.instance.impl.WorkItemImpl;
  33. import org.drools.runtime.Environment;
  34. import org.drools.runtime.process.ProcessInstance;
  35. import org.drools.runtime.process.WorkItemHandler;
  36. /**
  37. *
  38. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  39. */
  40. public class GormWorkItemManager implements WorkItemManager, Externalizable {
  41. private static final long serialVersionUID = 510l;
  42. private InternalKnowledgeRuntime kruntime;
  43. private Map<String, WorkItemHandler> workItemHandlers = new HashMap<String, WorkItemHandler>();
  44. private transient Map<Long, WorkItemInfo> workItems = new ConcurrentHashMap<Long, WorkItemInfo>();
  45. public GormWorkItemManager(InternalKnowledgeRuntime kruntime) {
  46. this.kruntime = kruntime;
  47. }
  48. @SuppressWarnings("unchecked")
  49. @Override
  50. public void readExternal(ObjectInput in) throws IOException,
  51. ClassNotFoundException {
  52. workItems = (Map<Long, WorkItemInfo>) in.readObject();
  53. kruntime = (InternalKnowledgeRuntime) in.readObject();
  54. workItemHandlers = (Map<String, WorkItemHandler>) in.readObject();
  55. }
  56. @Override
  57. public void writeExternal(ObjectOutput out) throws IOException {
  58. out.writeObject(workItems);
  59. out.writeObject(kruntime);
  60. out.writeObject(workItemHandlers);
  61. }
  62. @Override
  63. public void internalExecuteWorkItem(WorkItem workItem) {
  64. WorkItemInfo workItemInfo = GrailsIntegration.getGormDomainService().getNewWorkItemInfo(
  65. workItem,
  66. kruntime.getEnvironment());
  67. GrailsIntegration.getGormDomainService().saveDomain(workItemInfo);
  68. Long workItemId = workItemInfo.getId(); // XXX {bauna}(Long) ((GroovyObject) workItemInfo).invokeMethod("getId", null);
  69. ((WorkItemImpl) workItem).setId(workItemId);
  70. workItemInfo.generateBlob();
  71. WorkItemHandler handler = this.workItemHandlers.get(workItem.getName());
  72. if (handler != null) {
  73. handler.executeWorkItem(workItem, this);
  74. } else {
  75. throw new WorkItemHandlerNotFoundException("Could not find work item handler for "
  76. + workItem.getName(),
  77. workItem.getName());
  78. }
  79. }
  80. @Override
  81. public void internalAddWorkItem(WorkItem workItem) {
  82. }
  83. @Override
  84. public void internalAbortWorkItem(long id) {
  85. WorkItemInfo workItemInfo = GrailsIntegration
  86. .getGormDomainService().getWorkItemInfo(id);
  87. // work item may have been aborted
  88. if (workItemInfo != null) {
  89. WorkItem workItem = workItemInfo.getWorkItem(this.kruntime.getEnvironment());
  90. WorkItemHandler handler = workItemHandlers.get(workItem.getName());
  91. if (handler != null) {
  92. handler.abortWorkItem(workItem, this);
  93. } else {
  94. if ( workItems != null ) {
  95. workItems.remove( id );
  96. throw new WorkItemHandlerNotFoundException( "Could not find work item handler for " +
  97. workItem.getName(),
  98. workItem.getName() );
  99. }
  100. }
  101. if (workItems != null) {
  102. workItems.remove(id);
  103. }
  104. GrailsIntegration.getGormDomainService().deleteDomain(workItemInfo);
  105. }
  106. }
  107. @Override
  108. public Set<WorkItem> getWorkItems() {
  109. Set<WorkItem> wis = new HashSet<WorkItem>();
  110. for (WorkItemInfo wii : workItems.values()) {
  111. wis.add(wii.getWorkItem(kruntime.getEnvironment()));
  112. }
  113. return wis;
  114. }
  115. @Override
  116. public WorkItem getWorkItem(long id) {
  117. WorkItemInfo workItemInfo = workItems.get(id);
  118. WorkItem workItem = null;
  119. if (workItemInfo == null) {
  120. workItemInfo = GrailsIntegration
  121. .getGormDomainService().getWorkItemInfo(id);
  122. if (workItemInfo != null) {
  123. workItem = workItemInfo.getWorkItem(kruntime.getEnvironment());
  124. workItems.put(workItemInfo.getId(), workItemInfo);
  125. this.internalAddWorkItem(workItem);
  126. }
  127. } else {
  128. workItem = workItemInfo.getWorkItem(kruntime.getEnvironment());
  129. }
  130. return workItem;
  131. }
  132. @Override
  133. public void completeWorkItem(long id, Map<String, Object> results) {
  134. Environment env = this.kruntime.getEnvironment();
  135. WorkItemInfo workItemInfo = null;
  136. workItemInfo = this.workItems.get(id);
  137. if (workItemInfo != null) {
  138. workItemInfo = (WorkItemInfo) GrailsIntegration.getGormDomainService()
  139. .mergeDomain(workItemInfo);
  140. }
  141. if (workItemInfo == null) {
  142. workItemInfo = GrailsIntegration.getGormDomainService()
  143. .getWorkItemInfo(id);
  144. }
  145. // work item may have been aborted
  146. if (workItemInfo != null) {
  147. WorkItem workItem = (WorkItemImpl) workItemInfo.getWorkItem(env);
  148. workItem.setResults(results);
  149. ProcessInstance processInstance =
  150. kruntime.getProcessInstance(workItem.getProcessInstanceId());
  151. workItem.setState(WorkItem.COMPLETED);
  152. // process instance may have finished already
  153. if (processInstance != null) {
  154. processInstance.signalEvent("workItemCompleted", workItem);
  155. }
  156. GrailsIntegration.getGormDomainService().deleteDomain(workItemInfo);
  157. if (workItems != null) {
  158. this.workItems.remove(workItem.getId());
  159. }
  160. }
  161. }
  162. @Override
  163. public void abortWorkItem(long id) {
  164. Environment env = this.kruntime.getEnvironment();
  165. WorkItemInfo workItemInfo = this.workItems.get(id);
  166. if (workItemInfo != null) {
  167. GrailsIntegration.getGormDomainService().mergeDomain(workItemInfo);
  168. }
  169. if (workItemInfo == null) {
  170. workItemInfo = GrailsIntegration.getGormDomainService().getWorkItemInfo(id);
  171. }
  172. // work item may have been aborted
  173. if (workItemInfo != null) {
  174. WorkItem workItem = (WorkItemImpl) workItemInfo.getWorkItem(env);
  175. ProcessInstance processInstance = kruntime.getProcessInstance(workItem.getProcessInstanceId());
  176. workItem.setState(WorkItem.ABORTED);
  177. // process instance may have finished already
  178. if (processInstance != null) {
  179. processInstance.signalEvent("workItemAborted", workItem);
  180. }
  181. GrailsIntegration.getGormDomainService().deleteDomain(workItemInfo);
  182. if (workItems != null) {
  183. workItems.remove(workItem.getId());
  184. }
  185. }
  186. }
  187. @Override
  188. public void registerWorkItemHandler(String workItemName,
  189. WorkItemHandler handler) {
  190. this.workItemHandlers.put(workItemName, handler);
  191. }
  192. @Override
  193. public void clear() {
  194. this.workItems.clear();
  195. }
  196. }