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