List<Trip> trips = Trip.find("pattern = ? and serviceCalendar = ? ORDER by id", pattern, calendar).fetch();
List<TripPatternStop> stopList = TripPatternStop.find("pattern = ? ORDER BY stopSequence", pattern).fetch();
StringWriter csvString = new StringWriter();
CSVWriter csvWriter = new CSVWriter(csvString);
String[] headerBase = "trip_id, pattern_id, block_id, headsign, short_name".split(",");
String[] headerStopNames = new String[headerBase.length + stopList.size() + 1];
String[] headerStopIds = new String[headerBase.length + stopList.size() + 1];
String[] headerStopTravelTimes = new String[headerBase.length + stopList.size() + 1];
String[] headerStopDwellTimes = new String[headerBase.length + stopList.size() + 1];
String[] headerStopTravelCumulative = new String[headerBase.length + stopList.size() + 1];
Integer cumulativeTravelTime = 0;
headerStopNames[headerBase.length] = "stop_name";
headerStopIds[headerBase.length] = "stop_id";
headerStopTravelTimes[headerBase.length] = "travel_time";
headerStopDwellTimes[headerBase.length] = "dwell_time";
headerStopTravelCumulative[headerBase.length] = "cumulative_time";
HashMap<Integer, Integer> stopColumnIndex = new HashMap<Integer, Integer>();
HashMap<Long, Integer> patternStopColumnIndex = new HashMap<Long, Integer>();
for(TripPatternStop patternStop : stopList)
{
if(patternStop.defaultDwellTime == null)
{
patternStop.defaultDwellTime = 0;
}
cumulativeTravelTime = cumulativeTravelTime + patternStop.defaultTravelTime + patternStop.defaultDwellTime;
Logger.info(patternStop.stopSequence.toString());
headerStopNames[headerBase.length + patternStop.stopSequence + 1] = patternStop.stop.stopName;
headerStopIds[headerBase.length + patternStop.stopSequence + 1] = patternStop.stop.id.toString();
headerStopTravelTimes[headerBase.length + patternStop.stopSequence + 1] = "=\"" + TimeExtensions.ccyAmount(patternStop.defaultTravelTime) + "\"";
headerStopDwellTimes[headerBase.length + patternStop.stopSequence + 1] = "=\"" + TimeExtensions.ccyAmount(patternStop.defaultDwellTime) + "\"";
headerStopTravelCumulative[headerBase.length + patternStop.stopSequence + 1] = "=\"" + TimeExtensions.ccyAmount(cumulativeTravelTime) + "\"";
stopColumnIndex.put(patternStop.stopSequence + 1, headerBase.length + patternStop.stopSequence);
patternStopColumnIndex.put(patternStop.id, headerBase.length + patternStop.stopSequence);
}
String[] header = (String[]) ArrayUtils.addAll(headerBase, headerStopNames);
csvWriter.writeNext(headerBase);
csvWriter.writeNext(headerStopNames);
csvWriter.writeNext(headerStopIds);
csvWriter.writeNext(headerStopTravelTimes);
csvWriter.writeNext(headerStopDwellTimes);
csvWriter.writeNext(headerStopTravelCumulative);
for(Trip trip : trips)
{
String[] tripTimes = new String[headerBase.length + stopList.size() + 1];
tripTimes[0] = trip.id.toString();
tripTimes[1] = patternId.toString();
tripTimes[2] = trip.blockId;
tripTimes[3] = trip.tripHeadsign;
tripTimes[4] = trip.tripShortName;
List<StopTime> stopTimes = StopTime.find("trip = ? order by stopSequence", trip).fetch();
try
{
Date startTime = dfTime.parse("00:00 AM");
for(StopTime stopTime : stopTimes)
{
if(stopTime.departureTime != null)
{
Date newTime = new Date(startTime.getTime() + (stopTime.departureTime * 1000));
String timeString = dfTime.format(newTime);
if(stopTime.pickupType != null && stopTime.pickupType.equals(StopTimePickupDropOffType.NONE))
timeString += " <";
if(stopTime.dropOffType != null && stopTime.dropOffType.equals(StopTimePickupDropOffType.NONE))
timeString += " >";
if(stopTime.patternStop == null)
tripTimes[stopColumnIndex.get(stopTime.stopSequence) + 1] = timeString;
else
tripTimes[patternStopColumnIndex.get(stopTime.patternStop.id) + 1] = timeString;
}
else
{
String boardAlightStatus = "";
if(stopTime.pickupType != null && stopTime.pickupType.equals(StopTimePickupDropOffType.NONE))
boardAlightStatus += " <";
if(stopTime.dropOffType != null && stopTime.dropOffType.equals(StopTimePickupDropOffType.NONE))
boardAlightStatus += " >";
if(stopTime.patternStop == null)
tripTimes[stopColumnIndex.get(stopTime.stopSequence) + 1] = "-" + boardAlightStatus;
else
tripTimes[patternStopColumnIndex.get(stopTime.patternStop.id) + 1] = "-" + boardAlightStatus;
}
}
}
catch(Exception e)
{
Logger.error(e.toString());
}
csvWriter.writeNext(tripTimes);
}
renderText(csvString);
}