parameterize, indentation, imports

This commit is contained in:
Redhuan D. Oon 2008-11-20 13:58:38 +00:00
parent 36d10d08fb
commit 9c1c4ea524
1 changed files with 166 additions and 161 deletions

View File

@ -18,14 +18,22 @@
*****************************************************************************/
package org.eevolution.tools;
import java.io.*;
import java.math.*;
import java.sql.*;
import java.util.logging.*;
import org.compiere.*;
import org.compiere.dbPort.*;
import org.compiere.db.*;
import org.compiere.util.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.logging.Level;
import org.compiere.Adempiere;
import org.compiere.util.CLogMgt;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.DisplayType;
import org.compiere.util.Env;
/**
* Generate Model Classes extending PO.
* Base class for CMP interface - will be extended to create byte code directly
@ -61,7 +69,7 @@ public class GenerateModel
/** File Header */
public static final String COPY =
"/******************************************************************************\n"
"/******************************************************************************\n"
+" * Product: Adempiere ERP & CRM Smart Business Solution *\n"
+" * Copyright (C) 1999-2007 ComPiere, Inc. All Rights Reserved. *\n"
+" * This program is free software; you can redistribute it and/or modify it *\n"
@ -78,9 +86,6 @@ public class GenerateModel
+" * or via info@compiere.org or http://www.compiere.org/license.html *\n"
+" *****************************************************************************/\n";
/** Generated on */
private Timestamp s_run = new Timestamp(System.currentTimeMillis());
/** Logger */
private static CLogger log = CLogger.getCLogger (GenerateModel.class);
@ -142,79 +147,79 @@ public class GenerateModel
String className = "X_" + tableName;
//
StringBuffer start = new StringBuffer ()
.append (COPY)
.append ("package " + packageName + ";\n"
.append (COPY)
.append ("package " + packageName + ";\n"
+ "/** Generated Model - DO NOT CHANGE */\n");
if (!packageName.equals("org.compiere.model"))
start.append("import org.compiere.model.*;");
start.append("import java.util.*;"
+ "import java.sql.*;"
+ "import java.math.*;"
+ "import org.compiere.util.*;"
// Class
+ "/** Generated Model for ").append(tableName).append("\n"
+ " * @author Adempiere (generated) \n"
+ " * @version ").append(Adempiere.MAIN_VERSION).append(" - $Id$")
// .append(s_run) // Timestamp
.append(" */\n"
+ "public class ").append(className).append(" extends PO"
+ "{"
// Standard Constructor
+ "/** Standard Constructor\n@param ctx context\n@param "
+ keyColumn + " id\n@param trxName transaction\n*/\n"
+ "public ").append(className).append(" (Properties ctx, int ").append(keyColumn)
.append(", String trxName)"
+ "{"
+ "super (ctx, ").append(keyColumn).append(", trxName);"
+ "/** if (").append(keyColumn).append(" == 0)"
+ "{").append(mandatory).append("} */\n"
+ "}" // Constructor End
// Short Constructor
// + "/** Short Constructor */\n"
// + "public ").append(className).append(" (Properties ctx, int ").append(keyColumn).append(")"
// + "{"
// + "this (ctx, ").append(keyColumn).append(", null);"
// + "}" // Constructor End
+ "import java.sql.*;"
+ "import java.math.*;"
+ "import org.compiere.util.*;"
// Class
+ "/** Generated Model for ").append(tableName).append("\n"
+ " * @author Adempiere (generated) \n"
+ " * @version ").append(Adempiere.MAIN_VERSION).append(" - $Id$")
// .append(s_run) // Timestamp
.append(" */\n"
+ "public class ").append(className).append(" extends PO"
+ "{"
// Standard Constructor
+ "/** Standard Constructor\n@param ctx context\n@param "
+ keyColumn + " id\n@param trxName transaction\n*/\n"
+ "public ").append(className).append(" (Properties ctx, int ").append(keyColumn)
.append(", String trxName)"
+ "{"
+ "super (ctx, ").append(keyColumn).append(", trxName);"
+ "/** if (").append(keyColumn).append(" == 0)"
+ "{").append(mandatory).append("} */\n"
+ "}" // Constructor End
// Short Constructor
// + "/** Short Constructor */\n"
// + "public ").append(className).append(" (Properties ctx, int ").append(keyColumn).append(")"
// + "{"
// + "this (ctx, ").append(keyColumn).append(", null);"
// + "}" // Constructor End
// Load Constructor
+ "/** Load Constructor \n@param ctx context\n@param rs result set \n@param trxName transaction\n*/\n"
+ "public ").append(className).append(" (Properties ctx, ResultSet rs, String trxName)"
+ "{"
+ "super (ctx, rs, trxName);"
+ "}" // Load Constructor End
//
// globalqss - Grant independence to GenerateModel from AD_Table_ID
+ "/** TableName=").append(tableName).append(" */\n"
+ "public static final String Table_Name=\"").append(tableName).append("\";\n"
// + "public static final int Table_ID=").append(AD_Table_ID).append(";\n"
+ "/** AD_Table_ID=").append(AD_Table_ID).append(" */\n"
+ "public static final int Table_ID=MTable.getTable_ID(Table_Name);\n"
//
// globalqss
// + "protected static KeyNamePair Model = new KeyNamePair(").append(AD_Table_ID).append(",\"").append(tableName).append("\");\n"
+ "protected static KeyNamePair Model = new KeyNamePair(Table_ID, Table_Name);\n"
//
+ "protected BigDecimal accessLevel = BigDecimal.valueOf(").append(accessLevel).append(");"
+ "/** AccessLevel\n@return ").append(accessLevelInfo).append("\n*/\n"
+ "protected int get_AccessLevel()"
+ "{"
+ "return accessLevel.intValue();"
+ "}"
//
+ "/** Load Meta Data\n@param ctx context\n@return PO Info\n*/\n"
+ "protected POInfo initPO (Properties ctx)"
+ "{"
+ "POInfo poi = POInfo.getPOInfo (ctx, Table_ID);"
+ "return poi;"
+ "}" // initPO
//
+ "/** Info\n@return info\n*/\n"
+ "public String toString()"
+ "{"
+ "StringBuffer sb = new StringBuffer (\"").append(className).append("[\")"
+ ".append(get_ID()).append(\"]\");"
+ "return sb.toString();"
+ "}");
// Load Constructor
+ "/** Load Constructor \n@param ctx context\n@param rs result set \n@param trxName transaction\n*/\n"
+ "public ").append(className).append(" (Properties ctx, ResultSet rs, String trxName)"
+ "{"
+ "super (ctx, rs, trxName);"
+ "}" // Load Constructor End
//
// globalqss - Grant independence to GenerateModel from AD_Table_ID
+ "/** TableName=").append(tableName).append(" */\n"
+ "public static final String Table_Name=\"").append(tableName).append("\";\n"
// + "public static final int Table_ID=").append(AD_Table_ID).append(";\n"
+ "/** AD_Table_ID=").append(AD_Table_ID).append(" */\n"
+ "public static final int Table_ID=MTable.getTable_ID(Table_Name);\n"
//
// globalqss
// + "protected static KeyNamePair Model = new KeyNamePair(").append(AD_Table_ID).append(",\"").append(tableName).append("\");\n"
+ "protected static KeyNamePair Model = new KeyNamePair(Table_ID, Table_Name);\n"
//
+ "protected BigDecimal accessLevel = BigDecimal.valueOf(").append(accessLevel).append(");"
+ "/** AccessLevel\n@return ").append(accessLevelInfo).append("\n*/\n"
+ "protected int get_AccessLevel()"
+ "{"
+ "return accessLevel.intValue();"
+ "}"
//
+ "/** Load Meta Data\n@param ctx context\n@return PO Info\n*/\n"
+ "protected POInfo initPO (Properties ctx)"
+ "{"
+ "POInfo poi = POInfo.getPOInfo (ctx, Table_ID);"
+ "return poi;"
+ "}" // initPO
//
+ "/** Info\n@return info\n*/\n"
+ "public String toString()"
+ "{"
+ "StringBuffer sb = new StringBuffer (\"").append(className).append("[\")"
+ ".append(get_ID()).append(\"]\");"
+ "return sb.toString();"
+ "}");
StringBuffer end = new StringBuffer ("}");
//
@ -276,10 +281,10 @@ public class GenerateModel
boolean IsEncrypted = "Y".equals(rs.getString(16));
//
sb.append(createColumnMethods (mandatory,
columnName, isUpdateable, isMandatory,
displayType, AD_Reference_Value_ID, fieldLength,
defaultValue, ValueMin, ValueMax, VFormat,
Callout, Name, Description, virtualColumn, IsEncrypted));
columnName, isUpdateable, isMandatory,
displayType, AD_Reference_Value_ID, fieldLength,
defaultValue, ValueMin, ValueMax, VFormat,
Callout, Name, Description, virtualColumn, IsEncrypted));
//
if (seqNo == 1 && "Y".equals(rs.getString("IsIdentifier"))) {
if (!isKeyNamePairCreated) {
@ -293,8 +298,8 @@ public class GenerateModel
}
// Create COLUMNNAME_ property (teo_sarca, [ 1662447 ])
sb.append("/** Column name ").append(columnName).append(" */\n")
.append("public static final String COLUMNNAME_").append(columnName)
.append(" = \"").append(columnName).append("\";");
.append("public static final String COLUMNNAME_").append(columnName)
.append(" = \"").append(columnName).append("\";");
}
rs.close();
pstmt.close();
@ -339,14 +344,14 @@ public class GenerateModel
@return set/get method
*/
private String createColumnMethods (StringBuffer mandatory,
String columnName, boolean isUpdateable, boolean isMandatory,
int displayType, int AD_Reference_ID, int fieldLength,
String defaultValue, String ValueMin, String ValueMax, String VFormat,
String Callout, String Name, String Description,
boolean virtualColumn, boolean IsEncrypted)
String columnName, boolean isUpdateable, boolean isMandatory,
int displayType, int AD_Reference_ID, int fieldLength,
String defaultValue, String ValueMin, String ValueMax, String VFormat,
String Callout, String Name, String Description,
boolean virtualColumn, boolean IsEncrypted)
{
// Clazz
Class clazz = DisplayType.getClass(displayType, true);
Class<?> clazz = DisplayType.getClass(displayType, true);
if (defaultValue == null)
defaultValue = "";
if (DisplayType.isLOB(displayType)) // No length check for LOBs
@ -354,8 +359,8 @@ public class GenerateModel
// Handle Posted
if (columnName.equalsIgnoreCase("Posted")
|| columnName.equalsIgnoreCase("Processed")
|| columnName.equalsIgnoreCase("Processing"))
|| columnName.equalsIgnoreCase("Processed")
|| columnName.equalsIgnoreCase("Processing"))
{
clazz = Boolean.class;
AD_Reference_ID = 0;
@ -368,7 +373,7 @@ public class GenerateModel
}
// String Key
else if (columnName.equalsIgnoreCase("AD_Language")
|| columnName.equalsIgnoreCase("EntityType"))
|| columnName.equalsIgnoreCase("EntityType"))
{
clazz = String.class;
}
@ -406,7 +411,7 @@ public class GenerateModel
setValue = "set_ValueNoCheckE";
}
sb.append("void set").append(columnName).append(" (").append(dataType).append(" ").append(columnName).append(")"
+ "{");
+ "{");
// List Validation
if (AD_Reference_ID != 0)
{
@ -427,17 +432,17 @@ public class GenerateModel
{
int firstOK = 1; // Valid ID 0
if (columnName.equals("AD_Client_ID") || columnName.equals("AD_Org_ID")
|| columnName.equals("Record_ID") || columnName.equals("C_DocType_ID")
|| columnName.equals("Node_ID") || columnName.equals("AD_Role_ID")
|| columnName.equals("M_AttributeSet_ID") || columnName.equals("M_AttributeSetInstance_ID"))
|| columnName.equals("Record_ID") || columnName.equals("C_DocType_ID")
|| columnName.equals("Node_ID") || columnName.equals("AD_Role_ID")
|| columnName.equals("M_AttributeSet_ID") || columnName.equals("M_AttributeSetInstance_ID"))
firstOK = 0;
sb.append("if (").append (columnName)
.append (" < ").append(firstOK).append(") throw new IllegalArgumentException (\"")
.append(columnName).append(" is mandatory.\");");
.append (" < ").append(firstOK).append(") throw new IllegalArgumentException (\"")
.append(columnName).append(" is mandatory.\");");
}
else // set optional _ID to null if 0
sb.append("if (").append (columnName).append (" <= 0) ")
.append(setValue).append(" (\"").append(columnName).append("\", null); else \n");
.append(setValue).append(" (\"").append(columnName).append("\", null); else \n");
}
sb.append(setValue).append(" (\"").append(columnName).append("\", Integer.valueOf(").append(columnName).append("));");
}
@ -449,9 +454,9 @@ public class GenerateModel
if (isMandatory && AD_Reference_ID == 0) // does not apply to int/boolean
{
sb.append("if (")
.append (columnName).append (" == null)"
.append (columnName).append (" == null)"
+ " throw new IllegalArgumentException (\"")
.append(columnName).append(" is mandatory.\");");
.append(columnName).append(" is mandatory.\");");
}
// String length check
if (clazz.equals(String.class) && fieldLength > 0)
@ -460,15 +465,15 @@ public class GenerateModel
if (!isMandatory)
sb.append(columnName).append(" != null && ");
sb.append(columnName).append(".length() > ").append(fieldLength)
.append("){log.warning(\"Length > ")
.append(fieldLength).append(" - truncated\");")
.append(columnName).append(" = ")
.append(columnName).append(".substring(0,").append(fieldLength-1).append(");}");
.append("){log.warning(\"Length > ")
.append(fieldLength).append(" - truncated\");")
.append(columnName).append(" = ")
.append(columnName).append(".substring(0,").append(fieldLength-1).append(");}");
}
//
sb.append (setValue).append(" (\"").append (columnName).append ("\", ")
.append (columnName).append (");");
.append (columnName).append (");");
}
sb.append("}");
@ -523,27 +528,27 @@ public class GenerateModel
sb.append("() {");
if (clazz.equals(Integer.class))
sb.append("Integer ii = (Integer)")
.append(getValue).append("(\"").append(columnName).append("\");"
+ "if (ii == null)"
+ " return 0;"
+ "return ii.intValue();");
.append(getValue).append("(\"").append(columnName).append("\");"
+ "if (ii == null)"
+ " return 0;"
+ "return ii.intValue();");
else if (clazz.equals(BigDecimal.class))
sb.append("BigDecimal bd = (BigDecimal)").append(getValue)
.append("(\"").append(columnName).append("\");"
+ "if (bd == null)"
+ " return Env.ZERO;"
+ "return bd;");
.append("(\"").append(columnName).append("\");"
+ "if (bd == null)"
+ " return Env.ZERO;"
+ "return bd;");
else if (clazz.equals(Boolean.class))
sb.append("Object oo = ").append(getValue)
.append("(\"").append(columnName).append("\");"
+ "if (oo != null) { if (oo instanceof Boolean) return ((Boolean)oo).booleanValue(); return \"Y\".equals(oo);}"
+ "return false;");
.append("(\"").append(columnName).append("\");"
+ "if (oo != null) { if (oo instanceof Boolean) return ((Boolean)oo).booleanValue(); return \"Y\".equals(oo);}"
+ "return false;");
else if (dataType.equals("Object"))
sb.append("return ").append(getValue)
.append("(\"").append(columnName).append("\");");
.append("(\"").append(columnName).append("\");");
else
sb.append("return (").append(dataType).append(")").append(getValue)
.append("(\"").append(columnName).append("\");");
.append("(\"").append(columnName).append("\");");
sb.append("}");
//
return sb.toString();
@ -564,16 +569,16 @@ public class GenerateModel
public static final String NEXTACTION_FollowUp = "F";
*/
private String addListValidation (StringBuffer sb, int AD_Reference_ID,
String columnName, boolean nullable)
String columnName, boolean nullable)
{
StringBuffer retValue = new StringBuffer();
retValue.append("\n/** ").append(columnName).append(" AD_Reference_ID=").append(AD_Reference_ID) .append(" */\n")
.append("public static final int ").append(columnName.toUpperCase())
.append("_AD_Reference_ID=").append(AD_Reference_ID).append(";");
.append("public static final int ").append(columnName.toUpperCase())
.append("_AD_Reference_ID=").append(AD_Reference_ID).append(";");
//
boolean found = false;
StringBuffer values = new StringBuffer("Reference_ID=")
.append(AD_Reference_ID);
.append(AD_Reference_ID);
StringBuffer statement = new StringBuffer();
if (nullable)
statement.append("if (").append(columnName).append(" == null");
@ -591,19 +596,19 @@ public class GenerateModel
values.append(" - ").append(value);
if (statement.length() == 0)
statement.append("if (").append(columnName)
.append(".equals(\"").append(value).append("\")");
.append(".equals(\"").append(value).append("\")");
else
statement.append(" || ").append(columnName)
.append(".equals(\"").append(value).append("\")");
.append(".equals(\"").append(value).append("\")");
//
if (!found)
{
found = true;
if (!nullable)
sb.append("if (")
.append (columnName).append (" == null)"
+ " throw new IllegalArgumentException (\"")
.append(columnName).append(" is mandatory\");");
.append (columnName).append (" == null)"
+ " throw new IllegalArgumentException (\"")
.append(columnName).append(" is mandatory\");");
}
@ -650,8 +655,8 @@ public class GenerateModel
}
retValue.append("/** ").append(name).append(" = ").append(value).append(" */\n");
retValue.append("public static final String ").append(columnName.toUpperCase())
.append("_").append(nameClean)
.append(" = \"").append(value).append("\";");
.append("_").append(nameClean)
.append(" = \"").append(value).append("\";");
}
rs.close();
pstmt.close();
@ -674,11 +679,11 @@ public class GenerateModel
pstmt = null;
}
statement.append(")"
+ "; "
+ "else "
+ "throw new IllegalArgumentException (\"").append(columnName)
.append(" Invalid value - \" + ").append(columnName)
.append(" + \" - ").append(values).append("\");");
+ "; "
+ "else "
+ "throw new IllegalArgumentException (\"").append(columnName)
.append(" Invalid value - \" + ").append(columnName)
.append(" + \" - ").append(values).append("\");");
//
if (found && !columnName.equals("EntityType"))
sb.append (statement);
@ -697,8 +702,8 @@ public class GenerateModel
if (displayType != DisplayType.String)
method = "String.valueOf(" + method + ")";
StringBuffer sb = new StringBuffer("/** Get Record ID/ColumnName\n@return ID/ColumnName pair\n*/"
+ "public KeyNamePair getKeyNamePair() "
+ "{return new KeyNamePair(get_ID(), ").append(method).append(");}");
+ "public KeyNamePair getKeyNamePair() "
+ "{return new KeyNamePair(get_ID(), ").append(method).append(");}");
return sb;
} // createKeyNamePair
@ -757,7 +762,7 @@ public class GenerateModel
public String toString()
{
StringBuffer sb = new StringBuffer ("GenerateModel[")
.append("]");
.append("]");
return sb.toString();
} // toString
@ -780,7 +785,7 @@ public class GenerateModel
{
org.compiere.Adempiere.startupEnvironment(true);
CLogMgt.setLevel(Level.FINE);
// CLogMgt.setLevel(Level.ALL);
// CLogMgt.setLevel(Level.ALL);
log.info("Generate Model $Revision: 1.5 $");
log.info("----------------------------------");
// first parameter
@ -815,7 +820,7 @@ public class GenerateModel
System.exit(1);
}
StringBuffer sql = new StringBuffer("EntityType IN (")
.append(entityType).append(")");
.append(entityType).append(")");
log.info(sql.toString());
log.info("----------------------------------");
@ -827,10 +832,10 @@ public class GenerateModel
// complete sql
sql.insert(0, "SELECT AD_Table_ID "
+ "FROM AD_Table "
+ "WHERE (TableName IN ('RV_WarehousePrice','RV_BPartner')" // special views
+ " OR IsView='N')"
+ " AND TableName NOT LIKE '%_Trl' AND ");
+ "FROM AD_Table "
+ "WHERE (TableName IN ('RV_WarehousePrice','RV_BPartner')" // special views
+ " OR IsView='N')"
+ " AND TableName NOT LIKE '%_Trl' AND ");
// globalqss
sql.append(" AND TableName LIKE ").append(tableLike);
//