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.aspect.core;
018:
019: import java.lang.annotation.Annotation;
020:
021: import org.romaframework.aspect.core.annotation.AnnotationConstants;
022: import org.romaframework.aspect.core.annotation.CoreClass;
023: import org.romaframework.aspect.core.annotation.CoreField;
024: import org.romaframework.aspect.core.feature.CoreClassFeatures;
025: import org.romaframework.aspect.core.feature.CoreFieldFeatures;
026: import org.romaframework.aspect.session.SessionListener;
027: import org.romaframework.core.Utility;
028: import org.romaframework.core.aspect.Aspect;
029: import org.romaframework.core.config.RomaApplicationContext;
030: import org.romaframework.core.flow.Controller;
031: import org.romaframework.core.flow.ObjectContext;
032: import org.romaframework.core.schema.SchemaClassDefinition;
033: import org.romaframework.core.schema.SchemaClassResolver;
034: import org.romaframework.core.schema.SchemaElement;
035: import org.romaframework.core.schema.SchemaEvent;
036: import org.romaframework.core.schema.SchemaField;
037: import org.romaframework.core.util.DynaBean;
038: import org.romaframework.xml.config.XmlConfigActionType;
039: import org.romaframework.xml.config.XmlConfigAspectFieldTypeCore;
040: import org.romaframework.xml.config.XmlConfigClassType;
041: import org.romaframework.xml.config.XmlConfigFieldType;
042:
043: public class CoreAspect implements Aspect {
044:
045: public static final String ASPECT_NAME = "core";
046:
047: public void configClass(SchemaClassDefinition iClass,
048: Annotation iAnnotation, XmlConfigClassType iXmlNode) {
049: DynaBean features = iClass.getFeatures(ASPECT_NAME);
050: if (features == null) {
051: // CREATE EMPTY FEATURES
052: features = new CoreClassFeatures();
053: iClass.setFeatures(ASPECT_NAME, features);
054: }
055:
056: readClassAnnotation(iClass, iAnnotation, features);
057: }
058:
059: private void readClassAnnotation(SchemaClassDefinition iClass,
060: Annotation iAnnotation, DynaBean features) {
061: CoreClass annotation = (CoreClass) iAnnotation;
062:
063: if (annotation != null) {
064: // PROCESS ANNOTATIONS
065: // ANNOTATION ATTRIBUTES (IF DEFINED) OVERWRITE DEFAULT VALUES
066: if (annotation != null) {
067: if (annotation.orderFields() != AnnotationConstants.DEF_VALUE)
068: features.setAttribute(
069: CoreClassFeatures.ORDER_FIELDS, annotation
070: .orderFields());
071: if (annotation.orderActions() != AnnotationConstants.DEF_VALUE)
072: features.setAttribute(
073: CoreClassFeatures.ORDER_ACTIONS, annotation
074: .orderActions());
075: }
076: }
077: }
078:
079: public void startup() {
080: SchemaClassResolver classResolver = ObjectContext.getInstance()
081: .getComponent(SchemaClassResolver.class);
082:
083: // REGISTER THE APPLICATION DOMAIN AS FIRST ONE PATH
084: classResolver.addDomainPackage(RomaApplicationContext
085: .getInstance().getApplicationPackage());
086: classResolver.addDomainPackage(Utility.ROMA_PACKAGE
087: + Utility.PACKAGE_SEPARATOR + "core");
088: classResolver.addPackage("java.lang");
089:
090: Controller.getInstance().registerListener(
091: SessionListener.class, ObjectContext.getInstance());
092: }
093:
094: public void shutdown() {
095: }
096:
097: public void configAction(SchemaElement iAction,
098: Annotation iActionAnnotation,
099: Annotation iGenericAnnotation, XmlConfigActionType iXmlNode) {
100: }
101:
102: public void configField(SchemaField iField,
103: Annotation iFieldAnnotation, Annotation iGenericAnnotation,
104: Annotation iGetterAnnotation, XmlConfigFieldType iXmlNode) {
105: DynaBean features = iField.getFeatures(ASPECT_NAME);
106: if (features == null) {
107: // CREATE EMPTY FEATURES
108: features = new CoreFieldFeatures();
109: iField.setFeatures(ASPECT_NAME, features);
110: }
111:
112: readFieldAnnotation(iField, iFieldAnnotation, features);
113: readFieldAnnotation(iField, iGetterAnnotation, features);
114: readFieldXml(iField, iXmlNode);
115: setFieldDefaults(iField);
116: }
117:
118: private void readFieldAnnotation(SchemaField iField,
119: Annotation iAnnotation, DynaBean features) {
120: CoreField annotation = (CoreField) iAnnotation;
121:
122: if (annotation != null) {
123: // PROCESS ANNOTATIONS
124: // ANNOTATION ATTRIBUTES (IF DEFINED) OVERWRITE DEFAULT VALUES
125: if (annotation != null) {
126: if (annotation.embeddedType() != Object.class)
127: features.setAttribute(
128: CoreFieldFeatures.EMBEDDED_TYPE, annotation
129: .embeddedType());
130: if (annotation.embedded() != AnnotationConstants.UNSETTED)
131: features
132: .setAttribute(
133: CoreFieldFeatures.EMBEDDED,
134: annotation.embedded() == AnnotationConstants.TRUE);
135: if (annotation.useRuntimeType() != AnnotationConstants.UNSETTED)
136: features
137: .setAttribute(
138: CoreFieldFeatures.USE_RUNTIME_TYPE,
139: annotation.useRuntimeType() == AnnotationConstants.TRUE);
140: }
141: }
142: }
143:
144: private void readFieldXml(SchemaField iField,
145: XmlConfigFieldType iXmlNode) {
146: // PROCESS DESCRIPTOR CFG
147: // DESCRIPTOR ATTRIBUTES (IF DEFINED) OVERWRITE DEFAULT AND ANNOTATION
148: // VALUES
149: if (iXmlNode == null || iXmlNode.getAspects() == null)
150: return;
151:
152: DynaBean features = iField.getFeatures(ASPECT_NAME);
153:
154: XmlConfigAspectFieldTypeCore descriptor = iXmlNode.getAspects()
155: .getCore();
156:
157: if (descriptor != null) {
158: if (descriptor.isSetEmbeddedType())
159: features.setAttribute(CoreFieldFeatures.EMBEDDED_TYPE,
160: descriptor.getEmbeddedType());
161: if (descriptor.getEmbedded()) {
162: features.setAttribute(CoreFieldFeatures.EMBEDDED,
163: Boolean.TRUE);
164: } else {
165: features.setAttribute(CoreFieldFeatures.EMBEDDED,
166: Boolean.FALSE);
167: }
168:
169: if (descriptor.getUseRuntimeType()) {
170: features.setAttribute(
171: CoreFieldFeatures.USE_RUNTIME_TYPE,
172: Boolean.TRUE);
173: } else {
174: features.setAttribute(
175: CoreFieldFeatures.USE_RUNTIME_TYPE,
176: Boolean.FALSE);
177: }
178: }
179: }
180:
181: private void setFieldDefaults(SchemaField field) {
182: }
183:
184: public void configEvent(SchemaEvent event, Annotation annotation,
185: Annotation iGenericAnnotation, XmlConfigActionType node) {
186: }
187:
188: public String aspectName() {
189: return ASPECT_NAME;
190: }
191: }
|