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