/**
  * When a form is submitted, this method attempts to pair that form with the patient that is its
  * subject. If there is more than one possibility, the form is posted to the changelog with a list
  * of suggested patients. If there is only one real possibility, but that possibility does not
  * match exactly, it is also posted to the changelog with a snippet about what did not match and
  * what did.
  *
  * @param formResponse
  */
 public void handleFormResponse(FormResponse formResponse) {
   // if the form submitted is not a medic form, then do nothing
   if (!isMedicForm(formResponse.getParentForm())) {
     return;
   }
   // get the medic form equivalent of the form submitted
   MedicForm mForm = formDao.getMedicFormForForm(formResponse.getParentForm());
   CommunityHealthWorker submitter =
       chwDao.getCommunityHealthWorkerByPhoneNumber(formResponse.getSubmitter());
   MedicFormResponse mfr = new MedicFormResponse(formResponse, mForm, submitter, null);
   mfr.setSubject(getFinalCandidate(mfr));
   formResponseDao.saveMedicFormResponse(mfr);
 }
 /** Handle incoming FrontlineEventNotification */
 public void notify(FrontlineEventNotification notification) {
   if (notification instanceof EntitySavedNotification<?>) {
     EntitySavedNotification<?> entitySavedNotification =
         (EntitySavedNotification<?>) notification;
     if (entitySavedNotification.getDatabaseEntity() instanceof FormResponse) {
       FormResponse formResponse = (FormResponse) entitySavedNotification.getDatabaseEntity();
       Form form = formResponse.getParentForm();
       if (formResponse.getParentForm().getName().equalsIgnoreCase(formName)) {
         LOG.debug("Form '%s' Received From (%s)", form.getName(), formResponse.getSubmitter());
         final List<Category> categories =
             categoryDao.getAllCategories(mappingSetupDao.getDefaultSetup());
         final List<Location> locations =
             locationDao.getAllLocations(mappingSetupDao.getDefaultSetup());
         Incident incident = new Incident();
         incident.setMarked(true);
         incident.setMappingSetup(mappingSetupDao.getDefaultSetup());
         Contact contact = getContact(formResponse.getSubmitter());
         if (contact != null) {
           incident.setFirstName(contact.getName());
           incident.setEmailAddress(contact.getEmailAddress());
         }
         int index = 0;
         for (FormField formField : form.getFields()) {
           if (formField.getType().hasValue()) {
             ResponseValue value = formResponse.getResults().get(index);
             if (formField.getLabel().equalsIgnoreCase(MappingMessages.getTitle())) {
               LOG.debug("Incident Title: %s", value);
               incident.setTitle(value.toString());
             } else if (formField.getLabel().equalsIgnoreCase(MappingMessages.getDescription())) {
               LOG.debug("Incident Description: %s", value);
               incident.setDescription(value.toString());
             } else if (formField.getLabel().equalsIgnoreCase(MappingMessages.getDate())) {
               LOG.debug("Incident Date: %s", value);
               SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
               try {
                 Date date = dateFormat.parse(value.toString());
                 if (incident.getIncidentDate() != null) {
                   Calendar current = Calendar.getInstance();
                   current.setTime(incident.getIncidentDate());
                   Calendar updated = Calendar.getInstance();
                   updated.setTime(date);
                   current.set(Calendar.YEAR, updated.get(Calendar.YEAR));
                   current.set(Calendar.MONTH, updated.get(Calendar.MONTH));
                   current.set(Calendar.DAY_OF_MONTH, updated.get(Calendar.DAY_OF_MONTH));
                   incident.setIncidentDate(current.getTime());
                 } else {
                   incident.setIncidentDate(date);
                 }
               } catch (ParseException ex) {
                 LOG.error("ParseException: %s", ex);
               }
             } else if (formField.getLabel().equalsIgnoreCase(MappingMessages.getTime())) {
               LOG.debug("Incident Time: %s", value);
               SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
               try {
                 Date date = dateFormat.parse(value.toString());
                 if (incident.getIncidentDate() != null) {
                   Calendar current = Calendar.getInstance();
                   current.setTime(incident.getIncidentDate());
                   Calendar updated = Calendar.getInstance();
                   updated.setTime(date);
                   current.set(Calendar.HOUR_OF_DAY, updated.get(Calendar.HOUR_OF_DAY));
                   current.set(Calendar.MINUTE, updated.get(Calendar.MINUTE));
                   incident.setIncidentDate(current.getTime());
                 } else {
                   incident.setIncidentDate(date);
                 }
               } catch (ParseException ex) {
                 LOG.error("ParseException: %s", ex);
               }
             } else if (isCategoryField(categories, formField.getLabel())) {
               if (isTrue(value.toString())) {
                 for (Category category : categories) {
                   if (category.getTitle().equalsIgnoreCase(formField.getLabel())) {
                     LOG.debug("Incident Category: %s", category.getTitle());
                     incident.addCategory(category);
                     break;
                   }
                 }
               }
             } else if (isLocationField(locations, formField.getLabel())) {
               if (isTrue(value.toString())) {
                 for (Location location : locations) {
                   if (location.getName().equalsIgnoreCase(formField.getLabel())) {
                     LOG.debug("Incident Location: %s", location.getName());
                     incident.setLocation(location);
                     break;
                   }
                 }
               }
             } else if (formField
                 .getLabel()
                 .equalsIgnoreCase(MappingMessages.getLocationOther())) {
               LOG.debug("Incident Other Location: %s", value);
               // TODO set other location
             } else {
               LOG.error("Unknown Field: %s", formField.getLabel());
             }
             index++;
           }
         }
         try {
           incidentDao.saveIncident(incident);
           LOG.debug("New Incident Created: %s", incident.getTitle());
           pluginController.setStatus(MappingMessages.getIncidentCreatedFromForm());
           pluginController.refreshIncidentMap();
           pluginController.refreshIncidentReports();
         } catch (DuplicateKeyException ex) {
           LOG.error("DuplicateKeyException: %s", ex);
         }
       }
     }
   }
 }