📄 genericfactorygenerator.java
字号:
// but the next field should revert to 'foo' JFieldView fieldView = (JFieldView)fieldList.elementAt (index); JField jf = fieldView.getField (); if (jf.isDbColumn ()) { String elType = findSetType (jf); String colName = jf.getJavaName (); if (jf instanceof JCompositeField) { JCompositeField jcf = (JCompositeField)jf; JClass for_class = jcf.getJClass (); if (fieldView.getAsView ()) { // as view foreign table.. // TODO: figure out what to do.. if (Checks.exists (fieldView.getViewFormat ())) { JView foreignView = for_class.getView (fieldView.getViewFormat ()); // now join.. Vector viewFields = foreignView.getFieldList (); if (jf.isInline()) { // fields are in this table so we can build .. resultRip += "\t\tfoo.set" + jf.getGetSet() + " (new " + foreignView.getViewName() + " ());\n"; for (int i=0; i< viewFields.size (); i++) { JFieldView subFieldView = (JFieldView)viewFields.elementAt (i); JField for_field = subFieldView.getField (); if (!toSelect.equals ("")) toSelect += ",\"\n\t\t\t+ \""; // toSelect += jcf.getPrefix () + "_" + for_field.getSqlName (); colName = jcf.getJavaName() + "_" + for_field.getJavaName(); String subElType = findSetType (for_field); resultRip += writeOutAField (for_field, subElType, colName, "foo.get" + jcf.getGetSet () + "()", subElType); } } // else TODO: PMB !! when in seperate table.. //toSelect += jf.getJavaName () + "." + for_field.getSqlName () + " as " + jf.getJavaName (); // elType = null; // colName += "Oid"; } } else { // single field.. in another table String fieldName = fieldView.getViewFormat (); JField for_field = for_class.getFieldByName (fieldName); if (for_field == null || for_field instanceof JCompositeField) { throw new SchemaException (currentClass, jf, MessageTranslator.translate ("ephman.abra.tools.noview", new Object []{fieldName, for_class.getClassName ()})); } if (!toSelect.equals ("")) toSelect += ",\"\n\t\t\t+ \""; elType = findSetType (for_field); if (jf.isInline()) { // field is already in this table toSelect += jcf.getPrefix()+ "_" + for_field.getSqlName (); colName = jcf.getJavaName() + "_" + for_field.getJavaName(); //expectedVarName = "foo.get"+jf.getGetSet()+" ()"; //jf = for_field; } else {// do join with foreign table toSelect += jf.getJavaName () + "." + for_field.getSqlName () + " as " + jf.getJavaName (); tables += ", " + for_class.getTableName () + " " + jf.getJavaName (); if (!whereClause.equals ("")) whereClause += " and \"\n\t\t\t+ \""; whereClause += "this." + jf.getSqlName() + "=" + jf.getJavaName () + "." + for_class.getPrimaryKey (); if (!jf.isRequired ()) whereClause += "(+)"; colName = "\"" + jf.getJavaName () + "\""; } } } else { if (!toSelect.equals ("")) toSelect += ",\"\n\t\t\t+ \""; toSelect += "this." + jf.getSqlName (); } // jf simple type if (elType != null) { resultRip += writeOutAField (jf, elType, colName, expectedVarName, elType); } } } // now print out stuff.. outFile.write ("\t\t\t+ \"" + toSelect + "\"\n"); outFile.write ("\t\t\t+ \" from " + currentClass.getTableName () + " this" + tables + "\"\n"); outFile.write ("\t\t\t+ \" where " + whereClause + "\";\n"); outFile.write ("\t\treturn result;\n\t}\n\n"); // now make from Rs outFile.write ("\tpublic " + view.getViewName () + " make" + u_name + "FromResultSet (ResultSet rs) throws SQLException {\n"); outFile.write ("\t\t" + view.getViewName () + " foo = new " + view.getViewName () + " ();\n"); outFile.write (resultRip); outFile.write ("\t\treturn foo;\n\t}\n\n"); // now a wrapper outFile.write ("\tpublic ViewLookup " + view.getFormatName () + "Lookup;\n\n"); constructorString += ("\t\t" + view.getFormatName () + "Lookup = new ViewLookup (make" + u_name + "LookupString (),\n"); constructorString += ("\t\t\t\t\"make" + u_name + "FromResultSet\", " + "\n\t\t\t\tthis);\n"); } // Many to Many routines // ok protected void writeManyToManyMethods (FileWriter outFile, JClass currentClass, Vector allFields) throws IOException, SchemaException { // write many to many code try { if (allFields.size () != 2) throw new SchemaException ("ephman.abra.tools.many2manyne2", currentClass.getSchemaFileName ()); JCompositeField field1 = (JCompositeField)allFields.elementAt (0); JCompositeField field2 = (JCompositeField)allFields.elementAt (1); writeMMCode (outFile, field1, field2, currentClass.getTableName ()); } catch (Exception e) { System.out.println (e.getMessage ()); System.out.println ("Unable to generate Many to many factory for class " + currentClass.getClassName ()); } } private void writeMMCode (FileWriter outFile, JCompositeField field1, JCompositeField field2, String tableName) throws IOException, SchemaException { JClass class1 = field1.getJClass (); JClass class2 = field2.getJClass (); // insert outFile.write ("\n\tprotected String makeInsertString () {\n"); outFile.write ("\t\tString result = \"insert into " + tableName + "\" +\n"); outFile.write ("\t\t\t\"(" + field1.getSqlName () + "," + field2.getSqlName () + ")"); outFile.write ("\" +\n\t\t\t\" values (?, ?)\";\n"); outFile.write ("\t\treturn result;\n\t}\n"); // remove outFile.write ("\n\tprotected String makeDeleteString () {\n"); outFile.write ("\t\tString result = \"delete from " + tableName + " where \" +\n"); outFile.write ("\t\t\t\"" + field1.getSqlName () + "=? and " + field2.getSqlName () + "=?\";\n"); outFile.write ("\t\treturn result;\n\t}\n"); // check if relationship exists ? outFile.write ("\n\tprotected String makeQueryString () {\n"); outFile.write ("\t\tString result = \"select * from " + tableName + " where \" +\n"); outFile.write ("\t\t\t\"" + field1.getSqlName () + "=? and " + field2.getSqlName () + "=?\";\n"); outFile.write ("\t\treturn result;\n\t}\n"); /* 1 to 2*/ boolean selfRel = class1.getClassName ().equals (class2.getClassName ()); writeAtoB (outFile, field1, field2, tableName); writeDelete (outFile, field1, field2, tableName, selfRel); if (!selfRel) { writeAtoB (outFile, field2, field1, tableName); writeDelete (outFile, field2, field1, tableName, false); } } private void writeDelete (FileWriter outFile, JCompositeField field1, JCompositeField field2, String tableName, boolean selfRel) throws IOException, SchemaException { JClass class1 = field1.getJClass (); outFile.write ("\n\tprotected String make" + class1.getClassName () + "DeleteString () {\n"); outFile.write ("\t\tString result = \"delete from " + tableName + " where \" +\n"); outFile.write ("\t\t\t\"" + field1.getSqlName () + "=?"); if (selfRel) outFile.write (" or " + field2.getSqlName () + "=?"); outFile.write ("\";\n"); outFile.write ("\t\treturn result;\n\t}\n"); } private void writeAtoB (FileWriter outFile, JCompositeField field1, JCompositeField field2, String tableName) throws IOException, SchemaException { JClass class1 = field1.getJClass (); outFile.write ("\n\tprotected String make"+class1.getClassName ()+"sLookupString () {\n"); outFile.write ("\t\tString result = \"select * from " + tableName + " mm, \" +\n"); outFile.write ("\t\t\t\"" +class1.getTableName ()+" ft where \" + \n"); outFile.write ("\t\t\t\"mm."+ field1.getSqlName () + "=ft." + class1.getPrimaryKey ()); outFile.write ("\" +\n\t\t\t\" and mm." + field2.getSqlName ()+"=?"); if (class1.isEndDateable ()) outFile.write (" and ft." + END_DATE + " is null"); outFile.write ("\";\n"); outFile.write ("\t\treturn result;\n\t}\n"); } public static final String VERSION_NUMBER = "version_number"; static HashSet nativeClassSet = new HashSet (); static HashMap nativeSetTypes = new HashMap (); // this is the Abra to db type map protected HashMap typeMap = null; public HashMap getTypeMap () { if (typeMap == null) createTypeMap (); return typeMap; } protected void createTypeMap () { typeMap = new HashMap (); typeMap.put (INTEGER, "int"); typeMap.put (LONG, "int"); // no difference in the db.. typeMap.put (INTEGER_OBJ, "int"); typeMap.put (STRING, "varchar"); // needs length arg typeMap.put (DOUBLE, "double precision"); typeMap.put (DOUBLE_OBJ, "double precision"); typeMap.put (FLOAT, "float"); typeMap.put (CLOB, "clob"); typeMap.put (BLOB, "blob"); typeMap.put (TIMESTAMP, "date"); typeMap.put (CHARACTER, "char (1)"); typeMap.put (BOOLEAN, "char(1)"); typeMap.put (BOOLEAN_OBJ, "char(1)"); typeMap.put (BIG_DECIMAL, "number"); //needs length arg ie 38,3 typeMap.put (BIG_INTEGER, "number(38,0)"); } { nativeClassSet.add ("Integer"); nativeClassSet.add ("Double"); // nativeClassSet.add ("Character"); nativeClassSet.add ("Long"); nativeSetTypes.put ("Integer", new NativeObjectMapping ("Integer", "Int", "intValue ()", "INTEGER")); nativeSetTypes.put ("Long", new NativeObjectMapping ("Long", "Long", "longValue ()", "LONG")); nativeSetTypes.put ("Double", new NativeObjectMapping ("Double", "Double", "doubleValue ()", "DOUBLE")); } class NativeObjectMapping { String javaClass; String setName; String nativeGetter; String sqlTypes; public NativeObjectMapping (String c, String s, String g, String t) { javaClass = c; setName = s; nativeGetter = g; sqlTypes = t; } } abstract class InlineFieldWriter { JCompositeField jcf; JClass currentClass; InlineFieldWriter (JCompositeField jcf, JClass currentClass) { this.jcf = jcf; this.currentClass = currentClass; prefix = jcf.getPrefix () + "_"; } String prefix = null; public void execute (FileWriter outFile, boolean isFirst) throws SchemaException, IOException { JClass fieldClass = jcf.getJClass (); Vector fields = new Vector (fieldClass.getAllFields ().values ()); String subPrimaryKey = fieldClass.getPrimaryKey (); writeOnce (outFile, fieldClass); for (int j = 0; j < fields.size (); j++) { JField subField = (JField)fields.elementAt (j); String sqlName = subField.getSqlName (); String preName = jcf.getJavaName () + "_"; if (!subField.isDbColumn () || sqlName.equals (subPrimaryKey)) { // do nothing .. } else if (subField instanceof JCompositeField) { throw new SchemaException ("ephman.abra.tools.nocompinline", currentClass.getSchemaFileName ()); } else { writeOneField (outFile, subField, sqlName, preName, isFirst); if (isFirst) isFirst = false; } } } public abstract void writeOneField (FileWriter outFile, JField subField, String sqlName, String preName, boolean isFirst) throws IOException, SchemaException; public void writeOnce (FileWriter outFile, JClass fieldClass) throws IOException { // override if neccessary } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -