private boolean isCategoryField(List<Category> categories, String fieldLabel) { for (Category category : categories) { if (category.getTitle().toLowerCase().startsWith(fieldLabel.toLowerCase())) { return true; } } return false; }
/** Create Ushahidi-specific form fields */ public boolean addFormFields() { LOG.debug("createUshahidiForms"); try { for (Form form : this.formDao.getAllForms()) { if (form.getName().equalsIgnoreCase(formName)) { LOG.debug("Ushahidi Form already exists, exiting."); return true; } } Form form = new Form(formName); // TITLE addFormField(form, FormFieldType.TEXT_FIELD, MappingMessages.getTitle()); // DESCRIPTION addFormField(form, FormFieldType.TEXT_AREA, MappingMessages.getDescription()); // DATE addFormField(form, FormFieldType.DATE_FIELD, MappingMessages.getDate()); // TIME addFormField(form, FormFieldType.TIME_FIELD, MappingMessages.getTime()); // CATEGORIES addFormField(form, FormFieldType.TRUNCATED_TEXT, MappingMessages.getCategories()); for (Category category : categoryDao.getAllCategories(mappingSetupDao.getDefaultSetup())) { addFormField(form, FormFieldType.CHECK_BOX, category.getTitle()); } // LOCATION addFormField(form, FormFieldType.TRUNCATED_TEXT, MappingMessages.getLocation()); for (Location location : locationDao.getAllLocations(mappingSetupDao.getDefaultSetup())) { if (location.getName() != null && location.getName().equalsIgnoreCase("unknown") == false) { addFormField(form, FormFieldType.CHECK_BOX, location.getName()); } } // OTHER LOCATION addFormField(form, FormFieldType.TEXT_FIELD, MappingMessages.getLocationOther()); this.formDao.saveForm(form); LOG.debug("Form Created: %s", form.getName()); return true; } catch (Exception ex) { ex.printStackTrace(); } return false; }
/** 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); } } } } }