001: package org.romaframework.module.designer.serializer.xml;
002:
003: import java.lang.reflect.Method;
004: import java.util.HashMap;
005: import java.util.Map;
006: import java.util.Map.Entry;
007: import org.apache.xmlbeans.XmlObject;
008: import org.romaframework.core.serializer.DefaultValues;
009: import org.romaframework.core.serializer.RomaObjectSerializer;
010: import org.romaframework.core.serializer.RomaSerializationException;
011: import org.romaframework.core.util.DynaBean;
012:
013: public abstract class AbstractSchemaSerializer extends
014: RomaObjectSerializer {
015: protected final static String LEVEL = "level";
016: protected final static Integer INITIAL_LEVEL = new Integer(0);
017: protected final static int MAX_SERIALIZATION_LEVEL = 3;
018:
019: protected void createAspectXml(String aspectName,
020: DynaBean iFeatures, XmlObject iToSet)
021: throws RomaSerializationException {
022: if (iFeatures == null) {
023: return;
024: }
025: Map<String, Object> features = iFeatures.getAttributes();
026: for (Entry<String, Object> entry : features.entrySet()) {
027: try {
028: // TODO: serialize the area elements, postValidation doesn't exist
029: if (!entry.getKey().equals("form")
030: && !entry.getKey().equals("postValidation")) {
031: setFeatureOnXml(aspectName, entry, iToSet);
032: }
033: } catch (Exception e) {
034: e.printStackTrace();
035: throw new RomaSerializationException(
036: "Problems with field settings "
037: + entry.getKey() + " of aspect "
038: + aspectName + ": " + e.getMessage());
039: }
040: }
041: }
042:
043: protected void setFeatureOnXml(String aspectName,
044: Entry<String, Object> entry, XmlObject iToSet)
045: throws Exception {
046: DefaultValues initValues = DefaultValues.getInstance();
047: Object defValue = initValues.getDefaultValue(aspectName, entry
048: .getKey());
049: if (entry.getValue() != null
050: && !isDefaultValue(aspectName, entry)
051: && !entry.getValue().equals(defValue)
052: && !entry.getKey().equals("embeddedType")) {
053: Object valueToSet = getValueToSet(entry, iToSet);
054: Class[] types = new Class[1];
055: if (valueToSet.getClass().equals(Boolean.class)) {
056: types[0] = boolean.class;
057: } else if (valueToSet.getClass().equals(Integer.class)) {
058: types[0] = int.class;
059: } else if (valueToSet.getClass().equals(Byte.class)) {
060: types[0] = byte.class;
061: } else {
062: types[0] = valueToSet.getClass();
063: }
064: Method method = iToSet.getClass().getMethod(
065: getMethodName(entry.getKey()), types);
066: Object[] params = new Object[1];
067: params[0] = valueToSet;
068: method.invoke(iToSet, params);
069: }
070: }
071:
072: protected String getMethodName(String fieldName) {
073: return "set" + fieldName.substring(0, 1).toUpperCase()
074: + fieldName.substring(1);
075: }
076:
077: /**
078: * It converts the Object in the schema to the Object of XmlBeans
079: *
080: * @param entry
081: * @param iToSet
082: * @return the object to set in Xml fragment
083: */
084: protected Object getValueToSet(Entry<String, Object> entry,
085: XmlObject iToSet) {
086: return entry.getValue();
087: }
088:
089: protected Object getAnnotationDefaultValue(
090: Entry<String, Object> entry, Class annotation)
091: throws NoSuchMethodException {
092: return annotation.getMethod(entry.getKey(), new Class[0])
093: .getDefaultValue();
094: }
095:
096: protected abstract boolean isDefaultValue(String aspectName,
097: Entry<String, Object> entry) throws Exception;
098:
099: @Override
100: public final XmlObject serialize(Object iToSerialize)
101: throws RomaSerializationException {
102: Map<String, Object> options = new HashMap<String, Object>();
103: options.put(LEVEL, INITIAL_LEVEL);
104: return serialize(iToSerialize, options);
105: }
106:
107: protected boolean serializeNextLevel(Map<String, Object> options) {
108: int actualLevel = (Integer) options.get(LEVEL);
109: return actualLevel <= MAX_SERIALIZATION_LEVEL;
110: }
111:
112: protected void increaseLevel(Map<String, Object> options) {
113: int actualLevel = (Integer) options.get(LEVEL);
114: actualLevel++;
115: options.put(LEVEL, new Integer(actualLevel));
116: }
117: }
|