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.flow;
018:
019: import java.lang.annotation.Annotation;
020:
021: import org.romaframework.aspect.core.annotation.AnnotationConstants;
022: import org.romaframework.aspect.flow.annotation.FlowAction;
023: import org.romaframework.aspect.flow.feature.FlowActionFeatures;
024: import org.romaframework.core.aspect.SelfRegistrantConfigurableAspect;
025: import org.romaframework.core.flow.Controller;
026: import org.romaframework.core.flow.ObjectContext;
027: import org.romaframework.core.flow.UserObjectEventListener;
028: import org.romaframework.core.schema.SchemaClass;
029: import org.romaframework.core.schema.SchemaElement;
030: import org.romaframework.core.schema.SchemaEvent;
031: import org.romaframework.core.schema.SchemaManager;
032: import org.romaframework.core.util.DynaBean;
033: import org.romaframework.xml.config.XmlConfigActionType;
034: import org.romaframework.xml.config.XmlConfigAspectActionTypeFlow;
035:
036: /**
037: * Abstract implementation for Flow Aspect.
038: *
039: * @author Luca Garulli (luca.garulli@assetdata.it)
040: */
041: public abstract class FlowAspectAbstract extends
042: SelfRegistrantConfigurableAspect<String> implements FlowAspect,
043: UserObjectEventListener {
044:
045: @Override
046: public void startup() {
047: Controller.getInstance().registerListener(
048: UserObjectEventListener.class, this );
049: super .startup();
050: }
051:
052: @Override
053: public void configAction(SchemaElement iAction,
054: Annotation iActionAnnotation,
055: Annotation iGenericAnnotation, XmlConfigActionType iXmlNode) {
056: DynaBean features = iAction.getFeatures(ASPECT_NAME);
057: if (features == null) {
058: // CREATE EMPTY FEATURES
059: features = new FlowActionFeatures();
060: iAction.setFeatures(ASPECT_NAME, features);
061: }
062:
063: readActionAnnotation(iAction, iActionAnnotation, features);
064: readActionXml(iAction, iXmlNode);
065: setActionDefaults(iAction);
066: }
067:
068: private void readActionAnnotation(SchemaElement iAction,
069: Annotation iAnnotation, DynaBean features) {
070: FlowAction annotation = (FlowAction) iAnnotation;
071:
072: if (annotation != null) {
073: // PROCESS ANNOTATIONS
074: // ANNOTATION ATTRIBUTES (IF DEFINED) OVERWRITE DEFAULT VALUES
075: if (annotation != null) {
076: if (!annotation.next().equals(Object.class))
077: features.setAttribute(FlowActionFeatures.NEXT,
078: annotation.next());
079: if (!annotation.position().equals(
080: AnnotationConstants.DEF_VALUE))
081: features.setAttribute(FlowActionFeatures.POSITION,
082: annotation.position());
083: if (!annotation.error().equals(
084: AnnotationConstants.DEF_VALUE))
085: features.setAttribute(FlowActionFeatures.ERROR,
086: annotation.error());
087: if (annotation.back() != AnnotationConstants.UNSETTED)
088: features
089: .setAttribute(
090: FlowActionFeatures.BACK,
091: annotation.back() == AnnotationConstants.TRUE);
092: }
093: }
094: }
095:
096: private void readActionXml(SchemaElement iAction,
097: XmlConfigActionType iXmlNode) {
098: // PROCESS DESCRIPTOR CFG
099: // DESCRIPTOR ATTRIBUTES (IF DEFINED) OVERWRITE DEFAULT AND ANNOTATION
100: // VALUES
101: if (iXmlNode == null || iXmlNode.getAspects() == null)
102: return;
103:
104: DynaBean features = iAction.getFeatures(ASPECT_NAME);
105:
106: XmlConfigAspectActionTypeFlow descriptor = iXmlNode
107: .getAspects().getFlow();
108:
109: if (descriptor != null) {
110: if (descriptor.isSetNext()) {
111: SchemaClass clazz = ObjectContext.getInstance()
112: .getComponent(SchemaManager.class)
113: .getClassInfo(descriptor.getNext());
114: if (clazz != null && clazz.getSchemaClass() != null) {
115: features.setAttribute(FlowActionFeatures.NEXT,
116: clazz.getSchemaClass().getClazz());
117: }
118: }
119: if (descriptor.isSetPosition())
120: features.setAttribute(FlowActionFeatures.POSITION,
121: descriptor.getPosition());
122: if (descriptor.isSetError())
123: features.setAttribute(FlowActionFeatures.ERROR,
124: descriptor.getError());
125: if (descriptor.isSetBack())
126: features.setAttribute(FlowActionFeatures.BACK,
127: descriptor.getBack());
128:
129: }
130: }
131:
132: @Override
133: public void configEvent(SchemaEvent iEvent,
134: Annotation iEventAnnotation, Annotation iGenericAnnotation,
135: XmlConfigActionType iXmlNode) {
136: DynaBean features = iEvent.getFeatures(ASPECT_NAME);
137: if (features == null) {
138: // CREATE EMPTY FEATURES
139: features = new FlowActionFeatures();
140: iEvent.setFeatures(ASPECT_NAME, features);
141: }
142:
143: readActionAnnotation(iEvent, iEventAnnotation, features);
144: readActionXml(iEvent, iXmlNode);
145: setActionDefaults(iEvent);
146: }
147:
148: private void setActionDefaults(SchemaElement iAction) {
149: DynaBean features = iAction.getFeatures(ASPECT_NAME);
150:
151: if (features.getAttribute(FlowActionFeatures.BACK) == null)
152: features.setAttribute(FlowActionFeatures.BACK, true);
153: }
154:
155: public String aspectName() {
156: return ASPECT_NAME;
157: }
158: }
|