@Override public void handleEvent(PersonArrivalEvent event) { try { if (isTransitScenario) { if (transitDriverIds.contains(event.getPersonId())) return; } TravellerChain chain = chains.get(event.getPersonId()); switch (event.getLegMode()) { case "walk": case "transit_walk": { Journey journey = chain.getJourneys().getLast(); Walk walk = journey.getWalks().getLast(); walk.setDest(network.getLinks().get(event.getLinkId()).getCoord()); walk.setEndTime(event.getTime()); walk.setDistance(walk.getDuration() * walkSpeed); break; } case TransportMode.car: { Journey journey = chain.getJourneys().getLast(); journey.setDest(network.getLinks().get(event.getLinkId()).getCoord()); journey.setEndTime(event.getTime()); Trip trip = journey.getTrips().getLast(); trip.setDistance(journey.getDistance()); trip.setEndTime(event.getTime()); chain.inCar = false; break; } case "pt": if (isTransitScenario) { Journey journey = chain.getJourneys().getLast(); Trip trip = journey.getTrips().getLast(); trip.setDest(network.getLinks().get(event.getLinkId()).getCoord()); trip.setEndTime(event.getTime()); journey.setPossibleTransfer(new Transfer()); journey.getPossibleTransfer().setStartTime(event.getTime()); journey.getPossibleTransfer().setFromTrip(trip); } else { Journey journey = chain.getJourneys().getLast(); journey.setEndTime(event.getTime()); journey.setDest(network.getLinks().get(event.getLinkId()).getCoord()); journey.setEndTime(event.getTime()); } break; default: Journey journey = chain.getJourneys().getLast(); journey.setEndTime(event.getTime()); journey.setDest(network.getLinks().get(event.getLinkId()).getCoord()); journey.setEndTime(event.getTime()); break; } } catch (Exception e) { System.err.println(e.getStackTrace()); System.err.println(event.toString()); } }
public void approve( int notificationId, int tripNo, int routeNo, RouteType routeType, String startTime, String endTime) { TripDAO tripDAO = new TripDAO(); RouteDAO routeDAO = new RouteDAO(); Route route = routeDAO.getRoutebyRouteNo(routeNo, routeType); Trip trip = tripDAO.readTripByRouteAndNo(route, tripNo); // if cannot find trip -> return and do nothing if (trip == null) { System.out.println("Approve Action have something wrong"); return; } boolean canApprove = false; LocalTime start = null; if (startTime != null) { start = parseTimeFromClient(startTime); canApprove = true; } LocalTime end = null; if (endTime != null) { end = parseTimeFromClient(endTime); canApprove = true; } if (canApprove) { if (start != null) { trip.setStartTime(start); } if (end != null) { trip.setEndTime(end); } tripDAO.update(trip); } // delete trip notification TripNotificationDAO tripNotificationDAO = new TripNotificationDAO(); TripNotification tripNof = tripNotificationDAO.read(notificationId); tripNotificationDAO.delete(tripNof); // delete notification NotificationDAO notificationDAO = new NotificationDAO(); Notification nof = notificationDAO.read(notificationId); notificationDAO.delete(nof); System.out.println("Trip Notification Approved"); }
@Override public String execute(ApplicationContext context) { String authenticated = super.execute(context); if (authenticated == null || !authenticated.equals(Role.STAFF.name())) { return PAGE.COMMON.LOGIN; } System.out.println("Approving... Trip Notification!"); // get parameter from browser String notificationIdParam = context.getParameter("nofId"); String notificationParam = context.getParameter("notification"); String tripNoParam = context.getParameter("tripNo"); String routeNoParam = context.getParameter("routeNo"); String routeTypeParam = context.getParameter("routeType"); if (tripNoParam == null || routeNoParam == null || notificationParam == null || notificationIdParam == null) { return Config.AJAX_FORMAT; } int nofId, tripNo, routeNo; try { tripNo = Integer.parseInt(tripNoParam); routeNo = Integer.parseInt(routeNoParam); nofId = Integer.parseInt(notificationIdParam); RouteType routeType = RouteType.valueOf(routeTypeParam); TripDAO tripDAO = new TripDAO(); RouteDAO routeDAO = new RouteDAO(); TripNotificationDAO tripNofDao = new TripNotificationDAO(); Route route = routeDAO.getRoutebyRouteNo(routeNo, routeType); if (route != null) { Trip trip = tripDAO.readTripByRouteAndNo(route, tripNo); if (trip != null) { TripNotification tripNof = tripNofDao.readTripNof(routeNo, tripNo, routeType); if (tripNof != null) { // if type = 0 -> update change if (tripNof.getType() == 0) { if (tripNof.getChangeStartTime() != null) { trip.setStartTime(tripNof.getChangeStartTime()); } if (tripNof.getChangeEndTime() != null) { trip.setEndTime(tripNof.getChangeEndTime()); } } // if type = 1 -> insert new trip if (tripNof.getType() == 1) { Trip tripNew = new Trip(); tripNew.setStartTime(tripNof.getChangeStartTime()); tripNew.setEndTime(tripNof.getChangeEndTime()); tripNew.setTripNo(tripNo); tripNew.setRoute(route); // build connection BuildUtils build = new BuildUtils(); build.buildCon(route, trip); tripDAO.create(tripNew); } // if type = 2 -> remove trip if (tripNof.getType() == 2) { tripDAO.deleteTrip(trip); } } } } } catch (NumberFormatException ex) { ex.printStackTrace(); } // reverse string /*String[] content = notificationParam.split("~"); String[] nof = content[1].trim().split(","); Map<String, String> result = new HashMap<String, String>(); for (String str : nof) { TripNofUtils utils = new TripNofUtils(str.trim()); utils.reverse(result); } String startTime = null, endTime = null; String resultStartTime = result.get(TripNofUtils.KEY_STARTTIME); if (resultStartTime != null) { startTime = resultStartTime; } String resultEndTime = result.get(TripNofUtils.KEY_ENDTIME); if (resultEndTime != null) { endTime = resultEndTime; } */ // approve trip // approve(nofId, tripNo, routeNo, RouteType.valueOf(routeTypeParam.toUpperCase()), startTime, // endTime); return Config.AJAX_FORMAT; }
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(); } } } }