001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.meta;
010:
011: import com.completex.objective.util.PropertyMap;
012: import com.completex.objective.components.persistency.Mappable;
013:
014: import java.util.Iterator;
015: import java.util.LinkedHashMap;
016: import java.util.Map;
017:
018: /**
019: * @author Gennady Krizhevsky
020: */
021: public class MetaCompound implements Mappable {
022:
023: public static final String TAG_DELEGATE_FACTORY = "delegateFactory";
024: public static final String TAG_OVER_RIDE_PARENT = "overRideParent";
025: public static final String TAG_COMPOUND_CHILDREN = "children";
026:
027: private MetaDelegateFactory delegateFactory;
028: private boolean overRideParent;
029: private Map children = new LinkedHashMap(); // Map<MetaCompoundChild>
030:
031: public MetaCompound() {
032: }
033:
034: public MetaCompound(Map map) {
035: fromMap(map);
036: }
037:
038: public MetaDelegateFactory getDelegateFactory() {
039: return delegateFactory;
040: }
041:
042: public boolean isOverRideParent() {
043: return overRideParent;
044: }
045:
046: public void setOverRideParent(boolean overRideParent) {
047: this .overRideParent = overRideParent;
048: }
049:
050: public Map getChildren() {
051: return children;
052: }
053:
054: public boolean hasChildren() {
055: return children != null && !children.isEmpty();
056: }
057:
058: public boolean isEmpty() {
059: return delegateFactory == null && !hasChildren();
060: }
061:
062: public Map toMap() {
063: Map map = new LinkedHashMap();
064: if (delegateFactory != null) {
065: map.put(TAG_DELEGATE_FACTORY, delegateFactory.toMap());
066: }
067: map.put(TAG_OVER_RIDE_PARENT, Boolean.valueOf(overRideParent));
068: Map childrenMap = new LinkedHashMap();
069: map.put(TAG_COMPOUND_CHILDREN, childrenMap);
070: for (Iterator it = children.keySet().iterator(); it.hasNext();) {
071: String key = (String) it.next();
072: MetaCompoundChild child = (MetaCompoundChild) children
073: .get(key);
074: childrenMap.put(key, child.toMap());
075: child.setName(key);
076: }
077: return map;
078: }
079:
080: public void fromMap(Map map) {
081: PropertyMap propertyMap = PropertyMap.toPropertyMap(map);
082: PropertyMap delegateFactoryMap = propertyMap
083: .getPropertyMap(TAG_DELEGATE_FACTORY);
084:
085: if (delegateFactoryMap != null && !delegateFactoryMap.isEmpty()) {
086: delegateFactory = new MetaDelegateFactory(
087: delegateFactoryMap);
088: }
089:
090: overRideParent = propertyMap.getBoolean(TAG_OVER_RIDE_PARENT);
091:
092: PropertyMap childrenMap = propertyMap
093: .getPropertyMap(TAG_COMPOUND_CHILDREN);
094: if (childrenMap != null) {
095: for (Iterator it = childrenMap.keySet().iterator(); it
096: .hasNext();) {
097: String key = (String) it.next();
098: try {
099: PropertyMap childMap = childrenMap
100: .getPropertyMap(key);
101: MetaCompoundChild child = new MetaCompoundChild(
102: childMap);
103: children.put(key, child);
104: child.setName(key);
105: } catch (ClassCastException e) {
106: throw new RuntimeException(
107: "Class cast exception while reading childMap by key "
108: + key, e);
109: }
110: }
111: }
112: }
113:
114: }
|