001: package simpleorm.quickstart;
002:
003: import org.apache.commons.sql.model.Column;
004: import org.apache.commons.sql.model.Database;
005: import org.apache.commons.sql.model.ForeignKey;
006: import org.apache.commons.sql.model.Reference;
007: import org.apache.commons.sql.model.Table;
008:
009: import java.io.File;
010: import java.io.FileWriter;
011: import java.io.IOException;
012: import java.io.Writer;
013:
014: import java.util.Date;
015: import java.util.HashMap;
016: import java.util.Iterator;
017: import java.util.Map;
018: import java.util.Vector;
019:
020: /**
021: * @author <a href="mailto:richard.schmidt@inform6.com">Richard Schmidt</a>
022: *
023: */
024: final class TableGenerate {
025: Table table;
026: INiceNameFormatter niceName;
027: Database db;
028: File root;
029:
030: /**Name of the classes and packages generated*/
031: String classNameBase;
032: String classNameBR;
033: String packageName;
034:
035: /**map of ColumnGenerate objects Keyed by ColumnName*/
036: Map hmColumns = new HashMap();
037:
038: /**Store the ColumnGenerate objects*/
039: Vector vtrColumns = new Vector();
040:
041: /**Store the primary ColumnGenerate here*/
042: Vector vtrPrimaryColumns = new Vector();
043:
044: /**Store the ForeignKeygenerate objects here*/
045: Vector vtrForeignKeys = new Vector();
046:
047: public TableGenerate(Database db, Table table,
048: INiceNameFormatter niceName, String packageName, File root) {
049: this .table = table;
050: this .niceName = niceName;
051: this .db = db;
052: this .root = root;
053: classNameBase = niceName.niceNameForTable(table.getName())
054: + "_gen";
055: classNameBR = niceName.niceNameForTable(table.getName());
056: this .packageName = packageName;
057:
058: loadColumns();
059: loadForeignKeys();
060: }
061:
062: /** put all the columns into the hash map*/
063: public void loadColumns() {
064: //Load up the fields
065: Iterator columnsEnum = table.getColumns().iterator();
066:
067: while (columnsEnum.hasNext()) {
068: ColumnGenerate column = new ColumnGenerate(table,
069: (Column) columnsEnum.next(), niceName);
070: hmColumns.put(column.column.getName(), column);
071: vtrColumns.add(column);
072:
073: if (column.isPrimary()) {
074: vtrPrimaryColumns.add(column);
075: }
076: }
077: }
078:
079: /**Add the foreign keys to the hash map*/
080: public void loadForeignKeys() {
081: //Now process the foreign Keys
082: Iterator keysEnum = table.getForeignKeys().iterator();
083:
084: while (keysEnum.hasNext()) {
085: ForeignKey key = (ForeignKey) keysEnum.next();
086: ForeignKeyGenerate keyGen = new ForeignKeyGenerate(table,
087: key, niceName, hmColumns);
088:
089: //Now add the referenceKey
090: vtrForeignKeys.add(keyGen);
091: }
092: }
093:
094: /**
095: * Generates the base class file.
096: * File will always be generated
097: */
098: public void generateBaseClass() throws IOException {
099: FileWriter out = new FileWriter(new File(root, classNameBase
100: + ".java"));
101:
102: //Output all the header stuff
103: out.write("package " + packageName + ";\n");
104: out.write("import simpleorm.core.*;\n");
105: out.write("import simpleorm.properties.SPropertyValue;\n");
106: out.write("import java.math.BigDecimal;\n");
107: out.write("import java.util.Date;\n");
108: out
109: .write("\n/**Base class of table "
110: + table.getName()
111: + ".<br>\n"
112: + "*Do not edit as will be regenerated by running SimpleORMGenerator\n"
113: + "*Generated on " + new Date().toString()
114: + "\n*/ \n\n");
115:
116: out.write("abstract class " + classNameBase
117: + " extends SRecordInstance");
118:
119: out.write(" implements java.io.Serializable {\n\n");
120: out
121: .write(" public static final SRecordMeta meta =\n new SRecordMeta("
122: + classNameBR
123: + ".class, \""
124: + table.getName()
125: + "\");\n\n");
126:
127: //Now generate the fields
128: out.write("//Columns in table\n");
129:
130: for (int i = 0; i < vtrColumns.size(); i++) {
131: ColumnGenerate field = (ColumnGenerate) vtrColumns.get(i);
132: field.outputStaticFields(out);
133: }
134:
135: //And the getters and setters
136: out.write("//Column getters and setters\n");
137:
138: for (int i = 0; i < vtrColumns.size(); i++) {
139: ColumnGenerate field = (ColumnGenerate) vtrColumns.get(i);
140: field.outputGettersAndSetters(out);
141: }
142:
143: //The foreign keys, only getters and setters
144: if (vtrForeignKeys.size() > 0) {
145: out.write("//Foreign key getters and setters\n");
146: }
147: for (int i = 0; i < vtrForeignKeys.size(); i++) {
148: ForeignKeyGenerate field = (ForeignKeyGenerate) vtrForeignKeys
149: .get(i);
150: field.outputGettersAndSetters(out);
151: }
152:
153: //the find or create methods
154: out.write("//Find and create\n");
155: genFindOrCreate(out);
156: getRefFindOrCreate(out);
157:
158: //now the end stuff
159: out.write("//specializes abstract method\n");
160: out.write(" public SRecordMeta getMeta() {\n");
161: out.write(" return meta;\n");
162: out.write(" }\n");
163: out.write("}\n");
164: out.close();
165: }
166:
167: /** Generate the Business Rules class.
168: * File will only be generated if file does not exist
169: */
170: public void generateBRClass() throws IOException {
171:
172: File brFile = new File(root, classNameBR + ".java");
173:
174: if (brFile.exists() == false) {
175: FileWriter out = new FileWriter(brFile);
176:
177: //Output all the header stuff
178: out.write("package " + packageName + ";\n");
179: out.write("import simpleorm.core.*;\n");
180: out.write("import simpleorm.properties.SPropertyValue;\n");
181: out
182: .write("\n/**Business rules class for table "
183: + table.getName()
184: + ".<br>\n"
185: + "*Will not be regenerated by SimpleORMGenerator, add any business rules "
186: + "to this class\n*/\n\n");
187: out.write("public class " + classNameBR + " extends "
188: + classNameBase);
189:
190: out.write(" implements java.io.Serializable {\n\n");
191: out.write("}\n");
192: out.close();
193: }
194: }
195:
196: /**Nice type casted FindOrCreate() for column parameters
197: public static Matter findOrCreate( String _fldClientno, String _fldMatterNo ){
198: return (Matter) meta.findOrCreate( new Object[] {new String( _fldClientno), new String( _fldMatterNo)});
199: } */
200: private void genFindOrCreate(Writer out) throws IOException {
201: Vector vtrParameters = new Vector();
202:
203: Iterator pKeys = table.getPrimaryKeyColumns().iterator();
204:
205: //find the column matching each primary keys and add its paramters to the list.
206: while (pKeys.hasNext()) {
207: Column key = (Column) pKeys.next();
208: ColumnGenerate column = (ColumnGenerate) hmColumns.get(key
209: .getName());
210: vtrParameters.add(column.getParameters());
211: }
212:
213: //Build up a string of the paramter type and variables
214: //eg "int _EmployeeID, SFieldReference _Department"
215: String params = "";
216:
217: //and a string of object params
218: //eg "new Integer( EmployeeID), _Department
219: String paramsVarables = "";
220:
221: for (int i = 0; i < vtrParameters.size(); i++) {
222: AParam param = (AParam) vtrParameters.get(i);
223: params += param.methodParam;
224: paramsVarables += param.objectParam;
225:
226: if (i < (vtrParameters.size() - 1)) {
227: params += ", ";
228: paramsVarables += ", ";
229: }
230: }
231:
232: //Now we can create the final string
233: String str = " public static " + classNameBR
234: + " findOrCreate( " + params + " ){\n"
235: + " return (" + classNameBR
236: + ") meta.findOrCreate( new Object[] {"
237: + paramsVarables + "});\n }\n";
238:
239: out.write(str);
240: }
241:
242: /**
243: * Nice type casted FindOrCreate() using reference fields
244: *
245: *
246: *
247: */
248: private void getRefFindOrCreate(Writer out) throws IOException {
249: //Look for those foreign keys that only use primary columns
250: for (int i = 0; i < vtrForeignKeys.size(); i++) {
251: ForeignKeyGenerate key = (ForeignKeyGenerate) vtrForeignKeys
252: .elementAt(i);
253:
254: if (key.isPrimary()) {
255: key.outputFindOrCreate(out);
256: }
257: }
258: }
259: }
|