You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.2 KiB
93 lines
3.2 KiB
package org.leolo.rail; |
|
|
|
import java.io.BufferedReader; |
|
import java.io.File; |
|
import java.io.FileInputStream; |
|
import java.io.FileNotFoundException; |
|
import java.io.IOException; |
|
import java.io.InputStreamReader; |
|
import java.sql.Connection; |
|
import java.sql.PreparedStatement; |
|
import java.sql.SQLException; |
|
import java.sql.Types; |
|
import java.text.ParseException; |
|
import java.text.SimpleDateFormat; |
|
import java.util.zip.GZIPInputStream; |
|
|
|
import org.apache.logging.log4j.LogManager; |
|
import org.apache.logging.log4j.Logger; |
|
import org.json.JSONObject; |
|
import org.leolo.rail.util.TUIDDateFormat; |
|
|
|
public class AssoicationProcessor extends BaseProcessor implements Runnable { |
|
|
|
private File targetFile; |
|
private Logger log = LogManager.getLogger(AssoicationProcessor.class); |
|
|
|
public AssoicationProcessor(File targetFile) { |
|
this.targetFile = targetFile; |
|
} |
|
|
|
@Override |
|
public void run() { |
|
log.atInfo().log("Processing {}", targetFile.getName()); |
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); |
|
TUIDDateFormat tdf = new TUIDDateFormat(); |
|
try( |
|
BufferedReader br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(targetFile)))); |
|
Connection conn = DatabaseManager.getInstance().getConnection(); |
|
PreparedStatement pstmt = conn.prepareStatement("REPLACE INTO ltp_assoication VALUES(?,?,?,?,?,?,?,?,?,?,?,?)") |
|
){ |
|
while(true) { |
|
String line = br.readLine(); |
|
if(line==null) { |
|
break; |
|
} |
|
JSONObject obj = new JSONObject(line); |
|
String mainTrainUID = obj.optString("main_train_uid"); |
|
String assocTrainUID = obj.optString("assoc_train_uid"); |
|
java.sql.Date startDate, endDate; |
|
try { |
|
startDate = new java.sql.Date(sdf.parse(obj.optString("assoc_start_date")).getTime()); |
|
endDate = new java.sql.Date(sdf.parse(obj.optString("assoc_end_date")).getTime()); |
|
} catch (ParseException e1) { |
|
log.warn("Unable to parse date! {}", e1.getMessage(), e1); |
|
continue; |
|
} |
|
String assoc_days = obj.optString("assoc_days"); |
|
String assoc_type = obj.optString("category"); |
|
String location = obj.optString("location"); |
|
String base_suffix = obj.optString("base_location_suffix"); |
|
String assoc_suffix = obj.optString("assoc_location_suffix"); |
|
String stp_ind = obj.optString("CIF_stp_indicator"); |
|
String auid = mainTrainUID + assocTrainUID + location + tdf.format(startDate) + tdf.format(endDate)+stp_ind; |
|
int hashCode = (auid+assoc_type+assoc_days).hashCode(); |
|
setString(pstmt, 1, auid); |
|
setString(pstmt, 2, mainTrainUID); |
|
setString(pstmt, 3, assocTrainUID); |
|
pstmt.setDate(4, startDate); |
|
pstmt.setDate(5, endDate); |
|
setString(pstmt, 6, assoc_days); |
|
setString(pstmt, 7, assoc_type); |
|
setString(pstmt, 8, location); |
|
setString(pstmt, 9, base_suffix); |
|
setString(pstmt, 10, assoc_suffix); |
|
setString(pstmt, 11, stp_ind); |
|
pstmt.setInt(12, hashCode); |
|
pstmt.addBatch(); |
|
} |
|
pstmt.executeBatch(); |
|
conn.commit(); |
|
} catch (FileNotFoundException e) { |
|
log.error(e.getMessage(), e); |
|
} catch (IOException e) { |
|
log.error(e.getMessage(), e); |
|
} catch (SQLException e) { |
|
log.error(e.getMessage(), e); |
|
} |
|
if(!targetFile.delete()) { |
|
log.warn("Unable to delete {}", targetFile.getName()); |
|
} |
|
} |
|
|
|
}
|
|
|