Ejemplo n.º 1
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);
      }
    }
  }