View Javadoc

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