3 changed files with 194 additions and 1 deletions
@ -1,15 +1,193 @@ |
|||||||
package org.leolo.rail.job; |
package org.leolo.rail.job; |
||||||
|
|
||||||
|
import java.sql.Connection; |
||||||
|
import java.sql.PreparedStatement; |
||||||
|
import java.sql.ResultSet; |
||||||
|
import java.sql.SQLException; |
||||||
|
import java.sql.Statement; |
||||||
|
import java.text.SimpleDateFormat; |
||||||
|
import java.util.Calendar; |
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager; |
||||||
|
import org.apache.logging.log4j.Logger; |
||||||
|
import org.leolo.rail.Constants; |
||||||
|
import org.leolo.rail.DatabaseManager; |
||||||
import org.quartz.Job; |
import org.quartz.Job; |
||||||
import org.quartz.JobExecutionContext; |
import org.quartz.JobExecutionContext; |
||||||
import org.quartz.JobExecutionException; |
import org.quartz.JobExecutionException; |
||||||
|
|
||||||
public class DayEndJob implements Job { |
public class DayEndJob implements Job { |
||||||
|
private Logger log = LogManager.getLogger(DayEndJob.class); |
||||||
|
|
||||||
@Override |
@Override |
||||||
public void execute(JobExecutionContext context) throws JobExecutionException { |
public void execute(JobExecutionContext context) throws JobExecutionException { |
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
log.atInfo().log("Start day-end job"); |
||||||
|
try(Connection conn = DatabaseManager.getInstance().getConnection()){ |
||||||
|
Calendar c = Calendar.getInstance(); |
||||||
|
c.add(Calendar.DAY_OF_MONTH, -1*Constants.Generic.DATA_RETAINTION_PERIOD); |
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
||||||
|
log.atInfo().log("Removing data on or before {}", sdf.format(c.getTime())); |
||||||
|
String cutoffDate = sdf.format(c.getTime()); |
||||||
|
//Delete LTP main records
|
||||||
|
try( |
||||||
|
PreparedStatement psQuery = conn.prepareStatement("SELECT suid FROM ltp_schedule WHERE end_date < ?"); |
||||||
|
){ |
||||||
|
psQuery.setString(1, cutoffDate); |
||||||
|
try( |
||||||
|
ResultSet rs = psQuery.executeQuery(); |
||||||
|
PreparedStatement psLoc = conn.prepareStatement("DELETE FROM ltp_location WHERE suid = ?"); |
||||||
|
PreparedStatement psSch = conn.prepareStatement("DELETE FROM ltp_schedule WHERE suid = ?"); |
||||||
|
){ |
||||||
|
int schCount = 0; |
||||||
|
while(rs.next()) { |
||||||
|
psLoc.setString(1, rs.getString(1)); |
||||||
|
psSch.setString(1, rs.getString(1)); |
||||||
|
psLoc.executeUpdate(); |
||||||
|
psSch.executeUpdate(); |
||||||
|
conn.commit(); |
||||||
|
schCount++; |
||||||
|
} |
||||||
|
log.atInfo().log("Deleted {} LTP schedules.", schCount); |
||||||
|
} |
||||||
|
} catch (SQLException e) { |
||||||
|
log.atError().log(e.getMessage(), e); |
||||||
|
}//Delete LTP main records
|
||||||
|
try( |
||||||
|
PreparedStatement psQuery = conn.prepareStatement("SELECT suid FROM stp_schedule WHERE end_date < ?"); |
||||||
|
){ |
||||||
|
psQuery.setString(1, cutoffDate); |
||||||
|
try( |
||||||
|
ResultSet rs = psQuery.executeQuery(); |
||||||
|
PreparedStatement psLoc = conn.prepareStatement("DELETE FROM stp_location WHERE suid = ?"); |
||||||
|
PreparedStatement psSch = conn.prepareStatement("DELETE FROM stp_schedule WHERE suid = ?"); |
||||||
|
){ |
||||||
|
int schCount = 0; |
||||||
|
while(rs.next()) { |
||||||
|
psLoc.setString(1, rs.getString(1)); |
||||||
|
psSch.setString(1, rs.getString(1)); |
||||||
|
psLoc.executeUpdate(); |
||||||
|
psSch.executeUpdate(); |
||||||
|
conn.commit(); |
||||||
|
schCount++; |
||||||
|
} |
||||||
|
log.atInfo().log("Deleted {} STP schedules.", schCount); |
||||||
|
} |
||||||
|
} catch (SQLException e) { |
||||||
|
log.atError().log(e.getMessage(), e); |
||||||
|
} |
||||||
|
//Delete LTP association records
|
||||||
|
try( |
||||||
|
PreparedStatement psQuery = conn.prepareStatement("SELECT auid FROM ltp_assoication WHERE end_date < ?"); |
||||||
|
){ |
||||||
|
psQuery.setString(1, cutoffDate); |
||||||
|
try( |
||||||
|
ResultSet rs = psQuery.executeQuery(); |
||||||
|
PreparedStatement psSch = conn.prepareStatement("DELETE FROM ltp_assoication WHERE auid = ?"); |
||||||
|
){ |
||||||
|
int assoCount = 0; |
||||||
|
while(rs.next()) { |
||||||
|
psSch.setString(1, rs.getString(1)); |
||||||
|
psSch.executeUpdate(); |
||||||
|
conn.commit(); |
||||||
|
assoCount++; |
||||||
|
} |
||||||
|
log.atInfo().log("Deleted {} assocication.", assoCount); |
||||||
|
} |
||||||
|
} catch (SQLException e) { |
||||||
|
log.atError().log(e.getMessage(), e); |
||||||
|
} |
||||||
|
//Delete Movements
|
||||||
|
try( |
||||||
|
PreparedStatement psQuery = conn.prepareStatement("SELECT DISTINCT train_id FROM current_train_movement WHERE movt_time < ?"); |
||||||
|
){ |
||||||
|
psQuery.setString(1, cutoffDate); |
||||||
|
int movtCount = 0; |
||||||
|
try( |
||||||
|
ResultSet rs = psQuery.executeQuery(); |
||||||
|
PreparedStatement psDel = conn.prepareStatement("DELETE FROM current_train_movement WHERE train_id = ? "); |
||||||
|
){ |
||||||
|
while(rs.next()) { |
||||||
|
psDel.setString(1, rs.getString(1)); |
||||||
|
psDel.executeUpdate(); |
||||||
|
conn.commit(); |
||||||
|
movtCount++; |
||||||
|
} |
||||||
|
log.atInfo().log("Deleted movement records for {} trains.", movtCount); |
||||||
|
|
||||||
|
} |
||||||
|
}catch (SQLException e) { |
||||||
|
log.atError().log(e.getMessage(), e); |
||||||
|
} |
||||||
|
//Delete cancellation
|
||||||
|
try( |
||||||
|
PreparedStatement psQuery = conn.prepareStatement("SELECT DISTINCT train_id FROM current_train_cancellation WHERE canx_time < ?"); |
||||||
|
){ |
||||||
|
psQuery.setString(1, cutoffDate); |
||||||
|
int movtCount = 0; |
||||||
|
try( |
||||||
|
ResultSet rs = psQuery.executeQuery(); |
||||||
|
PreparedStatement psDel = conn.prepareStatement("DELETE FROM current_train_cancellation WHERE train_id = ? "); |
||||||
|
){ |
||||||
|
while(rs.next()) { |
||||||
|
psDel.setString(1, rs.getString(1)); |
||||||
|
psDel.executeUpdate(); |
||||||
|
conn.commit(); |
||||||
|
movtCount++; |
||||||
|
} |
||||||
|
log.atInfo().log("Deleted cancellation records for {} trains.", movtCount); |
||||||
|
|
||||||
|
} |
||||||
|
}catch (SQLException e) { |
||||||
|
log.atError().log(e.getMessage(), e); |
||||||
|
} |
||||||
|
//Delete Movements
|
||||||
|
try( |
||||||
|
PreparedStatement psQuery = conn.prepareStatement("SELECT DISTINCT train_id FROM current_train_reinstatement WHERE rein_time < ?"); |
||||||
|
){ |
||||||
|
psQuery.setString(1, cutoffDate); |
||||||
|
int movtCount = 0; |
||||||
|
try( |
||||||
|
ResultSet rs = psQuery.executeQuery(); |
||||||
|
PreparedStatement psDel = conn.prepareStatement("DELETE FROM current_train_reinstatement WHERE train_id = ? "); |
||||||
|
){ |
||||||
|
while(rs.next()) { |
||||||
|
psDel.setString(1, rs.getString(1)); |
||||||
|
psDel.executeUpdate(); |
||||||
|
conn.commit(); |
||||||
|
movtCount++; |
||||||
|
} |
||||||
|
log.atInfo().log("Deleted reinstatement records for {} trains.", movtCount); |
||||||
|
|
||||||
|
} |
||||||
|
}catch (SQLException e) { |
||||||
|
log.atError().log(e.getMessage(), e); |
||||||
|
} |
||||||
|
//Delete activation
|
||||||
|
try( |
||||||
|
PreparedStatement psQuery = conn.prepareStatement("SELECT DISTINCT train_id FROM current_train WHERE op_date < ?"); |
||||||
|
){ |
||||||
|
psQuery.setString(1, cutoffDate); |
||||||
|
int movtCount = 0; |
||||||
|
try( |
||||||
|
ResultSet rs = psQuery.executeQuery(); |
||||||
|
PreparedStatement psDel = conn.prepareStatement("DELETE FROM current_train WHERE train_id = ? "); |
||||||
|
){ |
||||||
|
while(rs.next()) { |
||||||
|
psDel.setString(1, rs.getString(1)); |
||||||
|
psDel.executeUpdate(); |
||||||
|
conn.commit(); |
||||||
|
movtCount++; |
||||||
|
} |
||||||
|
log.atInfo().log("Deleted activation records for {} trains.", movtCount); |
||||||
|
|
||||||
|
} |
||||||
|
}catch (SQLException e) { |
||||||
|
log.atError().log(e.getMessage(), e); |
||||||
|
} |
||||||
|
} catch (SQLException e) { |
||||||
|
log.atError().log(e.getMessage(), e); |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue