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.krad.util.documentserializer;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.datadictionary.DataDictionary;
020import org.kuali.rice.krad.datadictionary.DocumentEntry;
021import org.kuali.rice.krad.datadictionary.WorkflowProperties;
022import org.kuali.rice.krad.datadictionary.WorkflowProperty;
023import org.kuali.rice.krad.datadictionary.WorkflowPropertyGroup;
024import org.kuali.rice.krad.document.Document;
025import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
026
027import java.util.List;
028
029/**
030 * This implementation of {@link PropertySerializabilityEvaluator} uses the <workflowProperties> defined within the data dictionary
031 * for a document.  If the property being serialized corresponds to one of the properties in the data dictionary, then it will be serialized.
032 * If a property specified in the data dictionary corresponds to a business object, then all primitives will be serialized of the business object.
033 * All primitives of a primitive that has already been serialized will be serialized as well.   If a property specified in the data dictionary corresponds
034 * to a collection, then all primitives of all collection elements will be serialized.
035 *
036 */
037public class BusinessObjectPropertySerializibilityEvaluator extends PropertySerializabilityEvaluatorBase implements PropertySerializabilityEvaluator {
038
039    /**
040     * Reads the data dictionary to determine which properties of the document should be serialized.
041     * 
042     * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator#initializeEvaluator(org.kuali.rice.krad.document.Document)
043     */
044        @Override
045    public void initializeEvaluatorForDocument(Document document) {
046        DataDictionary dictionary = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary();
047        DocumentEntry docEntry = dictionary.getDocumentEntry(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
048        WorkflowProperties workflowProperties = docEntry.getWorkflowProperties();
049        List<WorkflowPropertyGroup> groups = workflowProperties.getWorkflowPropertyGroups();
050        
051        serializableProperties = new PropertySerializerTrie();
052        
053        for (WorkflowPropertyGroup group : groups) {
054            // the basepath of each workflow property group is serializable
055            if (StringUtils.isEmpty(group.getBasePath())) {
056                // automatically serialize all primitives of document when the base path is null or empty string
057                serializableProperties.addSerializablePropertyName(document.getBasePathToDocumentDuringSerialization(), false);
058            }
059            else {
060               serializableProperties.addSerializablePropertyName(group.getBasePath(), false);
061            }
062            
063            for (WorkflowProperty property : group.getWorkflowProperties()) {
064                String fullPath;
065                if (StringUtils.isEmpty(group.getBasePath())) {
066                    fullPath = document.getBasePathToDocumentDuringSerialization() + "." + property.getPath();
067                }
068                else {
069                    fullPath = group.getBasePath() + "." + property.getPath(); 
070                }
071                serializableProperties.addSerializablePropertyName(fullPath, false);
072            }
073        }
074    }
075
076}