private List<ResponseValue> getResponseValues(String title) { List<ResponseValue> responseValues = new ArrayList<ResponseValue>(); boolean categorySpecified = false; boolean locationSpecified = false; final List<Category> categories = categoryDao.getAllCategories(this.mappingSetupDao.getDefaultSetup()); final List<Location> locations = locationDao.getAllLocations(this.mappingSetupDao.getDefaultSetup()); for (FormField formField : getForm().getFields()) { LOG.debug("FormField: %s", formField.getLabel()); if (formField.getLabel().equalsIgnoreCase(MappingMessages.getTitle())) { responseValues.add(new ResponseValue(title != null ? title : "Incident Title")); } else if (formField.getLabel().equalsIgnoreCase(MappingMessages.getDescription())) { responseValues.add(new ResponseValue(title != null ? title : "Incident Description")); } else if (formField.getLabel().equalsIgnoreCase(MappingMessages.getDate())) { SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); responseValues.add(new ResponseValue(dateFormat.format(new Date()))); } else if (formField.getLabel().equalsIgnoreCase(MappingMessages.getTime())) { SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm"); responseValues.add(new ResponseValue(dateFormat.format(new Date()))); } else if (isCategoryField(categories, formField.getLabel())) { responseValues.add(new ResponseValue(categorySpecified ? "false" : "true")); categorySpecified = true; } else if (isLocationField(locations, formField.getLabel())) { responseValues.add(new ResponseValue(locationSpecified ? "false" : "true")); locationSpecified = true; } else if (formField.getLabel().equalsIgnoreCase(MappingMessages.getLocationOther())) { responseValues.add(new ResponseValue("A Different Location")); } } return responseValues; }
/** 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); } } } } }