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;
010:
011: import com.completex.objective.util.TypeUtil;
012:
013: import java.util.ArrayList;
014: import java.util.Iterator;
015: import java.util.LinkedHashMap;
016: import java.util.List;
017: import java.util.Map;
018:
019: /**
020: * Under construction
021: *
022: * @author Gennady Krizhevsky
023: */
024: public class UserDefinedTypeMetaTable implements ModelConsts {
025: private String name;
026: private String alias;
027: private String typeCode;
028: private String super TypeName;
029: private String super TypeOwner;
030: private String typeId;
031: private String objectId;
032: private List dependsOnObjectIds = new ArrayList();
033: private Map modelByObjectId;
034: private List columnsList = new ArrayList(); // List of (UserDefinedTypeMetaColumn columns)
035: private Map columns = new LinkedHashMap(); // Map of (String columnName, UserDefinedTypeMetaColumn column)
036: //
037: // Collections specific:
038: //
039: private boolean collection;
040: private String collectionOfName;
041: private String collectionOfObjectId;
042:
043: public UserDefinedTypeMetaTable() {
044: }
045:
046: public UserDefinedTypeMetaTable(String name) {
047: this .name = name;
048: this .alias = name;
049: }
050:
051: //
052: // getters/setters:
053: //
054:
055: public Map getModelByObjectId() {
056: return modelByObjectId;
057: }
058:
059: public void setModelByObjectId(Map modelByObjectId) {
060: this .modelByObjectId = modelByObjectId;
061: }
062:
063: public String getName() {
064: return name;
065: }
066:
067: public void setName(String name) {
068: this .name = name;
069: }
070:
071: public String getTypeCode() {
072: return typeCode;
073: }
074:
075: public void setTypeCode(String typeCode) {
076: this .typeCode = typeCode;
077: }
078:
079: public String getSuperTypeName() {
080: return super TypeName;
081: }
082:
083: public void setSuperTypeName(String super TypeName) {
084: this .super TypeName = super TypeName;
085: }
086:
087: public String getTypeId() {
088: return typeId;
089: }
090:
091: public void setTypeId(String typeId) {
092: this .typeId = typeId;
093: }
094:
095: public String getObjectId() {
096: return objectId;
097: }
098:
099: public void setObjectId(String objectId) {
100: this .objectId = objectId;
101: if (modelByObjectId != null
102: && !modelByObjectId.containsKey(objectId)) {
103: modelByObjectId.put(objectId, this );
104: }
105: }
106:
107: public List getDependsOnObjectIds() {
108: return dependsOnObjectIds;
109: }
110:
111: public void setDependsOnObjectIds(List dependsOnObjectIds) {
112: this .dependsOnObjectIds = dependsOnObjectIds;
113: }
114:
115: public boolean isCollection() {
116: return collection;
117: }
118:
119: public void setCollection(boolean collection) {
120: this .collection = collection;
121: }
122:
123: public String getCollectionOfName() {
124: return collectionOfName;
125: }
126:
127: public void setCollectionOfName(String collectionOfName) {
128: this .collectionOfName = collectionOfName;
129: }
130:
131: public String getCollectionOfObjectId() {
132: return collectionOfObjectId;
133: }
134:
135: public void setCollectionOfObjectId(String collectionOfObjectId) {
136: this .collectionOfObjectId = collectionOfObjectId;
137: }
138:
139: public String getSuperTypeOwner() {
140: return super TypeOwner;
141: }
142:
143: public void setSuperTypeOwner(String super TypeOwner) {
144: this .super TypeOwner = super TypeOwner;
145: }
146:
147: public String getAlias() {
148: return alias;
149: }
150:
151: public void setAlias(String alias) {
152: this .alias = alias;
153: }
154:
155: //
156: // Columns related:
157: //
158:
159: public UserDefinedTypeMetaColumn getColumn(String columnName) {
160: return (UserDefinedTypeMetaColumn) columns.get(columnName);
161: }
162:
163: public boolean containsColumn(String columnName) {
164: return columns.containsKey(columnName);
165: }
166:
167: public UserDefinedTypeMetaColumn getColumn(int columnIndex) {
168: return (UserDefinedTypeMetaColumn) columnsList.get(columnIndex);
169: }
170:
171: public UserDefinedTypeMetaColumn addColumn(String columnName) {
172: if (columnName == null) {
173: throw new IllegalArgumentException("columnName == null");
174: }
175: UserDefinedTypeMetaColumn column = getColumn(columnName);
176: if (column == null) {
177: column = new UserDefinedTypeMetaColumn(columnName, this );
178: }
179: addColumn0(columnName, column);
180: return column;
181: }
182:
183: public UserDefinedTypeMetaColumn addColumn(
184: UserDefinedTypeMetaColumn column) {
185: if (column == null) {
186: throw new IllegalArgumentException("column == null");
187: }
188: addColumn0(column.getColumnName(), column);
189: return column;
190: }
191:
192: private void addColumn0(String columnKey,
193: UserDefinedTypeMetaColumn column) {
194: columns.put(columnKey, column);
195: columnsList.add(column);
196: }
197:
198: public int size() {
199: return columnsList.size();
200: }
201:
202: //
203: //
204: //
205: public void addDependsOnId(String id) {
206: if (!dependsOnObjectIds.contains(id)) {
207: dependsOnObjectIds.add(id);
208: }
209: }
210:
211: public Map toInternalMap() {
212: final Map tableMap = new LinkedHashMap();
213: tableMap.put(TABLE_NAME_TAG, name);
214: // Add columns:
215: Map columnsList = new LinkedHashMap();
216: tableMap.put(ModelConsts.COLUMNS_TAG, columnsList);
217: tableMap.put(TYPE_CODE_TAG, typeCode);
218: tableMap.put(SUPER_TYPE_NAME_TAG, super TypeName);
219: tableMap.put(SUPER_TYPE_OWNER_TAG, super TypeOwner);
220: tableMap.put(TYPE_ID_TAG, typeId);
221: tableMap.put(OBJECT_ID_TAG, objectId);
222: tableMap.put(IS_COLLECTION_TAG, Boolean.valueOf(collection));
223: tableMap.put(COLLECTION_OF_NAME_TAG, collectionOfName);
224: tableMap.put(COLLECTION_OF_OBJECT_ID_TAG, collectionOfObjectId);
225: for (Iterator it = columns.keySet().iterator(); it.hasNext();) {
226: String columnName = (String) it.next();
227: UserDefinedTypeMetaColumn column = getColumn(columnName);
228: columnsList.put(column.getColumnName(), column
229: .toInternalMap());
230: }
231:
232: return tableMap;
233: }
234:
235: public void fromInternalMap(Map tableMap) {
236: name = (String) tableMap.get(TABLE_NAME_TAG);
237: typeCode = (String) tableMap.get(TYPE_CODE_TAG);
238: super TypeName = (String) tableMap.get(SUPER_TYPE_NAME_TAG);
239: super TypeOwner = (String) tableMap.get(SUPER_TYPE_OWNER_TAG);
240: typeId = (String) tableMap.get(TYPE_ID_TAG);
241: objectId = (String) tableMap.get(OBJECT_ID_TAG);
242: collection = TypeUtil.extractBoolean(tableMap,
243: IS_COLLECTION_TAG);
244: collectionOfName = (String) tableMap
245: .get(COLLECTION_OF_NAME_TAG);
246: collectionOfObjectId = (String) tableMap
247: .get(COLLECTION_OF_OBJECT_ID_TAG);
248:
249: Map columnsMap = (Map) tableMap.get(COLUMNS_TAG);
250: for (Iterator it = columnsMap.keySet().iterator(); it.hasNext();) {
251: Object key = it.next();
252: Map columnMap = (Map) columnsMap.get(key);
253: String columnName = (String) columnMap.get(COLUMN_NAME_TAG);
254: UserDefinedTypeMetaColumn column = (UserDefinedTypeMetaColumn) columns
255: .get(columnName);
256: if (column == null) {
257: column = new UserDefinedTypeMetaColumn();
258: }
259: column.fromInternalMap(columnMap);
260: addColumn0(columnName, column);
261: }
262: }
263:
264: public Map toExternalMap() {
265: final Map tableMap = new LinkedHashMap();
266: tableMap.put(TABLE_ALIAS_TAG, alias == null ? name : alias);
267: tableMap.put(TABLE_NAME_TAG, name);
268:
269: // Add columns:
270: Map columnsMap = new LinkedHashMap();
271: tableMap.put(ModelConsts.COLUMNS_TAG, columnsMap);
272:
273: for (int i = 0; i < columnsList.size(); i++) {
274: UserDefinedTypeMetaColumn column = getColumn(i);
275: // Columns is map by ColumnNames:
276: columnsMap.put(column.getColumnName(), column
277: .toExternalMap());
278: }
279: return tableMap;
280: }
281:
282: public void fromExternalMap(Map tableMap) {
283: alias = (String) tableMap.get(ModelConsts.TABLE_ALIAS_TAG);
284: name = name != null ? name : (String) tableMap
285: .get(TABLE_NAME_TAG);
286: Map columnsMap = (Map) tableMap.get(COLUMNS_TAG);
287: for (Iterator it = columnsMap.keySet().iterator(); it.hasNext();) {
288: Object key = it.next();
289: Map columnMap = (Map) columnsMap.get(key);
290: String columnName = (String) columnMap.get(COLUMN_NAME_TAG);
291: UserDefinedTypeMetaColumn column = (UserDefinedTypeMetaColumn) columns
292: .get(columnName);
293: if (column != null) {
294: column.fromExternalMap(columnMap);
295: columns.put(columnName, column);
296: }
297: }
298: }
299:
300: }
|