📄 generatemodel.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Smart Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.util;
import java.sql.*;
import java.io.*;
import java.math.*;
import org.compiere.util.*;
/**
* Generate Model Classes extending PO.
* Base class for CMP interface - will be extended to create byte code directly
*
* @author Jorg Janke
* @version $Id: GenerateModel.java,v 1.9 2003/04/16 06:08:11 jjanke Exp $
*/
public class GenerateModel
{
/**
* Generate PO Class
* @param AD_Table_ID table id
*/
public GenerateModel(int AD_Table_ID)
{
// create column access methods
StringBuffer mandatory = new StringBuffer();
StringBuffer sb = createColumns(AD_Table_ID, mandatory);
// add header stuff
String tableName = createHeader(AD_Table_ID, sb, mandatory);
// Save it
writeToFile (sb, tableName + ".java");
} // GenerateModel
/**
* Add Header info to buffer
* @param AD_Table_ID table
* @param sb buffer
* @param mandatory init call for mandatory columns
* @return class name
*/
private String createHeader (int AD_Table_ID, StringBuffer sb, StringBuffer mandatory)
{
String tableName = "";
String sql = "SELECT TableName FROM AD_Table WHERE AD_Table_ID=?";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, AD_Table_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
tableName = rs.getString(1);
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
Log.error("GenerateModel.createHeader", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
if (tableName == null)
throw new RuntimeException ("TableName not found for ID=" + AD_Table_ID);
//
String keyColumn = tableName + "_ID";
String className = "M" + tableName.substring(tableName.indexOf('_')+1);
//
StringBuffer start = new StringBuffer ()
.append ("package org.compiere.model;"
+ "import java.util.*;"
+ "import java.sql.*;"
+ "import java.math.*;"
+ "import java.io.Serializable;"
+ "import org.compiere.util.*;"
//
+ "public class ").append(className).append(" extends PO"
+ "{"
//
+ "public ").append(className).append(" (Properties ctx, int ").append(keyColumn).append(")"
+ "{"
+ "super (ctx, ").append(keyColumn).append(");"
+ "if (").append(keyColumn).append(" == 0)"
+ "{").append(mandatory).append("}"
+ "}" // Constructor End
//
+ "protected POInfo initPO (Properties ctx)"
+ "{"
+ "int AD_Table_ID = ").append(AD_Table_ID).append(";"
+ "POInfo poi = POInfo.getPOInfo (ctx, AD_Table_ID);"
+ "return poi;"
+ "}" // initPO
//
+ "public boolean save ()"
+ "{"
+ "log.debug(\"save\");"
+ "return super.save();"
+ "}" // save
//
+ "public String toString()"
+ "{"
+ "StringBuffer sb = new StringBuffer (\"").append(className).append("[\")"
+ ".append(getID()).append(\"]\");"
+ "return sb.toString();"
+ "}");
StringBuffer end = new StringBuffer ("}");
//
sb.insert(0, start);
sb.append(end);
return className;
} // createHeader
/**
* Create Column access methods
* @param AD_Table_ID table
* @param mandatory init call for mandatory columns
* @return set/get method
*/
private StringBuffer createColumns (int AD_Table_ID, StringBuffer mandatory)
{
StringBuffer sb = new StringBuffer();
String sql = "SELECT c.ColumnName, c.IsUpdateable, c.IsMandatory,"
+ " c.AD_Reference_ID, c.AD_Reference_Value_ID "
+ "FROM AD_Column c "
+ "WHERE c.AD_Table_ID=?"
+ " AND c.ColumnName <> 'AD_Client_ID'"
+ " AND c.ColumnName <> 'AD_Org_ID'"
+ " AND c.ColumnName <> 'IsActive'"
+ " AND c.ColumnName NOT LIKE 'Created%'"
+ " AND c.ColumnName NOT LIKE 'Updated%'";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, AD_Table_ID);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
String columnName = rs.getString(1);
boolean isUpdateable = "Y".equals(rs.getString(2));
boolean isMandatory = "Y".equals(rs.getString(3));
int displayType = rs.getInt(4);
int AD_Reference_ID = rs.getInt(5);
//
sb.append(createMethods (mandatory,
columnName, isUpdateable, isMandatory, displayType, AD_Reference_ID));
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
Log.error("GenerateModel.createColumns", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
return sb;
} // createColumns
/**
* Create set/get methods
* @param mandatory init call for mandatory columns
* @param columnName column name
* @param isUpdateable updateable
* @param isMandatory mandatory
* @param displayType display type
* @param AD_Reference_ID validation reference
* @return set/get method
*/
private String createMethods (StringBuffer mandatory,
String columnName, boolean isUpdateable, boolean isMandatory,
int displayType, int AD_Reference_ID)
{
// Ignore Buttons
if (displayType == DisplayType.Button && AD_Reference_ID != 0)
return "";
//
Class clazz = DisplayType.getClass(displayType, true);
String dataType = clazz.getName();
dataType = dataType.substring(dataType.lastIndexOf('.')+1);
if (dataType.equals("Boolean"))
dataType = "boolean";
else if (dataType.equals("Integer"))
dataType = "int";
StringBuffer sb = new StringBuffer();
// Set ********
String setValue = "setValue";
if (isUpdateable)
sb.append("public ");
else
setValue = "setValueNoCheck";
sb.append("void set").append(columnName).append(" (").append(dataType).append(" ").append(columnName).append(")"
+ "{");
if (AD_Reference_ID != 0)
{
String staticVar = addListValidation (sb, AD_Reference_ID, columnName);
sb.insert(0, staticVar);
}
if (clazz.equals(Integer.class))
sb.append(setValue).append(" (\"").append(columnName).append("\", new Integer(").append(columnName).append("));");
else if (clazz.equals(Boolean.class))
sb.append(setValue).append(" (\"").append(columnName).append("\", new Boolean(").append(columnName).append("));");
else
{
if (isMandatory)
sb.append ("if (").append (columnName).append (" == null)"
+ " throw new IllegalArgumentException (\"").append(columnName).append(" is mandatory\");");
sb.append (setValue).append(" (\"").append (columnName).append ("\", ")
.append (columnName).append (");");
}
sb.append("}");
// Mandatory call
if (isMandatory)
{
mandatory.append("set").append(columnName).append(" (");
if (clazz.equals(Integer.class))
mandatory.append("0");
else if (clazz.equals(Boolean.class))
mandatory.append("false");
else if (clazz.equals(BigDecimal.class))
mandatory.append("Env.ZERO");
else if (clazz.equals(Timestamp.class))
mandatory.append("new Timestamp(System.currentTimeMillis())");
else
mandatory.append("null");
mandatory.append(");");
}
// Get ********
sb.append("public ").append(dataType);
if (clazz.equals(Boolean.class))
{
sb.append(" is");
if (columnName.toLowerCase().startsWith("is"))
sb.append(columnName.substring(2));
else
sb.append(columnName);
}
else
sb.append(" get").append(columnName);
sb.append("()"
+ "{");
if (clazz.equals(Integer.class))
sb.append("Integer ii = (Integer)getValue(\"").append(columnName).append("\");"
+ "if (ii == null)"
+ " return 0;"
+ "return ii.intValue();");
else if (clazz.equals(BigDecimal.class))
sb.append("BigDecimal bd = (BigDecimal)getValue(\"").append(columnName).append("\");"
+ "if (bd == null)"
+ " return Env.ZERO;"
+ "return bd;");
else if (clazz.equals(Boolean.class))
sb.append("Boolean bb = (Boolean)getValue(\"").append(columnName).append("\");"
+ "if (bb != null)"
+ " return bb.booleanValue();"
+ "return false;");
else
sb.append("return (").append(dataType).append(")getValue(\"").append(columnName).append("\");");
sb.append("}");
//
return sb.toString();
} // createMethods
/**
* Add List Validation
* @param sb buffer
* @param AD_Reference_ID reference
* @param columnName column
* @return static parameter
*/
private String addListValidation (StringBuffer sb, int AD_Reference_ID, String columnName)
{
StringBuffer retValue = new StringBuffer();
//
boolean found = false;
StringBuffer values = new StringBuffer("Reference_ID=")
.append(AD_Reference_ID);
StringBuffer statement = new StringBuffer();
String sql = "SELECT Value, Name FROM AD_Ref_List WHERE AD_Reference_ID=?";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, AD_Reference_ID);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
String value = rs.getString(1);
values.append(" - ").append(value);
if (statement.length() == 0)
statement.append("if (").append(columnName).append(".equals(\"").append(value).append("\")");
else
statement.append(" || ").append(columnName).append(".equals(\"").append(value).append("\")");
found = true;
//
String name = rs.getString(2);
retValue.append("public static final String ").append(columnName).append("_").append(name)
.append(" = \"").append(value).append("\";");
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
Log.error("GenerateMethod.addListValidation", e);
found = false;
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
statement.append(")"
+ "; "
+ "else "
+ "throw new IllegalArgumentException (\"").append(columnName)
.append(" Invalid value - ").append(values).append("\");");
//
if (found)
sb.append (statement);
return retValue.toString();
} // addListValidation
/*************************************************************************/
/**
* Write to file
* @param sb string buffer
* @param fileName file name
*/
private void writeToFile (StringBuffer sb, String fileName)
{
boolean wroteCR = false;
try
{
File out = new File (fileName);
FileWriter fw = new FileWriter (out);
for (int i = 0; i < sb.length(); i++)
{
char c = sb.charAt(i);
// after
if (c == ';' || c == '}')
{
fw.write (c);
fw.write(Env.NL);
}
// before & after
else if (c == '{')
{
fw.write(Env.NL);
fw.write (c);
fw.write(Env.NL);
}
else
fw.write (c);
}
fw.flush ();
fw.close ();
float size = out.length();
size /= 1024;
Log.trace(Log.l1_User, out.getAbsolutePath() + " - " + size + " kB");
}
catch (Exception ex)
{
Log.error("GenerateModel.writeToFile", ex);
}
} // writeToFile
/**
* String representation
* @return string representation
*/
public String toString()
{
StringBuffer sb = new StringBuffer ("GenerateModel[")
.append("]");
return sb.toString();
} // toString
/*************************************************************************/
/**
* Generate PO Model Class
* @param args not used
*/
public static void main (String[] args)
{
org.compiere.Compiere.startupClient();
String sql = "SELECT AD_Table_ID FROM AD_Table";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
new GenerateModel(rs.getInt(1));
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
Log.error("gen", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
/**
new GenerateModel(259);
new GenerateModel(260);
new GenerateModel(314);
//
new GenerateModel(318); // Invoice
new GenerateModel(333);
new GenerateModel(334);
//
new GenerateModel(291); // C_BPartner
new GenerateModel(292);
new GenerateModel(293);
new GenerateModel(298);
//
new GenerateModel(297); // BankAccount
new GenerateModel(398);
//
new GenerateModel(445);
new GenerateModel(446);
new GenerateModel(447);
new GenerateModel(448);
new GenerateModel(449);
new GenerateModel(450);
//
new GenerateModel(539); // Asset
new GenerateModel(541);
//
new GenerateModel(319); // InOut
new GenerateModel(320);
**/
} // main
} // GenerateModel
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -