001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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: */
016:
017: package org.romaframework.core.schema;
018:
019: import java.lang.annotation.Annotation;
020: import java.lang.reflect.Field;
021: import java.lang.reflect.Method;
022: import java.lang.reflect.Type;
023: import java.util.Collection;
024:
025: import org.romaframework.aspect.core.CoreAspect;
026: import org.romaframework.aspect.core.feature.CoreFieldFeatures;
027: import org.romaframework.aspect.view.ViewAspect;
028: import org.romaframework.core.aspect.Aspect;
029: import org.romaframework.core.aspect.AspectManager;
030: import org.romaframework.core.config.RomaApplicationContext;
031: import org.romaframework.core.exception.ConfigurationNotFoundException;
032: import org.romaframework.core.schema.config.EmbeddedSchemaConfiguration;
033: import org.romaframework.core.schema.config.SchemaConfiguration;
034: import org.romaframework.xml.config.XmlConfigClassType;
035: import org.romaframework.xml.config.XmlConfigFieldType;
036:
037: /**
038: * Represent a form field for an entity.
039: *
040: * @author Luca Garulli (luca.garulli@assetdata.it)
041: */
042: public class SchemaField extends SchemaElement {
043:
044: private Class<?> typeClass;
045: private Field field;
046: private Method getterMethod;
047: private Method setterMethod;
048: private SchemaClassDefinition complexClass;
049:
050: /**
051: * Return field's class information
052: *
053: * @return SchemaClass if found, otherwise null
054: */
055: public SchemaClass getClassInfo() {
056: // TRY TO SEARCH FOR INLINE SCHEMA DECLARATION
057: if (complexClass != null)
058: return complexClass.getSchemaClass();
059:
060: // SEARCH FOR CLASS DEFINITION
061: return RomaApplicationContext.getInstance().getBean(
062: SchemaManager.class).getClassInfo(
063: typeClass.getSimpleName());
064: }
065:
066: public Method getGetterMethod() {
067: return getterMethod;
068: }
069:
070: public Method getSetterMethod() {
071: return setterMethod;
072: }
073:
074: public SchemaField(SchemaClassDefinition iEntity) {
075: super (iEntity);
076: }
077:
078: @Override
079: public String toString() {
080: return name + " (field:" + field + ")";
081: }
082:
083: @Override
084: public Object clone() throws CloneNotSupportedException {
085: SchemaField copy = (SchemaField) super .clone();
086: copy.complexClass = complexClass;
087: copy.typeClass = typeClass;
088: copy.field = field;
089: copy.getterMethod = getterMethod;
090: copy.setterMethod = setterMethod;
091:
092: return copy;
093: }
094:
095: public void configure(ViewAspect iComponentFactory,
096: String iFieldName, Class<?> iFieldType, Field iField,
097: Method iGetterMethod, Method iSetterMethod) {
098: field = iField;
099: name = iFieldName;
100: typeClass = iFieldType;
101:
102: getterMethod = iGetterMethod;
103: setterMethod = iSetterMethod;
104:
105: processConfiguration();
106:
107: if (getterMethod != null) {
108: if (!getEmbeddedType(getterMethod.getGenericReturnType()))
109: if (field != null)
110: getEmbeddedType(field.getGenericType());
111: }
112: }
113:
114: /**
115: * Get embedded type using Generics Reflection.
116: *
117: * @param iType
118: * Type to check
119: * @return true if an embedded type was found, otherwise false
120: */
121: private boolean getEmbeddedType(Type iType) {
122: Class<?> embClass = SchemaHelper.getGenericClass(iType);
123: if (embClass != null) {
124: setEmbeddedType(embClass);
125: }
126:
127: return embClass != null;
128: }
129:
130: @Override
131: protected void processConfiguration() {
132: // BROWSE ALL ASPECTS
133: Collection<Aspect> aspects = AspectManager.getInstance()
134: .getConfigurationValues();
135:
136: String annotationName;
137: Annotation fieldAnnotation;
138: Annotation genericAnnotation;
139: Annotation getterAnnotation;
140:
141: SchemaConfiguration classDescriptor = entity.getSchemaClass()
142: .getDescriptor();
143:
144: XmlConfigFieldType parentDescriptor = null;
145:
146: for (Aspect aspect : aspects) {
147: // COMPOSE ANNOTATION NAME BY ASPECT
148: annotationName = aspect.aspectName();
149: annotationName = Character.toUpperCase(annotationName
150: .charAt(0))
151: + annotationName.substring(1);
152:
153: if (field != null) {
154: fieldAnnotation = searchForAnnotation(field,
155: annotationName + "Field", aspect.aspectName());
156: genericAnnotation = searchForAnnotation(field,
157: annotationName, aspect.aspectName());
158: } else {
159: fieldAnnotation = null;
160: genericAnnotation = null;
161: }
162:
163: if (getterMethod != null)
164: getterAnnotation = searchForAnnotation(getterMethod,
165: annotationName + "Field", aspect.aspectName());
166: else
167: getterAnnotation = null;
168:
169: parentDescriptor = null;
170:
171: if (classDescriptor != null
172: && classDescriptor.getType() != null
173: && classDescriptor.getType().getFields() != null) {
174: // SEARCH FORM DEFINITION IN DESCRIPTOR
175: XmlConfigFieldType[] allFields = classDescriptor
176: .getType().getFields().getFieldArray();
177:
178: XmlConfigFieldType tmpDescr = null;
179: for (short fieldNum = 0; fieldNum < allFields.length; ++fieldNum) {
180: tmpDescr = allFields[fieldNum];
181: if (tmpDescr.getName().equals(name)) {
182: // FOUND XML NODE
183: parentDescriptor = tmpDescr;
184: break;
185: }
186: }
187: }
188:
189: // CONFIGURE THE SCHEMA OBJECT WITH CURRENT ASPECT
190: aspect.configField(this , fieldAnnotation,
191: genericAnnotation, getterAnnotation,
192: parentDescriptor);
193: }
194:
195: if (parentDescriptor != null) {
196: // CONFIGURE EMBEDDED CLASS IF ANY
197: XmlConfigClassType subClass = parentDescriptor.getClass1();
198: if (subClass != null) {
199: // INLINE DESCRIPTOR
200: SchemaConfiguration fieldSchemaDescr = null;
201: SchemaConfiguration sourceDescr = getEntity()
202: .getSchemaClass().getDescriptor();
203: if (sourceDescr != null)
204: fieldSchemaDescr = new EmbeddedSchemaConfiguration(
205: subClass);
206:
207: setComplexClass(RomaApplicationContext.getInstance()
208: .getBean(SchemaManager.class).createClassInfo(
209: getEntity().getSchemaClass().getName()
210: + "." + getName(),
211: getTypeClass(), fieldSchemaDescr));
212: }
213: }
214:
215: if (complexClass == null) {
216: String className = getTypeClass().getName();
217: // NOT INLINE DESCRIPTOR
218: if (!getTypeClass().isPrimitive()
219: && !className.startsWith("java.")
220: && !(className.length() > 2 && className
221: .indexOf("java.") > -1)) {
222: try {
223: setComplexClass(RomaApplicationContext
224: .getInstance().getBean(SchemaManager.class)
225: .getClassInfo(
226: getTypeClass().getSimpleName()));
227: } catch (ConfigurationNotFoundException e) {
228: }
229: }
230: }
231: }
232:
233: public SchemaClassDefinition getComplexClass() {
234: return complexClass;
235: }
236:
237: public void setComplexClass(SchemaClassDefinition complexClass) {
238: this .complexClass = complexClass;
239: }
240:
241: public XmlConfigFieldType getDescriptorInfo() {
242: SchemaConfiguration classDescriptor = entity.getSchemaClass()
243: .getDescriptor();
244:
245: if (classDescriptor == null
246: || classDescriptor.getType() == null
247: || classDescriptor.getType().getFields() == null)
248: return null;
249:
250: XmlConfigFieldType descriptor;
251:
252: // SEARCH FORM DEFINITION IN DESCRIPTOR
253: XmlConfigFieldType[] allFields = classDescriptor.getType()
254: .getFields().getFieldArray();
255: for (short fieldNum = 0; fieldNum < allFields.length; ++fieldNum) {
256: descriptor = allFields[fieldNum];
257: if (descriptor.getName().equals(name)) {
258: // FOUND: SET THE ORDER AND RETURN IT
259: setOrder(fieldNum);
260: return descriptor;
261: }
262: }
263: return null;
264: }
265:
266: public Class<?> getTypeClass() {
267: return typeClass;
268: }
269:
270: public void setTypeClass(Class<?> clazz) {
271: typeClass = clazz;
272: }
273:
274: public Field getField() {
275: return field;
276: }
277:
278: public Type getEmbeddedType() {
279: return (Type) getFeature(CoreAspect.ASPECT_NAME,
280: CoreFieldFeatures.EMBEDDED_TYPE);
281: }
282:
283: public void setEmbeddedType(Type iEmbeddedType) {
284: setFeature(CoreAspect.ASPECT_NAME,
285: CoreFieldFeatures.EMBEDDED_TYPE, iEmbeddedType);
286: }
287:
288: }
|