/**
   * Processes requests to retire concept reference terms
   *
   * @param request the {@link WebRequest} object
   * @param conceptReferenceTermModel the concept reference term model for the term to retire
   * @param retireReason the reason why the concept reference term is being retired
   * @return the url to redirect to
   */
  @RequestMapping(method = RequestMethod.POST, value = "/admin/concepts/retireConceptReferenceTerm")
  public String retireConceptReferenceTerm(
      WebRequest request,
      @ModelAttribute(value = "conceptReferenceTermModel")
          ConceptReferenceTermModel conceptReferenceTermModel,
      @RequestParam(required = false, value = "retireReason") String retireReason) {

    if (!StringUtils.hasText(retireReason)) {
      retireReason = Context.getMessageSourceService().getMessage("general.default.retireReason");
    }

    try {
      ConceptReferenceTerm conceptReferenceTerm =
          conceptReferenceTermModel.getConceptReferenceTerm();
      Context.getConceptService().retireConceptReferenceTerm(conceptReferenceTerm, retireReason);
      if (log.isDebugEnabled()) {
        log.debug("Retired concept reference term with id: " + conceptReferenceTerm.getId());
      }
      request.setAttribute(
          WebConstants.OPENMRS_MSG_ATTR,
          Context.getMessageSourceService().getMessage("ConceptReferenceTerm.retired"),
          WebRequest.SCOPE_SESSION);

      return "redirect:" + FIND_CONCEPT_REFERENCE_TERM_URL;
    } catch (APIException e) {
      log.error("Error occurred while retiring concept reference term", e);
      request.setAttribute(
          WebConstants.OPENMRS_ERROR_ATTR,
          Context.getMessageSourceService().getMessage("ConceptReferenceTerm.retire.error"),
          WebRequest.SCOPE_SESSION);
    }

    // an error occurred
    return CONCEPT_REFERENCE_TERM_FORM;
  }
  /**
   * Processes requests to unretire concept reference terms
   *
   * @param request the {@link WebRequest} object
   * @param conceptReferenceTermModel the concept reference term model object for the term to
   *     unretire
   * @param retireReason the reason why the concept reference term is being unretired
   * @return the url to redirect to
   */
  @RequestMapping(
      method = RequestMethod.POST,
      value = "/admin/concepts/unretireConceptReferenceTerm")
  public String unretireConceptReferenceTerm(
      WebRequest request,
      @ModelAttribute(value = "conceptReferenceTermModel")
          ConceptReferenceTermModel conceptReferenceTermModel) {

    try {
      ConceptReferenceTerm conceptReferenceTerm =
          conceptReferenceTermModel.getConceptReferenceTerm();
      Context.getConceptService().unretireConceptReferenceTerm(conceptReferenceTerm);
      if (log.isDebugEnabled()) {
        log.debug("Unretired concept reference term with id: " + conceptReferenceTerm.getId());
      }
      request.setAttribute(
          WebConstants.OPENMRS_MSG_ATTR,
          Context.getMessageSourceService().getMessage("ConceptReferenceTerm.unretired"),
          WebRequest.SCOPE_SESSION);

      return "redirect:"
          + CONCEPT_REFERENCE_TERM_FORM_URL
          + ".form?conceptReferenceTermId="
          + conceptReferenceTerm.getConceptReferenceTermId();
    } catch (APIException e) {
      log.error("Error occurred while unretiring concept reference term", e);
      request.setAttribute(
          WebConstants.OPENMRS_ERROR_ATTR,
          Context.getMessageSourceService().getMessage("ConceptReferenceTerm.unretire.error"),
          WebRequest.SCOPE_SESSION);
    }

    // an error occurred, show the form
    return CONCEPT_REFERENCE_TERM_FORM;
  }
  public DataObject simplifyReferenceTerm(ConceptReferenceTerm term) throws Exception {

    List<Object> propertyNamesAndValues = new ArrayList<Object>();

    if (term == null
        || StringUtils.isBlank(term.getCode())
        || StringUtils.isEmpty(term.getCode())) {

      propertyNamesAndValues.add("termCode");
      propertyNamesAndValues.add("");
      propertyNamesAndValues.add("termId");
      propertyNamesAndValues.add("");
      propertyNamesAndValues.add("termName");
      propertyNamesAndValues.add("No Snomed CT Reference Terms Mapped To This Concept");

    } else {

      propertyNamesAndValues.add("termCode");
      propertyNamesAndValues.add((term.getCode()));
      propertyNamesAndValues.add("termId");
      propertyNamesAndValues.add((term.getId()));

      if (term.getName() != null) {
        propertyNamesAndValues.add("termName");
        propertyNamesAndValues.add(term.getName());
      }
    }
    DataObject dataObject = DataObject.create(propertyNamesAndValues);
    return dataObject;
  }