Пример #1
0
  private void cleanUp() {
    System.out.println("Cleaning up timestamp errors...");

    final List<Incident> incidents = trafficInfoService.read("incident", Incident.class);

    for (final Incident i : incidents) {
      final String message = i.getMessage();
      final String createDate = i.getCreateDate();

      // extract date occurred and time occurred from message
      final String messageValues[] = message.split(" ");
      final String dateTime = messageValues[0];

      // System.out.println("dateTime: " + dateTime);

      // time and date that incident occurred
      final String dateTimeValues[] = dateTime.split("\\)");
      final String createDateValues[] = createDate.split("T");

      final String dateOccurred = createDateValues[0]; // taken from create date (need the year)
      final String timeOccurred = dateTimeValues[1];
      final String dateOccurred2 = dateTimeValues[0]; // taken from message need the month and day

      final String dateOccurredValues[] = dateOccurred.split("-");
      final String dateOccurredValues2[] = dateOccurred2.split("/");
      final String timeOccurredValues[] = timeOccurred.split(":");

      // Joda Time: year, month, day, hour, min
      System.out.println(
          String.format(
              "Year: %s, month: %s, day: %s, hour: %s, min: %s",
              Integer.parseInt(dateOccurredValues[0]),
              Integer.parseInt(dateOccurredValues2[1]),
              Integer.parseInt(dateOccurredValues2[0].substring(1)),
              Integer.parseInt(timeOccurredValues[0]),
              Integer.parseInt(timeOccurredValues[1])));

      final DateTime dateTimeOccurred =
          new DateTime(
              Integer.parseInt(dateOccurredValues[0]),
              Integer.parseInt(dateOccurredValues2[1]),
              Integer.parseInt(dateOccurredValues2[0].substring(1)),
              Integer.parseInt(timeOccurredValues[0]),
              Integer.parseInt(timeOccurredValues[1]));

      i.setStartTimestamp(String.valueOf(dateTimeOccurred.getMillis()));

      System.out.println("Date: " + dateTimeOccurred);
      System.out.println("Date in Millis: " + dateTimeOccurred.getMillis());

      trafficInfoService.update(i);
    }

    System.out.println("Total incidents size: " + incidents.size());
  }
Пример #2
0
  private void filterIncidents(final ArrayList<Incident> incidentList) {
    // check if an incident already exists in DB
    // if exists, what needs to be updated
    // and which incidents have been resolved?
    // else, write incident to DB

    final List<String> ongoingIncidentIds = new ArrayList<String>();

    System.out.println("Size of incidentList: " + incidentList.size());

    for (final Incident incident : incidentList) {
      System.out.println("current incident: " + incident.getMessage());
      final Incident existingIncident = trafficInfoService.read(incident);

      if (existingIncident != null) {
        // already has incident in DB

        if (!existingIncident.getMessage().equals(incident.getMessage())) {
          // if message is different, update incidentMap information
          // service update will check again to update message if different
          extractIncidentInfo(incident);
        }

        System.out.println("Existing entry, updating DB: " + incident.getMessage());

        // retrieve ongoingIncident
        final Incident ongoingIncident =
            trafficInfoService.read(incident, "ongoing", Incident.class);

        // check if ongoing collections has incident, if yes, put the updated incident inside
        if (ongoingIncident != null) {
          // if contains incident, add it to list, at the end, compare with ongoingIncidents list
          // the non-overlapping ones are the resolved ones.
          ongoingIncidentIds.add(ongoingIncident.getId());

          System.out.println("Ongoing incident added to List: " + ongoingIncident.getMessage());

          // update ongoing and incident collections, new incident must replace the old
          // ongoingIncident
          trafficInfoService.update(incident, "ongoing", Incident.class);
          trafficInfoService.update(incident);
        } else {
          // not found in ongoing collection, add it to ongoing collection
          trafficInfoService.insert(incident, "ongoing");
          ongoingIncidentIds.add(incident.getId());

          // update incident collections only
          System.out.println("incident collection updated: " + incident.getMessage());
          trafficInfoService.update(incident);
        }

      } else {
        // incident does not exist in DB
        // extract incident info, then write it to DB
        System.out.println("New entry, adding to DB: " + incident.getMessage());

        final Incident tempIncident = extractIncidentInfo(incident);

        // insert to incident collection
        trafficInfoService.insert(tempIncident);

        // insert to ongoing collection
        trafficInfoService.insert(tempIncident, "ongoing");
        ongoingIncidentIds.add(tempIncident.getId());
      }
    }

    // the incident objects left inside ongoingIncidents are the ones resolved (non-overlapped)
    // remove these incident objects from ongoing collections

    // retrieve the ongoing traffic event of Incident type only
    final List<Incident> ongoingIncidents = trafficInfoService.read("ongoing", Incident.class);
    boolean ongoing;

    for (final Incident ongoingIncident : ongoingIncidents) {
      ongoing = false;

      for (final String ongoingIncidentId : ongoingIncidentIds) {
        if (ongoingIncident.getId().equals(ongoingIncidentId)) {
          // still ongoing, not resolved, break
          ongoing = true;

          break;
        }
      }

      // ongoingIncident not found to be ongoing anymore
      if (!ongoing) {
        trafficInfoService.delete(ongoingIncident, "ongoing", Incident.class);
      }
    }
  }