|
|
|
|
@ -12,8 +12,14 @@ import java.sql.PreparedStatement;
|
|
|
|
|
import java.sql.SQLException; |
|
|
|
|
import java.sql.Time; |
|
|
|
|
import java.sql.Types; |
|
|
|
|
import java.text.DateFormat; |
|
|
|
|
import java.text.FieldPosition; |
|
|
|
|
import java.text.ParseException; |
|
|
|
|
import java.text.ParsePosition; |
|
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.Hashtable; |
|
|
|
|
import java.util.Random; |
|
|
|
|
import java.util.zip.GZIPInputStream; |
|
|
|
|
|
|
|
|
|
import org.apache.logging.log4j.LogManager; |
|
|
|
|
@ -25,6 +31,30 @@ public class ScheduleProcessor implements Runnable {
|
|
|
|
|
|
|
|
|
|
private File fileName; |
|
|
|
|
private Logger log = LogManager.getLogger(getClass()); |
|
|
|
|
private static TUIDDateFormat tdf = new TUIDDateFormat(); |
|
|
|
|
|
|
|
|
|
public static final String SEQ_ID = "0123456789ABCDEFGHJKLMNPRSTUVWXY"; |
|
|
|
|
private static Random r = new Random(); |
|
|
|
|
private static class TUIDDateFormat extends DateFormat{ |
|
|
|
|
|
|
|
|
|
public static final String MONTH_ID = "MBTQPHSONDUE"; |
|
|
|
|
public static final String DAY_ID = "0123456789ABCDEFGHJKLMNPRSTUVWX"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public StringBuffer format(Date arg0, StringBuffer arg1, FieldPosition arg2) { |
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
arg1.append(MONTH_ID.charAt(arg0.getMonth())); |
|
|
|
|
arg1.append(DAY_ID.charAt(arg0.getDate()-1)); |
|
|
|
|
return arg1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Date parse(String arg0, ParsePosition arg1) { |
|
|
|
|
throw new UnsupportedOperationException(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ScheduleProcessor(File fileName){ |
|
|
|
|
this.fileName = fileName; |
|
|
|
|
@ -41,12 +71,27 @@ public class ScheduleProcessor implements Runnable {
|
|
|
|
|
return sb.toString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Time parseTime(String time) { |
|
|
|
|
private static Hashtable<String, Integer> uidMap = new Hashtable<>(); |
|
|
|
|
|
|
|
|
|
private static synchronized String getTUID(String uid, Date startDate, Date endDate) { |
|
|
|
|
String buid = uid+tdf.format(startDate)+tdf.format(endDate); |
|
|
|
|
int count = 0; |
|
|
|
|
if(uidMap.containsKey(buid)) { |
|
|
|
|
count = uidMap.get(buid); |
|
|
|
|
count +=1; |
|
|
|
|
uidMap.put(buid, count); |
|
|
|
|
}else { |
|
|
|
|
uidMap.put(buid, 0); |
|
|
|
|
} |
|
|
|
|
return buid+SEQ_ID.charAt(count); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private String parseTime(String time) { |
|
|
|
|
try { |
|
|
|
|
int hour = Integer.parseInt(time.substring(0, 2)); |
|
|
|
|
int min = Integer.parseInt(time.substring(2, 4)); |
|
|
|
|
boolean halfMin = time.length()>4 && 'H' == time.charAt(4); |
|
|
|
|
return new Time(hour*3_600_000+min*60_000+(halfMin?30_000:0)); |
|
|
|
|
return hour+":"+min+(halfMin?":30":":00"); |
|
|
|
|
}catch(RuntimeException e) { |
|
|
|
|
log.error("For time \"{}\":{}", time, e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
@ -65,7 +110,7 @@ public class ScheduleProcessor implements Runnable {
|
|
|
|
|
if(time==null||"".equals(time)) { |
|
|
|
|
stmt.setNull(pos, Types.TIME); |
|
|
|
|
}else { |
|
|
|
|
stmt.setTime(pos, parseTime(time)); |
|
|
|
|
stmt.setString(pos, parseTime(time)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -94,16 +139,20 @@ public class ScheduleProcessor implements Runnable {
|
|
|
|
|
if(line==null) { |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
String trainUID = getTUID(); |
|
|
|
|
JSONObject obj = new JSONObject(line);sMain.setString(1, trainUID); |
|
|
|
|
String trainUID; |
|
|
|
|
JSONObject obj = new JSONObject(line); |
|
|
|
|
sMain.setString(2, obj.optString("CIF_train_uid")); |
|
|
|
|
try { |
|
|
|
|
sMain.setDate(3, new java.sql.Date(sdf.parse(obj.optString("schedule_start_date")).getTime())); |
|
|
|
|
sMain.setDate(4, new java.sql.Date(sdf.parse(obj.optString("schedule_end_date")).getTime())); |
|
|
|
|
java.sql.Date startDate = new java.sql.Date(sdf.parse(obj.optString("schedule_start_date")).getTime()); |
|
|
|
|
java.sql.Date endDate = new java.sql.Date(sdf.parse(obj.optString("schedule_end_date")).getTime()); |
|
|
|
|
trainUID = getTUID(obj.optString("CIF_train_uid"), startDate, endDate); |
|
|
|
|
sMain.setDate(3, startDate); |
|
|
|
|
sMain.setDate(4, endDate); |
|
|
|
|
} catch (ParseException e1) { |
|
|
|
|
log.error(e1.getMessage(), e1); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
sMain.setString(1, trainUID); |
|
|
|
|
sMain.setString(5, obj.optString("schedule_days_runs")); |
|
|
|
|
sMain.setString(6, obj.optString("train_status")); |
|
|
|
|
sMain.setString(7, obj.optString("atoc_code")); |
|
|
|
|
@ -165,7 +214,7 @@ public class ScheduleProcessor implements Runnable {
|
|
|
|
|
sError.executeBatch(); |
|
|
|
|
sLoca.executeBatch(); |
|
|
|
|
conn.commit(); |
|
|
|
|
log.info("Batch committed."); |
|
|
|
|
log.info("Batch {} committed.", fileName.getName()); |
|
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
|
log.error(e.getMessage(), e); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|