001: package simpleorm.quickstart;
002:
003: import java.io.File;
004: import java.lang.reflect.Constructor;
005: import java.lang.reflect.InvocationTargetException;
006: import java.sql.Connection;
007: import java.sql.DriverManager;
008: import java.sql.SQLException;
009: import java.util.Hashtable;
010: import java.util.Iterator;
011:
012: import org.apache.commons.sql.io.JdbcModelReader;
013: import org.apache.commons.sql.model.Database;
014: import org.apache.commons.sql.model.Table;
015:
016: //import clientMatterTask.new_db.br.ClientMatterTaskFormatter;
017:
018: /**
019: * This class generates the SimpleORM mapping files for a (in this example)
020: * Sybase schema.
021: *
022: * Designed to run as an ANT task with the following system paramters:
023: * <ul>
024: * <li>database.url=jdbc:interbase://localhost/C:/Source/Java/ORM/SimpleORM/simpleorm/temp/test.gdb
025: * <li>database.username=sysdba
026: * <li>database.password=masterkey
027: * <li>database.driver=interbase.interclient.Driver
028: * <li>database.schema=
029: * <li>database.catelog=
030: * <li>quickstart.file=.
031: * <li>quickstart.packagename=simpleorm.dbtest
032: * <li>quickstart.getters_and_setters=true
033: * <li>quickstart.use_one_package=true
034: * <li>quickstart.INiceNameFormatter=simpleorm.quickstart.DefaultFormatter
035: *<ul>
036: *
037: * To run.
038: * <ol>
039: * <li>Insure that log4j.jar is in your classpath, along with your JDBC driver.
040: * <li>Set the system properties.
041: * <li>Create an instance of SimpleORMGenerate
042: * <li>Call method execute()
043: * </ol>
044: *
045: * @author <a href="mailto:richard.schmidt@inform6.com">Richard Schmidt</a> Original version Joe McDaniel.
046: * @version $Id: $
047: */
048: public class SimpleORMGenerator {
049:
050: private String dbDriver, dbUrl, dbUser, dbPassword, dbCatolog,
051: dbSchema;
052: private File rootDir;
053: private String packageName;
054: String classNameForFormatter;
055: private INiceNameFormatter niceName;
056:
057: private void loadFromSystemPreferences() {
058:
059: dbDriver = System.getProperty("database.driver");
060: dbUrl = System.getProperty("database.url");
061: dbUser = System.getProperty("database.user");
062: dbPassword = System.getProperty("database.password");
063: dbSchema = System.getProperty("database.schema", "");
064: dbCatolog = System.getProperty("database.catelog", "");
065: rootDir = new File(System.getProperty("quickstart.file", "."));
066: packageName = System.getProperty("quickstart.packagename");
067: classNameForFormatter = System.getProperty(
068: "quickstart.INiceNameFormatter",
069: "simpleorm.quickstart.DefaultFormatter");
070: }
071:
072: /**Use the default nice name formatter*/
073: public SimpleORMGenerator() throws Exception {
074:
075: }
076:
077: public void execute() throws Exception {
078:
079: loadFromSystemPreferences();
080: System.out.println("SimpleOrm code generator starting\n");
081: System.out.println("Your DB settings are:");
082: System.out.println("driver : " + dbDriver);
083: System.out.println("URL : " + dbUrl);
084: System.out.println("user : " + dbUser);
085: System.out.println("password : " + dbPassword);
086: System.out.println("schema : " + dbSchema);
087: System.out.println("catelog : " + dbCatolog);
088: System.out.println();
089: System.out.println("Other settings are");
090: System.out.println("directory : "
091: + rootDir.getAbsolutePath());
092: System.out.println("package name : " + packageName);
093: System.out.println("INiceName : " + classNameForFormatter);
094: System.out.println();
095:
096: //Load up the INiceName
097:
098: try {
099: niceName = (INiceNameFormatter) Class.forName(
100: classNameForFormatter).getConstructor(null)
101: .newInstance(null);
102:
103: } catch (Exception e) {
104: System.err
105: .println("Could not load INiceNameFormatter, will use DefaultFormatter instead");
106: e.printStackTrace();
107: niceName = new DefaultFormatter();
108: }
109: //Add on the package directory
110: rootDir = new File(rootDir.getAbsolutePath() + "\\"
111: + packageName.replace('.', '\\') + "\\");
112:
113: if (!rootDir.exists()) {
114: rootDir.mkdirs();
115: }
116:
117: // Load the database Driver.
118: Class.forName(dbDriver);
119:
120: // Attemtp to connect to a database.
121: Connection con = DriverManager.getConnection(dbUrl, dbUser,
122: dbPassword);
123: JdbcModelReader reader = new JdbcModelReader(con);
124: reader.setCatalog(dbCatolog);
125: reader.setSchema(dbSchema);
126: //Get the metta data from the database
127: Database db = reader.getDatabase();
128:
129: //Itterate trhough the tables, generating the files.
130: Iterator tablesEnum = db.getTables().iterator();
131: while (tablesEnum.hasNext()) {
132: try {
133:
134: TableGenerate table = new TableGenerate(db,
135: (Table) tablesEnum.next(), niceName,
136: packageName, rootDir);
137:
138: table.generateBaseClass();
139: table.generateBRClass();
140: System.out.println("Generated table "
141: + table.table.getName());
142: } catch (Exception e) {
143: e.printStackTrace();
144: }
145: }
146:
147: System.out.println("done");
148: }
149:
150: public static void main(String[] args) throws Exception {
151:
152: System.setProperty("database.driver",
153: "interbase.interclient.Driver");
154: System
155: .setProperty(
156: "database.url",
157: "jdbc:interbase://localhost/C:/Source/Java/ORM/SimpleORM/simpleorm/temp/test.gdb");
158:
159: System.setProperty("database.user", "sysdba");
160: System.setProperty("database.password", "masterkey");
161: System.setProperty("database.schema", "");
162: System.setProperty("database.catelog", "");
163: System.setProperty("quickstart.file", ".");
164: System
165: .setProperty("quickstart.packagename",
166: "simpleorm.new_db");
167: System.setProperty("quickstart.INiceNameFormatter",
168: "simpleorm.quickstart.DefaultFormatter");
169:
170: System
171: .setProperty("database.url",
172: "jdbc:interbase://localhost/C:/Source/Ostrich/Database/ostrich2.gdb");
173: // System.setProperty("quickstart.INiceNameFormatter","simpleorm.quickstart.SimpleORMNiceNameFormatter");
174: System.setProperty("quickstart.INiceNameFormatter",
175: "foursee.ots.orm.tables.OtsFormatter");
176: System.setProperty("quickstart.packagename",
177: "foursee.ots.simpleorm");
178:
179: SimpleORMGenerator instance = new SimpleORMGenerator();
180:
181: instance.execute();
182: }
183: }
|