FileOutputStream fileOutputStream = new FileOutputStream(uploadedFile);
IOUtils.copy(request.body, fileOutputStream);
fileOutputStream.close();
CSVReader csvReader = new CSVReader(new FileReader(uploadedFile));
int lineNum = 1;
HashMap<Integer, TripPatternStop> columnStopIndex = new HashMap<Integer, TripPatternStop>();
HashMap<Integer, Integer> columnStopDelta = new HashMap<Integer, Integer>();
Integer cumulativeTime = 0;
List<TripPatternStop> patternStops = TripPatternStop.find("pattern = ? order by stopSequence", pattern).fetch();
for(String[] csvLine : csvReader.readAll())
{
int columnIndex = 0;
if(lineNum == 3)
{
if(!csvLine[5].equals("stop_id"))
throw new Exception("Invalid stop_id row.");
for(String column : csvLine)
{
if(columnIndex > 5)
{
Stop stop = Stop.findById(Long.parseLong(column));
TripPatternStop patternStop = patternStops.get(0);
patternStops = patternStops.subList(1, patternStops.size());
if(!patternStop.stop.id.equals(stop.id))
throw new Exception("Stop ID " + stop.id + "doesn't match pattern sequence for stop " + patternStop.stop.id);
columnStopIndex.put(columnIndex, patternStop);
cumulativeTime += patternStop.defaultDwellTime != null? patternStop.defaultDwellTime: 0;
cumulativeTime += patternStop.defaultTravelTime != null? patternStop.defaultTravelTime: 0 ;
columnStopDelta.put(columnIndex, new Integer(cumulativeTime));
}
columnIndex++;
}
}
else if(lineNum > 6)
{
if(!csvLine[0].isEmpty())
{
Trip trip = new Trip();
trip.pattern = pattern;
trip.serviceCalendar = calendar;
trip.blockId = csvLine[2];
trip.tripHeadsign = csvLine[3];
trip.tripShortName = csvLine[4];
trip.useFrequency = false;
trip.save();
Integer firstTimepoint = null;
Integer columnCount = 0;
Integer previousTime = 0;
Integer dayOffset = 0;
for(String column : csvLine)
{
if(columnIndex > 5)
{
if(!column.isEmpty())
{
StopTime stopTime = new StopTime();
stopTime.trip = trip;
// check for board/alight only flag
if(column.contains(">")) {
column = column.replace(">", "");
// board only
stopTime.dropOffType = StopTimePickupDropOffType.NONE;
}
if(column.contains("<")) {
column = column.replace("<", "");
// alight only
stopTime.pickupType = StopTimePickupDropOffType.NONE;
}
column = column.trim();
if(column.equals("+"))
stopTime.departureTime = firstTimepoint + columnStopDelta.get(columnIndex);
else if(column.equals("-"))
stopTime.departureTime = null;
else
{
Integer currentTime;
try
{
currentTime = (dfTime.parse(column).getHours() * 60 * 60 ) + (dfTime.parse(column).getMinutes() * 60) + (dfTime.parse(column).getSeconds());
}
catch(ParseException e)
{
try
{
currentTime = (dfsTime.parse(column).getHours() * 60 * 60 ) + (dfsTime.parse(column).getMinutes() * 60) + (dfsTime.parse(column).getSeconds());
}
catch(ParseException e2)
{
continue;
}
}
// in case of time that decreases add a day to offset for trips that cross midnight boundary
if(previousTime > currentTime)
dayOffset += 24 * 60 * 60;
stopTime.departureTime = currentTime + dayOffset;
previousTime = currentTime;
if(firstTimepoint == null)
{
firstTimepoint = stopTime.departureTime;
}
}
stopTime.arrivalTime = stopTime.departureTime;
stopTime.patternStop = columnStopIndex.get(columnIndex);
stopTime.stop = columnStopIndex.get(columnIndex).stop;
stopTime.stopSequence = columnCount + 1;
stopTime.save();
columnCount++;
}
}
columnIndex++;
}
}
}
lineNum++;
}
csvReader.close();
}
catch(Exception e)
{
Logger.error(e.toString());
}