001: package com.completex.objective.tools.generators;
002:
003: import com.completex.objective.components.persistency.MetaColumn;
004: import com.completex.objective.components.persistency.MetaTable;
005: import com.completex.objective.components.persistency.meta.MetaCompoundChild;
006: import com.completex.objective.components.persistency.meta.MetaObjectModel;
007: import com.completex.objective.components.persistency.meta.MetaObjectReference;
008:
009: import java.util.ArrayList;
010: import java.util.Iterator;
011: import java.util.Map;
012:
013: /**
014: * @author Gennady Krizhevsky
015: */
016: public class CompoundChildHandler extends ComplexChildHandler {
017:
018: private ArrayList excludedChildColumns = new ArrayList() /* List of excluded columns <String> */;
019: private boolean overRideParent;
020:
021: public CompoundChildHandler(
022: MetaObjectReference metaObjectReference,
023: MetaModelsExtractor.ExtractStruct extractStruct,
024: MetaObjectModel metaObjectModel, String key,
025: boolean generateClasses) {
026: this .metaObjectReference = metaObjectReference;
027: this .extractStruct = extractStruct;
028: this .metaObjectModel = metaObjectModel;
029: this .key = key;
030: this .generateClassesOnly = generateClasses;
031:
032: handle();
033: }
034:
035: /**
036: * 1. Take a object reference.
037: * Find the bottom base (basic persistent object)
038: * 2. Get immediate parent of this object reference.
039: * if it is compound (has non-empty MetaCompound) then if it has NO childen
040: * mark this object reference with "overrideParent = true"
041: */
042: protected void handle() {
043: // 1. Take a object reference.
044: // Find the bottom base (basic persistent object)
045: extractRootMetaTable();
046: //
047: // 2. Get immediate parent of this object reference.
048: // if it is compound (has non-empty MetaCompound) then if it has NO childen
049: // mark this object reference with "overrideParent = true"
050: //
051: MetaObjectReference immediateParentRef = metaObjectModel
052: .getMetaObjectReference(metaObjectReference.getBase()
053: .getName());
054: populateOverRideParent(immediateParentRef);
055: }
056:
057: /**
058: * Get the child's meta table.
059: * <p/>
060: * Collect excluded child columns with containment type "IS".
061: * <p/>
062: * Exclude child columns with containment type "IS".
063: * <p/>
064: * Exclude child columns with containment type "IS".
065: * <p/>
066: * Extract child base name.
067: * <p/>
068: * Extract child base template.
069: *
070: * @param key
071: * @param childReferencing
072: * @param compoundChildrenMap
073: * @param compoundChildren
074: */
075: public void handleChild(String key,
076: MetaCompoundChild childReferencing,
077: Map compoundChildrenMap, Map compoundChildren) {
078: //
079: // Get the child's meta table:
080: //
081: MetaTable childMetaTable = extractChildMetaTable(childReferencing
082: .getBase());
083: //
084: // Collect excluded child columns with containment type "IS":
085: //
086: collectExcludedIsChildColumns(childReferencing, childMetaTable,
087: rootMetaTable);
088: for (Iterator iterator = compoundChildrenMap.keySet()
089: .iterator(); iterator.hasNext();) {
090: String otherKey = (String) iterator.next();
091: MetaCompoundChild otherChild = (MetaCompoundChild) compoundChildren
092: .get(otherKey);
093: if (!otherChild.getName()
094: .equals(childReferencing.getName())) {
095: MetaTable otherChildMetaTable = extractChildMetaTable(otherChild
096: .getBase());
097: collectExcludedIsChildColumns(childReferencing,
098: childMetaTable, otherChildMetaTable);
099: }
100: }
101:
102: //
103: // Exclude child columns with containment type "IS"
104: //
105: excludeIsChildColumn(childMetaTable);
106: //
107: // Extract child base name:
108: //
109: extractChildBaseName(childReferencing.getBase());
110: //
111: // Extract child Base Template:
112: //
113: extractChildBaseTemplate(childReferencing.getBase());
114: }
115:
116: private void collectExcludedIsChildColumns(MetaCompoundChild child,
117: MetaTable metaTable, MetaTable otherMetaTable) {
118: if (child.getContainmentType() == MetaCompoundChild.CONTAINMENT_IS) {
119: String childName = child.getName();
120: String rootTableName = otherMetaTable.getTableName();
121: String tableName = metaTable.getTableName();
122: if (!tableName.equals(rootTableName)) {
123: MetaColumn[] fkColumns = metaTable
124: .getColumnsInFk(rootTableName);
125: for (int i = 0; i < fkColumns.length; i++) {
126: MetaColumn fkColumn = fkColumns[i];
127: for (int j = 0; j < otherMetaTable.size(); j++) {
128: MetaColumn rootColumn = otherMetaTable
129: .getColumn(j);
130: if (fkColumn.getColumnAlias().equals(
131: rootColumn.getColumnAlias())) {
132: addExcludedChildColumn(metaTable
133: .getTableName(), fkColumn
134: .getColumnAlias());
135: getLogger()
136: .debug(
137: "Excluded MetaColumn: childName = "
138: + childName
139: + "; rootAlias = "
140: + rootColumn
141: .getColumnAlias()
142: + ""
143: + "; fkAlias = "
144: + fkColumn
145: .getColumnAlias());
146: }
147: }
148: }
149: }
150: }
151: }
152:
153: private void addExcludedChildColumn(String tableName,
154: String columnName) {
155: ExcludedColumn excludedColumn = new ExcludedColumn(tableName,
156: columnName);
157: excludedChildColumns.add(excludedColumn);
158: }
159:
160: private boolean isColumnExcluded(String tableName, String columnName) {
161: ExcludedColumn excludedColumn = new ExcludedColumn(tableName,
162: columnName);
163: return excludedChildColumns.contains(excludedColumn);
164: }
165:
166: /**
167: * Get immediate parent of this object reference.
168: * if it is compound (has non-empty MetaCompound) then if it has NO childen
169: * mark this object reference with "overrideParent = true"
170: *
171: * @param immediateParentRef
172: */
173: private void populateOverRideParent(
174: MetaObjectReference immediateParentRef) {
175: if (immediateParentRef != null
176: && !immediateParentRef.hasCompoundChildren()) {
177: overRideParent = true;
178: }
179: }
180:
181: private void excludeIsChildColumn(MetaTable childMetaTable) {
182: for (int i = 0; i < childMetaTable.size(); i++) {
183: MetaColumn column = childMetaTable.getColumn(i);
184: if (isColumnExcluded(childMetaTable.getTableName(), column
185: .getColumnAlias())) {
186: column.setExclude(true);
187: }
188: }
189: }
190:
191: public boolean isOverRideParent() {
192: return overRideParent;
193: }
194:
195: public Map getBaseTemplate() {
196: return baseTemplate;
197: }
198:
199: class ExcludedColumn {
200: String childTableName;
201: String childColumnAlias;
202:
203: public ExcludedColumn(String childTableName,
204: String childColumnAlias) {
205: this .childTableName = childTableName;
206: this .childColumnAlias = childColumnAlias;
207: }
208:
209: public boolean equals(Object o) {
210: if (this == o)
211: return true;
212: if (o == null || getClass() != o.getClass())
213: return false;
214:
215: final ExcludedColumn that = (ExcludedColumn) o;
216:
217: if (childColumnAlias != null ? !childColumnAlias
218: .equals(that.childColumnAlias)
219: : that.childColumnAlias != null)
220: return false;
221: if (childTableName != null ? !childTableName
222: .equals(that.childTableName)
223: : that.childTableName != null)
224: return false;
225:
226: return true;
227: }
228:
229: public int hashCode() {
230: int result;
231: result = (childTableName != null ? childTableName
232: .hashCode() : 0);
233: result = 29
234: * result
235: + (childColumnAlias != null ? childColumnAlias
236: .hashCode() : 0);
237: return result;
238: }
239: }
240:
241: }
|