View Javadoc

1   /**
2    * Copyright 2005-2014 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.core.api.uif.RemotableAttributeErrorContract;
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.attribute.WorkflowAttributeDefinition;
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  public class AttributeEDLConfigComponent extends SimpleWorkflowEDLConfigComponent {
36  
37      public List getMatchingParams(Element originalConfigElement, RequestParser requestParser, EDLContext edlContext) {
38          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          if (!edlContext.getUserAction().isLoadAction()) {
41              String attributeName = originalConfigElement.getAttribute("attributeName");
42              String attributePropertyName = originalConfigElement.getAttribute("name");
43  
44              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              document.clearAttributeContent();
48  
49              WorkflowAttributeDefinition.Builder attributeDefBuilder = getWorkflowAttributeDefinitionVO(attributeName, document);
50              
51              for (Iterator iter = matchingParams.iterator(); iter.hasNext();) {
52                  MatchingParam param = (MatchingParam) iter.next();
53                  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                  if (property == null) {
56                      property = PropertyDefinition.create(attributePropertyName, param.getParamValue());
57                  } else {
58                      // modify the current property
59                      attributeDefBuilder.getPropertyDefinitions().remove(property);
60                      property = PropertyDefinition.create(property.getName(), param.getParamValue());
61                  }
62                  attributeDefBuilder.addPropertyDefinition(property);
63  
64              }
65              
66              WorkflowAttributeDefinition attributeDef = attributeDefBuilder.build();
67              document.addAttributeDefinition(attributeDef);
68  
69              // validate if they are taking an action on the document (i.e. it's annotatable)
70              if (edlContext.getUserAction().isValidatableAction()) {
71                  List<? extends RemotableAttributeErrorContract> errors = document.validateAttributeDefinition(attributeDef);
72                  if (!errors.isEmpty()) {
73                      getEdlContext().setInError(true);
74                  }
75                  for (RemotableAttributeErrorContract error : errors) {
76                      MatchingParam param = getMatchingParam(matchingParams, error.getAttributeName());
77                      // if it doesn't match a param, then this is a global error
78                      if (param == null) {
79                          List globalErrors = (List) getEdlContext().getRequestParser().getAttribute(
80                                      RequestParser.GLOBAL_ERRORS_KEY);
81                          globalErrors.add(error.getMessage());
82                      } else {
83                          param.setError(Boolean.TRUE);
84                          param.setErrorMessage(error.getMessage());
85                      }
86                  }
87              }
88          }
89          return matchingParams;
90      }
91  
92      private WorkflowAttributeDefinition.Builder getWorkflowAttributeDefinitionVO(String attributeName, WorkflowDocument document) {
93          for (WorkflowAttributeDefinition attributeDefinition : document.getAttributeDefinitions()) {
94              if (attributeDefinition.getAttributeName().equals(attributeName)) {
95                  return WorkflowAttributeDefinition.Builder.create(attributeDefinition);
96              }
97          }
98          return WorkflowAttributeDefinition.Builder.create(attributeName);
99      }
100 
101     private MatchingParam getMatchingParam(List matchingParams, String name) {
102         for (Iterator iterator = matchingParams.iterator(); iterator.hasNext();) {
103             MatchingParam param = (MatchingParam) iterator.next();
104             if (param.getParamName().equals(name)) {
105                 return param;
106             }
107         }
108         return null;
109     }
110 }