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