/drools-core/src/main/java/org/drools/process/instance/WorkItemManager.java

https://github.com/bobmcwhirter/drools · Java · 120 lines · 93 code · 18 blank · 9 comment · 16 complexity · 18c95422708d6863c37ee67f588de8f1 MD5 · raw file

  1. package org.drools.process.instance;
  2. import java.io.Externalizable;
  3. import java.io.IOException;
  4. import java.io.ObjectInput;
  5. import java.io.ObjectOutput;
  6. import java.util.HashMap;
  7. import java.util.HashSet;
  8. import java.util.Map;
  9. import java.util.Set;
  10. import org.drools.WorkingMemory;
  11. import org.drools.process.instance.impl.WorkItemImpl;
  12. /**
  13. *
  14. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  15. */
  16. public class WorkItemManager implements Externalizable {
  17. private static final long serialVersionUID = 400L;
  18. private long workItemCounter;
  19. private Map<Long, WorkItem> workItems = new HashMap<Long, WorkItem>();
  20. private WorkingMemory workingMemory;
  21. private Map<String, WorkItemHandler> workItemHandlers = new HashMap<String, WorkItemHandler>();
  22. public WorkItemManager() {
  23. }
  24. public WorkItemManager(WorkingMemory workingMemory) {
  25. this.workingMemory = workingMemory;
  26. }
  27. public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  28. workItemCounter = in.readLong();
  29. workItems = (Map<Long, WorkItem>)in.readObject();
  30. workingMemory = (WorkingMemory)in.readObject();
  31. workItemHandlers = (Map<String, WorkItemHandler>)in.readObject();
  32. }
  33. public void writeExternal(ObjectOutput out) throws IOException {
  34. out.writeLong(workItemCounter);
  35. out.writeObject(workItems);
  36. out.writeObject(workingMemory);
  37. out.writeObject(workItemHandlers);
  38. }
  39. public void internalExecuteWorkItem(WorkItem workItem) {
  40. ((WorkItemImpl) workItem).setId(++workItemCounter);
  41. internalAddWorkItem(workItem);
  42. WorkItemHandler handler = (WorkItemHandler) this.workItemHandlers.get(workItem.getName());
  43. if (handler != null) {
  44. handler.executeWorkItem(workItem, this);
  45. } else {
  46. System.err.println("Could not find work item handler for " + workItem.getName());
  47. }
  48. }
  49. public void internalAddWorkItem(WorkItem workItem) {
  50. workItems.put(new Long(workItem.getId()), workItem);
  51. }
  52. public void internalAbortWorkItem(long id) {
  53. WorkItemImpl workItem = (WorkItemImpl) workItems.get(new Long(id));
  54. // work item may have been aborted
  55. if (workItem != null) {
  56. WorkItemHandler handler = (WorkItemHandler) this.workItemHandlers.get(workItem.getName());
  57. if (handler != null) {
  58. handler.abortWorkItem(workItem, this);
  59. } else {
  60. System.err.println("Could not find work item handler for " + workItem.getName());
  61. }
  62. }
  63. }
  64. public Set<WorkItem> getWorkItems() {
  65. return new HashSet<WorkItem>(workItems.values());
  66. }
  67. public WorkItem getWorkItem(long id) {
  68. return workItems.get(id);
  69. }
  70. public void completeWorkItem(long id, Map<String, Object> results) {
  71. WorkItemImpl workItem = (WorkItemImpl) workItems.get(new Long(id));
  72. // work item may have been aborted
  73. if (workItem != null) {
  74. workItem.setResults(results);
  75. ProcessInstance processInstance = workingMemory.getProcessInstance(workItem.getProcessInstanceId());
  76. workItem.setState(WorkItem.COMPLETED);
  77. // process instance may have finished already
  78. if (processInstance != null) {
  79. processInstance.signalEvent("workItemCompleted", workItem);
  80. }
  81. workItems.remove(new Long(id));
  82. workingMemory.fireAllRules();
  83. }
  84. }
  85. public void abortWorkItem(long id) {
  86. WorkItemImpl workItem = (WorkItemImpl) workItems.get(new Long(id));
  87. // work item may have been aborted
  88. if (workItem != null) {
  89. ProcessInstance processInstance = workingMemory.getProcessInstance(workItem.getProcessInstanceId());
  90. workItem.setState(WorkItem.ABORTED);
  91. // process instance may have finished already
  92. if (processInstance != null) {
  93. processInstance.signalEvent("workItemAborted", workItem);
  94. }
  95. workItems.remove(new Long(id));
  96. workingMemory.fireAllRules();
  97. }
  98. }
  99. public void registerWorkItemHandler(String workItemName, WorkItemHandler handler) {
  100. this.workItemHandlers.put(workItemName, handler);
  101. }
  102. }