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.adapter;
010:
011: import com.completex.objective.components.log.Log;
012: import com.completex.objective.components.persistency.ColumnType;
013: import com.completex.objective.components.persistency.JavaToMetaType;
014: import com.completex.objective.components.persistency.MetaColumn;
015: import com.completex.objective.components.persistency.MetaTable;
016: import com.completex.objective.components.persistency.UserDefinedTypeMetaModel;
017: import com.completex.objective.components.persistency.meta.MetaModel;
018: import com.completex.objective.components.persistency.meta.ModelLoader;
019: import com.completex.objective.components.persistency.meta.ModelLoaderPlugin;
020: import com.completex.objective.components.persistency.meta.impl.DatabaseModelLoaderImpl;
021: import com.completex.objective.components.persistency.meta.impl.FileModelLoaderImpl;
022: import com.completex.objective.components.persistency.meta.impl.NullModelLoaderImpl;
023: import com.completex.objective.components.persistency.meta.impl.UdtBaseModelLoaderPlugin;
024: import com.completex.objective.components.sdl.reader.SdlReader;
025: import com.completex.objective.components.sdl.reader.impl.SdlReaderImpl;
026:
027: import java.io.IOException;
028: import java.io.Reader;
029: import java.sql.Connection;
030: import java.sql.SQLException;
031: import java.util.Iterator;
032: import java.util.LinkedHashMap;
033: import java.util.Map;
034:
035: /**
036: * @author Gennady Krizhevsky
037: */
038: public class ModelLoaderAdapter implements ModelLoader {
039:
040: private ModelLoader internalFileModelLoader;
041: private ModelLoader externalFileModelLoader = new NullModelLoaderImpl();
042: private JavaToMetaType javaToMetaType;
043: private MetaColumn.GeneratorStruct creationDateStruct;
044: private MetaColumn.GeneratorStruct lastUpdatedStruct;
045: private Log logger;
046:
047: private static final SdlReader sdlReader = new SdlReaderImpl();
048: private Map plugins = new LinkedHashMap();
049:
050: public ModelLoaderAdapter(ModelLoader internalFileModelLoader,
051: ModelLoader externalFileModelLoader,
052: JavaToMetaType javaToMetaType, Log logger) {
053: this (internalFileModelLoader, externalFileModelLoader,
054: javaToMetaType, null, null, logger);
055: }
056:
057: public ModelLoaderAdapter(ModelLoader internalFileModelLoader,
058: ModelLoader externalFileModelLoader,
059: JavaToMetaType javaToMetaType,
060: MetaColumn.GeneratorStruct creationDateStruct,
061: MetaColumn.GeneratorStruct lastUpdatedStruct, Log logger) {
062: // assert internalFileModelLoader != null;
063: // assert logger != null;
064:
065: this .logger = logger;
066: this .internalFileModelLoader = internalFileModelLoader;
067: this .internalFileModelLoader.setJavaToMetaType(javaToMetaType);
068: this .creationDateStruct = creationDateStruct;
069: this .lastUpdatedStruct = lastUpdatedStruct;
070:
071: if (externalFileModelLoader != null) {
072: this .externalFileModelLoader = externalFileModelLoader;
073: this .externalFileModelLoader
074: .setJavaToMetaType(javaToMetaType);
075: }
076: setJavaToMetaType(javaToMetaType);
077: }
078:
079: protected Log getLogger() {
080: return logger;
081: }
082:
083: private void loadPluginsData(MetaModel model) throws Exception {
084: //
085: // Plugins related stuff here:
086: //
087: // For now - load only udt one:
088: UserDefinedTypeMetaModel pluginModel = null;
089: if (plugins != null) {
090: ModelLoaderPlugin plugin = (ModelLoaderPlugin) plugins
091: .get(UdtBaseModelLoaderPlugin.PLUGIN_KEY);
092: if (plugin != null) {
093: pluginModel = plugin.load(pluginModel);
094: model.setUserDefinedTypeMetaModel(pluginModel);
095: }
096: }
097: }
098:
099: private MetaModel loadModel() throws Exception {
100:
101: //
102: // Always 1st load the internal:
103: //
104: MetaModel model = internalFileModelLoader.load(null);
105: //
106: // Load external model if exists:
107: //
108: if (externalFileModelLoader != null) {
109: model = externalFileModelLoader.load(model);
110: }
111: //
112: // Populate model with extra data:
113: //
114: for (Iterator it = model.tableIterator(); it.hasNext();) {
115: Map.Entry entry = (Map.Entry) it.next();
116: MetaTable table = (MetaTable) entry.getValue();
117: for (int i = 0; i < table.size(); i++) {
118: MetaColumn column = table.getColumn(i);
119: if (lastUpdatedStruct != null) {
120: column.setLastUpdatedNames(lastUpdatedStruct);
121: }
122: if (creationDateStruct != null) {
123: column.setCreationDateNames(creationDateStruct);
124: }
125: }
126: }
127:
128: return model;
129: }
130:
131: public MetaModel load(MetaModel model) throws Exception {
132: model = loadModel();
133: //
134: // Plugins related stuff here:
135: //
136: loadPluginsData(model);
137: return model;
138: }
139:
140: public void registerPlugin(ModelLoaderPlugin plugin) {
141: String key = plugin.getPluginKey();
142: if (plugins.containsKey(key)) {
143: throw new RuntimeException(
144: "There is already storer plugin with key " + key);
145: }
146: plugins.put(key, plugin);
147: plugin.setJavaToMetaType(javaToMetaType);
148: }
149:
150: public void unregisterPlugin(String key) {
151: plugins.remove(key);
152: }
153:
154: public JavaToMetaType getJavaToMetaType() {
155: return javaToMetaType;
156: }
157:
158: public void setJavaToMetaType(JavaToMetaType javaToMetaType) {
159: this .javaToMetaType = javaToMetaType;
160: }
161:
162: //
163: //
164: // Auxilliary adapters
165: //
166: //
167: public static class InternalFileModelLoaderAdapter extends
168: FileModelLoaderImpl {
169:
170: public InternalFileModelLoaderAdapter(String descriptorPath,
171: String filterPattern) {
172: super (descriptorPath, filterPattern);
173: }
174:
175: public InternalFileModelLoaderAdapter(Reader reader,
176: String filterPattern) {
177: super (reader, filterPattern);
178: }
179:
180: protected MetaModel toModel(MetaModel model, Map modelMap) {
181: model.fromInternalMap(modelMap);
182: return model;
183: }
184:
185: protected Map readModelMap(Reader reader) throws IOException {
186: return (Map) sdlReader.read(reader);
187: }
188:
189: public void setJavaToMetaType(JavaToMetaType javaToMetaType) {
190: }
191: }
192:
193: public static class ExternalFileModelLoaderAdapter extends
194: FileModelLoaderImpl {
195:
196: public ExternalFileModelLoaderAdapter(String descriptorPath,
197: String filterPattern) {
198: super (descriptorPath, filterPattern);
199: }
200:
201: public ExternalFileModelLoaderAdapter(Reader reader,
202: String filterPattern) {
203: super (reader, filterPattern);
204: }
205:
206: protected MetaModel toModel(MetaModel model, Map modelMap) {
207: model.fromExternalMap(modelMap);
208: return model;
209: }
210:
211: protected Map readModelMap(Reader reader) throws IOException {
212: return (Map) sdlReader.read(reader);
213: }
214:
215: public void setJavaToMetaType(JavaToMetaType javaToMetaType) {
216: }
217: }
218:
219: public static class DatabaseModelLoaderAdapter extends
220: DatabaseModelLoaderImpl {
221:
222: private Connection connection;
223: private JavaToMetaType javaToMetaType;
224: private Log logger;
225:
226: public DatabaseModelLoaderAdapter(String schema,
227: String catalog, String filterPattern,
228: Connection connection, Log logger) {
229: this (schema, catalog, filterPattern, connection, null,
230: logger);
231: }
232:
233: DatabaseModelLoaderAdapter(String schema, String catalog,
234: String filterPattern, Connection connection,
235: JavaToMetaType javaToMetaType, Log logger) {
236: super (schema, catalog, filterPattern);
237: this .logger = logger;
238: this .connection = connection;
239: this .javaToMetaType = javaToMetaType;
240: }
241:
242: public void setJavaToMetaType(JavaToMetaType javaToMetaType) {
243: this .javaToMetaType = javaToMetaType;
244: }
245:
246: protected Connection getConnection() throws SQLException {
247: return connection;
248: }
249:
250: protected ColumnType columnType(String dataType,
251: int columnSize, int decimalDigits, boolean required) {
252: return javaToMetaType.dataType(dataType, columnSize,
253: decimalDigits, required);
254: }
255:
256: protected void debug(String message) {
257: logger.debug(message);
258: }
259:
260: protected void info(String message) {
261: logger.info(message);
262: }
263: }
264:
265: }
|