001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kns.util.documentserlializer;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kns.datadictionary.MaintainableCollectionDefinition;
020import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
021import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition;
022import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition;
023import org.kuali.rice.kns.datadictionary.MaintainableSubSectionHeaderDefinition;
024import org.kuali.rice.krad.bo.BusinessObject;
025import org.kuali.rice.krad.datadictionary.DataDictionary;
026import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry;
027import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
028import org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator;
029import org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluatorBase;
030import org.kuali.rice.krad.util.documentserializer.PropertySerializerTrie;
031import org.kuali.rice.krad.util.documentserializer.PropertyType;
032
033import java.util.ArrayList;
034import java.util.Collection;
035import java.util.List;
036import java.util.Map;
037
038public class MaintenanceDocumentPropertySerializibilityEvaluator
039                                extends PropertySerializabilityEvaluatorBase implements PropertySerializabilityEvaluator {
040
041    /**
042     * Reads the data dictionary to determine which properties of the document should be serialized.
043     * 
044     * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator#initializeEvaluator(org.kuali.rice.krad.document.Document)
045     */
046        @Override
047    public void initializeEvaluatorForDataObject(Object businessObject){
048        DataDictionary dictionary = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary();
049        MaintenanceDocumentEntry maintenanceDocumentEntry = (MaintenanceDocumentEntry)
050                dictionary.getMaintenanceDocumentEntryForBusinessObjectClass(businessObject.getClass());
051        serializableProperties = new PropertySerializerTrie();
052        populateSerializableProperties(maintenanceDocumentEntry.getMaintainableSections());
053        serializableProperties.addSerializablePropertyName("boNotes", true);
054        serializableProperties.addSerializablePropertyName("boNotes.attachment", true);
055    }
056
057    /**
058     * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator#determinePropertyType(java.lang.Object)
059     */
060    @Override
061    public PropertyType determinePropertyType(Object propertyValue) {
062        if (propertyValue == null) {
063            return PropertyType.PRIMITIVE;
064        }
065
066        if (propertyValue instanceof BusinessObject) {
067            return PropertyType.BUSINESS_OBJECT;
068        }
069
070        if (propertyValue instanceof Collection) {
071            return PropertyType.COLLECTION;
072        }
073
074        // In the case of Maintenance Documents treat Maps as PRIMITIVE
075        if (propertyValue instanceof Map) {
076            return PropertyType.PRIMITIVE;
077        }
078
079        return PropertyType.PRIMITIVE;
080    }
081
082    private void populateSerializableProperties(List<MaintainableSectionDefinition> maintainableSectionDefinitions){
083        for(MaintainableSectionDefinition maintainableSectionDefinition: maintainableSectionDefinitions){
084                populateSerializablePropertiesWithItems("", maintainableSectionDefinition.getMaintainableItems());
085        }
086    }
087
088    private void populateSerializablePropertiesWithItems(String basePath, List<MaintainableItemDefinition> maintainableItems){
089        for(MaintainableItemDefinition maintainableItemDefinition: maintainableItems){
090            if(maintainableItemDefinition instanceof MaintainableFieldDefinition){
091                serializableProperties.addSerializablePropertyName(getFullItemName(basePath, maintainableItemDefinition.getName()), true);
092            } else if(maintainableItemDefinition instanceof MaintainableCollectionDefinition){
093                serializableProperties.addSerializablePropertyName(getFullItemName(basePath, maintainableItemDefinition.getName()), true);
094                populateSerializablePropertiesWithItems(getFullItemName(basePath, maintainableItemDefinition.getName()), 
095                                getAllMaintainableFieldDefinitionsForSerialization(
096                                (MaintainableCollectionDefinition)maintainableItemDefinition));                 
097            } else if(maintainableItemDefinition instanceof MaintainableSubSectionHeaderDefinition){
098                //Ignore
099            }
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}