001: package org.romaframework.module.designer.serializer.xml;
002:
003: import java.util.Iterator;
004: import java.util.Map;
005: import java.util.Map.Entry;
006: import org.apache.xmlbeans.XmlObject;
007: import org.romaframework.aspect.core.CoreAspect;
008: import org.romaframework.aspect.core.annotation.CoreClass;
009: import org.romaframework.aspect.core.feature.CoreClassFeatures;
010: import org.romaframework.aspect.monitoring.MonitoringAspect;
011: import org.romaframework.aspect.monitoring.annotation.MonitoringClass;
012: import org.romaframework.aspect.reporting.ReportingAspect;
013: import org.romaframework.aspect.view.ViewAspect;
014: import org.romaframework.aspect.view.annotation.ViewClass;
015: import org.romaframework.core.schema.SchemaAction;
016: import org.romaframework.core.schema.SchemaClassDefinition;
017: import org.romaframework.core.schema.SchemaField;
018: import org.romaframework.core.serializer.RomaObjectSerializer;
019: import org.romaframework.core.serializer.RomaSerializationException;
020: import org.romaframework.core.serializer.RomaSerializerComponent;
021: import org.romaframework.core.util.DynaBean;
022: import org.romaframework.xml.config.XmlConfigAspectClassTypeMonitoring;
023: import org.romaframework.xml.config.XmlConfigAspectClassTypeReporting;
024: import org.romaframework.xml.config.XmlConfigAspectClassTypeView;
025: import org.romaframework.xml.config.XmlConfigClassType;
026: import org.romaframework.xml.config.XmlConfigClassType.Aspects;
027:
028: class SchemaClassDefinitionSerializer extends AbstractSchemaSerializer {
029: protected final static SchemaClassDefinitionSerializer instance = new SchemaClassDefinitionSerializer();
030:
031: public static RomaObjectSerializer getInstace() {
032: return instance;
033: }
034:
035: @Override
036: public XmlObject serialize(Object iToSerialize,
037: Map<String, Object> options)
038: throws RomaSerializationException {
039: increaseLevel(options);
040: SchemaClassDefinition toSerialize = (SchemaClassDefinition) iToSerialize;
041: XmlConfigClassType clazz = XmlConfigClassType.Factory
042: .newInstance();
043: Aspects aspects = Aspects.Factory.newInstance();
044:
045: XmlConfigAspectClassTypeMonitoring monitoring = XmlConfigAspectClassTypeMonitoring.Factory
046: .newInstance();
047: createAspectXml(MonitoringAspect.ASPECT_NAME, toSerialize
048: .getFeatures(MonitoringAspect.ASPECT_NAME), monitoring);
049:
050: if (isNotEndElement(monitoring))
051: aspects.addNewMonitoring().set(monitoring);
052:
053: XmlConfigAspectClassTypeView view = XmlConfigAspectClassTypeView.Factory
054: .newInstance();
055: createAspectXml(ViewAspect.ASPECT_NAME, toSerialize
056: .getFeatures(ViewAspect.ASPECT_NAME), view);
057:
058: XmlConfigAspectClassTypeReporting reporting = XmlConfigAspectClassTypeReporting.Factory
059: .newInstance();
060: createAspectXml(ReportingAspect.ASPECT_NAME, toSerialize
061: .getFeatures(ReportingAspect.ASPECT_NAME), reporting);
062:
063: if (isNotEndElement(reporting)) {
064: aspects.addNewReporting().set(reporting);
065: }
066:
067: if (isNotEndElement(view)) {
068: aspects.addNewView().set(view);
069: }
070:
071: if (isNotEndElement(aspects)) {
072: clazz.addNewAspects().set(aspects);
073: }
074: /**
075: * Setting Fields
076: */
077: DynaBean coreFeatures = toSerialize
078: .getFeatures(CoreAspect.ASPECT_NAME);
079: coreFeatures.getAttribute(CoreClassFeatures.ORDER_FIELDS);
080: XmlConfigClassType.Fields fields = clazz.addNewFields();
081: Iterator<SchemaField> fieldsIterator = toSerialize
082: .getFieldIterator();
083: while (fieldsIterator.hasNext()) {
084: XmlObject obj = RomaSerializerComponent.getInstance()
085: .serialize(fieldsIterator.next(), options);
086: if (isNotEndElement(obj)) {
087: fields.addNewField().set(obj);
088: }
089: }
090: /**
091: * Setting Actions
092: */
093: XmlConfigClassType.Actions actions = clazz.addNewActions();
094: Iterator<SchemaAction> actionsIterator = toSerialize
095: .getActionIterator();
096: while (actionsIterator.hasNext()) {
097: XmlObject obj = RomaSerializerComponent.getInstance()
098: .serialize(actionsIterator.next(), options);
099: if (isNotEndElement(obj)) {
100: actions.addNewAction().set(obj);
101: }
102: }
103: return clazz;
104: }
105:
106: @Override
107: protected Object getValueToSet(Entry<String, Object> entry,
108: XmlObject iToSet) {
109: if (entry.getKey().equals("columns")) {
110: return new Byte(entry.getValue().toString());
111: }
112: if (entry.getKey().equals("entity")) {
113: return ((Class) entry.getValue()).getSimpleName();
114: }
115: return entry.getValue();
116: }
117:
118: @Override
119: protected boolean isDefaultValue(String aspectName,
120: Entry<String, Object> entry) throws Exception {
121: Class annotation = null;
122: if (aspectName.equals(ViewAspect.ASPECT_NAME)) {
123: annotation = ViewClass.class;
124: } else if (aspectName.equals(MonitoringAspect.ASPECT_NAME)) {
125: annotation = MonitoringClass.class;
126: } else if (aspectName.equals(CoreAspect.ASPECT_NAME)) {
127: annotation = CoreClass.class;
128: }
129: if (annotation == null) {
130: return false;
131: }
132: Object defaultValue = getAnnotationDefaultValue(entry,
133: annotation);
134: if (entry.getValue().equals(defaultValue)) {
135: return true;
136: }
137: return false;
138: }
139: }
|