2 changed files with 246 additions and 1 deletions
@ -1,2 +1,191 @@ |
|||||||
package org.leolo.nrdatad.model;public class ScheduleAssociation { |
package org.leolo.nrdatad.model; |
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager; |
||||||
|
import org.apache.logging.log4j.Logger; |
||||||
|
import org.json.JSONException; |
||||||
|
import org.json.JSONObject; |
||||||
|
import org.leolo.nrdatad.util.TUIDDateFormat; |
||||||
|
|
||||||
|
import java.text.ParseException; |
||||||
|
import java.text.SimpleDateFormat; |
||||||
|
import java.util.Date; |
||||||
|
|
||||||
|
public class ScheduleAssociation { |
||||||
|
private static Logger log = LogManager.getLogger(); |
||||||
|
|
||||||
|
private String mainUid; |
||||||
|
private String assoUid; |
||||||
|
private Date startDate; |
||||||
|
private Date endDate; |
||||||
|
private String assoDays; |
||||||
|
private AssociationCategory associationCategory; |
||||||
|
private int associationDate; |
||||||
|
private String associationLocation; |
||||||
|
private String baseLocationSuffix; |
||||||
|
private String assocLocationSuffix; |
||||||
|
private ShortTermPlanningIndicator stpIndicator; |
||||||
|
|
||||||
|
private TUIDDateFormat tuidDateFormat = new TUIDDateFormat(); |
||||||
|
|
||||||
|
public String getAuid(){ |
||||||
|
return mainUid+assoUid+associationLocation+tuidDateFormat.format(startDate)+tuidDateFormat.format(endDate); |
||||||
|
} |
||||||
|
|
||||||
|
public String getMainUid() { |
||||||
|
return mainUid; |
||||||
|
} |
||||||
|
|
||||||
|
public void setMainUid(String mainUid) { |
||||||
|
this.mainUid = mainUid; |
||||||
|
} |
||||||
|
|
||||||
|
public String getAssoUid() { |
||||||
|
return assoUid; |
||||||
|
} |
||||||
|
|
||||||
|
public void setAssoUid(String assoUid) { |
||||||
|
this.assoUid = assoUid; |
||||||
|
} |
||||||
|
|
||||||
|
public Date getStartDate() { |
||||||
|
return startDate; |
||||||
|
} |
||||||
|
|
||||||
|
public void setStartDate(Date startDate) { |
||||||
|
this.startDate = startDate; |
||||||
|
} |
||||||
|
|
||||||
|
public Date getEndDate() { |
||||||
|
return endDate; |
||||||
|
} |
||||||
|
|
||||||
|
public void setEndDate(Date endDate) { |
||||||
|
this.endDate = endDate; |
||||||
|
} |
||||||
|
|
||||||
|
public String getAssoDays() { |
||||||
|
return assoDays; |
||||||
|
} |
||||||
|
|
||||||
|
public void setAssoDays(String assoDays) { |
||||||
|
this.assoDays = assoDays; |
||||||
|
} |
||||||
|
|
||||||
|
public AssociationCategory getAssociationCategory() { |
||||||
|
return associationCategory; |
||||||
|
} |
||||||
|
|
||||||
|
public void setAssociationCategory(AssociationCategory associationCategory) { |
||||||
|
this.associationCategory = associationCategory; |
||||||
|
} |
||||||
|
|
||||||
|
public int getAssociationDate() { |
||||||
|
return associationDate; |
||||||
|
} |
||||||
|
|
||||||
|
public void setAssociationDate(int associationDate) { |
||||||
|
this.associationDate = associationDate; |
||||||
|
} |
||||||
|
|
||||||
|
public String getAssociationLocation() { |
||||||
|
return associationLocation; |
||||||
|
} |
||||||
|
|
||||||
|
public void setAssociationLocation(String associationLocation) { |
||||||
|
this.associationLocation = associationLocation; |
||||||
|
} |
||||||
|
|
||||||
|
public String getBaseLocationSuffix() { |
||||||
|
return baseLocationSuffix; |
||||||
|
} |
||||||
|
|
||||||
|
public void setBaseLocationSuffix(String baseLocationSuffix) { |
||||||
|
this.baseLocationSuffix = baseLocationSuffix; |
||||||
|
} |
||||||
|
|
||||||
|
public String getAssocLocationSuffix() { |
||||||
|
return assocLocationSuffix; |
||||||
|
} |
||||||
|
|
||||||
|
public void setAssocLocationSuffix(String assocLocationSuffix) { |
||||||
|
this.assocLocationSuffix = assocLocationSuffix; |
||||||
|
} |
||||||
|
|
||||||
|
public ShortTermPlanningIndicator getStpIndicator() { |
||||||
|
return stpIndicator; |
||||||
|
} |
||||||
|
|
||||||
|
public void setStpIndicator(ShortTermPlanningIndicator stpIndicator) { |
||||||
|
this.stpIndicator = stpIndicator; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean equals(Object o) { |
||||||
|
if (this == o) return true; |
||||||
|
if (o == null || getClass() != o.getClass()) return false; |
||||||
|
|
||||||
|
ScheduleAssociation that = (ScheduleAssociation) o; |
||||||
|
|
||||||
|
if (associationDate != that.associationDate) return false; |
||||||
|
if (!mainUid.equals(that.mainUid)) return false; |
||||||
|
if (!assoUid.equals(that.assoUid)) return false; |
||||||
|
if (!startDate.equals(that.startDate)) return false; |
||||||
|
if (!endDate.equals(that.endDate)) return false; |
||||||
|
if (!assoDays.equals(that.assoDays)) return false; |
||||||
|
if (associationCategory != that.associationCategory) return false; |
||||||
|
if (!associationLocation.equals(that.associationLocation)) return false; |
||||||
|
if (baseLocationSuffix != null ? !baseLocationSuffix.equals(that.baseLocationSuffix) : that.baseLocationSuffix != null) |
||||||
|
return false; |
||||||
|
if (assocLocationSuffix != null ? !assocLocationSuffix.equals(that.assocLocationSuffix) : that.assocLocationSuffix != null) |
||||||
|
return false; |
||||||
|
return stpIndicator == that.stpIndicator; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int hashCode() { |
||||||
|
int result = mainUid.hashCode(); |
||||||
|
result = 31 * result + assoUid.hashCode(); |
||||||
|
result = 31 * result + startDate.hashCode(); |
||||||
|
result = 31 * result + endDate.hashCode(); |
||||||
|
result = 31 * result + assoDays.hashCode(); |
||||||
|
result = 31 * result + associationCategory.hashCode(); |
||||||
|
result = 31 * result + associationDate; |
||||||
|
result = 31 * result + associationLocation.hashCode(); |
||||||
|
result = 31 * result + (baseLocationSuffix != null ? baseLocationSuffix.hashCode() : 0); |
||||||
|
result = 31 * result + (assocLocationSuffix != null ? assocLocationSuffix.hashCode() : 0); |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
public static ScheduleAssociation parseJOSN(String obj) throws JSONException { |
||||||
|
return parseJSON(new JSONObject(obj)); |
||||||
|
} |
||||||
|
|
||||||
|
public static ScheduleAssociation parseJSON(JSONObject obj) throws JSONException { |
||||||
|
ScheduleAssociation scheduleAssociation = new ScheduleAssociation(); |
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); |
||||||
|
scheduleAssociation.mainUid=obj.optString("main_train_uid").strip(); |
||||||
|
scheduleAssociation.assoUid=obj.optString("assoc_train_uid").strip(); |
||||||
|
try { |
||||||
|
scheduleAssociation.startDate=sdf.parse(obj.optString("assoc_start_date")); |
||||||
|
scheduleAssociation.endDate=sdf.parse(obj.optString("assoc_end_date")); |
||||||
|
} catch (ParseException e) { |
||||||
|
log.atWarn().withThrowable(e).log("Unable to parse date"); |
||||||
|
} |
||||||
|
scheduleAssociation.assoDays=obj.optString("assoc_days"); |
||||||
|
scheduleAssociation.associationCategory = AssociationCategory.parseCode(obj.optString("category")); |
||||||
|
String dateIndicator = obj.optString("date_indicator"); |
||||||
|
if("N".equals(dateIndicator)){ |
||||||
|
scheduleAssociation.associationDate = 1; |
||||||
|
} else if ("P".equals(dateIndicator)) { |
||||||
|
scheduleAssociation.associationDate = -1; |
||||||
|
}else { |
||||||
|
scheduleAssociation.associationDate = 0; |
||||||
|
} |
||||||
|
scheduleAssociation.associationLocation = obj.optString("location").strip(); |
||||||
|
scheduleAssociation.baseLocationSuffix = obj.optString("base_location_suffix"); |
||||||
|
scheduleAssociation.assocLocationSuffix = obj.optString("assoc_location_suffix"); |
||||||
|
scheduleAssociation.stpIndicator = ShortTermPlanningIndicator.parseCode(obj.optString("CIF_stp_indicator")); |
||||||
|
return scheduleAssociation; |
||||||
|
} |
||||||
|
|
||||||
} |
} |
||||||
|
|||||||
@ -0,0 +1,56 @@ |
|||||||
|
package org.leolo.nrdatad.model; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
import java.text.ParseException; |
||||||
|
import java.text.SimpleDateFormat; |
||||||
|
|
||||||
|
import static org.junit.Assert.*; |
||||||
|
|
||||||
|
public class ScheduleAssociationTest { |
||||||
|
|
||||||
|
@Test public void basicCase() throws ParseException { |
||||||
|
String json = "{\"diagram_type\":\"T\",\"assoc_end_date\":\"2022-12-05T00:00:00Z\",\"assoc_location_suffix\":null," + |
||||||
|
"\"assoc_days\":\"1000000\",\"assoc_train_uid\":\"L77547\",\"transaction_type\":\"Create\"," + |
||||||
|
"\"date_indicator\":\"S\",\"base_location_suffix\":null,\"main_train_uid\":\"L77935\"," + |
||||||
|
"\"CIF_stp_indicator\":\"P\",\"assoc_start_date\":\"2022-05-16T00:00:00Z\"," + |
||||||
|
"\"location\":\"KNGX\",\"category\":\"NP\"}"; |
||||||
|
ScheduleAssociation sa = ScheduleAssociation.parseJOSN(json); |
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); |
||||||
|
assertEquals(sdf.parse("20221205"), sa.getEndDate()); |
||||||
|
assertEquals("", sa.getAssocLocationSuffix()); |
||||||
|
assertEquals("1000000", sa.getAssoDays()); |
||||||
|
assertEquals("L77547", sa.getAssoUid()); |
||||||
|
assertEquals(0, sa.getAssociationDate()); |
||||||
|
assertEquals("", sa.getBaseLocationSuffix()); |
||||||
|
assertEquals("L77935", sa.getMainUid()); |
||||||
|
assertEquals(ShortTermPlanningIndicator.PERMANENT, sa.getStpIndicator()); |
||||||
|
assertEquals(sdf.parse("20220516"), sa.getStartDate()); |
||||||
|
assertEquals("KNGX", sa.getAssociationLocation()); |
||||||
|
assertEquals(AssociationCategory.NEXT, sa.getAssociationCategory()); |
||||||
|
assertEquals("L77935L77547KNGXPFE4", sa.getAuid()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test public void vstpAssoc() throws ParseException { |
||||||
|
String json = "{\"diagram_type\":\"T\",\"assoc_end_date\":\"2022-12-05T00:00:00Z\",\"assoc_location_suffix\":null," + |
||||||
|
"\"assoc_days\":\"1000000\",\"assoc_train_uid\":\" 77547\",\"transaction_type\":\"Create\"," + |
||||||
|
"\"date_indicator\":\"S\",\"base_location_suffix\":null,\"main_train_uid\":\" 77935\"," + |
||||||
|
"\"CIF_stp_indicator\":\"P\",\"assoc_start_date\":\"2022-05-16T00:00:00Z\"," + |
||||||
|
"\"location\":\"KNGX\",\"category\":\"NP\"}"; |
||||||
|
ScheduleAssociation sa = ScheduleAssociation.parseJOSN(json); |
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); |
||||||
|
assertEquals(sdf.parse("20221205"), sa.getEndDate()); |
||||||
|
assertEquals("", sa.getAssocLocationSuffix()); |
||||||
|
assertEquals("1000000", sa.getAssoDays()); |
||||||
|
assertEquals("77547", sa.getAssoUid()); |
||||||
|
assertEquals(0, sa.getAssociationDate()); |
||||||
|
assertEquals("", sa.getBaseLocationSuffix()); |
||||||
|
assertEquals("77935", sa.getMainUid()); |
||||||
|
assertEquals(ShortTermPlanningIndicator.PERMANENT, sa.getStpIndicator()); |
||||||
|
assertEquals(sdf.parse("20220516"), sa.getStartDate()); |
||||||
|
assertEquals("KNGX", sa.getAssociationLocation()); |
||||||
|
assertEquals(AssociationCategory.NEXT, sa.getAssociationCategory()); |
||||||
|
assertEquals("7793577547KNGXPFE4", sa.getAuid()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue