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.kuali.rice.krad.bo.BusinessObject; 019import org.kuali.rice.krad.document.Document; 020import org.kuali.rice.krad.util.documentserializer.PropertySerializabilityMetadata.PropertySerializability; 021 022import java.util.Collection; 023import java.util.Map; 024 025/** 026 * This abstract implementation provides a default implementation of {@link #determinePropertyType(Object)}, which should suffice for most 027 * use cases. 028 * 029 */ 030public abstract class PropertySerializabilityEvaluatorBase implements PropertySerializabilityEvaluator { 031 032 protected PropertySerializerTrie serializableProperties; 033 034 @Override 035 public void initializeEvaluatorForDocument(Document document){ 036 037 } 038 039 @Override 040 public void initializeEvaluatorForDataObject(Object businessObject){ 041 042 } 043 044 /** 045 * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator#determinePropertyType(java.lang.Object) 046 */ 047 @Override 048 public PropertyType determinePropertyType(Object propertyValue) { 049 if (propertyValue == null) { 050 return PropertyType.PRIMITIVE; 051 } 052 if (propertyValue instanceof BusinessObject) { 053 return PropertyType.BUSINESS_OBJECT; 054 } 055 if (propertyValue instanceof Collection) { 056 return PropertyType.COLLECTION; 057 } 058 if (propertyValue instanceof Map) { 059 return PropertyType.MAP; 060 } 061 return PropertyType.PRIMITIVE; 062 } 063 064 /** 065 * Returns whether a child property of a given containing object should be serialized, based on the metadata provided in the data dictionary. 066 * 067 * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator#isPropertySerializable(org.kuali.rice.krad.util.documentserializer.DocumentSerializationState, java.lang.Object, java.lang.String, java.lang.Object) 068 */ 069 @Override 070 public boolean isPropertySerializable(SerializationState state, Object containingObject, String childPropertyName, Object childPropertyValue) { 071 boolean allPropertiesMatched = true; 072 073 PropertySerializabilityMetadata metadata = serializableProperties.getRootPropertySerializibilityMetadata(); 074 int i = 0; 075 for (; i < state.numPropertyElements(); i++) { 076 String nextPropertyName = state.getElementName(i); 077 PropertySerializabilityMetadata nextMetadata = metadata.getSerializableChildProperty(nextPropertyName); 078 079 if (nextMetadata == null) { 080 allPropertiesMatched = false; 081 break; 082 } 083 else { 084 // we've found the child... continue searching deeper 085 metadata = nextMetadata; 086 } 087 } 088 089 if (allPropertiesMatched) { 090 // complete match, so we determine if the child property is serializable 091 return evaluateCompleteMatch(state, containingObject, metadata, childPropertyName, childPropertyValue); 092 } 093 else { 094 // we have a partial match, so we have a different algorithm to determine serializibility 095 // partial matches can occur for primitives that contains nested primitives. For example, if we have a member field named 096 // "amount" of type KualiDecimal, then the XML will have to look something like <amount><value>100.00</value></amount>. 097 // It is likely that "value" isn't specified in the serializability path, so we need to make inferences about whether "value" is 098 // serializable 099 return evaluatePartialMatch(state, i, containingObject, metadata, childPropertyName, childPropertyValue); 100 } 101 } 102 103 /** 104 * Evaluates whether a property is serializable when all properties in the serialization state have been matched up with the properties 105 * defined in the data dictionary. 106 * 107 * @param state 108 * @param containingObject 109 * @param metadata 110 * @param childPropertyName 111 * @param childPropertyValue 112 * @return whether the child property is serializable 113 */ 114 protected boolean evaluateCompleteMatch(SerializationState state, Object containingObject, PropertySerializabilityMetadata metadata, String childPropertyName, Object childPropertyValue) { 115 if (metadata.getPropertySerializability().equals(PropertySerializability.SERIALIZE_OBJECT_AND_ALL_PRIMITIVES)) { 116 if (isPrimitiveObject(childPropertyValue)) { 117 return true; 118 } 119 } 120 return metadata.getSerializableChildProperty(childPropertyName) != null; 121 } 122 123 /** 124 * Evaluates whether a property is serializable when only some of the properties in the serialization state have been matched up with the 125 * serializable properties specified in the data dictionary. This often occurs when we determine whether to serialize a primitive of a serialized primitive 126 * 127 * @param state 128 * @param lastMatchedStateIndex the index of the state parameter that represents the last matched property 129 * @param containingObject the object containing the child property 130 * @param metadata metadata of the last matched property 131 * @param childPropertyName the name of the child property that we are going to determine whether it is serializable 132 * @param childPropertyValue the value of the child property that we are going to determine whether it is serializable 133 * @return whether the child property is serializable 134 */ 135 protected boolean evaluatePartialMatch(SerializationState state, int lastMatchedStateIndex, Object containingObject, PropertySerializabilityMetadata metadata, String childPropertyName, Object childPropertyValue) { 136 137 if (metadata.getPropertySerializability().equals(PropertySerializability.SERIALIZE_OBJECT_AND_ALL_PRIMITIVES)) { 138 return isPrimitiveObject(childPropertyValue); 139 } 140 return false; 141 } 142 143 /** 144 * Whether the object represents a primitive 145 * 146 * @param object 147 * @return 148 */ 149 protected boolean isPrimitiveObject(Object object) { 150 return PropertyType.PRIMITIVE.equals(determinePropertyType(object)); 151 } 152 153}