Exemplo n.º 1
0
 public static void exportXformToFile(File file, Form selectedForm) throws Exception {
   FileOutputStream outStream = new FileOutputStream(file);
   String fileContent = new String();
   for (FormField ff : selectedForm.getFields()) {
     if (ff.getType() == FormFieldType.DROP_DOWN_LIST) {
       System.out.println("-------in export----------- " + ff);
     }
     fileContent = fileContent + ff.getX_form();
   }
   fileContent = FormsPluginController.removeEmptyGroups(fileContent);
   outStream.write(fileContent.getBytes("UTF-8"));
   outStream.flush();
   outStream.close();
 }
 /** 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);
         }
       }
     }
   }
 }