📄 genericfactorygenerator.java
字号:
InlineFieldWriter iw = new InlineFieldWriter (jcf, currentClass) { public void writeOneField (FileWriter outFile, JField subField, String sqlName, String preName, boolean isFirst) throws IOException, SchemaException { String subVar = this.jcf.getJavaName (); outFile.write (writeOutAField (subField, subField.getSqlType (), subVar + "_" + subField.getJavaName (), subVar)); } public void writeOnce (FileWriter outFile, JClass fieldClass) throws IOException { String subVar = this.jcf.getJavaName (); outFile.write ("\t\t" + fieldClass.getClassName () + " " + subVar + " = new " + fieldClass.getClassName () + " ();\n"); outFile.write ("\t\tfoo.set" + this.jcf.getGetSet () + " (" + subVar + ");\n"); } }; iw.execute (outFile, false); /* JClass fieldClass = jcf.getJClass (); Vector fields = new Vector (fieldClass.getAllFields ().values ()); String subPrimaryKey = fieldClass.getPrimaryKey (); String prefix = jcf.getPrefix (); /* for (int j = 0; j < fields.size (); j++) { JField subField = (JField)fields.elementAt (j); String sqlName = subField.getSqlName (); if (sqlName.equals (subPrimaryKey)) { // do nothing .. } else if (subField instanceof JCompositeField) { throw new SchemaException ("ephman.abra.tools.nocompinline", currentClass.getSchemaFileName ()); } else { outFile.write (writeOutAField (subField, subField.getSqlType (), subVar + "_" + subField.getJavaName (), subVar)); } } */ } else { String get_set = jf.getGetSet () + "Oid"; outFile.write ("\t\tfoo.set" + get_set + " (rs.getInt" + " (" + jf.getJavaName ()+ "Oid));\n"); } } else if (jf.isDbColumn ()) { writeOutAField (outFile, jf); } } if (currentClass.hasDescendant ()) outFile.write ("\t\tfoo.makeTransient ();\n"); outFile.write ("\t}\n\n"); } protected void writeOutAField (FileWriter outFile, JField jf) throws IOException, SchemaException { outFile.write (writeOutAField (jf)); } protected String writeOutAField (JField jf) throws IOException, SchemaException { return writeOutAField (jf, jf.getSqlType (), jf.getJavaName ()); } protected String writeOutAField (JField jf, String fieldType, String colName) throws IOException, SchemaException { return writeOutAField (jf, fieldType, colName, "foo"); } protected String writeOutAField (JField jf, String fieldType, String colName, String varName) throws IOException, SchemaException { String stype = findSetType (jf); return writeOutAField (jf, fieldType, colName, varName, stype); } protected String writeOutAField (JField jf, String fieldType, String colName, String varName, String stype) throws IOException, SchemaException { String result = ""; if (jf.isDate ()) { result +="\t\t" + varName + ".set" + jf.getGetSet () + " (rs.getTimestamp" + " (" + colName+ "));\n"; } else if (jf.getObjectType().equals ("boolean")) { // boolean hack result += "\t\tString b" + jf.getGetSet () + " = rs.getString" + " (" + colName+ ");\n"; result += "\t\tif (b" + jf.getGetSet () + "!= null)\n"; result +="\t\t\t" + varName + ".set" + jf.getGetSet () + " (b" +jf.getGetSet () + ".equals (\"T\"));\n"; } else if (isClob (jf)) { // clob hack String clVar = jf.getJavaName () + "_CL"; result +="\t\tClob " + clVar + " = rs.getClob (" + colName + ");\n"; result +="\t\t" + varName + ".set" + jf.getGetSet () + " (" + clVar + ".getSubString (1, (int)" + clVar + ".length ()));\n"; } else if (jf.getObjectType().startsWith ("java.math")) { String cast_this = "(" + jf.getObjectType() + ")"; result +="\t\t" + varName + ".set" + jf.getGetSet () + " ("+ cast_this +"rs.getObject" + " (" + colName + "));\n"; } else { // not date/bool/or clob.. if (stype != null) { String cast_this = ""; String endBool = ""; if (stype.equals ("Object")) { cast_this = "new "+jf.getObjectType ()+" ("; endBool = ")"; NativeObjectMapping nom = (NativeObjectMapping)nativeSetTypes.get (jf.getObjectType ()); stype = nom.setName; } if (jf.getObjectType ().equals ("Boolean")) { //Boolean object.. result += "\t\tString t_" + jf.getGetSet () + " = rs.getString (" + colName + ");\n"; result += "\t\t" + varName + ".set" + jf.getGetSet () + "(getStringAsBoolean (t_" + jf.getGetSet () + "));\n"; } else result +="\t\t" + varName + ".set" + jf.getGetSet () + " ("+ cast_this +"rs.get" + stype + " (" + colName + ")"+endBool+");\n"; } else if (!colName.equals (jf.getJavaName ())) // is some weird thing try String result +="\t\t" + varName + ".set" + jf.getGetSet () + " (rs.getString" + " (" + colName + "));\n"; else result +="\t\t// don't know what to do for " + jf.getJavaName () + "\n"; } return result; } protected int writeInsertMethod (FileWriter factoryOutFile, JClass currentClass, Vector allFields) throws IOException, SchemaException { factoryOutFile.write ("\n/** this method returns the insert string for the db */\n\n"); factoryOutFile.write ("\tprotected String makeInsertString () {\n"); factoryOutFile.write ("\t\treturn insertString;\n\t}\n"); factoryOutFile.write ("\tprotected static final String insertString = "); factoryOutFile.write ("\"insert into " + currentClass.getTableName () + " (\" +\n\t\t"); int key_loc = -1; int vector_count = 0; boolean isFirst = true; String valuesString = ""; final String outChar = "?"; if (currentClass.isVersioned ()) { isFirst= false; factoryOutFile.write ("\"" + VERSION_NUMBER); valuesString += ("\t\t\" " + outChar); } for (int i = 0; i < allFields.size(); i++) { JField jf = (JField)allFields.elementAt (i); if (!jf.isDbColumn ()) vector_count++; else if (jf.isInline ()) { JCompositeField jcf = (JCompositeField)jf; JClass fieldClass = jcf.getJClass (); Vector fields = new Vector (fieldClass.getAllFields ().values ()); String subPrimaryKey = fieldClass.getPrimaryKey (); String prefix = jcf.getPrefix (); for (int j = 0; j < fields.size (); j++) { JField subField = (JField)fields.elementAt (j); String sqlName = subField.getSqlName (); if (!subField.isDbColumn() || sqlName.equals (subPrimaryKey)) { // do nothing .. } else if (subField instanceof JCompositeField) { throw new SchemaException ("ephman.abra.tools.nocompinline", currentClass.getSchemaFileName ()); } else { if (!isFirst) { factoryOutFile.write (",\" +\n\t\t"); valuesString += ",\" +\n"; } else isFirst = false; factoryOutFile.write ("\"" + prefix + "_" + sqlName); valuesString += ("\t\t\"" + outChar); } } } else { if (jf.getSqlName ().equals (currentClass.getPrimaryKey ())) { // is primary key key_loc = i - vector_count; if (setPKInInsertStmt ()) { // some dbs support auto-inc if (isFirst) isFirst = false; else valuesString += ",\" + \n"; valuesString += "\t\t\"" + getPrimaryKeyString (currentClass); if (!isFirst) factoryOutFile.write (",\" +\n\t\t\"" + jf.getSqlName ()); else factoryOutFile.write ("\"" + jf.getSqlName ()); } } else { // is not primary key.. if (!isFirst) factoryOutFile.write (",\" +\n\t\t\"" + jf.getSqlName ()); else factoryOutFile.write ("\"" + jf.getSqlName ()); if (!isFirst) { valuesString += (",\" +\n\t\t\" "); if (isClob (jf)) valuesString += (createClobString ()); else valuesString += (outChar); } else { // first output isFirst = false; valuesString += ("\t\t\" "); if (isClob (jf)) valuesString += (createClobString ()); else valuesString += (outChar); } } } } factoryOutFile.write (") values (\" +\n"); factoryOutFile.write (valuesString); factoryOutFile.write (")\";\n"); return key_loc; } // override if your db does something different.. protected String createClobString () { return "EMPTY_CLOB ()"; } /** override to false if auto increment is turned on.. * */ protected boolean setPKInInsertStmt () { return true;} protected void writeFactoryHeader (FileWriter factoryOutFile, JClass currentClass) throws IOException, SchemaException { String facImports[] = new String []{"java.sql.*", "org.ephman.abra.utils.*"}; factoryOutFile.write ("package " + currentClass.getPackageName () + ";\n\n"); for (int i = 0; i < facImports.length; i++) { factoryOutFile.write ("import " + facImports[i] + ";\n"); } if (!currentClass.getPackageName ().equals ("org.ephman.abra.database")) factoryOutFile.write ("import org.ephman.abra.database.*;\n"); factoryOutFile.write ("\n// Do not edit!! generated classes \n"); if (!currentClass.isManyToMany()) { factoryOutFile.write ("/** a factory to link the class " + currentClass.getClassName () + "\n"); factoryOutFile.write (" * to it's sql code and database table\n"); } else { factoryOutFile.write ("/** \n* " + currentClass.getClassDescription() + "\n"); } factoryOutFile.write (" * @version " + (new Date ()).toString () + "\n"); factoryOutFile.write (" * @author generated by Dave Knull\n */\n\n"); String toExt = ""; if (currentClass.hasParentFactory ()) { toExt += "extends "; if (currentClass.getParentFactory ().equals ("")) if (currentClass.isManyToMany ()) toExt += "ManyToManyFactoryBase"; else if (currentClass.isEndDateable ()) toExt += "EndDateFactoryBase"; else toExt += defaultExtend; else toExt += currentClass.getParentFactory (); } factoryOutFile.write ("public abstract class Abstract" + currentClass.getClassName () + "Factory " + toExt + "{\n\n"); } private void writeFactoryTrailer (FileWriter outFile, JClass currentClass) throws IOException, SchemaException { outFile.write ("\tprotected Abstract" + currentClass.getClassName () + "Factory () "); outFile.write ("{\n"); outFile.write ("\t\tsuper ();\n"); outFile.write (constructorString); outFile.write ("\t}\n\n"); outFile.write ("\n}\n"); } // View rip and lookup routines protected void writeViewCode (FileWriter outFile, JClass currentClass, Vector allFields) throws IOException, SchemaException { Iterator i = currentClass.getViewList (); while (i.hasNext ()) { JView view = (JView)i.next (); writeViewString (view, currentClass, outFile); //writeViewRs (view, currentClass, outFile); } } protected void writeViewString (JView view, JClass currentClass, FileWriter outFile) throws IOException, SchemaException { String u_name = view.getFormatName (); u_name = u_name.substring (0,1).toUpperCase () + u_name.substring (1); outFile.write ("\tpublic String make" + u_name + "LookupString () {\n"); outFile.write ("\t\tString result = \"select \"\n"); String toSelect = ""; String tables = ""; String whereClause = ""; String resultRip = ""; Vector fieldList = view.getAllFields (); for (int index=0; index < fieldList.size (); index++) { String expectedVarName = "foo"; // in this scope b/c inline fields may change the value
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -