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.adapter.StdOutputLogAdapter;
013: import com.completex.objective.components.persistency.JavaToMetaTypeImpl;
014: import com.completex.objective.components.persistency.MetaTable;
015: import com.completex.objective.components.persistency.Record;
016: import com.completex.objective.components.persistency.core.impl.SqlLoaderPersistencyImpl;
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:
023: import java.io.*;
024: import java.sql.SQLException;
025: import java.util.Iterator;
026: import java.util.Map;
027: import java.util.Properties;
028:
029: /**
030: * @author Gennady Krizhevsky
031: */
032: public class SqlLoaderCtlGenerator {
033:
034: protected static final Log logger = StdOutputLogAdapter
035: .newLogInstance();
036:
037: protected ModelLoaderAdapter modelFileLoaderAdapter;
038: protected MetaModel model;
039: private SdlReader reader = new SdlReaderImpl();
040: private boolean debug = false;
041:
042: private static PersistentObjectGenerator.LineStruct lineStruct = new PersistentObjectGenerator.LineStruct();
043:
044: private static final String INTERNAL_DESC_PATH = "intern_path"; // Internal descriptor file path
045: private static final String EXTERNAL_DESC_PATH = "extern_path"; // External descriptor file path
046: private static final String OUT_DIR = "out_dir"; // Top level directory for generated classes
047: private static final String FILTER_PATTERN = "filter_pattern";
048:
049: String labels[] = { INTERNAL_DESC_PATH, EXTERNAL_DESC_PATH,
050: OUT_DIR, FILTER_PATTERN, };
051:
052: private static final String DEBUG = "-debug";
053: String flags[] = { DEBUG, };
054:
055: public SqlLoaderCtlGenerator(String propertiesPath)
056: throws Exception {
057: initialize(propertiesPath);
058: setUp();
059: process();
060: }
061:
062: private void process() throws FileNotFoundException {
063:
064: for (Iterator it = model.tableIterator(); it.hasNext();) {
065: Map.Entry entry = (Map.Entry) it.next();
066: MetaTable table = (MetaTable) entry.getValue();
067: String dataFileName = table.getTableAlias() + "-data.txt";
068: String ctlFileName = table.getTableAlias() + "-ctl.txt";
069: File outDir = new File(lineStruct.outputPath);
070: boolean rc = outDir.mkdirs();
071: PrintWriter writer = new PrintWriter(new FileOutputStream(
072: lineStruct.outputPath + "/" + ctlFileName));
073: Record record = new Record(table);
074: SqlLoaderPersistencyImpl
075: .createControlFile(
076: record,
077: dataFileName,
078: "|",
079: writer,
080: SqlLoaderPersistencyImpl.DEFAULT_ORACLE_DATE_FORMAT);
081: writer.close();
082: }
083:
084: }
085:
086: private void initialize(String propertiesPath) throws Exception {
087: Properties properties = new Properties();
088: properties.load(new FileInputStream(propertiesPath));
089: logger.debug("INPUT: " + properties);
090: String[] args = CmdLine.properties2args(properties, labels,
091: flags);
092: CmdLine cmdLine = new CmdLine(this .getClass().getName(), args,
093: labels, flags, null);
094:
095: lineStruct.inFilePath = cmdLine.getParam(INTERNAL_DESC_PATH,
096: true);
097: lineStruct.exFilePath = cmdLine.getParam(EXTERNAL_DESC_PATH,
098: true);
099: lineStruct.outputPath = cmdLine.getParam(OUT_DIR, true);
100: lineStruct.filterPattern = cmdLine.getParam(FILTER_PATTERN,
101: true);
102:
103: debug = cmdLine.getFlag(DEBUG);
104: System.out.println("Finished initialization ...");
105:
106: }
107:
108: public void setUp() throws Exception, SQLException {
109:
110: Reader internalFileReader = new BufferedReader(new FileReader(
111: lineStruct.inFilePath));
112: Reader externalFileReader = new BufferedReader(new FileReader(
113: lineStruct.exFilePath));
114: JavaToMetaTypeImpl javaToMetaType = new JavaToMetaTypeImpl();
115: modelFileLoaderAdapter = new ModelLoaderAdapter(
116: new ModelLoaderAdapter.InternalFileModelLoaderAdapter(
117: internalFileReader, lineStruct.filterPattern),
118: new ModelLoaderAdapter.ExternalFileModelLoaderAdapter(
119: externalFileReader, lineStruct.filterPattern),
120: javaToMetaType, logger);
121: model = modelFileLoaderAdapter.load(null);
122: System.out.println("Finished setup ...");
123: }
124:
125: //
126: //
127: // Util classes
128: //
129: //
130: static class LineStruct {
131: String inFilePath;
132: String exFilePath;
133: String outputPath;
134: String filterPattern;
135: }
136:
137: /**
138: * config/test/sql-loader-ctl.properties
139: * @param args
140: * @throws Exception
141: */
142: public static void main(String[] args) throws Exception {
143: SqlLoaderCtlGenerator generator = new SqlLoaderCtlGenerator(
144: args[0]);
145: }
146: }
|