001: /*
002: * Copyright 2006 Giordano Maestro (luca.garulli@assetdata.it) Licensed under
003: * the Apache License, Version 2.0 (the "License"); you may not use this file
004: * except in compliance with the License. You may obtain a copy of the License
005: * at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
006: * law or agreed to in writing, software distributed under the License is
007: * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
008: * KIND, either express or implied. See the License for the specific language
009: * governing permissions and limitations under the License.
010: */
011: package org.romaframework.module.designer.serializer.xml;
012:
013: import java.util.Map;
014: import java.util.Map.Entry;
015:
016: import org.apache.xmlbeans.XmlObject;
017: import org.romaframework.aspect.core.CoreAspect;
018: import org.romaframework.aspect.core.annotation.CoreField;
019: import org.romaframework.aspect.monitoring.MonitoringAspect;
020: import org.romaframework.aspect.monitoring.annotation.MonitoringField;
021: import org.romaframework.aspect.persistence.PersistenceAspect;
022: import org.romaframework.aspect.reporting.ReportingAspect;
023: import org.romaframework.aspect.view.ViewAspect;
024: import org.romaframework.aspect.view.annotation.ViewField;
025: import org.romaframework.aspect.view.feature.ViewFieldFeatures;
026: import org.romaframework.core.schema.SchemaField;
027: import org.romaframework.core.serializer.RomaObjectSerializer;
028: import org.romaframework.core.serializer.RomaSerializationException;
029: import org.romaframework.core.serializer.RomaSerializerComponent;
030: import org.romaframework.xml.config.XmlConfigAspectFieldTypeCore;
031: import org.romaframework.xml.config.XmlConfigAspectFieldTypeMonitoring;
032: import org.romaframework.xml.config.XmlConfigAspectFieldTypeReporting;
033: import org.romaframework.xml.config.XmlConfigAspectFieldTypeView;
034: import org.romaframework.xml.config.XmlConfigAspectTypePersistence;
035: import org.romaframework.xml.config.XmlConfigFieldType;
036: import org.romaframework.xml.config.XmlConfigFieldType.Aspects;
037:
038: class SchemaFieldSerializer extends AbstractSchemaSerializer {
039: protected final static SchemaFieldSerializer instance = new SchemaFieldSerializer();
040:
041: public static RomaObjectSerializer getInstace() {
042: return instance;
043: }
044:
045: @Override
046: public XmlObject serialize(Object iToSerialize,
047: Map<String, Object> options)
048: throws RomaSerializationException {
049: SchemaField toSerialize = (SchemaField) iToSerialize;
050: XmlConfigFieldType field = XmlConfigFieldType.Factory
051: .newInstance();
052: field.setName(toSerialize.getName());
053: Aspects aspects = Aspects.Factory.newInstance();
054: XmlConfigAspectFieldTypeCore core = XmlConfigAspectFieldTypeCore.Factory
055: .newInstance();
056: createAspectXml(CoreAspect.ASPECT_NAME, toSerialize
057: .getFeatures(CoreAspect.ASPECT_NAME), core);
058:
059: if (isNotEndElement(core))
060: aspects.addNewCore().set(core);
061:
062: // TODO: Manage Module configuration: XmlConfigAspectElementTypeModule
063: // module XmlConfigAspectElementTypeModule module = aspects.addNewModule();
064: XmlConfigAspectFieldTypeMonitoring monitoring = XmlConfigAspectFieldTypeMonitoring.Factory
065: .newInstance();
066: createAspectXml(MonitoringAspect.ASPECT_NAME, toSerialize
067: .getFeatures(MonitoringAspect.ASPECT_NAME), monitoring);
068: if (isNotEndElement(monitoring))
069: aspects.addNewMonitoring().set(monitoring);
070: XmlConfigAspectFieldTypeView view = XmlConfigAspectFieldTypeView.Factory
071: .newInstance();
072:
073: createAspectXml(ViewAspect.ASPECT_NAME, toSerialize
074: .getFeatures(ViewAspect.ASPECT_NAME), view);
075:
076: XmlConfigAspectTypePersistence persistence = XmlConfigAspectTypePersistence.Factory
077: .newInstance();
078: createAspectXml(PersistenceAspect.ASPECT_NAME, toSerialize
079: .getFeatures(PersistenceAspect.ASPECT_NAME),
080: persistence);
081: if (isNotEndElement(persistence)) {
082: aspects.addNewPersistence().set(persistence);
083: }
084:
085: XmlConfigAspectFieldTypeReporting reporting = XmlConfigAspectFieldTypeReporting.Factory
086: .newInstance();
087: createAspectXml(ReportingAspect.ASPECT_NAME, toSerialize
088: .getFeatures(ReportingAspect.ASPECT_NAME), reporting);
089:
090: if (isNotEndElement(reporting)) {
091: aspects.addNewReporting().set(reporting);
092: }
093:
094: if (isNotEndElement(view))
095: aspects.addNewView().set(view);
096: if (isNotEndElement(aspects))
097: field.addNewAspects().set(aspects);
098: /**
099: * The schemaClass
100: */
101: if (serializeNextLevel(options)) {
102: if (toSerialize.getComplexClass() != null) {
103: field.addNewClass1().set(
104: RomaSerializerComponent.getInstance()
105: .serialize(
106: toSerialize.getComplexClass(),
107: options));
108: }
109: }
110: return field;
111: }
112:
113: /**
114: * It converts the Object in the schema to the Object of XmlBeans
115: *
116: * @param entry
117: * @param iToSet
118: * @return the object to set in Xml fragment
119: */
120: @Override
121: protected Object getValueToSet(Entry<String, Object> entry,
122: XmlObject iToSet) {
123: if (entry.getKey().equals("embeddedType")) {
124: return null;
125: } else if (entry.getKey().equals("selectionMode")) {
126: if (entry.getValue().equals(
127: ViewFieldFeatures.SELECTION_MODE_INDEX)) {
128: return XmlConfigAspectFieldTypeView.SelectionMode.INDEX;
129: } else {
130: return XmlConfigAspectFieldTypeView.SelectionMode.VALUE;
131: }
132: } else {
133: return entry.getValue();
134: }
135: }
136:
137: @Override
138: protected boolean isDefaultValue(String aspectName,
139: Entry<String, Object> entry) throws Exception {
140: Class annotation = null;
141: if (aspectName.equals(ViewAspect.ASPECT_NAME)) {
142: annotation = ViewField.class;
143: } else if (aspectName.equals(MonitoringAspect.ASPECT_NAME)) {
144: annotation = MonitoringField.class;
145: } else if (aspectName.equals(CoreAspect.ASPECT_NAME)) {
146: annotation = CoreField.class;
147: }
148: if (annotation == null) {
149: return false;
150: }
151: Object defaultValue = getAnnotationDefaultValue(entry,
152: annotation);
153: // entry.getValue().equals(defaultValues.get(entry.getKey()))
154: if (entry.getValue().equals(defaultValue)) {
155: return true;
156: }
157: return false;
158: }
159: }
|