Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:camsys/onebusaway-nyc

  • Loading branch information...
commit 3e9ee98a76903368d8dd76ee81014f01ff6b30f1 2 parents fdbeb49 + 73d9797
@asutula asutula authored
View
52 ...c-tdm-adapters/src/main/java/org/onebusaway/nyc/transit_data_manager/adapters/api/processes/UTSUtil.java
@@ -0,0 +1,52 @@
+package org.onebusaway.nyc.transit_data_manager.adapters.api.processes;
+
+import org.onebusaway.nyc.transit_data_manager.adapters.ModelCounterpartConverter;
+import org.onebusaway.nyc.transit_data_manager.adapters.output.model.json.OperatorAssignment;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import tcip_final_3_0_5_1.SCHOperatorAssignment;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+
+public class UTSUtil {
+
+ private static Logger _log = LoggerFactory.getLogger(UTSUtil.class);
+
+ public List<OperatorAssignment> listConvertOpAssignTcipToJson(
+ ModelCounterpartConverter<SCHOperatorAssignment, OperatorAssignment> conv,
+ List<SCHOperatorAssignment> inputAssigns) {
+ LinkedHashMap<String, OperatorAssignment> passMap = new LinkedHashMap<String, OperatorAssignment>();
+
+ _log.debug("About to convert " + inputAssigns.size() + " SCHOperatorAssignments to OperatorAssignment using " + conv.getClass().getName());
+ List<OperatorAssignment> outputAssigns = new ArrayList<OperatorAssignment>();
+
+ for (SCHOperatorAssignment assignment : inputAssigns) {
+ //outputAssigns.add(conv.convert(assignment));
+ OperatorAssignment oa = conv.convert(assignment);
+ String pass = oa.getPassId();
+ if (passMap.containsKey(pass)) {
+ // we have a collision, decide if this record is newer than the stored record
+ // TODO
+ OperatorAssignment existing = passMap.get(pass);
+ if (oa.compareTo(existing) > 0) { // if newer (larger epoch delta)
+ passMap.remove(existing);
+ passMap.put(pass, oa);
+ }
+ } else {
+ passMap.put(pass, oa);
+ }
+ }
+
+ for (OperatorAssignment oa : passMap.values()) {
+ outputAssigns.add(oa);
+ }
+
+ _log.debug("Done converting operatorassignments to tcip.");
+ return outputAssigns;
+
+ }
+}
View
16 ...va/org/onebusaway/nyc/transit_data_manager/adapters/api/processes/UtsCrewAssignsToJsonOutputProcess.java
@@ -68,7 +68,7 @@ public void executeProcess() throws IOException {
DateTimeFormatter dateDTF = ISODateTimeFormat.date();
assignsForDateJson.setServiceDate(dateDTF.print(thisDate));
- jsonOpAssigns = listConvertOpAssignTcipToJson(tcipToJsonConverter,
+ jsonOpAssigns = new UTSUtil().listConvertOpAssignTcipToJson(tcipToJsonConverter,
data.getOperatorAssignmentsByServiceDate(thisDate)); // grab the
// assigns for
// this date and
@@ -90,18 +90,4 @@ public void executeProcess() throws IOException {
}
- private List<OperatorAssignment> listConvertOpAssignTcipToJson(
- ModelCounterpartConverter<SCHOperatorAssignment, OperatorAssignment> conv,
- List<SCHOperatorAssignment> inputAssigns) {
- List<OperatorAssignment> outputAssigns = new ArrayList<OperatorAssignment>();
-
- Iterator<SCHOperatorAssignment> assignTcipIt = inputAssigns.iterator();
-
- while (assignTcipIt.hasNext()) {
- outputAssigns.add(conv.convert(assignTcipIt.next()));
- }
-
- return outputAssigns;
- }
-
}
View
17 ...ers/src/main/java/org/onebusaway/nyc/transit_data_manager/adapters/input/model/MtaUtsCrewAssignment.java
@@ -11,7 +11,8 @@
public class MtaUtsCrewAssignment {
private UtsMappingTool mappingTool = null;
-
+ private static Pattern LETTERS_NUMBERS_PATTERN = Pattern.compile("^(\\D*)(\\d+)$");
+ private static Pattern ROUTE_RUN_PATTERN = Pattern.compile("^(.*)-(.*)$");
public MtaUtsCrewAssignment() {
mappingTool = new UtsMappingTool();
}
@@ -42,8 +43,15 @@ public void setRouteField(String routeField) {
this.routeField = routeField;
}
- public void setRunNumberField(String runNumberField) {
- this.runNumberField = runNumberField;
+ public void setRunNumberField(String value) {
+
+ runNumberField = stripLeadingZeros(value);
+ }
+
+ private String stripLeadingZeros(String s) {
+ if (s.startsWith("0"))
+ return stripLeadingZeros(s.substring(1, s.length()));
+ return s;
}
public void setServIdField(String servIdField) {
@@ -106,8 +114,7 @@ public void setPassNumber(String value) {
try {
passNumberNumericPortion = Long.parseLong(value);
} catch (NumberFormatException nfea) {
- Pattern lettersNumbersPattern = Pattern.compile("^(\\D*)(\\d+)$");
- Matcher matcher = lettersNumbersPattern.matcher(value);
+ Matcher matcher = LETTERS_NUMBERS_PATTERN.matcher(value);
if (matcher.find()) {
passNumberLeadingLetters = matcher.group(1); // The leading letter(s)
String passNumberNumStr = matcher.group(2); // the Number
View
40 ...src/main/java/org/onebusaway/nyc/transit_data_manager/adapters/output/model/json/OperatorAssignment.java
@@ -1,5 +1,9 @@
package org.onebusaway.nyc.transit_data_manager.adapters.output.model.json;
+import org.joda.time.DateTime;
+import org.joda.time.format.DateTimeFormatter;
+import org.joda.time.format.ISODateTimeFormat;
+
/**
* A model class representing a single operator - run assignment. For use in
* creating JSON with Gson.
@@ -8,6 +12,8 @@
*
*/
public class OperatorAssignment {
+ private static final DateTimeFormatter DATE_PATTERN = ISODateTimeFormat.dateTimeNoMillis();
+
public OperatorAssignment() {
}
@@ -23,29 +29,63 @@ public OperatorAssignment() {
public void setAgencyId(String agencyId) {
this.agencyId = agencyId;
}
+
+ public String getAgencyId() {
+ return agencyId;
+ }
public void setPassId(String passId) {
this.passId = passId;
}
+ public String getPassId() {
+ return passId;
+ }
+
public void setRunRoute(String runRoute) {
this.runRoute = runRoute;
}
+ public String getRunRoute() {
+ return runRoute;
+ }
+
public void setRunNumber(String runNumber) {
this.runNumber = runNumber;
}
+ public String getRunNumber() {
+ return runNumber;
+ }
+
public void setDepot(String depot) {
this.depot = depot;
}
+ public String getDepot() {
+ return depot;
+ }
+
public void setServiceDate(String serviceDate) {
this.serviceDate = serviceDate;
}
+ public String getServiceDate() {
+ return serviceDate;
+ }
+
public void setUpdated(String updated) {
this.updated = updated;
}
+
+ public String getUpdated() {
+ return updated;
+ }
+
+ public int compareTo(OperatorAssignment oa) {
+ DateTime incomingRecord = DATE_PATTERN.parseDateTime(oa.getUpdated());
+ DateTime currentRecord = DATE_PATTERN.parseDateTime(getUpdated());
+ return currentRecord.compareTo(incomingRecord);
+ }
}
View
29 ...er/onebusaway-nyc-tdm-webapp/src/main/java/org/onebusaway/nyc/transit_data_manager/api/CrewResource.java
@@ -4,7 +4,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringWriter;
-import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
@@ -18,6 +17,7 @@
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.onebusaway.nyc.transit_data_manager.adapters.ModelCounterpartConverter;
+import org.onebusaway.nyc.transit_data_manager.adapters.api.processes.UTSUtil;
import org.onebusaway.nyc.transit_data_manager.adapters.api.processes.UtsCrewAssignsToDataCreator;
import org.onebusaway.nyc.transit_data_manager.adapters.data.OperatorAssignmentData;
import org.onebusaway.nyc.transit_data_manager.adapters.output.json.OperatorAssignmentFromTcip;
@@ -44,10 +44,14 @@
public class CrewResource {
public CrewResource() throws IOException {
- mostRecentPicker = new UtsCrewUploadsFilePicker(System.getProperty("tdm.crewAssignsUploadDir"));
+ if (System.getProperty("tdm.crewAssignsUploadDir") != null) {
+ mostRecentPicker = new UtsCrewUploadsFilePicker(System.getProperty("tdm.crewAssignsUploadDir"));
+ }
try {
- depotIdTranslator = new DepotIdTranslator(new File(System.getProperty("tdm.depotIdTranslationFile")));
+ if (System.getProperty("tdm.depotIdTranslationFile") != null) {
+ depotIdTranslator = new DepotIdTranslator(new File(System.getProperty("tdm.depotIdTranslationFile")));
+ }
} catch (IOException e) {
// Set depotIdTranslator to null and otherwise do nothing.
// Everything works fine without the depot id translator.
@@ -109,7 +113,7 @@ public Response getCrewAssignments(
ModelCounterpartConverter<SCHOperatorAssignment, OperatorAssignment> tcipToJsonConverter = new OperatorAssignmentFromTcip();
- List<OperatorAssignment> jsonOpAssigns = listConvertOpAssignTcipToJson(tcipToJsonConverter,
+ List<OperatorAssignment> jsonOpAssigns = new UTSUtil().listConvertOpAssignTcipToJson(tcipToJsonConverter,
data.getOperatorAssignmentsByServiceDate(serviceDate)); // grab the
// assigns for
// this date
@@ -158,23 +162,6 @@ private OperatorAssignmentData getOpAssignDataObjectForFile(File inputFile) thro
return process.generateDataObject();
}
- /*
- * This method also exists in the same form in
- * UtsCrewAssignsToJsonOutputProcess
- */
- private List<OperatorAssignment> listConvertOpAssignTcipToJson(
- ModelCounterpartConverter<SCHOperatorAssignment, OperatorAssignment> conv,
- List<SCHOperatorAssignment> inputAssigns) {
-
- _log.debug("About to convert " + inputAssigns.size() + " SCHOperatorAssignments to OperatorAssignment using " + conv.getClass().getName());
- List<OperatorAssignment> outputAssigns = new ArrayList<OperatorAssignment>();
- for (SCHOperatorAssignment assignment : inputAssigns) {
- outputAssigns.add(conv.convert(assignment));
- }
-
- _log.debug("Done converting operatorassignments to tcip.");
- return outputAssigns;
- }
}
View
123 ...nebusaway-nyc-tdm-webapp/src/test/java/org/onebusaway/nyc/transit_data_manager/api/CrewResourceTest.java
@@ -0,0 +1,123 @@
+package org.onebusaway.nyc.transit_data_manager.api;
+
+import static org.junit.Assert.*;
+
+import org.onebusaway.nyc.transit_data_manager.adapters.ModelCounterpartConverter;
+import org.onebusaway.nyc.transit_data_manager.adapters.api.processes.UTSUtil;
+import org.onebusaway.nyc.transit_data_manager.adapters.api.processes.UtsCrewAssignsToDataCreator;
+import org.onebusaway.nyc.transit_data_manager.adapters.data.OperatorAssignmentData;
+import org.onebusaway.nyc.transit_data_manager.adapters.output.json.OperatorAssignmentFromTcip;
+import org.onebusaway.nyc.transit_data_manager.adapters.output.model.json.OperatorAssignment;
+
+
+import org.joda.time.DateMidnight;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import tcip_final_3_0_5_1.SCHOperatorAssignment;
+
+import java.io.File;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.List;
+
+public class CrewResourceTest extends ResourceTest {
+
+ private static Logger _log = LoggerFactory.getLogger(CrewResourceTest.class);
+
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ @Test
+ public void testPaddedRunIds() throws Exception {
+ File tmpInFile = getCISFile();
+
+ UtsCrewAssignsToDataCreator process = new UtsCrewAssignsToDataCreator(tmpInFile);
+ // no need to set depotIdTranslater
+ OperatorAssignmentData data = process.generateDataObject();
+ assertNotNull(data);
+ DateMidnight serviceDate = new DateMidnight("2012-07-27");
+ List<SCHOperatorAssignment> list = data.getOperatorAssignmentsByServiceDate(serviceDate);
+ assertNotNull(list);
+ int i = 0;
+ for (SCHOperatorAssignment assignment: list) {
+ i++;
+ String runId = assignment.getRun().getDesignator();
+ if (runId.contains("-0")) {
+ _log.error("found runId=" + runId + " for assignment=" + assignment.toString());
+ } else {
+ //_log.info("found runId=" + runId + " for assignment=" + assignment.toString());
+ }
+ assertFalse(runId.contains("-0"));
+ }
+ assertTrue(i>0);
+ _log.error("processed " + i + " assignment records");
+ }
+
+
+ @Test
+ public void testDuplicateAssignments() throws Exception {
+ File tmpInFile = getCISFile();
+ UtsCrewAssignsToDataCreator process = new UtsCrewAssignsToDataCreator(tmpInFile);
+ // no need to set depotIdTranslater
+ OperatorAssignmentData data = process.generateDataObject();
+ assertNotNull(data);
+ DateMidnight serviceDate = new DateMidnight("2012-07-27");
+ List<SCHOperatorAssignment> list = data.getOperatorAssignmentsByServiceDate(serviceDate);
+ assertNotNull(list);
+
+ ModelCounterpartConverter<SCHOperatorAssignment, OperatorAssignment> tcipToJsonConverter = new OperatorAssignmentFromTcip();
+
+ List<OperatorAssignment> jsonOpAssigns = new UTSUtil().listConvertOpAssignTcipToJson(tcipToJsonConverter,
+ data.getOperatorAssignmentsByServiceDate(serviceDate));
+ HashMap<String, Integer> passCounts = new HashMap<String, Integer>();
+ HashMap<String, OperatorAssignment> passMap = new HashMap<String, OperatorAssignment>();
+ for (OperatorAssignment oa : jsonOpAssigns) {
+
+ String key = oa.getPassId();
+ if (passCounts.containsKey(key)) {
+
+ Integer val = passCounts.get(key);
+ passCounts.put(key, val + 1);
+ passMap.remove(key);
+ passMap.put(key, oa);
+ } else {
+ passCounts.put(key, 1);
+ passMap.put(key, oa);
+ }
+ }
+
+ boolean foundDuplicate = false;
+ for (String key : passCounts.keySet()) {
+ if (passCounts.get(key) != 1) {
+ _log.error("key=" + key + " has count= " + passCounts.get(key));
+ foundDuplicate = true;
+ }
+
+ }
+ assertFalse(foundDuplicate);
+ OperatorAssignment test1 = passMap.get("706005");
+ assertNotNull(test1);
+ assertEquals("455", test1.getRunNumber());
+ }
+
+ private File getCISFile() throws Exception {
+ File tmpInFile = File.createTempFile("tmp", ".tmp");
+ tmpInFile.deleteOnExit();
+
+ InputStream resource = this.getClass().getResourceAsStream("CIS.txt");
+ assertNotNull(resource);
+ copy(resource, tmpInFile.getCanonicalPath());
+ return tmpInFile;
+ }
+
+}
View
33 ...ebusaway-nyc-tdm-webapp/src/test/java/org/onebusaway/nyc/transit_data_manager/api/DepotResourceTest.java
@@ -11,9 +11,7 @@
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
-import java.io.DataOutputStream;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
@@ -26,10 +24,9 @@
* test that Depot API will ignore additional input, as long as its well formed.
*
*/
-public class DepotResourceTest {
+public class DepotResourceTest extends ResourceTest {
private static Logger _log = LoggerFactory.getLogger(DepotResourceTest.class);
- private static final int CHUNK_SIZE = 1024;
@Before
public void setUp() throws Exception {
@@ -83,33 +80,5 @@ public void test() throws Exception {
tmpOutFile.deleteOnExit();
}
- private void copy(InputStream source, String destinationFileName) {
- byte[] buff = new byte[CHUNK_SIZE];
- DataOutputStream destination = null;
- int read = 0;
- try {
- destination = new DataOutputStream(new FileOutputStream(
- destinationFileName));
- // lazy copy -- not recommend
- while ((read = source.read(buff)) > -1) {
- destination.write(buff, 0, read);
- }
- } catch (Exception any) {
- _log.error(any.toString());
- throw new RuntimeException(any);
- } finally {
- if (source != null)
- try {
- source.close();
- } catch (Exception any) {
- }
- if (destination != null)
- try {
- destination.close();
- } catch (Exception any) {
- }
- }
-
- }
}
View
58 ...er/onebusaway-nyc-tdm-webapp/src/test/java/org/onebusaway/nyc/transit_data_manager/api/ResourceTest.java
@@ -0,0 +1,58 @@
+package org.onebusaway.nyc.transit_data_manager.api;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.DataOutputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+
+/**
+ * Base class for testing resources, contains utility methods.
+ *
+ */
+public class ResourceTest {
+
+ private static Logger _log = LoggerFactory.getLogger(ResourceTest.class);
+ private static final int CHUNK_SIZE = 1024;
+
+ public ResourceTest() {
+
+ }
+
+ @Test
+ public void testNoop() {
+ // pass
+ }
+
+ protected void copy(InputStream source, String destinationFileName) {
+ byte[] buff = new byte[CHUNK_SIZE];
+ DataOutputStream destination = null;
+ int read = 0;
+ try {
+ destination = new DataOutputStream(new FileOutputStream(
+ destinationFileName));
+ // lazy copy -- not recommend
+ while ((read = source.read(buff)) > -1) {
+ destination.write(buff, 0, read);
+ }
+ } catch (Exception any) {
+ _log.error(any.toString());
+ throw new RuntimeException(any);
+ } finally {
+ if (source != null)
+ try {
+ source.close();
+ } catch (Exception any) {
+ }
+ if (destination != null)
+ try {
+ destination.close();
+ } catch (Exception any) {
+ }
+ }
+
+ }
+
+}
View
11,001 ...manager/onebusaway-nyc-tdm-webapp/src/test/resources/org/onebusaway/nyc/transit_data_manager/api/CIS.txt
11,001 additions, 0 deletions not shown
Please sign in to comment.
Something went wrong with that request. Please try again.