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.tools.generators;
010:
011: import com.completex.objective.components.persistency.BasicManagedQueryFactory;
012: import com.completex.objective.components.persistency.CallParameter;
013: import com.completex.objective.components.persistency.JavaToMetaType;
014: import com.completex.objective.components.persistency.Parameter;
015: import com.completex.objective.components.persistency.core.impl.query.SqlManagedQueryFactoryImpl;
016: import com.completex.objective.components.persistency.meta.ModelLoader;
017: import com.completex.objective.components.persistency.meta.impl.QueryModelLoaderImpl;
018: import com.completex.objective.util.PropertyMap;
019:
020: import java.io.FileReader;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.sql.SQLException;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.LinkedHashMap;
027: import java.util.List;
028: import java.util.Map;
029:
030: /**
031: * Args[0]: query-descriptor-config.sdl
032: *
033: * @author Gennady Krizhevsky
034: */
035: public class QueryPersistentDescriptorGenerator extends
036: AbstractPersistentDescriptorGenerator {
037:
038: private BasicManagedQueryFactory[] factories;
039: private QueryModelLoaderImpl.TableNames tableNames;
040:
041: public static final String TAG_QUERY_DESC_CONFIG_FILE = "query_desc_file";
042: public static final String TAG_CLASS_NAME = "className";
043: public static final String TAG_ALIASES = "aliases";
044: public static final String TAG_SQL = "sql";
045: public static final String TAG_DEFAULT_PARAMETERS = "defaultParameters";
046: public static final String TAG_IS_CALL = "isCall";
047:
048: public static final String DEFAULT_INTERNAL_DESC_FILE_NAME = "query_internal_desc_file.sdl";
049: public static final String DEFAULT_EXTERNAL_DESC_FILE_NAME = "query_external_desc_file.sdl";
050:
051: public QueryPersistentDescriptorGenerator() {
052: }
053:
054: public QueryPersistentDescriptorGenerator(String propertiesPath)
055: throws IOException, IllegalAccessException,
056: ClassNotFoundException, InstantiationException,
057: SQLException {
058: setup(propertiesPath);
059: }
060:
061: public QueryPersistentDescriptorGenerator(PropertyMap propertyMap)
062: throws IOException, IllegalAccessException,
063: ClassNotFoundException, InstantiationException,
064: SQLException {
065: setup(propertyMap);
066: }
067:
068: public QueryPersistentDescriptorGenerator(InputStream inputStream)
069: throws IOException, IllegalAccessException,
070: InstantiationException, ClassNotFoundException,
071: SQLException {
072: setup(inputStream);
073: }
074:
075: protected void beforeInitModelLoader(PropertyMap propertyMap)
076: throws IOException {
077: instantiateFactories(propertyMap);
078: instantiateTableNames(propertyMap);
079: }
080:
081: protected void extractExternalFileName(PropertyMap propertyMap) {
082: externalFileName = propertyMap
083: .getProperty(TAG_EXTERNAL_DESC_FILE,
084: DEFAULT_EXTERNAL_DESC_FILE_NAME);
085: }
086:
087: protected void extractInternalFileName(PropertyMap propertyMap) {
088: internalFileName = propertyMap
089: .getProperty(TAG_INTERNAL_DESC_FILE,
090: DEFAULT_INTERNAL_DESC_FILE_NAME);
091: }
092:
093: protected ModelLoader getDatabaseModelLoaderAdapter(
094: String filterPattern, JavaToMetaType javaToMetaType,
095: String schema, String catalog) {
096: return new QueryModelLoaderImpl(persistency, factories,
097: tableNames, filterPattern, javaToMetaType, logger);
098: }
099:
100: /**
101: // TEST_MVASJA = {
102: // aliases = [vasja, vasja1, ...]
103: // className = NULL
104: // sql = "select 'vasja' from dual where 1=?"
105: // defaultParameters = [ 1 ]
106: // }
107: * @param propertyMap
108: * @throws IOException
109: */
110: private void instantiateTableNames(PropertyMap propertyMap)
111: throws IOException {
112: if (tableNames == null) {
113: PropertyMap properties = extractQueryConfig(propertyMap);
114: LinkedHashMap aliases = new LinkedHashMap();
115:
116: for (Iterator it = properties.keySet().iterator(); it
117: .hasNext();) {
118: String tableName = (String) it.next();
119: Map configMap = (Map) properties.get(tableName);
120: List aliasesForTable = (List) configMap
121: .get(TAG_ALIASES);
122: if (aliasesForTable != null) {
123: aliases.put(tableName, aliasesForTable);
124: }
125: }
126: tableNames = new QueryModelLoaderImpl.TableNames(aliases);
127: }
128: }
129:
130: /**
131: // TEST_MVASJA = {
132: // className = NULL
133: // sql = "select 'vasja' from dual where 1=?"
134: // defaultParameters = [ 1 ]
135: // }
136: * @param propertyMap
137: * @throws IOException
138: */
139: private void instantiateFactories(PropertyMap propertyMap)
140: throws IOException {
141: if (factories == null) {
142: ArrayList factoryList = instantiateFactoryList(propertyMap);
143: factories = (BasicManagedQueryFactory[]) factoryList
144: .toArray(new BasicManagedQueryFactory[factoryList
145: .size()]);
146: }
147: }
148:
149: private ArrayList instantiateFactoryList(PropertyMap propertyMap)
150: throws IOException {
151: ArrayList factoryList = new ArrayList();
152: PropertyMap properties = extractQueryConfig(propertyMap);
153:
154: for (Iterator it = properties.keySet().iterator(); it.hasNext();) {
155: String queryName = (String) it.next();
156: PropertyMap queryMap = properties.getPropertyMap(queryName);
157: String className = queryMap.getProperty(TAG_CLASS_NAME);
158: BasicManagedQueryFactory factory;
159: if (className == null) {
160: factory = factoryFromSql(queryName, queryMap);
161: } else {
162: factory = factoryFromClassName(className, factoryList);
163: }
164: boolean isCall = BasicManagedQueryFactory.SupportedDelegates
165: .isCall(factory.getDelegateType());
166: Parameter[] parameters = extractDefaultParameters(queryMap,
167: isCall);
168: factory.setDefaultParameters(parameters);
169: factoryList.add(factory);
170: }
171: return factoryList;
172: }
173:
174: private PropertyMap extractQueryConfig(PropertyMap propertyMap)
175: throws IOException {
176: String queryConfigFileName = propertyMap
177: .getProperty(TAG_QUERY_DESC_CONFIG_FILE);
178: if (queryConfigFileName == null) {
179: throw new IllegalArgumentException("Neither factories nor "
180: + TAG_QUERY_DESC_CONFIG_FILE
181: + " are found in properties");
182: }
183: queryConfigFileName = queryConfigFileName.trim();
184: FileReader reader = new FileReader(queryConfigFileName);
185: Map map = (Map) sdlReader.read(reader);
186: return PropertyMap.toPropertyMap(map);
187: }
188:
189: private Parameter[] extractDefaultParameters(PropertyMap queryMap,
190: boolean isCall) {
191: Parameter[] parameters = null;
192: List defaultParameters = queryMap
193: .getList(TAG_DEFAULT_PARAMETERS);
194: if (defaultParameters != null) {
195: parameters = new Parameter[defaultParameters.size()];
196: for (int i = 0; i < defaultParameters.size(); i++) {
197: Map parameter = (Map) defaultParameters.get(i);
198: if (isCall) {
199: PropertyMap parameterMap = PropertyMap
200: .toPropertyMap(parameter);
201: boolean isRefCursor = parameterMap
202: .getBoolean(CallParameter.TAG_REF_CURSOR);
203: if (isRefCursor) {
204: parameters[i] = CallParameter.PARAMETER_REF_CURSOR;
205: } else {
206: parameters[i] = new CallParameter(parameterMap);
207: }
208: } else {
209: parameters[i] = new Parameter(parameter);
210: }
211: }
212: }
213: return parameters;
214: }
215:
216: private BasicManagedQueryFactory factoryFromClassName(
217: String className, ArrayList factoryList) {
218: BasicManagedQueryFactory factory;
219: factory = QueryModelLoaderImpl.instantiateFactory(className);
220: // factoryList.add(factory);
221: return factory;
222: }
223:
224: private BasicManagedQueryFactory factoryFromSql(String queryName,
225: PropertyMap queryMap) {
226: BasicManagedQueryFactory factory;
227: String sql = queryMap.getProperty(TAG_SQL);
228: if (sql == null) {
229: throw new IllegalArgumentException("Neither "
230: + TAG_CLASS_NAME + " nor " + TAG_SQL
231: + " found in config");
232: }
233: Class delegateClass;
234: if (sql.trim().startsWith("{")) {
235: delegateClass = BasicManagedQueryFactory.SupportedDelegates.CALL_FACTORY;
236: } else {
237: delegateClass = BasicManagedQueryFactory.SupportedDelegates.QUERY_FACTORY;
238: }
239: factory = new SqlManagedQueryFactoryImpl(queryName, sql,
240: delegateClass);
241: return factory;
242: }
243:
244: public BasicManagedQueryFactory[] getFactories() {
245: return factories;
246: }
247:
248: public void setFactories(BasicManagedQueryFactory[] factories) {
249: this .factories = factories;
250: }
251:
252: public QueryModelLoaderImpl.TableNames getTableNames() {
253: return tableNames;
254: }
255:
256: public void setTableNames(QueryModelLoaderImpl.TableNames tableNames) {
257: this .tableNames = tableNames;
258: }
259:
260: public static void main(String[] args) throws Exception {
261: if (args.length < 1) {
262: System.out
263: .println("ERROR: PersistentDescriptorGenerator has to have one command line parameter - <descritor.properties> ");
264: System.exit(1);
265: }
266: QueryPersistentDescriptorGenerator generator = new QueryPersistentDescriptorGenerator(
267: args[0]);
268: // MetaModel model = generator.generate();
269: generator.generate();
270: generator.dispose();
271: // System.out.println("\n=============================================================\n");
272: // System.out.println(" -- Model -- ");
273: // System.out.println("\n-------------------------------------------------------------\n");
274: // System.out.println(model);
275: }
276: }
|