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.impl;
010:
011: import com.completex.objective.components.persistency.JavaToMetaType;
012: import com.completex.objective.components.persistency.UserDefinedTypeMetaModel;
013: import com.completex.objective.components.persistency.meta.ModelLoaderPlugin;
014: import com.completex.objective.components.sdl.reader.SdlReader;
015: import com.completex.objective.components.sdl.reader.impl.SdlReaderImpl;
016: import com.completex.objective.util.PropertyMap;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.io.Reader;
021: import java.sql.Connection;
022: import java.sql.SQLException;
023: import java.util.Map;
024:
025: /**
026: * @author Gennady Krizhevsky
027: */
028: public class UdtBaseModelLoaderPlugin implements ModelLoaderPlugin {
029:
030: public static final String PLUGIN_KEY = "udt";
031:
032: private static final SdlReader sdlReader = new SdlReaderImpl();
033:
034: protected ExternalFileModelLoaderPluginAdapter externalFileModelLoaderPlugin;
035: protected InternalFileModelLoaderPluginAdapter internalFileModelLoaderPlugin;
036:
037: protected String externalDescPath;
038: protected String internalDescPath;
039: //
040: // input parameter:
041: //
042: protected boolean useDatabase;
043:
044: protected UserDefinedTypeMetaModel userDefinedTypeMetaModel = new UserDefinedTypeMetaModel();
045: protected JavaToMetaType javaToMetaType;
046:
047: public UdtBaseModelLoaderPlugin() {
048: }
049:
050: public UdtBaseModelLoaderPlugin(String externalDescPath,
051: String internalDescPath) {
052: this (externalDescPath, internalDescPath, false);
053: }
054:
055: public UdtBaseModelLoaderPlugin(String externalDescPath,
056: String internalDescPath, boolean skipInternal) {
057: this .externalDescPath = externalDescPath;
058: this .internalDescPath = internalDescPath;
059: instanciatePlugins(skipInternal);
060: }
061:
062: public UdtBaseModelLoaderPlugin(String externalFileName,
063: String internalFileName, String outDir) {
064: this (externalFileName, internalFileName, outDir, false);
065: }
066:
067: protected UdtBaseModelLoaderPlugin(String externalFileName,
068: String internalFileName, String outDir, boolean skipInternal) {
069: setup(outDir, externalFileName, internalFileName, skipInternal);
070: }
071:
072: private void setup(String outDir, String externalFileName,
073: String internalFileName, boolean skipInternal) {
074: composeAndSetDescPaths(outDir, externalFileName,
075: internalFileName);
076: instanciatePlugins(skipInternal);
077: }
078:
079: public String getPluginKey() {
080: return PLUGIN_KEY;
081: }
082:
083: private void composeAndSetDescPaths(String outDir,
084: String externalFileName, String internalFileName) {
085: externalDescPath = outDir + "/" + externalFileName;
086: internalDescPath = outDir + "/" + internalFileName;
087: }
088:
089: public void configure(PropertyMap udtProperties) throws IOException {
090: String outDir = udtProperties.getProperty("out_dir", true);
091: String externalFileName = udtProperties.getProperty(
092: "external_desc_file", true);
093: String internalFileName = udtProperties.getProperty(
094: "internal_desc_file", true);
095: boolean useDatabase = udtProperties.getBoolean("use_database",
096: Boolean.TRUE, true);
097:
098: setup(outDir, externalFileName, internalFileName, useDatabase);
099: }
100:
101: private void instanciatePlugins(boolean skipInternal) {
102: if (new File(externalDescPath).exists()) {
103: externalFileModelLoaderPlugin = new ExternalFileModelLoaderPluginAdapter(
104: externalDescPath);
105: }
106: if (!skipInternal) {
107: internalFileModelLoaderPlugin = new InternalFileModelLoaderPluginAdapter(
108: internalDescPath);
109: }
110: this .useDatabase = skipInternal;
111: }
112:
113: public UserDefinedTypeMetaModel load() throws Exception {
114: return load(null);
115: }
116:
117: public boolean needsConnection() {
118: return false;
119: }
120:
121: public void setConnection(Connection connection) {
122: }
123:
124: public UserDefinedTypeMetaModel load(
125: UserDefinedTypeMetaModel modelPar) throws SQLException {
126: //
127: // If we use database then it has to go 1st since added/removed columns
128: // will be identified by the 1st model
129: //
130: loadInternal(modelPar);
131: //
132: // 2nd load external descriptor from file:
133: //
134: loadExternal();
135: return userDefinedTypeMetaModel;
136: }
137:
138: protected void loadInternal(UserDefinedTypeMetaModel modelPar)
139: throws SQLException {
140: if (useDatabase) {
141: userDefinedTypeMetaModel = loadModelFromDb(modelPar);
142: } else {
143: userDefinedTypeMetaModel = internalFileModelLoaderPlugin
144: .load(modelPar);
145: if (userDefinedTypeMetaModel == null) {
146: throw new RuntimeException(
147: "Cannot load file from 'internalFileModelLoaderPlugin' while 'useDatabase'"
148: + " flag is set to 'false'");
149: }
150: }
151: }
152:
153: protected UserDefinedTypeMetaModel loadModelFromDb(
154: UserDefinedTypeMetaModel modelPar) throws SQLException {
155: throw new UnsupportedOperationException();
156: }
157:
158: protected void loadExternal() {
159: if (externalFileModelLoaderPlugin != null) {
160: userDefinedTypeMetaModel = externalFileModelLoaderPlugin
161: .load(userDefinedTypeMetaModel);
162: }
163: }
164:
165: //
166: //
167: //
168: public ExternalFileModelLoaderPluginAdapter getExternalFileModelLoaderPlugin() {
169: return externalFileModelLoaderPlugin;
170: }
171:
172: public InternalFileModelLoaderPluginAdapter getInternalFileModelLoaderPlugin() {
173: return internalFileModelLoaderPlugin;
174: }
175:
176: public JavaToMetaType getJavaToMetaType() {
177: return javaToMetaType;
178: }
179:
180: public void setJavaToMetaType(JavaToMetaType javaToMetaType) {
181: this .javaToMetaType = javaToMetaType;
182: }
183:
184: //
185: //
186: // Auxilliary adapters
187: //
188: //
189: public static class InternalFileModelLoaderPluginAdapter extends
190: FileModelLoaderPluginImpl {
191:
192: public void configure(PropertyMap udtProperties)
193: throws IOException {
194: }
195:
196: public String getPluginKey() {
197: return PLUGIN_KEY;
198: }
199:
200: public InternalFileModelLoaderPluginAdapter(
201: String descriptorPath) {
202: super (descriptorPath, null);
203: }
204:
205: public InternalFileModelLoaderPluginAdapter(Reader reader) {
206: super (reader, null);
207: }
208:
209: protected UserDefinedTypeMetaModel toModel(
210: UserDefinedTypeMetaModel model, Map modelMap) {
211: model.fromInternalMap(modelMap);
212: return model;
213: }
214:
215: protected Map readModelMap(Reader reader) throws IOException {
216: return (Map) sdlReader.read(reader);
217: }
218: }
219:
220: public static class ExternalFileModelLoaderPluginAdapter extends
221: FileModelLoaderPluginImpl {
222:
223: public void configure(PropertyMap udtProperties)
224: throws IOException {
225: }
226:
227: public String getPluginKey() {
228: return PLUGIN_KEY;
229: }
230:
231: public ExternalFileModelLoaderPluginAdapter(
232: String descriptorPath) {
233: super (descriptorPath, null);
234: }
235:
236: public ExternalFileModelLoaderPluginAdapter(Reader reader) {
237: super (reader, null);
238: }
239:
240: protected UserDefinedTypeMetaModel toModel(
241: UserDefinedTypeMetaModel model, Map modelMap) {
242: model.fromExternalMap(modelMap);
243: return model;
244: }
245:
246: protected Map readModelMap(Reader reader) throws IOException {
247: return (Map) sdlReader.read(reader);
248: }
249: }
250: }
|