Coverage Report - org.kuali.rice.edl.impl.components.GlobalAttributeComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
GlobalAttributeComponent
0%
0/57
0%
0/26
9.5
 
 1  
 /*
 2  
  * Copyright 2008-2009 The Kuali Foundation
 3  
  * 
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * Executes validations and generates XML for workflow attributes that are defined on the EDL Definitions.
 41  
  * These attribute definitions exist in a form similiar to the following:
 42  
  *
 43  
  * <attributes>
 44  
  *   <attribute name="AccountAttribute">
 45  
  *     <field edlField="finCoaCd" attributeField="finCoaCd"/>
 46  
  *     <field edlField="accountNbr" attributeField="accountNbr"/>
 47  
  *     <field edlField="totalDollarAmount" attributeField="totalDollarAmount"/>
 48  
  *   </attribute>
 49  
  * </attributes>
 50  
  *
 51  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 52  
  */
 53  0
 public class GlobalAttributeComponent extends SimpleWorkflowEDLConfigComponent implements EDLModelComponent  {
 54  
 
 55  
         public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
 56  
             //String action = edlContext.getRequestParser().getParameterValueAsString(WorkflowDocumentActions.USER_ACTION_REQUEST_KEY);
 57  
             // we don't want to clear the attribute content if they are just opening up the document to view it!
 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  
                         //                         clear attribute content so that duplicate attribute values are not added during submission of a new EDL form values version
 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  
                             // modify the current property
 85  0
                             attributeDefBuilder.getPropertyDefinitions().remove(property);
 86  0
                             property = PropertyDefinition.create(property.getName(), value);
 87  
                                         }
 88  0
                                         attributeDefBuilder.addPropertyDefinition(property);
 89  
                                 }                                
 90  
                                 
 91  
                                 // validate if they are taking an action on the document (i.e. it's annotatable)
 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  
                                     // modify the current property
 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  
 }