Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PropertySerializabilityEvaluatorBase |
|
| 3.142857142857143;3.143 |
1 | /* | |
2 | * Copyright 2007-2008 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.documentserializer; | |
17 | ||
18 | import java.util.Collection; | |
19 | ||
20 | import org.kuali.rice.kns.bo.BusinessObject; | |
21 | import org.kuali.rice.kns.document.Document; | |
22 | import org.kuali.rice.kns.util.documentserializer.PropertySerializabilityMetadata.PropertySerializability; | |
23 | ||
24 | /** | |
25 | * This abstract implementation provides a default implementation of {@link #determinePropertyType(Object)}, which should suffice for most | |
26 | * use cases. | |
27 | * | |
28 | */ | |
29 | 0 | public abstract class PropertySerializabilityEvaluatorBase implements PropertySerializabilityEvaluator { |
30 | ||
31 | protected PropertySerializerTrie serializableProperties; | |
32 | ||
33 | public void initializeEvaluator(Document document){ | |
34 | ||
35 | 0 | } |
36 | ||
37 | public void initializeEvaluator(Object businessObject){ | |
38 | ||
39 | 0 | } |
40 | ||
41 | /** | |
42 | * @see org.kuali.rice.kns.util.documentserializer.PropertySerializabilityEvaluator#determinePropertyType(java.lang.Object) | |
43 | */ | |
44 | public PropertyType determinePropertyType(Object propertyValue) { | |
45 | 0 | if (propertyValue == null) { |
46 | 0 | return PropertyType.PRIMITIVE; |
47 | } | |
48 | 0 | if (propertyValue instanceof BusinessObject) { |
49 | 0 | return PropertyType.BUSINESS_OBJECT; |
50 | } | |
51 | 0 | if (propertyValue instanceof Collection) { |
52 | 0 | return PropertyType.COLLECTION; |
53 | } | |
54 | 0 | return PropertyType.PRIMITIVE; |
55 | } | |
56 | ||
57 | /** | |
58 | * Returns whether a child property of a given containing object should be serialized, based on the metadata provided in the data dictionary. | |
59 | * | |
60 | * @see org.kuali.rice.kns.util.documentserializer.PropertySerializabilityEvaluator#isPropertySerializable(org.kuali.rice.kns.util.documentserializer.DocumentSerializationState, java.lang.Object, java.lang.String, java.lang.Object) | |
61 | */ | |
62 | public boolean isPropertySerializable(SerializationState state, Object containingObject, String childPropertyName, Object childPropertyValue) { | |
63 | 0 | boolean allPropertiesMatched = true; |
64 | ||
65 | 0 | PropertySerializabilityMetadata metadata = serializableProperties.getRootPropertySerializibilityMetadata(); |
66 | 0 | int i = 0; |
67 | 0 | for (; i < state.numPropertyElements(); i++) { |
68 | 0 | String nextPropertyName = state.getElementName(i); |
69 | 0 | PropertySerializabilityMetadata nextMetadata = metadata.getSerializableChildProperty(nextPropertyName); |
70 | ||
71 | 0 | if (nextMetadata == null) { |
72 | 0 | allPropertiesMatched = false; |
73 | 0 | break; |
74 | } | |
75 | else { | |
76 | // we've found the child... continue searching deeper | |
77 | 0 | metadata = nextMetadata; |
78 | } | |
79 | } | |
80 | ||
81 | 0 | if (allPropertiesMatched) { |
82 | // complete match, so we determine if the child property is serializable | |
83 | 0 | return evaluateCompleteMatch(state, containingObject, metadata, childPropertyName, childPropertyValue); |
84 | } | |
85 | else { | |
86 | // we have a partial match, so we have a different algorithm to determine serializibility | |
87 | // partial matches can occur for primitives that contains nested primitives. For example, if we have a member field named | |
88 | // "amount" of type KualiDecimal, then the XML will have to look something like <amount><value>100.00</value></amount>. | |
89 | // It is likely that "value" isn't specified in the serializability path, so we need to make inferences about whether "value" is | |
90 | // serializable | |
91 | 0 | return evaluatePartialMatch(state, i, containingObject, metadata, childPropertyName, childPropertyValue); |
92 | } | |
93 | } | |
94 | ||
95 | /** | |
96 | * Evaluates whether a property is serializable when all properties in the serialization state have been matched up with the properties | |
97 | * defined in the data dictionary. | |
98 | * | |
99 | * @param state | |
100 | * @param containingObject | |
101 | * @param metadata | |
102 | * @param childPropertyName | |
103 | * @param childPropertyValue | |
104 | * @return whether the child property is serializable | |
105 | */ | |
106 | protected boolean evaluateCompleteMatch(SerializationState state, Object containingObject, PropertySerializabilityMetadata metadata, String childPropertyName, Object childPropertyValue) { | |
107 | 0 | if (metadata.getPropertySerializability().equals(PropertySerializability.SERIALIZE_OBJECT_AND_ALL_PRIMITIVES)) { |
108 | 0 | if (isPrimitiveObject(childPropertyValue)) { |
109 | 0 | return true; |
110 | } | |
111 | } | |
112 | 0 | return metadata.getSerializableChildProperty(childPropertyName) != null; |
113 | } | |
114 | ||
115 | /** | |
116 | * Evaluates whether a property is serializable when only some of the properties in the serialization state have been matched up with the | |
117 | * serializable properties specified in the data dictionary. This often occurs when we determine whether to serialize a primitive of a serialized primitive | |
118 | * | |
119 | * @param state | |
120 | * @param lastMatchedStateIndex the index of the state parameter that represents the last matched property | |
121 | * @param containingObject the object containing the child property | |
122 | * @param metadata metadata of the last matched property | |
123 | * @param childPropertyName the name of the child property that we are going to determine whether it is serializable | |
124 | * @param childPropertyValue the value of the child property that we are going to determine whether it is serializable | |
125 | * @return whether the child property is serializable | |
126 | */ | |
127 | protected boolean evaluatePartialMatch(SerializationState state, int lastMatchedStateIndex, Object containingObject, PropertySerializabilityMetadata metadata, String childPropertyName, Object childPropertyValue) { | |
128 | ||
129 | 0 | if (metadata.getPropertySerializability().equals(PropertySerializability.SERIALIZE_OBJECT_AND_ALL_PRIMITIVES)) { |
130 | 0 | return isPrimitiveObject(childPropertyValue); |
131 | } | |
132 | 0 | return false; |
133 | } | |
134 | ||
135 | /** | |
136 | * Whether the object represents a primitive | |
137 | * | |
138 | * @param object | |
139 | * @return | |
140 | */ | |
141 | protected boolean isPrimitiveObject(Object object) { | |
142 | 0 | return PropertyType.PRIMITIVE.equals(determinePropertyType(object)); |
143 | } | |
144 | ||
145 | } |