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