View Javadoc
1   /*
2    * Copyright 2008 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.ole.sys.spring.datadictionary;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.rice.krad.datadictionary.DocumentCollectionPath;
25  import org.kuali.rice.krad.datadictionary.DocumentValuePathGroup;
26  import org.kuali.rice.krad.datadictionary.RoutingAttribute;
27  import org.kuali.rice.krad.datadictionary.RoutingTypeDefinition;
28  import org.kuali.rice.krad.datadictionary.SearchingAttribute;
29  import org.kuali.rice.krad.datadictionary.SearchingTypeDefinition;
30  import org.kuali.rice.krad.datadictionary.WorkflowAttributeMetadata;
31  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
32  import org.springframework.beans.factory.xml.ParserContext;
33  import org.w3c.dom.Element;
34  import org.w3c.dom.Node;
35  import org.w3c.dom.NodeList;
36  
37  public class WorkflowAttributesBeanDefinitionParser extends KualiBeanDefinitionParserBase {
38      private static final String SEARCHING_ATTRIBUTE = "searchingAttribute";
39      private static final String ROUTING_ATTRIBUTE = "routingAttribute";
40      private static final String DOCUMENT_VALUE_ATTRIBUTE = "documentValue";
41      private static final String SEARCHING_TYPES_ELEMENT = "searchingType";
42      private static final String ROUTING_TYPES_ELEMENT = "routingType";
43  
44      @Override
45      protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder bean) {
46          handleAbstractAttribute(element, bean);
47  
48          parseChildElements(element, bean);
49      }
50  
51      protected void parseChildElements(Element workflowAttributeElement, BeanDefinitionBuilder bean) {
52          NodeList children = workflowAttributeElement.getChildNodes();
53          List<SearchingTypeDefinition> searchingAttributesMap = parseSearchableAttributes(children);
54          bean.addPropertyValue("searchingTypeDefinitions", searchingAttributesMap);
55          
56          Map<String, RoutingTypeDefinition> routingTypesMap = parseRoutingTypes(children);
57          bean.addPropertyValue("routingTypeDefinitions", routingTypesMap);
58      }
59      
60      protected List<SearchingTypeDefinition> parseSearchableAttributes(NodeList workflowAttributesChildren) {
61          List<SearchingTypeDefinition> searchingAttributesMap = new ArrayList<SearchingTypeDefinition>();
62          for (int i = 0; i < workflowAttributesChildren.getLength(); i++) {
63              Node workflowAttributesChild = workflowAttributesChildren.item(i);
64              if (workflowAttributesChild.getNodeType() == Node.ELEMENT_NODE) {
65                  if (((Element) workflowAttributesChild).getLocalName().equals(SEARCHING_TYPES_ELEMENT)) {
66                      
67                      SearchingTypeDefinition searchingTypeDefinition = parseSearchingTypes(workflowAttributesChild.getChildNodes());
68                      
69                      searchingAttributesMap.add(searchingTypeDefinition);
70                  }
71              }
72          }
73          return searchingAttributesMap;
74      }
75      
76      protected List<Element> extractWorkflowAttributeElements(NodeList nodes, String attributeElementName) {
77          List<Element> results = new ArrayList<Element>();
78          
79          for (int i = 0; i < nodes.getLength(); i++) {
80              Node node = nodes.item(i);
81              if (node.getNodeType() == Node.ELEMENT_NODE) {
82                  if (StringUtils.isEmpty(attributeElementName) || ((Element) node).getLocalName().equals(attributeElementName)) {
83                      results.add((Element) node);
84                  }
85              }
86          }
87          return results;
88      }
89      
90      protected SearchingTypeDefinition parseSearchingTypes(NodeList workflowAttributesChildren) {
91          SearchingTypeDefinition  searchingTypeDefinition  = new SearchingTypeDefinition();
92          
93          for (int i = 0; i < workflowAttributesChildren.getLength(); i++) {
94              Node workflowAttributesChild = workflowAttributesChildren.item(i);
95              if (workflowAttributesChild.getNodeType() == Node.ELEMENT_NODE) {
96                  if (((Element) workflowAttributesChild).getLocalName().equals(SEARCHING_ATTRIBUTE)){
97                      searchingTypeDefinition.setSearchingAttribute((SearchingAttribute)parseAttributeDefinition((Element) workflowAttributesChild));
98                  }else if(((Element) workflowAttributesChild).getLocalName().equals(DOCUMENT_VALUE_ATTRIBUTE)){
99                      if(searchingTypeDefinition.getDocumentValues() == null){
100                         searchingTypeDefinition.setDocumentValues(new ArrayList<String>());
101                     }
102                     searchingTypeDefinition.getDocumentValues().addAll(parseDocumentValueAttributeDefinition((Element) workflowAttributesChild));
103                 }
104             }
105         }
106         
107         return searchingTypeDefinition;
108     }
109     protected WorkflowAttributeMetadata parseAttributeDefinition(Element workflowAttributeDefinitionElement) {
110         WorkflowAttributeMetadata workflowAttributeMetadata = null;
111         if(workflowAttributeDefinitionElement.getLocalName().equals(SEARCHING_ATTRIBUTE)){
112             return parseSearchingAttribute(workflowAttributeDefinitionElement);
113         }else if(workflowAttributeDefinitionElement.getLocalName().equals(ROUTING_ATTRIBUTE)){
114             return parseRoutingAttribute(workflowAttributeDefinitionElement);
115         }
116         return workflowAttributeMetadata;
117     }
118     
119     
120     protected WorkflowAttributeMetadata parseSearchingAttribute(Element workflowAttributeDefinitionElement) {
121         SearchingAttribute workflowAttributeMetadata = new SearchingAttribute();
122 
123         String businessObjectClassName = workflowAttributeDefinitionElement.getAttribute("businessObjectClassName");
124         if (StringUtils.isNotBlank(businessObjectClassName)) {
125             try {
126                 Class.forName(businessObjectClassName);
127                 workflowAttributeMetadata.setBusinessObjectClassName(businessObjectClassName);
128             }
129             catch (ClassNotFoundException e) {
130                 throw new RuntimeException("Unable to find class of name " + businessObjectClassName + " when parsing workflowAttribute");
131             }
132         }
133         String attributeName = workflowAttributeDefinitionElement.getAttribute("attributeName");
134         if (StringUtils.isNotBlank(attributeName)) {
135             workflowAttributeMetadata.setAttributeName(attributeName);
136         }
137         
138         return workflowAttributeMetadata;
139     }
140     
141     protected WorkflowAttributeMetadata parseRoutingAttribute(Element workflowAttributeDefinitionElement) {
142         RoutingAttribute workflowAttributeMetadata = new RoutingAttribute();
143 
144         String attributeName = workflowAttributeDefinitionElement.getAttribute("qualificationAttributeName");
145         if (StringUtils.isNotBlank(attributeName)) {
146             workflowAttributeMetadata.setQualificationAttributeName(attributeName);
147         }
148         
149         return workflowAttributeMetadata;
150     }
151     
152     protected List<String> parseDocumentValueAttributeDefinition(Element workflowAttributeDefinitionElement) {
153 
154         List<String>paths = new ArrayList<String>();
155         paths.add(workflowAttributeDefinitionElement.getAttribute("path"));
156        
157         return paths;
158     }
159     protected Map<String, RoutingTypeDefinition> parseRoutingTypes(NodeList workflowAttributesChildren) {
160         Map<String, RoutingTypeDefinition> routingTypesMap = new HashMap<String, RoutingTypeDefinition>();
161         
162         for (int i = 0; i < workflowAttributesChildren.getLength(); i++) {
163             Node workflowAttributesChild = workflowAttributesChildren.item(i);
164             if (workflowAttributesChild.getNodeType() == Node.ELEMENT_NODE) {
165                 if (((Element) workflowAttributesChild).getLocalName().equals(ROUTING_TYPES_ELEMENT)) {
166                     RoutingTypeDefinition routingTypeDefinition = new RoutingTypeDefinition();
167                     Element routingTypeElement = (Element) workflowAttributesChild;
168                     
169                     String name = routingTypeElement.getAttribute("nodeName");
170                     
171                     List<RoutingAttribute> routingAttributes = new ArrayList<RoutingAttribute>();
172                     List<DocumentValuePathGroup> documentValuePathGroups = new ArrayList<DocumentValuePathGroup>();
173                     List<Element> workflowAttributeList = extractWorkflowAttributeElements(routingTypeElement.getChildNodes(), "");
174                     for (int j = 0; j < workflowAttributeList.size(); j++) {
175                         Element workflowAttributeDefinitionElement = (Element) workflowAttributeList.get(j);
176                         if(workflowAttributeDefinitionElement.getLocalName().equals("routingAttributes")){
177                             List<Element> routingAttributeList = extractWorkflowAttributeElements(workflowAttributeDefinitionElement.getChildNodes(), "routingAttribute");
178                             for(Element routingAttribute:routingAttributeList){
179                                 routingAttributes.add((RoutingAttribute)parseAttributeDefinition(routingAttribute));
180                             }
181                         }
182                         else if(workflowAttributeDefinitionElement.getLocalName().equals("documentValuePathGroup")){
183                             documentValuePathGroups.add(parseDocumentValuesPathGroup(workflowAttributeDefinitionElement));
184                         }
185                     }
186                     routingTypeDefinition.setDocumentValuePathGroups(documentValuePathGroups);
187                     routingTypeDefinition.setRoutingAttributes(routingAttributes);
188                     routingTypesMap.put(name, routingTypeDefinition);
189                 }
190             }
191         }
192         
193         return routingTypesMap;
194     }
195     
196     protected DocumentCollectionPath parseDocumentCollectionPath(Element workflowAttributeDefinitionElement) {
197         DocumentCollectionPath documentCollectionPath = new DocumentCollectionPath();
198         for(Element element:extractWorkflowAttributeElements(workflowAttributeDefinitionElement.getChildNodes(), "documentCollectionPath")){
199             documentCollectionPath.setNestedCollection(parseDocumentCollectionPath(element));
200         }
201         List<String>paths = new ArrayList<String>();
202         for(Element element:extractWorkflowAttributeElements(workflowAttributeDefinitionElement.getChildNodes(), "documentValue")){
203             paths.addAll(parseDocumentValueAttributeDefinition(element));
204         }
205         documentCollectionPath.setDocumentValues(paths);
206         
207         String collectionName = workflowAttributeDefinitionElement.getAttribute("path");
208         documentCollectionPath.setCollectionPath(collectionName);
209         
210         return documentCollectionPath;
211     }
212     protected DocumentValuePathGroup parseDocumentValuesPathGroup(Element workflowAttributeDefinitionElement) {
213         DocumentValuePathGroup documentValuePathGroup = new DocumentValuePathGroup();
214         
215         List<Element> documentCollectionPathElements = extractWorkflowAttributeElements(workflowAttributeDefinitionElement.getChildNodes(), "documentCollectionPath");
216         if( documentCollectionPathElements.size() > 0){
217             documentValuePathGroup.setDocumentCollectionPath(parseDocumentCollectionPath(documentCollectionPathElements.get(0)));
218         }
219         List<String>paths = new ArrayList<String>();
220         for(Element element:extractWorkflowAttributeElements(workflowAttributeDefinitionElement.getChildNodes(), "documentValue")){
221             paths.addAll(parseDocumentValueAttributeDefinition(element));
222         }
223         documentValuePathGroup.setDocumentValues(paths);
224 
225         return documentValuePathGroup;
226     }
227     
228     @Override
229     protected String getBaseBeanTypeParent(Element element) {
230         return "WorkflowAttributes";
231     }
232 
233 }