use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class ModelInterfaceGenerator method getReferenceClassName.
public static String getReferenceClassName(int AD_Table_ID, String columnName, int displayType, int AD_Reference_ID) {
String referenceClassName = null;
//
if (displayType == DisplayType.TableDir || (displayType == DisplayType.Search && AD_Reference_ID == 0)) {
// teo_sarca: BF [ 1817768 ] Isolate hardcoded table direct columns
String refTableName = MQuery.getZoomTableName(columnName);
referenceClassName = "I_" + refTableName;
MTable table = MTable.get(Env.getCtx(), refTableName);
if (table != null) {
String entityType = table.getEntityType();
String modelpackage = getModelPackage(entityType);
if (modelpackage != null) {
referenceClassName = modelpackage + "." + referenceClassName;
}
if (!isGenerateModelGetterForEntity(AD_Table_ID, entityType)) {
referenceClassName = null;
}
} else {
throw new RuntimeException("No table found for " + refTableName);
}
} else if (displayType == DisplayType.Table || (displayType == DisplayType.Search && AD_Reference_ID > 0)) {
// TODO: HARDCODED: do not generate model getter for Fact_Acct.Account_ID
if (AD_Table_ID == 270 && columnName.equals("Account_ID"))
return null;
// TODO: HARDCODED: do not generate model getter for GL_DistributionLine.Account_ID
if (AD_Table_ID == 707 && columnName.equals("Account_ID"))
return null;
//
final String sql = "SELECT t.TableName, t.EntityType, ck.AD_Reference_ID" + " FROM AD_Ref_Table rt" + " INNER JOIN AD_Table t ON (t.AD_Table_ID=rt.AD_Table_ID)" + " INNER JOIN AD_Column ck ON (ck.AD_Table_ID=rt.AD_Table_ID AND ck.AD_Column_ID=rt.AD_Key)" + " WHERE rt.AD_Reference_ID=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, AD_Reference_ID);
rs = pstmt.executeQuery();
if (rs.next()) {
final String refTableName = rs.getString(1);
final String entityType = rs.getString(2);
final int refDisplayType = rs.getInt(3);
if (refDisplayType == DisplayType.ID) {
referenceClassName = "I_" + refTableName;
String modelpackage = getModelPackage(entityType);
if (modelpackage != null) {
referenceClassName = modelpackage + "." + referenceClassName;
}
if (!isGenerateModelGetterForEntity(AD_Table_ID, entityType)) {
referenceClassName = null;
}
}
}
} catch (SQLException e) {
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
} else if (displayType == DisplayType.Location) {
referenceClassName = "I_C_Location";
} else if (displayType == DisplayType.Locator) {
referenceClassName = "I_M_Locator";
} else if (displayType == DisplayType.Account) {
referenceClassName = "I_C_ValidCombination";
} else if (displayType == DisplayType.PAttribute) {
referenceClassName = "I_M_AttributeSetInstance";
} else {
// TODO - Handle other types
//sb.append("\tpublic I_"+columnName+" getI_").append(columnName).append("(){return null; };");
}
//
return referenceClassName;
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class ModelInterfaceGenerator method createHeader.
/**
* Add Header info to buffer
*
* @param AD_Table_ID table
* @param sb buffer
* @param mandatory init call for mandatory columns
* @param packageName package name
* @return class name
*/
private String createHeader(int AD_Table_ID, StringBuffer sb, StringBuffer mandatory) {
String tableName = "";
int accessLevel = 0;
String sql = "SELECT TableName, AccessLevel FROM AD_Table WHERE AD_Table_ID=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, AD_Table_ID);
rs = pstmt.executeQuery();
if (rs.next()) {
tableName = rs.getString(1);
accessLevel = rs.getInt(2);
}
} catch (SQLException e) {
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
if (tableName == null)
throw new RuntimeException("TableName not found for ID=" + AD_Table_ID);
//
String accessLevelInfo = accessLevel + " ";
if (accessLevel >= 4)
accessLevelInfo += "- System ";
if (accessLevel == 2 || accessLevel == 3 || accessLevel == 6 || accessLevel == 7)
accessLevelInfo += "- Client ";
if (accessLevel == 1 || accessLevel == 3 || accessLevel == 5 || accessLevel == 7)
accessLevelInfo += "- Org ";
//
String className = "I_" + tableName;
//
StringBuffer start = new StringBuffer().append(COPY).append("package ").append(packageName).append(";").append(NL);
if (!packageName.equals("org.compiere.model")) {
addImportClass("org.compiere.model.*");
}
addImportClass(java.math.BigDecimal.class);
addImportClass(org.compiere.util.KeyNamePair.class);
createImports(start);
// Interface
start.append("/** Generated Interface for ").append(tableName).append("\n").append(" * @author Adempiere (generated) \n").append(" * @version ").append(Adempiere.MAIN_VERSION).append(//.append(" - ").append(s_run).append("\n")
NL).append(" */\n").append("public interface ").append(className).append(" {").append("\n").append(" /** TableName=").append(tableName).append(" */\n").append(" public static final String Table_Name = \"").append(tableName).append("\";\n").append(" /** AD_Table_ID=").append(AD_Table_ID).append(" */\n").append(" public static final int Table_ID = MTable.getTable_ID(Table_Name);\n").append(// INFO - Should this be here???
" KeyNamePair Model = new KeyNamePair(Table_ID, Table_Name);\n").append(" /** AccessLevel = ").append(accessLevelInfo).append("\n").append(" */\n").append(" BigDecimal accessLevel = BigDecimal.valueOf(").append(accessLevel).append(// INFO - Should this be here???
");\n").append(" /** Load Meta Data */\n");
StringBuffer end = new StringBuffer("}");
//
sb.insert(0, start);
sb.append(end);
return className;
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class ModelInterfaceGenerator method main.
/***************************************************************************
* Generate Interface.
*
* <pre>
* Example: java GenerateInterafce.class mydirectory myPackage 'U','A'
* would generate entity type User and Application classes into mydirectory.
* Without parameters, the default is used:
* C:\extend\src\compiere\model\ compiere.model 'U','A'
*
* </pre>
*
* @param args
* directory package entityType - directory where to save the
* generated file - package of the classes to be generated -
* entityType to be generated
*/
public static void main(String[] args) {
Adempiere.startupEnvironment(true);
CLogMgt.setLevel(Level.FINE);
log.info("Generate Interface $Revision: 1.0 $");
log.info("----------------------------------");
// first parameter
String directory = "C:\\Adempiere\\adempiere-all\\extend\\src\\compiere\\model\\";
if (args.length > 0)
directory = args[0];
if (directory == null || directory.length() == 0) {
System.err.println("No Directory");
System.exit(1);
}
log.info("Directory: " + directory);
// second parameter
String packageName = "compiere.model";
if (args.length > 1)
packageName = args[1];
if (packageName == null || packageName.length() == 0) {
System.err.println("No package");
System.exit(1);
}
log.info("Package: " + packageName);
// third parameter
// User, Application
String entityType = "'U','A'";
if (args.length > 2)
entityType = args[2];
if (entityType == null || entityType.length() == 0) {
System.err.println("No EntityType");
System.exit(1);
}
StringBuffer sql = new StringBuffer("EntityType IN (").append(entityType).append(")");
log.info(sql.toString());
log.info("----------------------------------");
// Table name like
// All tables
String tableLike = "'%'";
//tableLike = "'AD_OrgInfo', 'AD_Role', 'C_CashLine', 'C_Currency', 'C_Invoice', 'C_Order', 'C_Payment', 'M_InventoryLine', 'M_PriceList', 'M_Product', 'U_POSTerminal'"; // only specific tables
if (args.length > 3)
tableLike = args[3];
log.info("Table Like: " + tableLike);
// complete sql
sql.insert(0, "SELECT AD_Table_ID " + "FROM AD_Table " + // special views
"WHERE (TableName IN ('RV_WarehousePrice','RV_BPartner')" + " OR IsView='N') AND IsActive = 'Y' AND TableName NOT LIKE '%_Trl' AND ");
sql.append(" AND TableName LIKE ").append(tableLike);
//sql.append(" AND TableName IN (").append( tableLike ).append(")");
sql.append(" ORDER BY TableName");
//
int count = 0;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql.toString(), null);
rs = pstmt.executeQuery();
while (rs.next()) {
new ModelInterfaceGenerator(rs.getInt(1), directory, packageName);
count++;
}
} catch (SQLException e) {
throw new DBException(e, sql.toString());
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
log.info("Generated = " + count);
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class MAttributeSet method getMAttributes.
/**
* Get Attribute Array
* @param instanceAttributes true if for instance
* @return instance or product attribute array
*/
public MAttribute[] getMAttributes(boolean instanceAttributes) {
if ((m_instanceAttributes == null && instanceAttributes) || m_productAttributes == null && !instanceAttributes) {
String sql = "SELECT mau.M_Attribute_ID " + "FROM M_AttributeUse mau" + " INNER JOIN M_Attribute ma ON (mau.M_Attribute_ID=ma.M_Attribute_ID) " + "WHERE mau.IsActive='Y' AND ma.IsActive='Y'" + // #1,2
" AND mau.M_AttributeSet_ID=? AND ma.IsInstanceAttribute=? " + "ORDER BY mau.SeqNo";
ArrayList<MAttribute> list = new ArrayList<MAttribute>();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql, get_TrxName());
pstmt.setInt(1, getM_AttributeSet_ID());
pstmt.setString(2, instanceAttributes ? "Y" : "N");
rs = pstmt.executeQuery();
while (rs.next()) {
MAttribute ma = new MAttribute(getCtx(), rs.getInt(1), get_TrxName());
list.add(ma);
}
} catch (SQLException ex) {
throw new DBException(ex, sql);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
// Differentiate attributes
if (instanceAttributes) {
m_instanceAttributes = new MAttribute[list.size()];
list.toArray(m_instanceAttributes);
} else {
m_productAttributes = new MAttribute[list.size()];
list.toArray(m_productAttributes);
}
}
//
if (instanceAttributes) {
if (isInstanceAttribute() != m_instanceAttributes.length > 0)
setIsInstanceAttribute(m_instanceAttributes.length > 0);
}
// Return
if (instanceAttributes)
return m_instanceAttributes;
return m_productAttributes;
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class PreparedStatementProxy method init.
// PreparedStatementProxy
/**
* Initialise the prepared statement wrapper object
*/
protected void init() {
try {
Connection conn = null;
Trx trx = p_vo.getTrxName() == null ? null : Trx.get(p_vo.getTrxName(), false);
if (trx != null) {
conn = trx.getConnection();
} else {
if (p_vo.getResultSetConcurrency() == ResultSet.CONCUR_UPDATABLE)
m_conn = DB.getConnectionRW();
else
m_conn = DB.getConnectionRO();
conn = m_conn;
}
if (conn == null)
throw new DBException("No Connection");
p_stmt = conn.prepareStatement(p_vo.getSql(), p_vo.getResultSetType(), p_vo.getResultSetConcurrency());
} catch (Exception e) {
log.log(Level.SEVERE, p_vo.getSql(), e);
throw new DBException(e);
}
}
Aggregations