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.kns.util.documentserlializer;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.kns.datadictionary.MaintainableCollectionDefinition;
20  import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
21  import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition;
22  import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition;
23  import org.kuali.rice.kns.datadictionary.MaintainableSubSectionHeaderDefinition;
24  import org.kuali.rice.krad.bo.BusinessObject;
25  import org.kuali.rice.krad.datadictionary.DataDictionary;
26  import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry;
27  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
28  import org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator;
29  import org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluatorBase;
30  import org.kuali.rice.krad.util.documentserializer.PropertySerializerTrie;
31  import org.kuali.rice.krad.util.documentserializer.PropertyType;
32  
33  import java.util.ArrayList;
34  import java.util.Collection;
35  import java.util.List;
36  import java.util.Map;
37  
38  public class MaintenanceDocumentPropertySerializibilityEvaluator
39  				extends PropertySerializabilityEvaluatorBase implements PropertySerializabilityEvaluator {
40  
41      /**
42       * Reads the data dictionary to determine which properties of the document should be serialized.
43       * 
44       * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator#initializeEvaluator(org.kuali.rice.krad.document.Document)
45       */
46  	@Override
47      public void initializeEvaluatorForDataObject(Object businessObject){
48          DataDictionary dictionary = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary();
49          MaintenanceDocumentEntry maintenanceDocumentEntry = (MaintenanceDocumentEntry)
50          	dictionary.getMaintenanceDocumentEntryForBusinessObjectClass(businessObject.getClass());
51          serializableProperties = new PropertySerializerTrie();
52          populateSerializableProperties(maintenanceDocumentEntry.getMaintainableSections());
53          serializableProperties.addSerializablePropertyName("boNotes", true);
54          serializableProperties.addSerializablePropertyName("boNotes.attachment", true);
55      }
56  
57      /**
58       * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator#determinePropertyType(java.lang.Object)
59       */
60      @Override
61      public PropertyType determinePropertyType(Object propertyValue) {
62          if (propertyValue == null) {
63              return PropertyType.PRIMITIVE;
64          }
65  
66          if (propertyValue instanceof BusinessObject) {
67              return PropertyType.BUSINESS_OBJECT;
68          }
69  
70          if (propertyValue instanceof Collection) {
71              return PropertyType.COLLECTION;
72          }
73  
74          // In the case of Maintenance Documents treat Maps as PRIMITIVE
75          if (propertyValue instanceof Map) {
76              return PropertyType.PRIMITIVE;
77          }
78  
79          return PropertyType.PRIMITIVE;
80      }
81  
82      private void populateSerializableProperties(List<MaintainableSectionDefinition> maintainableSectionDefinitions){
83          for(MaintainableSectionDefinition maintainableSectionDefinition: maintainableSectionDefinitions){
84          	populateSerializablePropertiesWithItems("", maintainableSectionDefinition.getMaintainableItems());
85          }
86      }
87  
88      private void populateSerializablePropertiesWithItems(String basePath, List<MaintainableItemDefinition> maintainableItems){
89      	for(MaintainableItemDefinition maintainableItemDefinition: maintainableItems){
90              if(maintainableItemDefinition instanceof MaintainableFieldDefinition){
91                  serializableProperties.addSerializablePropertyName(getFullItemName(basePath, maintainableItemDefinition.getName()), true);
92              } else if(maintainableItemDefinition instanceof MaintainableCollectionDefinition){
93              	serializableProperties.addSerializablePropertyName(getFullItemName(basePath, maintainableItemDefinition.getName()), true);
94              	populateSerializablePropertiesWithItems(getFullItemName(basePath, maintainableItemDefinition.getName()), 
95              			getAllMaintainableFieldDefinitionsForSerialization(
96              			(MaintainableCollectionDefinition)maintainableItemDefinition));            	
97              } else if(maintainableItemDefinition instanceof MaintainableSubSectionHeaderDefinition){
98              	//Ignore
99              }
100     	}
101     }
102 
103     private String getFullItemName(String basePath, String itemName){
104     	return StringUtils.isEmpty(basePath) ? itemName : basePath+"."+itemName;
105     }
106     
107     public List<MaintainableItemDefinition> getAllMaintainableFieldDefinitionsForSerialization(
108     		MaintainableCollectionDefinition maintainableCollectionDefinition){
109 		List<MaintainableItemDefinition> allMaintainableItemDefinitions = new ArrayList<MaintainableItemDefinition>();
110 
111 		if(maintainableCollectionDefinition.getMaintainableFields()!=null){
112 			allMaintainableItemDefinitions.addAll(maintainableCollectionDefinition.getMaintainableFields());
113 		}
114 
115 		if(maintainableCollectionDefinition.getSummaryFields()!=null){
116 			allMaintainableItemDefinitions.addAll(
117 					(List<MaintainableFieldDefinition>)maintainableCollectionDefinition.getSummaryFields());
118 		}
119 
120 		if(maintainableCollectionDefinition.getDuplicateIdentificationFields()!=null){
121 			allMaintainableItemDefinitions.addAll(maintainableCollectionDefinition.getDuplicateIdentificationFields());
122 		}
123 
124 		/*if(maintainableCollectionMap!=null){
125 			updateMaintainableCollectionDefinitionForSerialization(maintainableCollectionMap.values());
126 			allMaintainableItemDefinitions.addAll(maintainableCollectionMap.values());
127 		}*/
128 		if(maintainableCollectionDefinition.getMaintainableCollections()!=null){
129 			allMaintainableItemDefinitions.addAll(maintainableCollectionDefinition.getMaintainableCollections());
130 		}
131 
132 		return allMaintainableItemDefinitions;
133 	}
134     
135 }