diff --git a/base/src/org/adempiere/model/GenericPO.java b/base/src/org/adempiere/model/GenericPO.java new file mode 100644 index 0000000000..4f269d9600 --- /dev/null +++ b/base/src/org/adempiere/model/GenericPO.java @@ -0,0 +1,141 @@ +/******************************************************************************* + * Product: Adempiere ERP & CRM Smart Business Solution * Copyright (C) + * 1999-2006 Adempiere, Inc. All Rights Reserved. * This program is free + * software; you can redistribute it and/or modify it * under the terms version + * 2 of the GNU General Public License as published * by the Free Software + * Foundation. This program is distributed in the hope * that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied * warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General + * Public License for more details. * You should have received a copy of the GNU + * General Public License along * with this program; if not, write to the Free + * Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA. * + * + * Copyright (C) 2004 Marco LOMBARDO. lombardo@mayking.com + * Contributor(s): Low Heng Sin hengsin@avantz.com + * __________________________________________ + ******************************************************************************/ + +// ---------------------------------------------------------------------- +// Generic PO. +package org.adempiere.model; + +// import for GenericPO +import java.util.*; +import java.sql.*; +import org.compiere.model.*; + +/** + * Generic PO implementation, this can be use together with ModelValidator as alternative to the classic + * generated model class and extend ( X_ & M_ ) approach. + * + * Originally for used to insert/update data from adempieredata.xml file in 2pack. + * @author Marco LOMBARDO + * @contributor Low Heng Sin + */ +public class GenericPO extends PO { + + /** + * @param tableName + * @param ctx + * @param ID + */ + public GenericPO(String tableName, Properties ctx, int ID) { + super(new PropertiesWrapper(ctx, tableName), ID, null, null); + } + + /** + * @param tableName + * @param ctx + * @param rs + */ + public GenericPO(String tableName, Properties ctx, ResultSet rs) { + super(new PropertiesWrapper(ctx, tableName), 0, null, rs); + } + + /** + * @param tableName + * @param ctx + * @param ID + * @param trxName + */ + public GenericPO(String tableName, Properties ctx, int ID, String trxName) { + super(new PropertiesWrapper(ctx, tableName), ID, trxName, null); + } + + /** + * @param tableName + * @param ctx + * @param rs + * @param trxName + */ + public GenericPO(String tableName, Properties ctx, ResultSet rs, String trxName) { + super(new PropertiesWrapper(ctx, tableName), 0, trxName, rs); + } + + private int tableID = 0; + + private String tableName = null; + + /** Load Meta Data */ + protected POInfo initPO(Properties ctx) { + PropertiesWrapper wrapper = (PropertiesWrapper)ctx; + p_ctx = wrapper.source; + tableName = wrapper.tableName; + tableID = MTable.getTable_ID(tableName); + // log.info("Table_ID: "+Table_ID); + POInfo poi = POInfo.getPOInfo(ctx, tableID); + return poi; + } + + public String toString() { + StringBuffer sb = new StringBuffer("GenericPO[Table=").append( + "" + tableID + ",ID=").append(get_ID()).append("]"); + return sb.toString(); + } + + public static final int AD_ORGTRX_ID_AD_Reference_ID = 130; + + /** + * Set Trx Organization. Performing or initiating organization + */ + public void setAD_OrgTrx_ID(int AD_OrgTrx_ID) { + if (AD_OrgTrx_ID == 0) + set_Value("AD_OrgTrx_ID", null); + else + set_Value("AD_OrgTrx_ID", new Integer(AD_OrgTrx_ID)); + } + + /** + * Get Trx Organization. Performing or initiating organization + */ + public int getAD_OrgTrx_ID() { + Integer ii = (Integer) get_Value("AD_OrgTrx_ID"); + if (ii == null) + return 0; + return ii.intValue(); + } + + @Override + protected int get_AccessLevel() { + return Integer.parseInt(p_info.getAccessLevel()); + } + +} // GenericPO + +/** + * Wrapper class to workaround the limit of PO constructor that doesn't take a tableName or + * tableID parameter. Note that in the generated class scenario ( X_ ), tableName and tableId + * is generated as a static field. + * @author Low Heng Sin + * + */ +class PropertiesWrapper extends Properties { + protected Properties source; + protected String tableName; + + PropertiesWrapper(Properties source, String tableName) { + this.source = source; + this.tableName = tableName; + } +} diff --git a/base/src/org/adempiere/pipo/GenericPO.java b/base/src/org/adempiere/pipo/GenericPO.java deleted file mode 100644 index 1b8dc7bf33..0000000000 --- a/base/src/org/adempiere/pipo/GenericPO.java +++ /dev/null @@ -1,115 +0,0 @@ -/******************************************************************************* - * Product: Adempiere ERP & CRM Smart Business Solution * Copyright (C) - * 1999-2006 Adempiere, Inc. All Rights Reserved. * This program is free - * software; you can redistribute it and/or modify it * under the terms version - * 2 of the GNU General Public License as published * by the Free Software - * Foundation. This program is distributed in the hope * that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied * warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General - * Public License for more details. * You should have received a copy of the GNU - * General Public License along * with this program; if not, write to the Free - * Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA - * 02111-1307 USA. * - * - * Copyright (C) 2004 Marco LOMBARDO. lombardo@mayking.com Contributor(s): - * __________________________________________ - ******************************************************************************/ - -// ---------------------------------------------------------------------- -// Generic PO. -// Used to insert/update data from a adempieredata.xml file. -package org.adempiere.pipo; - -// import for GenericPO -import java.util.*; -import java.sql.*; -import org.compiere.model.*; - -public class GenericPO extends PO { - - // private Logger log = Logger.getCLogger(getClass()); - - /** Standard Constructor */ - public GenericPO(Properties ctx, int ID) { - super(ctx, ID, null, null); - } - - /** Load Constructor */ - public GenericPO(Properties ctx, ResultSet rs) { - super(ctx, 0, null, rs); - } - - public GenericPO(Properties ctx, int ID, String trxName) { - super(ctx, ID, trxName, null); - } - - public GenericPO(Properties ctx, ResultSet rs, String trxName) { - super(ctx, 0, trxName, rs); - } - - private int Table_ID = 0; - - /** Load Meta Data */ - protected POInfo initPO(Properties ctx) { - Table_ID = Integer.valueOf(ctx.getProperty("adempieredataTable_ID")) - .intValue(); - // log.info("Table_ID: "+Table_ID); - POInfo poi = POInfo.getPOInfo(ctx, Table_ID); - return poi; - } - - public String toString() { - StringBuffer sb = new StringBuffer("GenericPO[Table=").append( - "" + Table_ID + ",ID=").append(get_ID()).append("]"); - return sb.toString(); - } - - public static final int AD_ORGTRX_ID_AD_Reference_ID = 130; - - /** - * Set Trx Organization. Performing or initiating organization - */ - public void setAD_OrgTrx_ID(int AD_OrgTrx_ID) { - if (AD_OrgTrx_ID == 0) - set_Value("AD_OrgTrx_ID", null); - else - set_Value("AD_OrgTrx_ID", new Integer(AD_OrgTrx_ID)); - } - - /** - * Get Trx Organization. Performing or initiating organization - */ - public int getAD_OrgTrx_ID() { - Integer ii = (Integer) get_Value("AD_OrgTrx_ID"); - if (ii == null) - return 0; - return ii.intValue(); - } - - // setValue - public void setValue(String columnName, Object value) { - set_Value(columnName, value); - } - - // setValueNoCheck - public void setValueNoCheck(String columnName, Object value) { - set_ValueNoCheck(columnName, value); - } - - // setValue - public void setValue(int index, Object value) { - set_Value(index, value); - } - - public void copyRS(PO From, PO To) { - copyValues(From, To); - } - - @Override - protected int get_AccessLevel() { - // TODO Auto-generated method stub - return 0; - } - -} // GenericPO - diff --git a/base/src/org/adempiere/pipo/handler/DataElementHandler.java b/base/src/org/adempiere/pipo/handler/DataElementHandler.java index 68a3f5b596..0ac753fcd2 100644 --- a/base/src/org/adempiere/pipo/handler/DataElementHandler.java +++ b/base/src/org/adempiere/pipo/handler/DataElementHandler.java @@ -30,9 +30,9 @@ import java.util.logging.Level; import javax.xml.transform.sax.TransformerHandler; +import org.adempiere.model.GenericPO; import org.adempiere.pipo.AbstractElementHandler; import org.adempiere.pipo.Element; -import org.adempiere.pipo.GenericPO; import org.adempiere.pipo.IDFinder; import org.adempiere.pipo.exception.POSaveFailedException; import org.compiere.model.POInfo; @@ -95,11 +95,11 @@ public class DataElementHandler extends AbstractElementHandler { Attributes atts = element.attributes; log.info(elementValue+" "+atts.getValue("name")); String d_rowname = atts.getValue("name"); - ctx.setProperty("adempieredataTable_ID", String.valueOf(get_IDWithColumn(ctx, "AD_Table", "TableName", d_tablename))); + // name can be null if there are keyXname attributes. if (!d_rowname.equals("")){ int id = get_ID(ctx, d_tablename, d_rowname); - genericPO = new GenericPO(ctx, id, getTrxName(ctx)); + genericPO = new GenericPO(d_tablename, ctx, id, getTrxName(ctx)); if (id > 0){ AD_Backup_ID = copyRecord(ctx,d_tablename,genericPO); objectStatus = "Update"; @@ -130,13 +130,13 @@ public class DataElementHandler extends AbstractElementHandler { ResultSet rs = pstmt.executeQuery(); if (rs.next()) { objectStatus = "Update"; - genericPO = new GenericPO(ctx, rs, getTrxName(ctx)); + genericPO = new GenericPO(d_tablename, ctx, rs, getTrxName(ctx)); rs.close(); pstmt.close(); pstmt = null; } else { - genericPO = new GenericPO(ctx, 0, getTrxName(ctx)); + genericPO = new GenericPO(d_tablename, ctx, 0, getTrxName(ctx)); rs.close(); pstmt.close(); pstmt = null; @@ -144,11 +144,11 @@ public class DataElementHandler extends AbstractElementHandler { // set keyXname. CURRENT_KEY = "key1name"; if (!atts.getValue(CURRENT_KEY).equals("")) { - genericPO.setValueNoCheck(atts.getValue(CURRENT_KEY), atts.getValue("lookup"+CURRENT_KEY)); + genericPO.set_ValueOfColumn(atts.getValue(CURRENT_KEY), atts.getValue("lookup"+CURRENT_KEY)); } CURRENT_KEY = "key2name"; if (!atts.getValue(CURRENT_KEY).equals("")) { - genericPO.setValueNoCheck(atts.getValue(CURRENT_KEY), atts.getValue("lookup"+CURRENT_KEY)); + genericPO.set_ValueOfColumn(atts.getValue(CURRENT_KEY), atts.getValue("lookup"+CURRENT_KEY)); } } @@ -157,8 +157,7 @@ public class DataElementHandler extends AbstractElementHandler { log.info ("keyXname attribute. init from rs error."+e); } } - // reset Table ID for GenericPO. - ctx.setProperty("adempieredataTable_ID", "0"); + // for debug GenericPO. if (false) { POInfo poInfo = POInfo.getPOInfo(ctx, get_ID(ctx, "AD_Table", d_tablename)); @@ -170,11 +169,11 @@ public class DataElementHandler extends AbstractElementHandler { } // globalqss: set AD_Client_ID to the client setted in adempieredata if (getClientId(ctx) > 0 && genericPO.getAD_Client_ID() != getClientId(ctx)) - genericPO.setValue("AD_Client_ID", getClientId(ctx)); + genericPO.set_ValueOfColumn("AD_Client_ID", getClientId(ctx)); // if new. TODO: no defaults for keyXname. if (!d_rowname.equals("") && ((Integer)(genericPO.get_Value(d_tablename+"_ID"))).intValue() == 0) { log.info("new genericPO, table: "+d_tablename+" name:"+d_rowname); - genericPO.setValue("Name", d_rowname); + genericPO.set_ValueOfColumn("Name", d_rowname); // Set defaults. //TODO: get defaults from configuration HashMap defaults = new HashMap(); @@ -185,11 +184,11 @@ public class DataElementHandler extends AbstractElementHandler { while (iter.hasNext()) { thisValue = (ArrayList)iter.next(); if (((String)(thisValue.get(2))).equals("String")) - genericPO.setValue((String)thisValue.get(0), (String)thisValue.get(1)); + genericPO.set_ValueOfColumn((String)thisValue.get(0), (String)thisValue.get(1)); else if (((String)(thisValue.get(2))).equals("Integer")) - genericPO.setValue((String)thisValue.get(0), Integer.valueOf((String)thisValue.get(1))); + genericPO.set_ValueOfColumn((String)thisValue.get(0), Integer.valueOf((String)thisValue.get(1))); else if (((String)(thisValue.get(2))).equals("Boolean")) - genericPO.setValue((String)thisValue.get(0), new Boolean(((String)thisValue.get(1)).equals("true") ? true : false)); + genericPO.set_ValueOfColumn((String)thisValue.get(0), new Boolean(((String)thisValue.get(1)).equals("true") ? true : false)); } } } @@ -235,31 +234,31 @@ public class DataElementHandler extends AbstractElementHandler { || atts.getValue("class").equals("List")|| atts.getValue("class").equals("Yes-No") || atts.getValue("class").equals("Button") || atts.getValue("class").equals("Memo")|| atts.getValue("class").equals("Text Long")) { - genericPO.setValue(atts.getValue("name").toString(), atts.getValue("value").toString()); + genericPO.set_ValueOfColumn(atts.getValue("name").toString(), atts.getValue("value").toString()); } else if (atts.getValue("class").equals("Number") || atts.getValue("class").equals("Amount") || atts.getValue("class").equals("Quantity")|| atts.getValue("class").equals("Costs+Prices")){ - genericPO.setValue(atts.getValue("name").toString(), new BigDecimal(atts.getValue("value"))); + genericPO.set_ValueOfColumn(atts.getValue("name").toString(), new BigDecimal(atts.getValue("value"))); } else if (atts.getValue("class").equals("Integer") || atts.getValue("class").equals("ID") || atts.getValue("class").equals("Table Direct")|| atts.getValue("class").equals("Table") || atts.getValue("class").equals("Location (Address)")|| atts.getValue("class").equals("Account") || atts.getValue("class").equals("Color)")|| atts.getValue("class").equals("Search") || atts.getValue("class").equals("Locator (WH)")|| atts.getValue("class").equals("Product Attribute")) { - genericPO.setValue(atts.getValue("name").toString(), Integer.valueOf(atts.getValue("value"))); + genericPO.set_ValueOfColumn(atts.getValue("name").toString(), Integer.valueOf(atts.getValue("value"))); } else if (atts.getValue("class").equals("Boolean")) { - genericPO.setValue(atts.getValue("name"), new Boolean(atts.getValue("value").equals("true") ? true : false)); + genericPO.set_ValueOfColumn(atts.getValue("name"), new Boolean(atts.getValue("value").equals("true") ? true : false)); } else if (atts.getValue("class").equals("Date") || atts.getValue("class").equals("Date+Time") || atts.getValue("class").equals("Time")) { - genericPO.setValue(atts.getValue("name").toString(), Timestamp.valueOf(atts.getValue("value"))); + genericPO.set_ValueOfColumn(atts.getValue("name").toString(), Timestamp.valueOf(atts.getValue("value"))); }//Binary, Radio, RowID, Image not supported } else { // value is null if (atts.getValue("lookupname") != null && !"".equals(atts.getValue("lookupname"))) { // globalqss - bring support from XML2AD to lookupname String m_tablename = atts.getValue("name").substring(0, atts.getValue("name").length()-3); - genericPO.setValue(atts.getValue("name"), new Integer(getIDbyName(ctx, m_tablename, atts.getValue("lookupname")))); + genericPO.set_ValueOfColumn(atts.getValue("name"), new Integer(getIDbyName(ctx, m_tablename, atts.getValue("lookupname")))); } } } diff --git a/base/src/org/compiere/model/MTable.java b/base/src/org/compiere/model/MTable.java index a1fd69f383..de1b93a6c8 100644 --- a/base/src/org/compiere/model/MTable.java +++ b/base/src/org/compiere/model/MTable.java @@ -22,6 +22,7 @@ import java.sql.*; import java.util.*; import java.util.logging.*; +import org.adempiere.model.GenericPO; import org.compiere.db.CConnection; import org.compiere.interfaces.Server; import org.compiere.util.*; @@ -134,7 +135,8 @@ public class MTable extends X_AD_Table /** Packages for Model Classes */ private static final String[] s_packages = new String[] { "compiere.model", // globalqss allow compatibility with other plugins - "adempiere.model", // Extensions + "adempiere.model", // Extensions + "org.adempiere.model", "org.compiere.model", "org.compiere.wf", "org.compiere.print", "org.compiere.impexp" }; @@ -178,7 +180,7 @@ public class MTable extends X_AD_Table Class cache = s_classCache.get(tableName); if (cache != null) { - //Object.class indicate no PO class for tableName + //Object.class indicate no generated PO class for tableName if (cache.equals(Object.class)) return null; else @@ -440,9 +442,13 @@ public class MTable extends X_AD_Table Class clazz = getClass(tableName); if (clazz == null) { - log.log(Level.WARNING, "(id) - Class not found for " + tableName); - return null; + //log.log(Level.WARNING, "(id) - Class not found for " + tableName); + //return null; + log.log(Level.INFO, "Using GenericPO for " + tableName); + GenericPO po = new GenericPO(tableName, getCtx(), new Integer(Record_ID), trxName); + return po; } + boolean errorLogged = false; try { @@ -501,9 +507,13 @@ public class MTable extends X_AD_Table Class clazz = getClass(tableName); if (clazz == null) { - log.log(Level.SEVERE, "(rs) - Class not found for " + tableName); - return null; + //log.log(Level.SEVERE, "(rs) - Class not found for " + tableName); + //return null; + log.log(Level.INFO, "Using GenericPO for " + tableName); + GenericPO po = new GenericPO(tableName, getCtx(), rs, trxName); + return po; } + boolean errorLogged = false; try { diff --git a/base/src/org/compiere/model/PO.java b/base/src/org/compiere/model/PO.java index f93941a74f..920768a044 100644 --- a/base/src/org/compiere/model/PO.java +++ b/base/src/org/compiere/model/PO.java @@ -1048,7 +1048,7 @@ public abstract class PO * @param from old, existing & unchanged PO * @param to new, not saved PO */ - protected static void copyValues (PO from, PO to) + public static void copyValues (PO from, PO to) { s_log.fine("From ID=" + from.get_ID() + " - To ID=" + to.get_ID()); // Different Classes