/**
   * Access a prepared FormTemplate object for adding. This method also handles state when changing
   * screen during the creation process.
   *
   * @param templateId - form template id.
   * @param model - ModelMap
   * @return navigates to TemplateEdit
   */
  @RequestMapping(method = RequestMethod.GET, value = "/TemplateAdd")
  public String addTemplates(
      @RequestParam(value = "templateId", required = false) Long templateId, ModelMap model) {
    FormTemplate template = new FormTemplate();
    model.addAttribute("formTemplate", template);

    // expose CustomSubCategories
    CustomCategory customCategory = template.getCustomCategory();
    if (customCategory == null) {
      customCategory = new CustomCategory();
      template.setCustomCategory(customCategory);
    }
    model.addAttribute("customCategory", customCategory);

    List<CustomSubCategory> subCategories = customCategory.getCustomSubCategories();
    if (subCategories == null) {
      subCategories = new ArrayList<CustomSubCategory>();
      customCategory.setCustomSubCategories(subCategories);
    }
    model.addAttribute("customSubCategories", subCategories);

    // expose StaticCategories
    StaticCategory contentCategory =
        staticCategoryRepository.find(StaticCategoryRepository.STATIC_CONTENT_CATEGORY);
    StaticCategory functionCategory =
        staticCategoryRepository.find(StaticCategoryRepository.STATIC_FUNCTION_CATEGORY);
    StaticCategory otherCategory =
        staticCategoryRepository.find(StaticCategoryRepository.STATIC_OTHER_CATEGORY);
    model.addAttribute("contentCategory", contentCategory);
    model.addAttribute("functionCategory", functionCategory);
    model.addAttribute("otherCategory", otherCategory);

    return "TemplateEdit";
  }
  /**
   * Persist a FormTempate.
   *
   * @param formTemplate - FormTemplate.
   * @param result - used for errorhandling.
   * @param status - used for session state handling.
   * @param model - ModelMap
   * @return navigate to Templatelist or back to TemplateEdit.
   */
  @Transactional
  @RequestMapping(method = RequestMethod.POST)
  public String addTemplate(
      @Valid @ModelAttribute("formTemplate") FormTemplate formTemplate,
      BindingResult result,
      SessionStatus status,
      ModelMap model) {
    if (result.hasErrors()) {
      return "TemplateEdit";
    }

    CustomCategory customCategory = formTemplate.getCustomCategory();
    if (customCategory != null) {
      for (Iterator<CustomSubCategory> it = customCategory.getCustomSubCategories().iterator();
          it.hasNext(); ) {
        CustomSubCategory subCategory = it.next();
        if (StringUtils.isBlank(subCategory.getName())) {
          it.remove();
        }
      }
    }

    formTemplate.setLastChanged(new Date());
    if (formTemplate.getId() == null) {
      formTemplateRepository.persist(formTemplate);
    } else {
      formTemplateRepository.merge(formTemplate);
    }

    status.setComplete();

    return "redirect:TemplateList";
  }