Example #1
0
 private void cleanDB() {
   EntityManager entityManager = StopTime.entityManager();
   entityManager.createQuery("DELETE FROM StopTime").executeUpdate();
   entityManager.createQuery("DELETE FROM Trip").executeUpdate();
   entityManager.createQuery("DELETE FROM Route").executeUpdate();
   entityManager.createQuery("DELETE FROM Stop").executeUpdate();
 }
Example #2
0
 private void parseStopTimes(BufferedReader reader) throws IOException {
   HashSet<String> processedStopTimes = new HashSet<>();
   Calendar wholeDay = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
   wholeDay.setTimeInMillis(0);
   Date dayBeginning = wholeDay.getTime();
   wholeDay.add(Calendar.DAY_OF_YEAR, 1);
   wholeDay.add(Calendar.MILLISECOND, -1);
   Date dayEnd = wholeDay.getTime();
   SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
   String line = reader.readLine(); // skip column names
   while ((line = reader.readLine()) != null) {
     // trip_id,arrival_time,departure_time,stop_id,stop_sequence,shape_dist_traveled
     String[] split = line.split(",");
     if (split.length < 6) {
       write(line);
       continue;
     }
     Integer tripId = trips.get(split[0]);
     Trip trip = tripId != null ? Trip.findTrip(tripId) : null;
     Integer stopId = stops.get(split[3]);
     Stop stop = Stop.findStop(stopId);
     if (trip != null && stop != null) {
       try {
         Date arrival = format.parse(split[1].replace("\"", ""));
         Date startTime = trip.getStartTime();
         if (startTime == null || arrival.before(startTime)) {
           trip.setStartTime(arrival);
         }
       } catch (ParseException e) {
         write(line);
         continue;
       }
       try {
         Date departure = format.parse(split[2].replace("\"", ""));
         Date endTime = trip.getEndTime();
         if (endTime == null || departure.after(endTime)) {
           trip.setEndTime(departure);
         }
       } catch (ParseException e) {
         write(line);
         continue;
       }
       if (trip.getStartTime() != null && trip.getEndTime() != null) {
         Calendar diff = Calendar.getInstance();
         diff.setTimeInMillis(trip.getEndTime().getTime() - trip.getStartTime().getTime());
         if (diff.get(Calendar.DAY_OF_YEAR) > 1) {
           trip.setStartTime(dayBeginning);
           trip.setEndTime(dayEnd);
         }
       }
       String stopTimeKey = split[3] + ":" + tripId + ":" + stopId;
       if (!processedStopTimes.contains(stopTimeKey)) {
         StopTime stopTime = new StopTime();
         stopTime.setSequence(Integer.parseInt(split[4].replace("\"", "").trim()));
         stopTime.setTrip(trip);
         stopTime.setStop(stop);
         processedStopTimes.add(stopTimeKey);
         stopTime.persist();
       }
     }
   }
 }