Пример #1
0
 /**
  * Creaets an OpenMRS Obs instance
  *
  * @param formField the form field that specifies the concept associated with the Obs
  * @param value value associated with the Obs
  * @param datetime date/time associated with the Obs (may be null)
  * @param accessionNumber accession number associatd with the Obs (may be null)
  * @return the created Obs instance
  */
 public static Obs createObs(
     FormField formField, Object value, Date datetime, String accessionNumber) {
   Concept concept = formField.getField().getConcept();
   if (concept == null)
     throw new FormEntryException(
         "Can't create an Obs for a formField that doesn't represent a Concept");
   return createObs(concept, value, datetime, accessionNumber);
 }
  /**
   * The onSubmit function receives the form/command object that was modified by the input form and
   * saves it to the db
   *
   * @see
   *     org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
   *     javax.servlet.http.HttpServletResponse, java.lang.Object,
   *     org.springframework.validation.BindException)
   */
  @Override
  protected ModelAndView onSubmit(
      HttpServletRequest request, HttpServletResponse response, Object obj, BindException errors)
      throws Exception {

    HttpSession httpSession = request.getSession();
    String view = getFormView();

    if (Context.isAuthenticated()) {
      Form form = (Form) obj;
      MessageSourceAccessor msa = getMessageSourceAccessor();
      String action = request.getParameter("action");
      if (action == null) {
        httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Form.not.saved");
      } else {
        if (action.equals(msa.getMessage("Form.save"))) {
          try {
            // save form
            form = Context.getFormService().saveForm(form);
            httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "Form.saved");
          } catch (Exception e) {
            log.error("Error while saving form " + form.getFormId(), e);
            errors.reject(e.getMessage());
            httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Form.not.saved");
            return showForm(request, response, errors);
          }
        } else if (action.equals(msa.getMessage("Form.delete"))) {
          try {
            Context.getFormService().purgeForm(form);
            httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "Form.deleted");
          } catch (DataIntegrityViolationException e) {
            httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Form.cannot.delete");
            return new ModelAndView(new RedirectView("formEdit.form?formId=" + form.getFormId()));
          } catch (Exception e) {
            log.error("Error while deleting form " + form.getFormId(), e);
            errors.reject(e.getMessage());
            httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Form.cannot.delete");
            return showForm(request, response, errors);
            // return new ModelAndView(new RedirectView(getSuccessView()));
          }
        } else if (action.equals(msa.getMessage("Form.updateSortOrder"))) {

          FormService fs = Context.getFormService();

          TreeMap<Integer, TreeSet<FormField>> treeMap = FormUtil.getFormStructure(form);
          for (Map.Entry<Integer, TreeSet<FormField>> entry : treeMap.entrySet()) {
            Integer parentFormFieldId = entry.getKey();
            float sortWeight = 0;
            for (FormField formField : entry.getValue()) {
              formField.setSortWeight(sortWeight);
              fs.saveFormField(formField);
              sortWeight += 50;
            }
          }

        } else {
          try {
            Context.getFormService().duplicateForm(form);
            httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "Form.duplicated");
          } catch (Exception e) {
            log.error("Error while duplicating form " + form.getFormId(), e);
            errors.reject(e.getMessage());
            httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Form.cannot.duplicate");
            return showForm(request, response, errors);
          }
        }

        view = getSuccessView();
      }
    }

    return new ModelAndView(new RedirectView(view));
  }
 /** @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) */
 public int compare(FormField formField, FormField other) {
   int temp =
       OpenmrsUtil.compareWithNullAsGreatest(formField.getPageNumber(), other.getPageNumber());
   if (temp == 0) {
     temp =
         OpenmrsUtil.compareWithNullAsGreatest(
             formField.getFieldNumber(), other.getFieldNumber());
   }
   if (temp == 0) {
     temp =
         OpenmrsUtil.compareWithNullAsGreatest(formField.getFieldPart(), other.getFieldPart());
   }
   if (temp == 0
       && formField.getPageNumber() == null
       && formField.getFieldNumber() == null
       && formField.getFieldPart() == null) {
     temp =
         OpenmrsUtil.compareWithNullAsGreatest(formField.getSortWeight(), other.getSortWeight());
   }
   if (temp == 0) {
     // to prevent ties
     temp =
         OpenmrsUtil.compareWithNullAsGreatest(
             formField.getFormFieldId(), other.getFormFieldId());
   }
   return temp;
 }