001/**
002 * Copyright 2005-2018 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
038/**
039 * @deprecated Only used in KNS classes, use KRAD.
040 */
041@Deprecated
042public class MaintenanceDocumentPropertySerializibilityEvaluator
043                                extends PropertySerializabilityEvaluatorBase implements PropertySerializabilityEvaluator {
044
045    /**
046     * Reads the data dictionary to determine which properties of the document should be serialized.
047     * 
048     * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator#initializeEvaluator(org.kuali.rice.krad.document.Document)
049     */
050        @Override
051    public void initializeEvaluatorForDataObject(Object businessObject){
052        DataDictionary dictionary = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary();
053        MaintenanceDocumentEntry maintenanceDocumentEntry = (MaintenanceDocumentEntry)
054                dictionary.getMaintenanceDocumentEntryForBusinessObjectClass(businessObject.getClass());
055        serializableProperties = new PropertySerializerTrie();
056        populateSerializableProperties(maintenanceDocumentEntry.getMaintainableSections());
057        serializableProperties.addSerializablePropertyName("boNotes", true);
058        serializableProperties.addSerializablePropertyName("boNotes.attachment", true);
059    }
060
061    /**
062     * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator#determinePropertyType(java.lang.Object)
063     */
064    @Override
065    public PropertyType determinePropertyType(Object propertyValue) {
066        if (propertyValue == null) {
067            return PropertyType.PRIMITIVE;
068        }
069
070        if (propertyValue instanceof BusinessObject) {
071            return PropertyType.BUSINESS_OBJECT;
072        }
073
074        if (propertyValue instanceof Collection) {
075            return PropertyType.COLLECTION;
076        }
077
078        // In the case of Maintenance Documents treat Maps as PRIMITIVE
079        if (propertyValue instanceof Map) {
080            return PropertyType.PRIMITIVE;
081        }
082
083        return PropertyType.PRIMITIVE;
084    }
085
086    private void populateSerializableProperties(List<MaintainableSectionDefinition> maintainableSectionDefinitions){
087        for(MaintainableSectionDefinition maintainableSectionDefinition: maintainableSectionDefinitions){
088                populateSerializablePropertiesWithItems("", maintainableSectionDefinition.getMaintainableItems());
089        }
090    }
091
092    private void populateSerializablePropertiesWithItems(String basePath, List<MaintainableItemDefinition> maintainableItems){
093        for(MaintainableItemDefinition maintainableItemDefinition: maintainableItems){
094            if(maintainableItemDefinition instanceof MaintainableFieldDefinition){
095                serializableProperties.addSerializablePropertyName(getFullItemName(basePath, maintainableItemDefinition.getName()), true);
096            } else if(maintainableItemDefinition instanceof MaintainableCollectionDefinition){
097                serializableProperties.addSerializablePropertyName(getFullItemName(basePath, maintainableItemDefinition.getName()), true);
098                populateSerializablePropertiesWithItems(getFullItemName(basePath, maintainableItemDefinition.getName()), 
099                                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}