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.log.Log;
012: import com.completex.objective.components.log.impl.PrimitiveLogImpl;
013: import com.completex.objective.components.log.adapter.StdOutputLogAdapter;
014: import com.completex.objective.components.persistency.ColumnType;
015: import com.completex.objective.components.persistency.JavaToMetaTypeImpl;
016: import com.completex.objective.components.persistency.MetaTable;
017: import com.completex.objective.components.persistency.meta.MetaModel;
018: import com.completex.objective.components.persistency.meta.adapter.ModelLoaderAdapter;
019: import com.completex.objective.components.sdl.reader.SdlReader;
020: import com.completex.objective.components.sdl.reader.impl.SdlReaderImpl;
021: import com.completex.objective.util.cmd.CmdLine;
022: import freemarker.template.Configuration;
023: import freemarker.template.ObjectWrapper;
024: import freemarker.template.Template;
025: import freemarker.template.TemplateException;
026:
027: import java.io.*;
028: import java.sql.SQLException;
029: import java.util.*;
030:
031: /**
032: * @author Gennady Krizhevsky
033: */
034: public class PersistentFactoryGenerator {
035: protected static final Log logger = StdOutputLogAdapter
036: .newLogInstance();
037:
038: public static final String SUFFIX = "Ctl";
039:
040: protected ModelLoaderAdapter objectModelFileLoaderAdapter;
041: protected ModelLoaderAdapter ctlModelFileLoaderAdapter;
042: protected MetaModel objectModel;
043: protected MetaModel ctlModel;
044:
045: public static final List imports = new ArrayList();
046:
047: static {
048: // imports.add("com.completex.objective.components.persistency.LifeCycleController");
049: // imports.add("com.completex.objective.components.persistency.Record");
050: // imports.add("com.completex.objective.components.persistency.rule.impl.NumberSizeFieldValidator");
051: // imports.add("com.completex.objective.components.persistency.rule.impl.RecordValidatorImpl");
052: // imports.add("com.completex.objective.components.persistency.rule.impl.RecordConvertorImpl");
053: // imports.add("com.completex.objective.components.persistency.rule.impl.TrimRightFieldConvertor");
054: }
055:
056: private static final String INTERNAL_DESC_PATH = "intern_path"; // Internal descriptor file path
057: private static final String EXTERNAL_DESC_PATH = "extern_path"; // External descriptor file path
058: private static final String OUT_DIR = "out_dir"; // Top level directory for generated classes
059: private static final String PACKAGE = "package"; // Package
060: private static final String TEMPLATE_PATH = "template_path"; // Package
061: private static final String OBJECT_FILTER_PATTERN = "object_filter_pattern";
062: private static final String CTL_FILTER_PATTERN = "ctl_filter_pattern";
063: private static final String CLASS_NAME = "class_name";
064:
065: String labels[] = { INTERNAL_DESC_PATH, EXTERNAL_DESC_PATH,
066: OUT_DIR, PACKAGE, TEMPLATE_PATH, OBJECT_FILTER_PATTERN,
067: CTL_FILTER_PATTERN, CLASS_NAME, };
068:
069: private static final String DEBUG = "-debug";
070: String flags[] = { DEBUG, };
071:
072: private SdlReader reader = new SdlReaderImpl();
073:
074: private Writer writer;
075:
076: private static LineStruct lineStruct = new LineStruct();
077:
078: private DependencyEntity rootEntity = new DependencyEntity("root",
079: "root", 0);
080: private boolean debug = false;
081:
082: public PersistentFactoryGenerator(String propertiesPath)
083: throws Exception {
084: initialize(propertiesPath);
085: new PersistentObjectGenerator();
086: setUp();
087: process();
088: }
089:
090: private void initialize(String propertiesPath) throws Exception {
091: Properties properties = new Properties();
092: properties.load(new FileInputStream(propertiesPath));
093: logger.debug("INPUT: " + properties);
094: String[] args = CmdLine.properties2args(properties, labels,
095: flags);
096: CmdLine cmdLine = new CmdLine(this .getClass().getName(), args,
097: labels, flags, null);
098:
099: lineStruct.inFilePath = cmdLine.getParam(INTERNAL_DESC_PATH,
100: true);
101: lineStruct.exFilePath = cmdLine.getParam(EXTERNAL_DESC_PATH,
102: true);
103: lineStruct.outputPath = cmdLine.getParam(OUT_DIR, true);
104: lineStruct.packageName = cmdLine.getParam(PACKAGE, true);
105: lineStruct.templatePath = cmdLine.getParam(TEMPLATE_PATH, true);
106: lineStruct.objectFilterPattern = cmdLine
107: .getParam(OBJECT_FILTER_PATTERN);
108: lineStruct.ctlFilterPattern = cmdLine
109: .getParam(CTL_FILTER_PATTERN);
110: lineStruct.className = cmdLine.getParam(CLASS_NAME);
111:
112: debug = cmdLine.getFlag(DEBUG);
113:
114: if (logger instanceof PrimitiveLogImpl) {
115: ((PrimitiveLogImpl) logger).setDebugEnabled(debug);
116: }
117:
118: }
119:
120: public void setUp() throws Exception, SQLException {
121:
122: Reader internalFileReader = new BufferedReader(new FileReader(
123: lineStruct.inFilePath));
124: Reader externalFileReader = new BufferedReader(new FileReader(
125: lineStruct.exFilePath));
126: JavaToMetaTypeImpl javaToMetaType = new JavaToMetaTypeImpl();
127: objectModelFileLoaderAdapter = new ModelLoaderAdapter(
128: new ModelLoaderAdapter.InternalFileModelLoaderAdapter(
129: internalFileReader,
130: lineStruct.objectFilterPattern),
131: new ModelLoaderAdapter.ExternalFileModelLoaderAdapter(
132: externalFileReader,
133: lineStruct.objectFilterPattern),
134: javaToMetaType, logger);
135: objectModel = objectModelFileLoaderAdapter.load(null);
136: internalFileReader.close();
137: externalFileReader.close();
138:
139: internalFileReader = new BufferedReader(new FileReader(
140: lineStruct.inFilePath));
141: externalFileReader = new BufferedReader(new FileReader(
142: lineStruct.exFilePath));
143: ctlModelFileLoaderAdapter = new ModelLoaderAdapter(
144: new ModelLoaderAdapter.InternalFileModelLoaderAdapter(
145: internalFileReader, lineStruct.ctlFilterPattern),
146: new ModelLoaderAdapter.ExternalFileModelLoaderAdapter(
147: externalFileReader, lineStruct.ctlFilterPattern),
148: javaToMetaType, logger);
149: ctlModel = ctlModelFileLoaderAdapter.load(null);
150: internalFileReader.close();
151: externalFileReader.close();
152: }
153:
154: private void process() throws IOException, TemplateException {
155: generateTemplate();
156: }
157:
158: private void generateTemplate() throws IOException,
159: TemplateException {
160: Date timestamp = new Date();
161: String outputDir = FileUtil.makePath(lineStruct.packageName,
162: lineStruct.outputPath);
163:
164: Configuration cfg = Configuration.getDefaultConfiguration();
165: cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
166:
167: File templateFile = new File(lineStruct.templatePath);
168: if (!templateFile.exists()) {
169: throw new RuntimeException("templateFile ["
170: + lineStruct.templatePath + "] does not exist");
171: }
172: cfg
173: .setDirectoryForTemplateLoading(templateFile
174: .getParentFile());
175: String fileName = templateFile.getName();
176: Template template = cfg.getTemplate(templateFile.getName());
177:
178: Map templateModel = new LinkedHashMap();
179: templateModel.put("package", lineStruct.packageName);
180: templateModel.put("timestamp", timestamp);
181: templateModel.put("imports", imports);
182: templateModel.put("className", lineStruct.className);
183: ArrayList tables = new ArrayList();
184: templateModel.put("tables", tables);
185: for (Iterator it = objectModel.tableIterator(); it.hasNext();) {
186: Map.Entry entry = (Map.Entry) it.next();
187: System.out.println("ENTRY " + entry);
188: System.out.println("ENTRY CLASS "
189: + entry.getClass().getName());
190: MetaTable table = (MetaTable) entry.getValue();
191:
192: boolean mainPattern = table.getTableAlias()
193: .lastIndexOf("_") > 0;
194: String className = PersistentObjectGenerator.javaName(table
195: .getTableAlias(), null, null, true);
196: String classVar = PersistentObjectGenerator
197: .lowerFirstChar(className);
198: String ctlClassName = PersistentObjectGenerator.javaName(
199: table.getTableAlias(), null, SUFFIX, true);
200: String ctlClassVar = PersistentObjectGenerator
201: .lowerFirstChar(ctlClassName);
202: Map tableMap = new LinkedHashMap();
203: tables.add(tableMap);
204:
205: tableMap.put("className", className);
206: tableMap.put("classVar", classVar);
207: tableMap.put("ctlClassName", ctlClassName);
208: tableMap.put("ctlClassVar", ctlClassVar);
209: tableMap.put("tableName", table.getTableName());
210: tableMap.put("tableAlias", table.getTableAlias());
211: tableMap.put("asize", "" + table.size());
212: tableMap.put("keySize", "" + table.keySize());
213: if (ctlModel.getTable(table.getTableName()) != null) {
214: tableMap.put("hasCtl", "a");
215: } else {
216: System.err.println("Table " + table.getTableName()
217: + " will be created w/o/ Ctl");
218: tableMap.put("hasCtl", "b");
219: }
220:
221: logger.debug("outputDir = " + outputDir + "; table = "
222: + table.getTableAlias());
223:
224: }
225: Writer writer = new FileWriter(new File(outputDir,
226: lineStruct.className + ".java"));
227: template.process(templateModel, writer);
228: writer.flush();
229: writer.close();
230: }
231:
232: public static String javaType(ColumnType type) {
233: String javaType = (String) PersistentObjectGenerator
234: .getType2JavaMap().get(type);
235: // assert javaType != null : "Cannot TYPE_2_JAVA for type " + type;
236:
237: // System.out.println("javaType for type " + type + " is " + javaType);
238:
239: return javaType;
240: }
241:
242: //
243: //
244: // Util classes
245: //
246: //
247: static class LineStruct {
248: String inFilePath;
249: String exFilePath;
250: String outputPath;
251: String packageName;
252: String templatePath;
253: String objectFilterPattern;
254: String ctlFilterPattern;
255: String className;
256: }
257:
258: /**
259: *
260: * @param args
261: * @throws Exception
262: */
263: public static void main(String[] args) throws Exception {
264: System.out.println("STARTED GENERATION ...");
265:
266: new PersistentFactoryGenerator(args[0]);
267:
268: System.out.println("FINISHED GENERATION");
269: }
270:
271: }
|