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