use of org.adempiere.model.GenericPO in project adempiere by adempiere.
the class MTable method getPO.
// getKeyColumns
/**************************************************************************
* Get PO Class Instance
* @param Record_ID record
* @param trxName
* @return PO for Record or null
*/
public PO getPO(int Record_ID, String trxName) {
String tableName = getTableName();
if (Record_ID != 0 && !isSingleKey()) {
log.log(Level.WARNING, "(id) - Multi-Key " + tableName);
return null;
}
Class<?> clazz = getClass(tableName);
if (clazz == 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 {
Constructor<?> constructor = null;
try {
constructor = clazz.getDeclaredConstructor(new Class[] { Properties.class, int.class, String.class });
} catch (Exception e) {
String msg = e.getMessage();
if (msg == null)
msg = e.toString();
log.warning("No transaction Constructor for " + clazz + " (" + msg + ")");
}
PO po = (PO) constructor.newInstance(new Object[] { getCtx(), new Integer(Record_ID), trxName });
if (po != null && po.get_ID() != Record_ID && Record_ID > 0)
return null;
return po;
} catch (Exception e) {
if (e.getCause() != null) {
Throwable t = e.getCause();
log.log(Level.SEVERE, "(id) - Table=" + tableName + ",Class=" + clazz, t);
errorLogged = true;
if (t instanceof Exception)
log.saveError("Error", (Exception) e.getCause());
else
log.saveError("Error", "Table=" + tableName + ",Class=" + clazz);
} else {
log.log(Level.SEVERE, "(id) - Table=" + tableName + ",Class=" + clazz, e);
errorLogged = true;
log.saveError("Error", "Table=" + tableName + ",Class=" + clazz);
}
}
if (!errorLogged)
log.log(Level.SEVERE, "(id) - Not found - Table=" + tableName + ", Record_ID=" + Record_ID);
return null;
}
use of org.adempiere.model.GenericPO in project adempiere by adempiere.
the class PO method insertTranslations.
// afterDelete
/**
* Insert (missing) Translation Records
* @return false if error (true if no translation or success)
*/
public boolean insertTranslations() {
// Not a translation table
if (m_IDs.length > 1 || m_IDs[0].equals(I_ZERO) || !p_info.isTranslated() || !(m_IDs[0] instanceof Integer))
return true;
//
ArrayList<String> tColumns = new ArrayList<String>();
for (int i = 0; i < p_info.getColumnCount(); i++) {
if (p_info.isColumnTranslated(i)) {
tColumns.add(p_info.getColumnName(i));
}
}
// Valid translation Columns
if (tColumns.size() == 0)
return true;
// Get Available languages
MLanguage[] langs = MLanguage.getMaintainLanguage(getCtx());
// Valid Languages
if (langs.length == 0)
return true;
// Get Keys
String tableName = p_info.getTableName() + "_Trl";
String keyColumn = m_KeyColumns[0];
// Verify if Table Exist
MTable table = MTable.get(getCtx(), tableName);
if (table == null) {
log.warning("Table [" + tableName + "] Not found");
return false;
}
// Save by Language
int no = 0;
for (MLanguage language : langs) {
GenericPO po = new GenericPO(tableName, getCtx(), -1);
// Set Values
po.set_Value(keyColumn, get_ID());
po.set_Value("IsTranslated", false);
po.set_Value("AD_Language", language.getAD_Language());
po.setAD_Client_ID(getAD_Client_ID());
po.setAD_Org_ID(0);
// Add Translation Column
for (String translationColumn : tColumns) {
Object value = get_Value(translationColumn);
po.set_Value(translationColumn, value);
}
// Save
po.saveEx(m_trxName);
// Increment
no++;
}
// Log
log.fine("#" + no);
return no >= 0;
}
use of org.adempiere.model.GenericPO in project adempiere by adempiere.
the class HouseKeeping method doIt.
//prepare
protected String doIt() throws Exception {
X_AD_HouseKeeping houseKeeping = new X_AD_HouseKeeping(getCtx(), p_AD_HouseKeeping_ID, get_TrxName());
int tableID = houseKeeping.getAD_Table_ID();
MTable table = new MTable(getCtx(), tableID, get_TrxName());
String tableName = table.getTableName();
String whereClause = houseKeeping.getWhereClause();
int noins = 0;
int noexp = 0;
int nodel = 0;
if (houseKeeping.isSaveInHistoric()) {
String sql = "INSERT INTO hst_" + tableName + " SELECT * FROM " + tableName;
if (whereClause != null && whereClause.length() > 0)
sql = sql + " WHERE " + whereClause;
noins = DB.executeUpdate(sql, get_TrxName());
if (noins == -1)
throw new AdempiereSystemError("Cannot insert into hst_" + tableName);
addLog("@Inserted@ " + noins);
}
//saveInHistoric
Date date = new Date();
if (houseKeeping.isExportXMLBackup()) {
String pathFile = houseKeeping.getBackupFolder();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String dateString = dateFormat.format(date);
FileWriter file = new FileWriter(pathFile + File.separator + tableName + dateString + ".xml");
String sql = "SELECT * FROM " + tableName;
if (whereClause != null && whereClause.length() > 0)
sql = sql + " WHERE " + whereClause;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer linexml = null;
try {
pstmt = DB.prepareStatement(sql, get_TrxName());
rs = pstmt.executeQuery();
while (rs.next()) {
GenericPO po = new GenericPO(tableName, getCtx(), rs, get_TrxName());
linexml = po.get_xmlString(linexml);
noexp++;
}
if (linexml != null)
file.write(linexml.toString());
file.close();
} catch (Exception e) {
throw e;
} finally {
DB.close(rs, pstmt);
pstmt = null;
rs = null;
}
addLog("@Exported@ " + noexp);
}
//XmlExport
String sql = "DELETE FROM " + tableName;
if (whereClause != null && whereClause.length() > 0)
sql = sql + " WHERE " + whereClause;
nodel = DB.executeUpdate(sql, get_TrxName());
if (nodel == -1)
throw new AdempiereSystemError("Cannot delete from " + tableName);
Timestamp time = new Timestamp(date.getTime());
houseKeeping.setLastRun(time);
houseKeeping.setLastDeleted(nodel);
houseKeeping.saveEx();
addLog("@Deleted@ " + nodel);
String msg = Msg.translate(getCtx(), tableName + "_ID") + " #" + nodel;
return msg;
}
use of org.adempiere.model.GenericPO in project adempiere by adempiere.
the class MTable method getPO.
// getPO
/**
* Get PO Class Instance
* @param rs result set
* @param trxName transaction
* @return PO for Record or null
*/
public PO getPO(ResultSet rs, String trxName) {
String tableName = getTableName();
Class<?> clazz = getClass(tableName);
if (clazz == 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 {
Constructor<?> constructor = clazz.getDeclaredConstructor(new Class[] { Properties.class, ResultSet.class, String.class });
PO po = (PO) constructor.newInstance(new Object[] { getCtx(), rs, trxName });
return po;
} catch (Exception e) {
log.log(Level.SEVERE, "(rs) - Table=" + tableName + ",Class=" + clazz, e);
errorLogged = true;
log.saveError("Error", "Table=" + tableName + ",Class=" + clazz);
}
if (!errorLogged)
log.log(Level.SEVERE, "(rs) - Not found - Table=" + tableName);
return null;
}
Aggregations