From d41aa83d8c44feda0f0cee3cf7bf599782513f6b Mon Sep 17 00:00:00 2001 From: LO Kam Tao Leo Date: Sun, 18 Dec 2022 16:49:26 +0000 Subject: [PATCH] Loading the train association data --- .../org/leolo/nrdatad/db/TrainAssociationDao.java | 25 ++++ .../db/mariadb/TrainAssociationDaoImpl.java | 162 +++++++++++++++++---- 2 files changed, 161 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/leolo/nrdatad/db/TrainAssociationDao.java b/src/main/java/org/leolo/nrdatad/db/TrainAssociationDao.java index f5d580e..395d89e 100644 --- a/src/main/java/org/leolo/nrdatad/db/TrainAssociationDao.java +++ b/src/main/java/org/leolo/nrdatad/db/TrainAssociationDao.java @@ -3,6 +3,7 @@ package org.leolo.nrdatad.db; import org.leolo.nrdatad.model.ScheduleAssociation; import java.sql.SQLException; +import java.util.Collection; public abstract class TrainAssociationDao extends BaseDao{ @@ -35,4 +36,28 @@ public abstract class TrainAssociationDao extends BaseDao{ public final int getHashCode(ScheduleAssociation scheduleAssociation) throws SQLException{ return getHashCode(scheduleAssociation.getAuid()); } + + public void insertAll(Collection scheduleAssociations) throws SQLException{ + for(ScheduleAssociation scheduleAssociation:scheduleAssociations){ + insert(scheduleAssociation); + } + } + + public void updateAll(Collection scheduleAssociations) throws SQLException{ + for (ScheduleAssociation scheduleAssociation:scheduleAssociations){ + update(scheduleAssociation); + } + } + + public void updateAllLastCheck(Collection scheduleAssociations) throws SQLException{ + for (ScheduleAssociation scheduleAssociation:scheduleAssociations){ + updateLastCheck(scheduleAssociation); + } + } + + public void replaceAll(Collection scheduleAssociations) throws SQLException{ + for (ScheduleAssociation scheduleAssociation:scheduleAssociations){ + replace(scheduleAssociation); + } + } } diff --git a/src/main/java/org/leolo/nrdatad/db/mariadb/TrainAssociationDaoImpl.java b/src/main/java/org/leolo/nrdatad/db/mariadb/TrainAssociationDaoImpl.java index 3209516..e1a704d 100644 --- a/src/main/java/org/leolo/nrdatad/db/mariadb/TrainAssociationDaoImpl.java +++ b/src/main/java/org/leolo/nrdatad/db/mariadb/TrainAssociationDaoImpl.java @@ -1,5 +1,7 @@ package org.leolo.nrdatad.db.mariadb; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.leolo.nrdatad.db.DatabaseManager; import org.leolo.nrdatad.db.TrainAssociationDao; import org.leolo.nrdatad.exception.NoSuchRecordException; @@ -9,9 +11,12 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.Collection; public class TrainAssociationDaoImpl extends TrainAssociationDao { + private Logger log = LogManager.getLogger(); + public TrainAssociationDaoImpl(DatabaseManager manager) { super(manager); } @@ -26,19 +31,7 @@ public class TrainAssociationDaoImpl extends TrainAssociationDao { "asso_suffix, stp_ind, hash_code, last_chk) " + //11-14 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())") ){ - setString(pstmt, 1, scheduleAssociation.getAuid()); - setString(pstmt, 2, scheduleAssociation.getMainUid()); - setString(pstmt, 3, scheduleAssociation.getAssoUid()); - setDate (pstmt, 4, scheduleAssociation.getStartDate()); - setDate (pstmt, 5, scheduleAssociation.getEndDate()); - setString(pstmt, 6, scheduleAssociation.getAssoDays()); - setString(pstmt, 7, scheduleAssociation.getAssociationCategory().getCode()); - setInt (pstmt, 8, scheduleAssociation.getAssociationDate()); - setString(pstmt, 9, scheduleAssociation.getAssociationLocation()); - setString(pstmt, 10, scheduleAssociation.getBaseLocationSuffix()); - setString(pstmt, 11, scheduleAssociation.getAssocLocationSuffix()); - setString(pstmt, 12, scheduleAssociation.getStpIndicator().getCode()); - setInt (pstmt, 13, scheduleAssociation.hashCode()); + fillInsertPreparedStatement(pstmt, scheduleAssociation); pstmt.executeUpdate(); conn.commit(); } @@ -66,19 +59,7 @@ public class TrainAssociationDaoImpl extends TrainAssociationDao { "asso_type=?,date_ind=?,asso_loc=?,base_suffix=?, asso_suffix=?," + "stp_ind=?, hash_code=?,last_chk=NOW() WHERE auid=?") ){ - setString(pstmt, 1, scheduleAssociation.getMainUid()); - setString(pstmt, 2, scheduleAssociation.getAssoUid()); - setDate (pstmt, 3, scheduleAssociation.getStartDate()); - setDate (pstmt, 4, scheduleAssociation.getEndDate()); - setString(pstmt, 5, scheduleAssociation.getAssoDays()); - setString(pstmt, 6, scheduleAssociation.getAssociationCategory().getCode()); - setInt (pstmt, 7, scheduleAssociation.getAssociationDate()); - setString(pstmt, 8, scheduleAssociation.getAssociationLocation()); - setString(pstmt, 9, scheduleAssociation.getBaseLocationSuffix()); - setString(pstmt, 10, scheduleAssociation.getAssocLocationSuffix()); - setString(pstmt, 11, scheduleAssociation.getStpIndicator().getCode()); - setInt (pstmt, 12, scheduleAssociation.hashCode()); - setString(pstmt, 13, scheduleAssociation.getAuid()); + fillUpdatePreparedStatement(pstmt, scheduleAssociation); pstmt.executeUpdate(); conn.commit(); } @@ -134,4 +115,133 @@ public class TrainAssociationDaoImpl extends TrainAssociationDao { } return null; } + + @Override + public void insertAll(Collection scheduleAssociations) throws SQLException { + try ( + Connection conn = getConnection(); + PreparedStatement pstmt = conn.prepareStatement("INSERT INTO train_association (" + + "auid, main_uid, asso_uid, start_date, end_date, " + //1-5 + "asso_days, asso_type, date_ind, asso_loc, base_suffix, " + //6-10 + "asso_suffix, stp_ind, hash_code, last_chk) " + //11-14 + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())") + ){ + for(ScheduleAssociation scheduleAssociation:scheduleAssociations) { + fillInsertPreparedStatement(pstmt, scheduleAssociation); + pstmt.addBatch(); + } + pstmt.executeBatch(); + conn.commit(); + } + } + + @Override + public void updateAll(Collection scheduleAssociations) throws SQLException { + try( + Connection conn = getConnection(); + PreparedStatement pstmt = conn.prepareStatement("UPDATE train_association SET " + + "main_uid=?, asso_uid=?, start_date=?, end_date=?, asso_days=?," + + "asso_type=?,date_ind=?,asso_loc=?,base_suffix=?, asso_suffix=?," + + "stp_ind=?, hash_code=?,last_chk=NOW() WHERE auid=?") + ){ + for(ScheduleAssociation scheduleAssociation:scheduleAssociations) { + fillUpdatePreparedStatement(pstmt, scheduleAssociation); + pstmt.addBatch(); + } + pstmt.executeBatch(); + conn.commit(); + } + } + + private void fillUpdatePreparedStatement(PreparedStatement pstmt, ScheduleAssociation scheduleAssociation) throws SQLException { + setString(pstmt, 1, scheduleAssociation.getMainUid()); + setString(pstmt, 2, scheduleAssociation.getAssoUid()); + setDate(pstmt, 3, scheduleAssociation.getStartDate()); + setDate(pstmt, 4, scheduleAssociation.getEndDate()); + setString(pstmt, 5, scheduleAssociation.getAssoDays()); + setString(pstmt, 6, scheduleAssociation.getAssociationCategory().getCode()); + setInt(pstmt, 7, scheduleAssociation.getAssociationDate()); + setString(pstmt, 8, scheduleAssociation.getAssociationLocation()); + setString(pstmt, 9, scheduleAssociation.getBaseLocationSuffix()); + setString(pstmt, 10, scheduleAssociation.getAssocLocationSuffix()); + setString(pstmt, 11, scheduleAssociation.getStpIndicator().getCode()); + setInt(pstmt, 12, scheduleAssociation.hashCode()); + setString(pstmt, 13, scheduleAssociation.getAuid()); + } + + @Override + public void updateAllLastCheck(Collection scheduleAssociations) throws SQLException { + try( + Connection conn = getConnection(); + PreparedStatement pstmt = conn.prepareStatement("UPDATE train_association SET last_chk = NOW() WHERE auid = ?") + ){ + for(ScheduleAssociation scheduleAssociation:scheduleAssociations) { + setString(pstmt, 1, scheduleAssociation.getAuid()); + pstmt.addBatch(); + } + pstmt.executeBatch(); + conn.commit(); + } + } + + @Override + public void replaceAll(Collection scheduleAssociations) throws SQLException { + int insCount = 0; + int updCount = 0; + int nopCount = 0; + try ( + Connection conn = getConnection(); + PreparedStatement psIns = conn.prepareStatement("INSERT INTO train_association (" + + "auid, main_uid, asso_uid, start_date, end_date, " + //1-5 + "asso_days, asso_type, date_ind, asso_loc, base_suffix, " + //6-10 + "asso_suffix, stp_ind, hash_code, last_chk) " + //11-14 + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())"); + PreparedStatement psUpd = conn.prepareStatement("UPDATE train_association SET " + + "main_uid=?, asso_uid=?, start_date=?, end_date=?, asso_days=?," + + "asso_type=?,date_ind=?,asso_loc=?,base_suffix=?, asso_suffix=?," + + "stp_ind=?, hash_code=?,last_chk=NOW() WHERE auid=?"); + + PreparedStatement psNop = conn.prepareStatement("UPDATE train_association SET last_chk = NOW() WHERE auid = ?") + ){ + for(ScheduleAssociation scheduleAssociation:scheduleAssociations){ + Object checkResult = _getHashCode(scheduleAssociation.getAuid()); + if(checkResult==null){ + //Insert + fillInsertPreparedStatement(psIns, scheduleAssociation); + psIns.addBatch(); + insCount++; + } else if (((int)checkResult) == scheduleAssociation.hashCode()){ + //NOP + setString(psNop, 1, scheduleAssociation.getAuid()); + psNop.addBatch(); + nopCount++; + } else { + //Update + fillUpdatePreparedStatement(psUpd, scheduleAssociation); + psUpd.addBatch(); + updCount++; + } + } + psIns.executeBatch(); + psNop.executeBatch(); + psUpd.executeBatch(); + log.atDebug().log("Batch info: {} Inserted. {} Updated. {} NOP.", insCount, updCount, nopCount); + } + } + + private void fillInsertPreparedStatement(PreparedStatement psIns, ScheduleAssociation scheduleAssociation) throws SQLException { + setString(psIns, 1, scheduleAssociation.getAuid()); + setString(psIns, 2, scheduleAssociation.getMainUid()); + setString(psIns, 3, scheduleAssociation.getAssoUid()); + setDate(psIns, 4, scheduleAssociation.getStartDate()); + setDate(psIns, 5, scheduleAssociation.getEndDate()); + setString(psIns, 6, scheduleAssociation.getAssoDays()); + setString(psIns, 7, scheduleAssociation.getAssociationCategory().getCode()); + setInt(psIns, 8, scheduleAssociation.getAssociationDate()); + setString(psIns, 9, scheduleAssociation.getAssociationLocation()); + setString(psIns, 10, scheduleAssociation.getBaseLocationSuffix()); + setString(psIns, 11, scheduleAssociation.getAssocLocationSuffix()); + setString(psIns, 12, scheduleAssociation.getStpIndicator().getCode()); + setInt(psIns, 13, scheduleAssociation.hashCode()); + } }