1 package org.smartcomps.twister.worklist.priv;
2
3 import org.smartcomps.twister.worklist.TwisterWorkList;
4 import org.smartcomps.twister.worklist.WorkItem;
5 import org.smartcomps.twister.worklist.priv.po.WorkItemImpl;
6 import org.smartcomps.twister.worklist.priv.dao.WorkItemDAO;
7 import org.smartcomps.twister.worklist.exception.InvalidMessageException;
8 import org.smartcomps.twister.worklist.exception.WorkListException;
9
10 import org.smartcomps.twister.common.persistence.DBSessionException;
11 import org.smartcomps.twister.common.persistence.CreationException;
12 import org.smartcomps.twister.common.persistence.FinderException;
13 import org.smartcomps.twister.common.persistence.DBSessionManager;
14 import org.smartcomps.twister.common.util.DurationUtil;
15 import org.smartcomps.twister.common.transaction.TransactionManager;
16 import org.smartcomps.twister.common.transaction.TransactionException;
17 import org.smartcomps.twister.engine.exception.EngineRuntimeException;
18 import org.smartcomps.twister.engine.TwisterEngineFactory;
19
20 import org.dom4j.Node;
21 import org.dom4j.DocumentHelper;
22 import org.dom4j.Document;
23 import org.relaxng.datatype.DatatypeException;
24
25 import java.util.Collection;
26 import java.util.Calendar;
27
28 import com.sun.msv.datatype.xsd.XSDatatype;
29 import com.sun.msv.datatype.xsd.DatatypeFactory;
30 import com.sun.msv.datatype.xsd.datetime.BigTimeDurationValueType;
31
32 /***
33 * Implementation of the WorkList interface.
34 */
35 public class WorkListImpl implements TwisterWorkList {
36
37 private static String THIS_PARTNER = "WorkListMgr";
38 private static String THIS_PORTTYPE = "WorkListAnswerPT";
39 private static String THIS_OPERATION = "receiveAnswer";
40
41
42 public Long createWorkItem(Document creationMessage) throws InvalidMessageException, CreationException {
43 WorkItemImpl impl = new WorkItemImpl();
44
45 Node userNode = creationMessage.selectSingleNode("/message/wipart/user");
46 Node groupNode = creationMessage.selectSingleNode("/message/wipart/group");
47 Node descriptionNode = creationMessage.selectSingleNode("/message/wipart/description");
48 Node deadlineNode = creationMessage.selectSingleNode("/message/wipart/deadline");
49
50 if (userNode == null && groupNode == null) {
51 throw new InvalidMessageException("Received message doesn't follow the necessary format " + creationMessage);
52 }
53
54 if (groupNode == null) {
55 impl.setUserOrGroup(userNode.getText());
56 } else {
57 impl.setUserOrGroup(groupNode.getText());
58 }
59 if (descriptionNode != null) {
60 impl.setDescription(descriptionNode.getText());
61 }
62
63 if (deadlineNode != null) {
64 try {
65 XSDatatype dateTimeType = DatatypeFactory.getTypeByName("dateTime");
66 if (dateTimeType.isValid(deadlineNode.getText(), null)) {
67 impl.setDueDate((Calendar)dateTimeType.createJavaObject(deadlineNode.getText(), null));
68 } else {
69 XSDatatype durationType = DatatypeFactory.getTypeByName("duration");
70 if (durationType.isValid(deadlineNode.getText(), null)) {
71 Calendar cal = Calendar.getInstance();
72 BigTimeDurationValueType duration =
73 (BigTimeDurationValueType) durationType.createJavaObject(deadlineNode.getText(), null);
74 DurationUtil.addDuration(cal, duration);
75 impl.setDueDate(cal);
76 } else {
77 throw new InvalidMessageException("The deadline node in the message could not be parsed " + creationMessage);
78 }
79 }
80 } catch (DatatypeException e) {
81 throw new EngineRuntimeException(e);
82 }
83 }
84
85 impl.setCreationDate(Calendar.getInstance());
86 impl.setStatus(WorkItem.ACTIVE);
87
88 boolean txStarted = false;
89 try {
90 DBSessionManager.getActiveSession();
91 txStarted = true;
92 } catch (DBSessionException e) { }
93 if (!txStarted) {
94 try {
95 TransactionManager.beginTransaction();
96 } catch (TransactionException e) {
97 throw new CreationException("Could not start transaction", e);
98 }
99 }
100
101 try {
102 WorkItemDAO.create(impl);
103 } catch (DBSessionException e) {
104 throw new CreationException(e);
105 }
106
107 if (!txStarted) {
108 try {
109 TransactionManager.commitTransaction();
110 } catch (TransactionException e) {
111 throw new CreationException("Could not start transaction", e);
112 }
113 }
114 return impl.getId();
115 }
116
117 public Collection getWorkList(String user) throws FinderException {
118 try {
119 TransactionManager.beginTransaction();
120 } catch (TransactionException e) {
121 throw new FinderException("Could not start transaction", e);
122 }
123
124 Collection result = null;
125 try {
126 result = WorkItemDAO.findByUserOrGroup(user);
127 } catch (DBSessionException e) {
128 throw new FinderException(e);
129 }
130
131 try {
132 TransactionManager.commitTransaction();
133 } catch (TransactionException e) {
134 throw new FinderException("Could not start transaction", e);
135 }
136
137 return result;
138 }
139
140 public void completeWorkItem(Long wiId) throws WorkListException {
141 try {
142 TransactionManager.beginTransaction();
143 } catch (TransactionException e) {
144 throw new WorkListException("Could not start transaction", e);
145 }
146
147 WorkItemImpl wi = null;
148 try {
149 wi = (WorkItemImpl) WorkItemDAO.findById(wiId, WorkItemImpl.class);
150 } catch (DBSessionException e) {
151 throw new WorkListException("A database exception occured.", e);
152 } catch (org.smartcomps.twister.common.persistence.FinderException e) {
153 throw new WorkListException("Could not find work item with id " + wiId, e);
154 }
155 wi.setStatus(WorkItem.COMPLETED);
156
157 Document doc = DocumentHelper.createDocument();
158 doc.addElement("message").addElement("wipart").addElement("workitemid").setText(wi.getId().toString());
159
160 try {
161 TransactionManager.commitTransaction();
162 } catch (TransactionException e) {
163 throw new WorkListException("Could not commit transaction", e);
164 }
165
166 TwisterEngineFactory.getEngine().acknowledge(THIS_PARTNER, THIS_PORTTYPE, THIS_OPERATION, doc);
167 }
168
169 }
This page was automatically generated by Maven