1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.edl.components; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Date; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
|
25 | |
import javax.xml.xpath.XPath; |
26 | |
import javax.xml.xpath.XPathConstants; |
27 | |
import javax.xml.xpath.XPathExpressionException; |
28 | |
import javax.xml.xpath.XPathFactory; |
29 | |
|
30 | |
import org.apache.commons.lang.StringUtils; |
31 | |
import org.apache.log4j.Logger; |
32 | |
import org.kuali.rice.core.util.RiceConstants; |
33 | |
import org.kuali.rice.kew.edl.EDLContext; |
34 | |
import org.kuali.rice.kew.edl.EDLModelComponent; |
35 | |
import org.kuali.rice.kew.edl.EDLXmlUtils; |
36 | |
import org.kuali.rice.kew.edl.RequestParser; |
37 | |
import org.kuali.rice.kew.edl.UserAction; |
38 | |
import org.kuali.rice.kew.exception.WorkflowException; |
39 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
40 | |
import org.kuali.rice.kew.service.WorkflowDocument; |
41 | |
import org.kuali.rice.kew.service.WorkflowInfo; |
42 | |
import org.kuali.rice.kew.util.KEWConstants; |
43 | |
import org.kuali.rice.kew.util.Utilities; |
44 | |
import org.kuali.rice.kew.util.XmlHelper; |
45 | |
import org.kuali.rice.kns.util.KNSConstants; |
46 | |
import org.w3c.dom.Document; |
47 | |
import org.w3c.dom.Element; |
48 | |
|
49 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 0 | public class WorkflowDocumentState implements EDLModelComponent { |
59 | |
|
60 | 0 | private static final Logger LOG = Logger.getLogger(WorkflowDocumentState.class); |
61 | |
|
62 | |
|
63 | 0 | private enum buttons{ACKNOWLEDGE, BLANKETAPPROVE, ROUTE, SAVE, COMPLETE, APPROVE, DISAPPROVE, |
64 | 0 | RETURNTOPREVIOUS, FYI, CANCEL}; |
65 | |
|
66 | |
public void updateDOM(Document dom, Element configElement, EDLContext edlContext) { |
67 | |
|
68 | |
try { |
69 | 0 | Element documentState = EDLXmlUtils.getDocumentStateElement(dom); |
70 | |
|
71 | 0 | Element dateTime = EDLXmlUtils.getOrCreateChildElement(documentState, "dateTime", true); |
72 | 0 | dateTime.appendChild(dom.createTextNode(RiceConstants.getDefaultDateAndTimeFormat().format(new Date()))); |
73 | |
|
74 | 0 | Element definition = EDLXmlUtils.getOrCreateChildElement(documentState, "definition", true); |
75 | 0 | definition.appendChild(dom.createTextNode(edlContext.getEdocLiteAssociation().getDefinition())); |
76 | |
|
77 | 0 | Element docType = EDLXmlUtils.getOrCreateChildElement(documentState, "docType", true); |
78 | 0 | docType.appendChild(dom.createTextNode(edlContext.getEdocLiteAssociation().getEdlName())); |
79 | |
|
80 | 0 | Element style = EDLXmlUtils.getOrCreateChildElement(documentState, "style", true); |
81 | 0 | String styleName = edlContext.getEdocLiteAssociation().getStyle(); |
82 | 0 | if (styleName == null) { |
83 | 0 | styleName = "Default"; |
84 | |
} |
85 | 0 | style.appendChild(dom.createTextNode(styleName)); |
86 | |
|
87 | 0 | Element showAttachments = EDLXmlUtils.getOrCreateChildElement(documentState, "showAttachments", true); |
88 | 0 | boolean showConstants = Utilities.getKNSParameterBooleanValue(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.ALL_DETAIL_TYPE, KEWConstants.SHOW_ATTACHMENTS_IND); |
89 | |
|
90 | 0 | showAttachments.appendChild(dom.createTextNode(Boolean.valueOf(showConstants).toString())); |
91 | |
|
92 | 0 | WorkflowDocument document = (WorkflowDocument)edlContext.getRequestParser().getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY); |
93 | 0 | WorkflowInfo info = new WorkflowInfo(); |
94 | |
|
95 | 0 | boolean documentEditable = false; |
96 | 0 | if (document != null) { |
97 | 0 | List validActions = determineValidActions(document); |
98 | |
|
99 | 0 | documentEditable = isEditable(edlContext, validActions); |
100 | |
|
101 | 0 | edlContext.getTransformer().setParameter("readOnly", String.valueOf(documentEditable)); |
102 | 0 | addActions(dom, documentState, validActions); |
103 | 0 | boolean isAnnotatable = isAnnotatable(validActions); |
104 | 0 | EDLXmlUtils.createTextElementOnParent(documentState, "annotatable", String.valueOf(isAnnotatable)); |
105 | 0 | EDLXmlUtils.createTextElementOnParent(documentState, "docId", document.getRouteHeaderId().toString()); |
106 | 0 | Element workflowDocumentStatus = EDLXmlUtils.getOrCreateChildElement(documentState, "workflowDocumentState", true); |
107 | 0 | EDLXmlUtils.createTextElementOnParent(workflowDocumentStatus, "status", document.getStatusDisplayValue()); |
108 | 0 | EDLXmlUtils.createTextElementOnParent(workflowDocumentStatus, "createDate", RiceConstants.getDefaultDateAndTimeFormat().format(document.getDateCreated())); |
109 | 0 | String[] nodeNames = document.getPreviousNodeNames(); |
110 | 0 | if (nodeNames.length > 0) { |
111 | 0 | Element previousNodes = EDLXmlUtils.getOrCreateChildElement(documentState, "previousNodes", true); |
112 | |
|
113 | 0 | for (int i = 0; i < nodeNames.length; i++) { |
114 | 0 | EDLXmlUtils.createTextElementOnParent(previousNodes, "node", nodeNames[i]); |
115 | |
} |
116 | |
} |
117 | 0 | String[] currentNodeNames = info.getCurrentNodeNames(document.getRouteHeaderId()); |
118 | 0 | for (String currentNodeName : currentNodeNames) { |
119 | 0 | EDLXmlUtils.createTextElementOnParent(documentState, "currentNodeName", currentNodeName); |
120 | |
} |
121 | |
|
122 | |
} |
123 | |
|
124 | 0 | Element editable = EDLXmlUtils.getOrCreateChildElement(documentState, "editable", true); |
125 | 0 | editable.appendChild(dom.createTextNode(String.valueOf(documentEditable))); |
126 | |
|
127 | |
|
128 | 0 | EDLXmlUtils.createTextElementOnParent(documentState, "actionable", "true"); |
129 | |
|
130 | 0 | List globalErrors = (List)edlContext.getRequestParser().getAttribute(RequestParser.GLOBAL_ERRORS_KEY); |
131 | 0 | List globalMessages = (List)edlContext.getRequestParser().getAttribute(RequestParser.GLOBAL_MESSAGES_KEY); |
132 | 0 | Map<String, String> globalFieldErrors = (Map)edlContext.getRequestParser().getAttribute(RequestParser.GLOBAL_FIELD_ERRORS_KEY); |
133 | 0 | EDLXmlUtils.addErrorsAndMessagesToDocument(dom, globalErrors, globalMessages, globalFieldErrors); |
134 | 0 | if (LOG.isDebugEnabled()) { |
135 | 0 | LOG.debug("Transforming dom " + XmlHelper.jotNode(dom, true)); |
136 | |
} |
137 | 0 | } catch (Exception e) { |
138 | 0 | throw new WorkflowRuntimeException(e); |
139 | 0 | } |
140 | 0 | } |
141 | |
|
142 | |
public static List<String> determineValidActions(WorkflowDocument wfdoc) throws WorkflowException { |
143 | 0 | String[] flags = new String[10]; |
144 | 0 | List<String> list = new ArrayList<String>(); |
145 | |
|
146 | 0 | if (wfdoc == null) { |
147 | 0 | list.add(UserAction.ACTION_CREATE); |
148 | 0 | return list; |
149 | |
} |
150 | |
|
151 | 0 | if (wfdoc.isAcknowledgeRequested()) { |
152 | 0 | flags[buttons.ACKNOWLEDGE.ordinal()] = UserAction.ACTION_ACKNOWLEDGE; |
153 | |
} |
154 | |
|
155 | 0 | if (wfdoc.isApprovalRequested()) { |
156 | 0 | if (wfdoc.isBlanketApproveCapable()) { |
157 | 0 | flags[buttons.BLANKETAPPROVE.ordinal()] = UserAction.ACTION_BLANKETAPPROVE; |
158 | |
} |
159 | 0 | if (!wfdoc.stateIsSaved()) { |
160 | 0 | flags[buttons.APPROVE.ordinal()] = UserAction.ACTION_APPROVE; |
161 | 0 | flags[buttons.DISAPPROVE.ordinal()] = UserAction.ACTION_DISAPPROVE; |
162 | |
} |
163 | |
|
164 | |
|
165 | 0 | flags[buttons.SAVE.ordinal()] = UserAction.ACTION_SAVE; |
166 | 0 | if (wfdoc.getPreviousNodeNames().length > 0) { |
167 | 0 | flags[buttons.RETURNTOPREVIOUS.ordinal()] = UserAction.ACTION_RETURN_TO_PREVIOUS; |
168 | |
} |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | 0 | else if (wfdoc.isCompletionRequested()) { |
174 | 0 | flags[buttons.COMPLETE.ordinal()] = UserAction.ACTION_COMPLETE; |
175 | 0 | if (wfdoc.isBlanketApproveCapable()) { |
176 | 0 | flags[buttons.BLANKETAPPROVE.ordinal()] = UserAction.ACTION_BLANKETAPPROVE; |
177 | |
} |
178 | |
} |
179 | |
|
180 | 0 | if (wfdoc.isFYIRequested()) { |
181 | 0 | flags[buttons.FYI.ordinal()] = UserAction.ACTION_FYI; |
182 | |
} |
183 | |
|
184 | 0 | if (wfdoc.isRouteCapable()) { |
185 | 0 | flags[buttons.ROUTE.ordinal()] = UserAction.ACTION_ROUTE; |
186 | 0 | if (wfdoc.isBlanketApproveCapable()) { |
187 | 0 | flags[buttons.BLANKETAPPROVE.ordinal()] = UserAction.ACTION_BLANKETAPPROVE; |
188 | |
} |
189 | |
} |
190 | |
|
191 | 0 | if (wfdoc.isApprovalRequested() || wfdoc.isRouteCapable()) { |
192 | 0 | flags[buttons.SAVE.ordinal()] = UserAction.ACTION_SAVE; |
193 | |
} |
194 | |
|
195 | 0 | if (wfdoc.isCompletionRequested() || wfdoc.isRouteCapable()) { |
196 | 0 | flags[buttons.CANCEL.ordinal()] = UserAction.ACTION_CANCEL; |
197 | |
} |
198 | |
|
199 | 0 | for (int i = 0; i < flags.length; i++) { |
200 | 0 | if (flags[i] != null) { |
201 | 0 | list.add(flags[i]); |
202 | |
} |
203 | |
} |
204 | |
|
205 | 0 | return list; |
206 | |
} |
207 | |
|
208 | |
public static boolean isEditable(EDLContext edlContext, List actions) { |
209 | 0 | boolean editable = false; |
210 | 0 | editable = listContainsItems(actions, UserAction.EDITABLE_ACTIONS); |
211 | |
|
212 | 0 | Document edlDom = KEWServiceLocator.getEDocLiteService().getDefinitionXml(edlContext.getEdocLiteAssociation()); |
213 | |
|
214 | 0 | XPath xpath = XPathFactory.newInstance().newXPath(); |
215 | 0 | String xpathExpression = "//config/param[@name='alwaysEditable']"; |
216 | |
try { |
217 | 0 | String match = (String) xpath.evaluate(xpathExpression, edlDom, XPathConstants.STRING); |
218 | 0 | if (!StringUtils.isBlank(match) && match.equals("true")) { |
219 | 0 | return true; |
220 | |
} |
221 | 0 | } catch (XPathExpressionException e) { |
222 | 0 | throw new WorkflowRuntimeException("Unable to evaluate xpath expression " + xpathExpression, e); |
223 | 0 | } |
224 | |
|
225 | 0 | return editable; |
226 | |
} |
227 | |
|
228 | |
|
229 | |
public static void addActions(Document dom, Element documentState, List actions) { |
230 | 0 | Element actionsPossible = EDLXmlUtils.getOrCreateChildElement(documentState, "actionsPossible", true); |
231 | 0 | Iterator it = actions.iterator(); |
232 | 0 | while (it.hasNext()) { |
233 | 0 | String action = it.next().toString(); |
234 | 0 | Element actionElement = dom.createElement(action); |
235 | |
|
236 | |
|
237 | 0 | if (!Character.isUpperCase(action.charAt(0))) { |
238 | 0 | StringBuffer sb = new StringBuffer(action); |
239 | 0 | sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); |
240 | 0 | action = sb.toString(); |
241 | |
} |
242 | 0 | actionElement.setAttribute("title", action); |
243 | 0 | actionsPossible.appendChild(actionElement); |
244 | 0 | } |
245 | |
|
246 | 0 | Element annotatable = EDLXmlUtils.getOrCreateChildElement(documentState, "annotatable", true); |
247 | 0 | annotatable.appendChild(dom.createTextNode(String.valueOf(isAnnotatable(actions)))); |
248 | 0 | } |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
public static boolean listContainsItems(List list, Object[] items) { |
254 | 0 | for (int i = 0; i < items.length; i++) { |
255 | 0 | if (list.contains(items[i])) return true; |
256 | |
} |
257 | 0 | return false; |
258 | |
} |
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
public static boolean isAnnotatable(List actions) { |
270 | 0 | return listContainsItems(actions, UserAction.ANNOTATABLE_ACTIONS); |
271 | |
} |
272 | |
|
273 | |
} |