001: package com.completex.objective.tools.generators;
002:
003: import com.completex.objective.components.log.Log;
004: import com.completex.objective.components.persistency.meta.Referencing;
005: import com.completex.objective.components.persistency.meta.MetaRef;
006: import com.completex.objective.components.persistency.meta.MetaObjectReference;
007: import com.completex.objective.components.persistency.meta.MetaObjectModel;
008: import com.completex.objective.components.persistency.MetaTable;
009: import com.completex.objective.components.OdalRuntimeException;
010: import com.completex.objective.util.StringUtil;
011:
012: import java.util.Map;
013:
014: /**
015: * @author Gennady Krizhevsky
016: */
017: public class BaseExtractHandler {
018: private Log logger = Log.NULL_LOGGER;
019: //
020: // In:
021: //
022: protected MetaObjectReference metaObjectReference;
023: protected MetaModelsExtractor.ExtractStruct extractStruct;
024: protected MetaObjectModel metaObjectModel;
025: protected String key;
026: protected boolean generateClassesOnly;
027: protected boolean _interface;
028: //
029: // Out:
030: //
031: protected Map baseTemplate;
032: protected MetaModelsExtractor.ExtractStructEntry baseEntry;
033: protected String baseName;
034: protected String baseClassPackageName;
035: private boolean root;
036:
037: protected BaseExtractHandler() {
038: }
039:
040: public BaseExtractHandler(MetaObjectReference metaObjectReference,
041: MetaModelsExtractor.ExtractStruct extractStruct,
042: MetaObjectModel metaObjectModel, String key,
043: boolean generateClasses) {
044: this .metaObjectReference = metaObjectReference;
045: this .extractStruct = extractStruct;
046: this .metaObjectModel = metaObjectModel;
047: this .key = key;
048: this .generateClassesOnly = generateClasses;
049: }
050:
051: public String extractBase(Referencing childReferencing) {
052: //
053: // Extract child base name:
054: //
055: return extractChildBaseName(childReferencing.getBase());
056: }
057:
058: protected String extractChildBaseName(MetaRef baseRef) {
059: baseName = null;
060: String childBaseName = baseRef.getName();
061: MetaObjectReference childObjectReference = metaObjectModel
062: .getMetaObjectReference(childBaseName);
063:
064: baseNameByClassName(baseRef, childObjectReference);
065:
066: if (baseName == null) {
067: //
068: // We did not find base reference in the composite-desc file - try to find it in
069: // external desc file:
070: //
071: String baseRefName = baseRef.getName();
072: MetaModelsExtractor.ExtractStructEntry entry = extractStruct
073: .getEntry(baseRefName);
074: if (entry != null) {
075: baseNameByEntry(entry);
076: root = true;
077: } else if (childObjectReference != null) {
078: baseName = extractChildBaseName(childObjectReference
079: .getBase());
080: }
081: }
082: invalidateNull(baseRef);
083: return baseName;
084: }
085:
086: private void invalidateNull(MetaRef baseRef) {
087: if (baseName == null) {
088: throw new IllegalArgumentException(
089: "Cannot find base reference ["
090: + baseRef.getName()
091: + "] neither in composite descriptor nor in the basic one");
092: }
093: }
094:
095: private void baseNameByClassName(MetaRef baseRef,
096: MetaObjectReference childObjectReference) {
097: if (baseRef.getClassName() != null && generateClassesOnly) {
098: baseName = baseRef.getClassName();
099: } else if (childObjectReference != null) {
100: if (generateClassesOnly) {
101: if (childObjectReference.getClassName() != null) {
102: baseName = childObjectReference.getClassName();
103: }
104: } else {
105: if (childObjectReference.getInterfaceName() != null) {
106: baseName = childObjectReference.getInterfaceName();
107: }
108: }
109: }
110: }
111:
112: private void baseNameByEntry(
113: MetaModelsExtractor.ExtractStructEntry entry) {
114: MetaTable baseMetaTable = entry.getMetaTable();
115:
116: Number index = entry.getModelIndex();
117: MetaModelsExtractor.LineStructBlock refBlock = extractStruct
118: .getBlocks()[index.intValue()];
119: //
120: // Now we have all the data to generate name of the base class:
121: //
122: String fullPrefix;
123: boolean mainPattern = CompositePoGenerator
124: .isMainPattern(baseMetaTable);
125: MetaModelsExtractor.LineStruct classStruct = refBlock.classStruct;
126: MetaModelsExtractor.LineStruct intfStruct = refBlock.intfStruct;
127: if (generateClassesOnly) {
128: baseClassPackageName = classStruct.packageName;
129: entry.setBaseClassPackageName(baseClassPackageName);
130: fullPrefix = baseClassPackageName + "."
131: + classStruct.classPrefix;
132: baseName = CompositePoGenerator.javaName(baseMetaTable
133: .getTableAlias(), fullPrefix,
134: classStruct.classSuffix, true);
135: } else {
136: if (intfStruct == null) {
137: throw new OdalRuntimeException(
138: "Interface Struct for basic class not "
139: + "found meaning that '"
140: + CompositePoGenerator.GENERATE_INTERFACES
141: + "' property is set to 'false' in the basic PO generator config file. "
142: + "Since composite interfaces inherit the basic ones you have to set this "
143: + "property to 'true' in the basic config file and regenerate the basic POs. Alternatively you may set this property to "
144: + "'false' in the composite config file.");
145: }
146: fullPrefix = intfStruct.packageName + "."
147: + intfStruct.classPrefix;
148: baseName = CompositePoGenerator.javaName(baseMetaTable
149: .getTableAlias(), fullPrefix,
150: intfStruct.classSuffix, true);
151: _interface = true;
152: }
153: }
154:
155: public Map extractChildBaseTemplate(MetaRef baseRef) {
156: String refName = baseRef.getName();
157: return extractChildBaseTemplate(refName);
158: }
159:
160: public Map extractChildBaseTemplate(String refName) {
161: MetaObjectReference refReference = null;
162: if (metaObjectModel != null) {
163: refReference = metaObjectModel
164: .getMetaObjectReference(refName);
165: }
166:
167: if (refReference != null) {
168: //
169: // Extract recursively until refReference == null
170: //
171: extractChildBaseTemplate(refReference.getBase());
172: } else {
173: //
174: // We did not find base reference in the composite-desc file - try to find it in
175: // external desc file:
176: //
177: MetaModelsExtractor.ExtractStructEntry entry = extractStruct
178: .getEntry(refName);
179: baseEntry = entry;
180:
181: if (entry != null) {
182: this .root = true;
183: Number index = entry.getModelIndex();
184: MetaModelsExtractor.LineStructBlock refBlock = extractStruct
185: .getBlocks()[index.intValue()];
186: MetaTable metaTable = entry.getMetaTable();
187: //
188: // Now we have all the data to generate name of the base class:
189: //
190: MetaModelsExtractor.LineStruct classStruct = refBlock.classStruct;
191: MetaModelsExtractor.LineStruct intfStruct = refBlock.intfStruct;
192: if (generateClassesOnly) {
193: baseClassPackageName = classStruct.packageName;
194: entry.setBaseClassPackageName(baseClassPackageName);
195: baseTemplate = extractStruct
196: .getMetaModelsExtractor()
197: .prepareClassTableTemplateMap(metaTable,
198: classStruct, intfStruct);
199: } else {
200: baseTemplate = extractStruct
201: .getMetaModelsExtractor()
202: .prepareInterfaceTableMap(metaTable,
203: intfStruct);
204: }
205: } else {
206: throw new IllegalArgumentException(
207: "Cannot find base reference ["
208: + refName
209: + "] neither in composite descriptor nor in the basic one");
210: }
211: }
212: return baseTemplate;
213: }
214:
215: public Log getLogger() {
216: return logger;
217: }
218:
219: public void setLogger(Log logger) {
220: this .logger = logger;
221: }
222:
223: public String getBaseName() {
224: return baseName;
225: }
226:
227: public MetaModelsExtractor.ExtractStructEntry getBaseEntry() {
228: return baseEntry;
229: }
230:
231: public boolean isInterface() {
232: return _interface;
233: }
234:
235: public String getShortBaseName() {
236: return StringUtil.shortClassName(baseName);
237: }
238:
239: public boolean isRoot() {
240: return root;
241: }
242:
243: public String getBaseClassPackageName() {
244: return baseClassPackageName;
245: }
246:
247: public void setBaseClassPackageName(String baseClassPackageName) {
248: this.baseClassPackageName = baseClassPackageName;
249: }
250:
251: }
|