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-2008 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.edl.impl.components;
 18  
 
 19  
 import java.util.Iterator;
 20  
 import java.util.List;
 21  
 
 22  
 import org.kuali.rice.edl.impl.EDLContext;
 23  
 import org.kuali.rice.edl.impl.RequestParser;
 24  
 import org.kuali.rice.kew.api.WorkflowDocument;
 25  
 import org.kuali.rice.kew.api.document.PropertyDefinition;
 26  
 import org.kuali.rice.kew.api.document.WorkflowAttributeDefinition;
 27  
 import org.kuali.rice.kew.api.document.WorkflowAttributeValidationError;
 28  
 import org.w3c.dom.Element;
 29  
 
 30  
 /**
 31  
  * Populates workflow rule attributes associated with the current configElement.
 32  
  * 
 33  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 34  
  * 
 35  
  */
 36  0
 public class AttributeEDLConfigComponent extends SimpleWorkflowEDLConfigComponent {
 37  
 
 38  
     public List getMatchingParams(Element originalConfigElement, RequestParser requestParser, EDLContext edlContext) {
 39  0
         List matchingParams = super.getMatchingParams(originalConfigElement, requestParser, edlContext);
 40  
         // we don't want to clear the attribute content if they are just opening up the document to view it!
 41  0
         if (!edlContext.getUserAction().isLoadAction()) {
 42  0
             String attributeName = originalConfigElement.getAttribute("attributeName");
 43  0
             String attributePropertyName = originalConfigElement.getAttribute("name");
 44  
 
 45  0
             WorkflowDocument document = (WorkflowDocument) requestParser
 46  
                     .getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
 47  
             // clear attribute content so that duplicate attribute values are not added during submission of a new EDL form values version
 48  0
             document.clearAttributeContent();
 49  
 
 50  0
             WorkflowAttributeDefinition.Builder attributeDefBuilder = getWorkflowAttributeDefinitionVO(attributeName, document);
 51  
             
 52  0
             for (Iterator iter = matchingParams.iterator(); iter.hasNext();) {
 53  0
                 MatchingParam param = (MatchingParam) iter.next();
 54  0
                 PropertyDefinition property = attributeDefBuilder.getPropertyDefinition(attributePropertyName);
 55  
                 //if the prop doesn't exist create it and add it to the definition otherwise update the property value
 56  0
                 if (property == null) {
 57  0
                     property = PropertyDefinition.create(attributePropertyName, param.getParamValue());
 58  
                 } else {
 59  
                     // modify the current property
 60  0
                     attributeDefBuilder.getPropertyDefinitions().remove(property);
 61  0
                     property = PropertyDefinition.create(property.getName(), param.getParamValue());
 62  
                 }
 63  0
                 attributeDefBuilder.addPropertyDefinition(property);
 64  
 
 65  0
             }
 66  
             
 67  0
             WorkflowAttributeDefinition attributeDef = attributeDefBuilder.build();
 68  0
             document.addAttributeDefinition(attributeDef);
 69  
 
 70  
             // validate if they are taking an action on the document (i.e. it's annotatable)
 71  0
             if (edlContext.getUserAction().isValidatableAction()) {
 72  0
                 List<WorkflowAttributeValidationError> errors = document.validateAttributeDefinition(attributeDef);
 73  0
                 if (!errors.isEmpty()) {
 74  0
                     getEdlContext().setInError(true);
 75  
                 }
 76  0
                 for (WorkflowAttributeValidationError error : errors) {
 77  0
                     MatchingParam param = getMatchingParam(matchingParams, error.getKey());
 78  
                     // if it doesn't match a param, then this is a global error
 79  0
                     if (param == null) {
 80  0
                         List globalErrors = (List) getEdlContext().getRequestParser().getAttribute(
 81  
                                     RequestParser.GLOBAL_ERRORS_KEY);
 82  0
                         globalErrors.add(error.getMessage());
 83  0
                     } else {
 84  0
                         param.setError(Boolean.TRUE);
 85  0
                         param.setErrorMessage(error.getMessage());
 86  
                     }
 87  0
                 }
 88  
             }
 89  
         }
 90  0
         return matchingParams;
 91  
     }
 92  
 
 93  
     private WorkflowAttributeDefinition.Builder getWorkflowAttributeDefinitionVO(String attributeName, WorkflowDocument document) {
 94  0
         for (WorkflowAttributeDefinition attributeDefinition : document.getAttributeDefinitions()) {
 95  0
             if (attributeDefinition.getAttributeName().equals(attributeName)) {
 96  0
                 return WorkflowAttributeDefinition.Builder.create(attributeDefinition);
 97  
             }
 98  
         }
 99  0
         return WorkflowAttributeDefinition.Builder.create(attributeName);
 100  
     }
 101  
 
 102  
     private MatchingParam getMatchingParam(List matchingParams, String name) {
 103  0
         for (Iterator iterator = matchingParams.iterator(); iterator.hasNext();) {
 104  0
             MatchingParam param = (MatchingParam) iterator.next();
 105  0
             if (param.getParamName().equals(name)) {
 106  0
                 return param;
 107  
             }
 108  0
         }
 109  0
         return null;
 110  
     }
 111  
 }