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