use of org.compiere.model.X_AD_HouseKeeping 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;
}
Aggregations