View Javadoc

1   /*
2    * Copyright 2007-2009 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.documentserializer;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.rice.kns.bo.BusinessObject;
23  import org.kuali.rice.kns.datadictionary.DataDictionary;
24  import org.kuali.rice.kns.datadictionary.MaintainableCollectionDefinition;
25  import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
26  import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition;
27  import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition;
28  import org.kuali.rice.kns.datadictionary.MaintainableSubSectionHeaderDefinition;
29  import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry;
30  import org.kuali.rice.kns.service.KNSServiceLocator;
31  
32  
33  public class MaintenanceDocumentPropertySerializibilityEvaluator 
34  				extends PropertySerializabilityEvaluatorBase implements PropertySerializabilityEvaluator {
35  
36      /**
37       * Reads the data dictionary to determine which properties of the document should be serialized.
38       * 
39       * @see org.kuali.rice.kns.util.documentserializer.PropertySerializabilityEvaluator#initializeEvaluator(org.kuali.rice.kns.document.Document)
40       */
41      public void initializeEvaluator(BusinessObject businessObject){
42          DataDictionary dictionary = KNSServiceLocator.getDataDictionaryService().getDataDictionary();
43          MaintenanceDocumentEntry maintenanceDocumentEntry = 
44          	dictionary.getMaintenanceDocumentEntryForBusinessObjectClass(businessObject.getClass());
45          serializableProperties = new PropertySerializerTrie();
46          populateSerializableProperties(maintenanceDocumentEntry.getMaintainableSections());
47          serializableProperties.addSerializablePropertyName("boNotes", true);
48          serializableProperties.addSerializablePropertyName("boNotes.attachment", true);
49      }
50      
51      private void populateSerializableProperties(List<MaintainableSectionDefinition> maintainableSectionDefinitions){
52          for(MaintainableSectionDefinition maintainableSectionDefinition: maintainableSectionDefinitions){
53          	populateSerializablePropertiesWithItems("", maintainableSectionDefinition.getMaintainableItems());
54          }
55      }
56  
57      private void populateSerializablePropertiesWithItems(String basePath, List<MaintainableItemDefinition> maintainableItems){
58      	for(MaintainableItemDefinition maintainableItemDefinition: maintainableItems){
59              if(maintainableItemDefinition instanceof MaintainableFieldDefinition){
60                  serializableProperties.addSerializablePropertyName(getFullItemName(basePath, maintainableItemDefinition.getName()), true);
61              } else if(maintainableItemDefinition instanceof MaintainableCollectionDefinition){
62              	serializableProperties.addSerializablePropertyName(getFullItemName(basePath, maintainableItemDefinition.getName()), true);
63              	populateSerializablePropertiesWithItems(getFullItemName(basePath, maintainableItemDefinition.getName()), 
64              			getAllMaintainableFieldDefinitionsForSerialization(
65              			(MaintainableCollectionDefinition)maintainableItemDefinition));            	
66              } else if(maintainableItemDefinition instanceof MaintainableSubSectionHeaderDefinition){
67              	//Ignore
68              }
69      	}
70      }
71  
72      private String getFullItemName(String basePath, String itemName){
73      	return StringUtils.isEmpty(basePath) ? itemName : basePath+"."+itemName;
74      }
75      
76      public List<MaintainableItemDefinition> getAllMaintainableFieldDefinitionsForSerialization(
77      		MaintainableCollectionDefinition maintainableCollectionDefinition){
78  		List<MaintainableItemDefinition> allMaintainableItemDefinitions = new ArrayList<MaintainableItemDefinition>();
79  
80  		if(maintainableCollectionDefinition.getMaintainableFields()!=null){
81  			allMaintainableItemDefinitions.addAll(maintainableCollectionDefinition.getMaintainableFields());
82  		}
83  
84  		if(maintainableCollectionDefinition.getSummaryFields()!=null){
85  			allMaintainableItemDefinitions.addAll(
86  					(List<MaintainableFieldDefinition>)maintainableCollectionDefinition.getSummaryFields());
87  		}
88  
89  		if(maintainableCollectionDefinition.getDuplicateIdentificationFields()!=null){
90  			allMaintainableItemDefinitions.addAll(maintainableCollectionDefinition.getDuplicateIdentificationFields());
91  		}
92  
93  		/*if(maintainableCollectionMap!=null){
94  			updateMaintainableCollectionDefinitionForSerialization(maintainableCollectionMap.values());
95  			allMaintainableItemDefinitions.addAll(maintainableCollectionMap.values());
96  		}*/
97  		if(maintainableCollectionDefinition.getMaintainableCollections()!=null){
98  			allMaintainableItemDefinitions.addAll(maintainableCollectionDefinition.getMaintainableCollections());
99  		}
100 
101 		return allMaintainableItemDefinitions;
102 	}
103     
104 }