Search in sources :

Example 1 with X_AD_HouseKeeping

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;
}
Also used : AdempiereSystemError(org.compiere.util.AdempiereSystemError) GenericPO(org.adempiere.model.GenericPO) FileWriter(java.io.FileWriter) X_AD_HouseKeeping(org.compiere.model.X_AD_HouseKeeping) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Date(java.util.Date) MTable(org.compiere.model.MTable) ResultSet(java.sql.ResultSet) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

FileWriter (java.io.FileWriter)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Timestamp (java.sql.Timestamp)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 GenericPO (org.adempiere.model.GenericPO)1 MTable (org.compiere.model.MTable)1 X_AD_HouseKeeping (org.compiere.model.X_AD_HouseKeeping)1 AdempiereSystemError (org.compiere.util.AdempiereSystemError)1