1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.edl.components; |
17 | |
|
18 | |
import java.util.Map; |
19 | |
|
20 | |
import javax.xml.xpath.XPath; |
21 | |
import javax.xml.xpath.XPathConstants; |
22 | |
|
23 | |
import org.kuali.rice.kew.dto.PropertyDefinitionDTO; |
24 | |
import org.kuali.rice.kew.dto.WorkflowAttributeDefinitionDTO; |
25 | |
import org.kuali.rice.kew.dto.WorkflowAttributeValidationErrorDTO; |
26 | |
import org.kuali.rice.kew.edl.EDLContext; |
27 | |
import org.kuali.rice.kew.edl.EDLModelComponent; |
28 | |
import org.kuali.rice.kew.edl.RequestParser; |
29 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
30 | |
import org.kuali.rice.kew.rule.xmlrouting.XPathHelper; |
31 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
32 | |
import org.kuali.rice.kew.service.WorkflowDocument; |
33 | |
import org.w3c.dom.Document; |
34 | |
import org.w3c.dom.Element; |
35 | |
import org.w3c.dom.NodeList; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public class GlobalAttributeComponent extends SimpleWorkflowEDLConfigComponent implements EDLModelComponent { |
53 | |
|
54 | |
public void updateDOM(Document dom, Element configElement, EDLContext edlContext) { |
55 | |
|
56 | |
|
57 | 0 | if (!edlContext.getUserAction().isLoadAction()) { |
58 | 0 | RequestParser requestParser = edlContext.getRequestParser(); |
59 | |
try { |
60 | 0 | WorkflowDocument document = (WorkflowDocument)requestParser.getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY); |
61 | |
|
62 | 0 | document.clearAttributeContent(); |
63 | 0 | Document edlDef = KEWServiceLocator.getEDocLiteService().getDefinitionXml(edlContext.getEdocLiteAssociation()); |
64 | 0 | XPath xpath = XPathHelper.newXPath(edlDef); |
65 | 0 | NodeList attributeNodes = (NodeList)xpath.evaluate("/edl/attributes/attribute", edlDef, XPathConstants.NODESET); |
66 | 0 | for (int index = 0; index < attributeNodes.getLength(); index++) { |
67 | 0 | Element attributeElem = (Element)attributeNodes.item(index); |
68 | 0 | String attributeName = attributeElem.getAttribute("name"); |
69 | |
|
70 | |
|
71 | 0 | WorkflowAttributeDefinitionDTO attributeDef = getWorkflowAttributeDefinitionVO(attributeName, document); |
72 | |
|
73 | 0 | NodeList fieldNodes = (NodeList)xpath.evaluate("./field", attributeElem, XPathConstants.NODESET); |
74 | 0 | for (int fIndex = 0; fIndex < fieldNodes.getLength(); fIndex++) { |
75 | 0 | Element fieldElem = (Element)fieldNodes.item(fIndex); |
76 | 0 | String edlField = fieldElem.getAttribute("edlField"); |
77 | 0 | String attributeField = fieldElem.getAttribute("attributeField"); |
78 | 0 | PropertyDefinitionDTO property = attributeDef.getProperty(attributeField); |
79 | 0 | String value = requestParser.getParameterValue(edlField); |
80 | 0 | if (property == null) { |
81 | 0 | property = new PropertyDefinitionDTO(attributeField, value); |
82 | 0 | attributeDef.addProperty(property); |
83 | |
} else { |
84 | 0 | property.setValue(value); |
85 | |
} |
86 | |
} |
87 | |
|
88 | 0 | boolean curAttrValid = true; |
89 | 0 | if (edlContext.getUserAction().isValidatableAction()) { |
90 | 0 | WorkflowAttributeValidationErrorDTO[] errors = document.validateAttributeDefinition(attributeDef); |
91 | 0 | if (errors.length > 0) { |
92 | 0 | edlContext.setInError(true); |
93 | 0 | curAttrValid = false; |
94 | |
} |
95 | 0 | Map<String, String> fieldErrors = (Map<String, String>)edlContext.getRequestParser().getAttribute(RequestParser.GLOBAL_FIELD_ERRORS_KEY); |
96 | 0 | for (int atIndex = 0; atIndex < errors.length; atIndex++) { |
97 | 0 | WorkflowAttributeValidationErrorDTO error = errors[atIndex]; |
98 | 0 | fieldErrors.put(error.getKey(), error.getMessage()); |
99 | |
} |
100 | |
} |
101 | |
|
102 | |
|
103 | 0 | if(curAttrValid){ |
104 | 0 | document.addAttributeDefinition(attributeDef ); |
105 | 0 | for (int fIndex = 0; fIndex < fieldNodes.getLength(); fIndex++) { |
106 | 0 | Element fieldElem = (Element)fieldNodes.item(fIndex); |
107 | 0 | String edlField = fieldElem.getAttribute("edlField"); |
108 | 0 | String attributeField = fieldElem.getAttribute("attributeField"); |
109 | 0 | PropertyDefinitionDTO property = attributeDef.getProperty(attributeField); |
110 | 0 | String value = requestParser.getParameterValue(edlField); |
111 | 0 | if (property == null) { |
112 | 0 | property = new PropertyDefinitionDTO(attributeField, value); |
113 | 0 | attributeDef.addProperty(property); |
114 | |
} else { |
115 | 0 | property.setValue(value); |
116 | |
} |
117 | |
} |
118 | |
} |
119 | |
|
120 | |
} |
121 | 0 | } catch (Exception e) { |
122 | 0 | if (e instanceof RuntimeException) { |
123 | 0 | throw (RuntimeException)e; |
124 | |
} |
125 | 0 | throw new WorkflowRuntimeException("Failed to process attribute.", e); |
126 | 0 | } |
127 | |
} |
128 | 0 | } |
129 | |
|
130 | |
private WorkflowAttributeDefinitionDTO getWorkflowAttributeDefinitionVO(String attributeName, WorkflowDocument document) { |
131 | 0 | for (int i = 0; i < document.getAttributeDefinitions().length; i++) { |
132 | 0 | WorkflowAttributeDefinitionDTO workflowAttributeDef = (WorkflowAttributeDefinitionDTO)document.getAttributeDefinitions()[i]; |
133 | 0 | if (workflowAttributeDef.getAttributeName().equals(attributeName)) { |
134 | 0 | return workflowAttributeDef; |
135 | |
} |
136 | |
} |
137 | 0 | WorkflowAttributeDefinitionDTO workflowAttributeDef = new WorkflowAttributeDefinitionDTO(attributeName); |
138 | 0 | return workflowAttributeDef; |
139 | |
} |
140 | |
|
141 | |
} |