1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.rule; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.jdom.Document; |
20 | |
import org.jdom.Element; |
21 | |
import org.kuali.rice.core.api.util.xml.XmlHelper; |
22 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
23 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
24 | |
import org.kuali.rice.kew.doctype.service.DocumentTypeService; |
25 | |
import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl; |
26 | |
import org.kuali.rice.kew.routeheader.DocumentContent; |
27 | |
import org.kuali.rice.kew.rule.xmlrouting.XPathHelper; |
28 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
29 | |
import org.kuali.rice.kns.web.ui.Field; |
30 | |
import org.kuali.rice.kns.web.ui.Row; |
31 | |
|
32 | |
import javax.xml.xpath.XPath; |
33 | |
import javax.xml.xpath.XPathExpressionException; |
34 | |
import java.io.StringReader; |
35 | |
import java.util.ArrayList; |
36 | |
import java.util.Collection; |
37 | |
import java.util.Iterator; |
38 | |
import java.util.List; |
39 | |
import java.util.Map; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public class RuleRoutingAttribute implements WorkflowRuleAttribute { |
49 | |
|
50 | |
private static final long serialVersionUID = -8884711461398770563L; |
51 | |
|
52 | |
private static final String DOC_TYPE_NAME_PROPERTY = "docTypeFullName"; |
53 | |
private static final String DOC_TYPE_NAME_KEY = "docTypeFullName"; |
54 | |
|
55 | |
private static final String LOOKUPABLE_CLASS = "org.kuali.rice.kew.doctype.bo.DocumentType"; |
56 | |
private static final String DOC_TYPE_NAME_LABEL = "Document type name"; |
57 | |
|
58 | |
private static final String DOC_TYPE_NAME_XPATH = "//newMaintainableObject/businessObject/docTypeName"; |
59 | |
private static final String DOC_TYPE_NAME_DEL_XPATH = "//newMaintainableObject/businessObject/delegationRuleBaseValues/docTypeName"; |
60 | |
|
61 | |
private String doctypeName; |
62 | |
private List<Row> rows; |
63 | |
private boolean required; |
64 | |
|
65 | |
public RuleRoutingAttribute(String docTypeName) { |
66 | 0 | this(); |
67 | 0 | setDoctypeName(docTypeName); |
68 | 0 | } |
69 | |
|
70 | 0 | public RuleRoutingAttribute() { |
71 | 0 | buildRows(); |
72 | 0 | } |
73 | |
|
74 | |
private void buildRows() { |
75 | 0 | rows = new ArrayList<Row>(); |
76 | |
|
77 | 0 | List<Field> fields = new ArrayList<Field>(); |
78 | 0 | fields.add(new Field(DOC_TYPE_NAME_LABEL, "", Field.TEXT, false, DOC_TYPE_NAME_PROPERTY, "", false, false, null, LOOKUPABLE_CLASS)); |
79 | |
|
80 | 0 | rows.add(new Row(fields)); |
81 | 0 | } |
82 | |
|
83 | |
public boolean isMatch(DocumentContent docContent, List ruleExtensions) { |
84 | 0 | setDoctypeName(getRuleDocumentTypeFromRuleExtensions(ruleExtensions)); |
85 | 0 | DocumentTypeService service = (DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE); |
86 | |
|
87 | |
try { |
88 | 0 | String docTypeName = getDocTypNameFromXML(docContent); |
89 | 0 | if (docTypeName.equals(getDoctypeName())) { |
90 | 0 | return true; |
91 | |
} |
92 | 0 | DocumentType documentType = service.findByName(docTypeName); |
93 | 0 | while (documentType != null && documentType.getParentDocType() != null) { |
94 | 0 | documentType = documentType.getParentDocType(); |
95 | 0 | if(documentType.getName().equals(getDoctypeName())){ |
96 | 0 | return true; |
97 | |
} |
98 | |
} |
99 | 0 | } catch (XPathExpressionException e) { |
100 | 0 | throw new WorkflowRuntimeException(e); |
101 | 0 | } |
102 | |
|
103 | |
|
104 | 0 | if (ruleExtensions.isEmpty()) { |
105 | 0 | return true; |
106 | |
} |
107 | 0 | return false; |
108 | |
} |
109 | |
|
110 | |
protected String getRuleDocumentTypeFromRuleExtensions(List ruleExtensions) { |
111 | 0 | for (Iterator extensionsIterator = ruleExtensions.iterator(); extensionsIterator.hasNext();) { |
112 | 0 | RuleExtensionBo extension = (RuleExtensionBo) extensionsIterator.next(); |
113 | 0 | if (extension.getRuleTemplateAttribute().getRuleAttribute().getResourceDescriptor().equals(getClass().getName())) { |
114 | 0 | for (Iterator valuesIterator = extension.getExtensionValues().iterator(); valuesIterator.hasNext();) { |
115 | 0 | RuleExtensionValue extensionValue = (RuleExtensionValue) valuesIterator.next(); |
116 | 0 | String key = extensionValue.getKey(); |
117 | 0 | String value = extensionValue.getValue(); |
118 | 0 | if (key.equals(DOC_TYPE_NAME_KEY)) { |
119 | 0 | return value; |
120 | |
} |
121 | 0 | } |
122 | |
} |
123 | 0 | } |
124 | 0 | return null; |
125 | |
} |
126 | |
|
127 | |
public List getRuleRows() { |
128 | 0 | return rows; |
129 | |
} |
130 | |
|
131 | |
public List getRoutingDataRows() { |
132 | 0 | return rows; |
133 | |
} |
134 | |
|
135 | |
public String getDocContent() { |
136 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(getDoctypeName())) { |
137 | 0 | return "<ruleRouting><doctype>" + getDoctypeName() + "</doctype></ruleRouting>"; |
138 | |
} else { |
139 | 0 | return ""; |
140 | |
} |
141 | |
} |
142 | |
|
143 | |
|
144 | |
private String getDocTypNameFromXML(DocumentContent docContent) throws XPathExpressionException { |
145 | 0 | XPath xPath = XPathHelper.newXPath(); |
146 | 0 | String docTypeName = xPath.evaluate(DOC_TYPE_NAME_XPATH, docContent.getDocument()); |
147 | |
|
148 | 0 | if (StringUtils.isBlank(docTypeName)) { |
149 | 0 | docTypeName = xPath.evaluate(DOC_TYPE_NAME_DEL_XPATH, docContent.getDocument()); |
150 | |
|
151 | 0 | if (StringUtils.isBlank(docTypeName)) { |
152 | 0 | throw new WorkflowRuntimeException("Could not locate Document Type Name on the document: " + |
153 | |
docContent.getRouteContext().getDocument().getDocumentId()); |
154 | |
} |
155 | |
} |
156 | 0 | return docTypeName; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
public List<RuleRoutingAttribute> parseDocContent(DocumentContent docContent) { |
161 | |
try { |
162 | 0 | Document doc2 = (Document) XmlHelper.buildJDocument(new StringReader(docContent.getDocContent())); |
163 | |
|
164 | 0 | List<RuleRoutingAttribute> doctypeAttributes = new ArrayList<RuleRoutingAttribute>(); |
165 | 0 | Collection<Element> ruleRoutings = XmlHelper.findElements(doc2.getRootElement(), "docTypeName"); |
166 | 0 | List<String> usedDTs = new ArrayList<String>(); |
167 | 0 | for (Iterator<Element> iter = ruleRoutings.iterator(); iter.hasNext();) { |
168 | 0 | Element ruleRoutingElement = (Element) iter.next(); |
169 | |
|
170 | |
|
171 | 0 | Element docTypeElement = ruleRoutingElement; |
172 | 0 | String elTxt = docTypeElement.getText(); |
173 | 0 | if (docTypeElement != null && !usedDTs.contains(elTxt)) { |
174 | 0 | usedDTs.add(elTxt); |
175 | 0 | doctypeAttributes.add(new RuleRoutingAttribute(elTxt)); |
176 | |
} |
177 | 0 | } |
178 | |
|
179 | 0 | return doctypeAttributes; |
180 | 0 | } catch (Exception e) { |
181 | 0 | throw new RuntimeException(e); |
182 | |
} |
183 | |
} |
184 | |
|
185 | |
public List getRuleExtensionValues() { |
186 | 0 | List extensions = new ArrayList(); |
187 | |
|
188 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(getDoctypeName())) { |
189 | 0 | RuleExtensionValue extension = new RuleExtensionValue(); |
190 | 0 | extension.setKey(DOC_TYPE_NAME_KEY); |
191 | 0 | extension.setValue(getDoctypeName()); |
192 | 0 | extensions.add(extension); |
193 | |
} |
194 | |
|
195 | 0 | return extensions; |
196 | |
} |
197 | |
|
198 | |
public List validateRoutingData(Map paramMap) { |
199 | 0 | List errors = new ArrayList(); |
200 | 0 | setDoctypeName((String) paramMap.get(DOC_TYPE_NAME_PROPERTY)); |
201 | 0 | if (isRequired() && org.apache.commons.lang.StringUtils.isEmpty(getDoctypeName())) { |
202 | 0 | errors.add(new WorkflowServiceErrorImpl("doc type is not valid.", "routetemplate.ruleroutingattribute.doctype.invalid")); |
203 | |
} |
204 | |
|
205 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(getDoctypeName())) { |
206 | 0 | DocumentTypeService service = (DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE); |
207 | 0 | DocumentType documentType = service.findByName(getDoctypeName()); |
208 | 0 | if (documentType == null) { |
209 | 0 | errors.add(new WorkflowServiceErrorImpl("doc type is not valid", "routetemplate.ruleroutingattribute.doctype.invalid")); |
210 | |
} |
211 | |
} |
212 | 0 | return errors; |
213 | |
} |
214 | |
|
215 | |
public List validateRuleData(Map paramMap) { |
216 | 0 | return validateRoutingData(paramMap); |
217 | |
} |
218 | |
|
219 | |
public String getDoctypeName() { |
220 | 0 | return this.doctypeName; |
221 | |
} |
222 | |
|
223 | |
public void setDoctypeName(String docTypeName) { |
224 | 0 | this.doctypeName = docTypeName; |
225 | 0 | } |
226 | |
|
227 | |
public void setRequired(boolean required) { |
228 | 0 | this.required = required; |
229 | 0 | } |
230 | |
|
231 | |
public boolean isRequired() { |
232 | 0 | return required; |
233 | |
} |
234 | |
} |