Coverage Report - org.kuali.rice.edl.impl.components.AttributeEDLConfigComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
AttributeEDLConfigComponent
0%
0/43
0%
0/22
5.333
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.Iterator;
 19  
 import java.util.List;
 20  
 
 21  
 import org.kuali.rice.edl.impl.EDLContext;
 22  
 import org.kuali.rice.edl.impl.RequestParser;
 23  
 import org.kuali.rice.kew.api.WorkflowDocument;
 24  
 import org.kuali.rice.kew.api.document.PropertyDefinition;
 25  
 import org.kuali.rice.kew.api.document.attribute.WorkflowAttributeDefinition;
 26  
 import org.kuali.rice.kew.api.document.attribute.WorkflowAttributeValidationError;
 27  
 import org.w3c.dom.Element;
 28  
 
 29  
 /**
 30  
  * Populates workflow rule attributes associated with the current configElement.
 31  
  * 
 32  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 33  
  * 
 34  
  */
 35  0
 public class AttributeEDLConfigComponent extends SimpleWorkflowEDLConfigComponent {
 36  
 
 37  
     public List getMatchingParams(Element originalConfigElement, RequestParser requestParser, EDLContext edlContext) {
 38  0
         List matchingParams = super.getMatchingParams(originalConfigElement, requestParser, edlContext);
 39  
         // we don't want to clear the attribute content if they are just opening up the document to view it!
 40  0
         if (!edlContext.getUserAction().isLoadAction()) {
 41  0
             String attributeName = originalConfigElement.getAttribute("attributeName");
 42  0
             String attributePropertyName = originalConfigElement.getAttribute("name");
 43  
 
 44  0
             WorkflowDocument document = (WorkflowDocument) requestParser
 45  
                     .getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
 46  
             // clear attribute content so that duplicate attribute values are not added during submission of a new EDL form values version
 47  0
             document.clearAttributeContent();
 48  
 
 49  0
             WorkflowAttributeDefinition.Builder attributeDefBuilder = getWorkflowAttributeDefinitionVO(attributeName, document);
 50  
             
 51  0
             for (Iterator iter = matchingParams.iterator(); iter.hasNext();) {
 52  0
                 MatchingParam param = (MatchingParam) iter.next();
 53  0
                 PropertyDefinition property = attributeDefBuilder.getPropertyDefinition(attributePropertyName);
 54  
                 //if the prop doesn't exist create it and add it to the definition otherwise update the property value
 55  0
                 if (property == null) {
 56  0
                     property = PropertyDefinition.create(attributePropertyName, param.getParamValue());
 57  
                 } else {
 58  
                     // modify the current property
 59  0
                     attributeDefBuilder.getPropertyDefinitions().remove(property);
 60  0
                     property = PropertyDefinition.create(property.getName(), param.getParamValue());
 61  
                 }
 62  0
                 attributeDefBuilder.addPropertyDefinition(property);
 63  
 
 64  0
             }
 65  
             
 66  0
             WorkflowAttributeDefinition attributeDef = attributeDefBuilder.build();
 67  0
             document.addAttributeDefinition(attributeDef);
 68  
 
 69  
             // validate if they are taking an action on the document (i.e. it's annotatable)
 70  0
             if (edlContext.getUserAction().isValidatableAction()) {
 71  0
                 List<WorkflowAttributeValidationError> errors = document.validateAttributeDefinition(attributeDef);
 72  0
                 if (!errors.isEmpty()) {
 73  0
                     getEdlContext().setInError(true);
 74  
                 }
 75  0
                 for (WorkflowAttributeValidationError error : errors) {
 76  0
                     MatchingParam param = getMatchingParam(matchingParams, error.getKey());
 77  
                     // if it doesn't match a param, then this is a global error
 78  0
                     if (param == null) {
 79  0
                         List globalErrors = (List) getEdlContext().getRequestParser().getAttribute(
 80  
                                     RequestParser.GLOBAL_ERRORS_KEY);
 81  0
                         globalErrors.add(error.getMessage());
 82  0
                     } else {
 83  0
                         param.setError(Boolean.TRUE);
 84  0
                         param.setErrorMessage(error.getMessage());
 85  
                     }
 86  0
                 }
 87  
             }
 88  
         }
 89  0
         return matchingParams;
 90  
     }
 91  
 
 92  
     private WorkflowAttributeDefinition.Builder getWorkflowAttributeDefinitionVO(String attributeName, WorkflowDocument document) {
 93  0
         for (WorkflowAttributeDefinition attributeDefinition : document.getAttributeDefinitions()) {
 94  0
             if (attributeDefinition.getAttributeName().equals(attributeName)) {
 95  0
                 return WorkflowAttributeDefinition.Builder.create(attributeDefinition);
 96  
             }
 97  
         }
 98  0
         return WorkflowAttributeDefinition.Builder.create(attributeName);
 99  
     }
 100  
 
 101  
     private MatchingParam getMatchingParam(List matchingParams, String name) {
 102  0
         for (Iterator iterator = matchingParams.iterator(); iterator.hasNext();) {
 103  0
             MatchingParam param = (MatchingParam) iterator.next();
 104  0
             if (param.getParamName().equals(name)) {
 105  0
                 return param;
 106  
             }
 107  0
         }
 108  0
         return null;
 109  
     }
 110  
 }