EditCustomFieldAction.java

  1. /*
  2.  * This software was designed and created by Jason Carroll.
  3.  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
  4.  * The author can be reached at jcarroll@cowsultants.com
  5.  * ITracker website: http://www.cowsultants.com
  6.  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it only under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  */

  18. package org.itracker.web.actions.admin.configuration;

  19. import org.apache.commons.beanutils.PropertyUtils;
  20. import org.apache.log4j.Logger;
  21. import org.apache.struts.action.*;
  22. import org.itracker.SystemConfigurationException;
  23. import org.itracker.core.resources.ITrackerResources;
  24. import org.itracker.model.Configuration;
  25. import org.itracker.model.CustomField;
  26. import org.itracker.model.CustomFieldValue;
  27. import org.itracker.model.Language;
  28. import org.itracker.model.util.CustomFieldUtilities;
  29. import org.itracker.model.util.SystemConfigurationUtilities;
  30. import org.itracker.services.ConfigurationService;
  31. import org.itracker.web.actions.base.ItrackerBaseAction;
  32. import org.itracker.web.forms.CustomFieldForm;
  33. import org.itracker.web.util.Constants;
  34. import org.itracker.web.util.ServletContextUtils;

  35. import javax.servlet.ServletException;
  36. import javax.servlet.http.HttpServletRequest;
  37. import javax.servlet.http.HttpServletResponse;
  38. import javax.servlet.http.HttpSession;
  39. import java.io.IOException;
  40. import java.util.HashMap;
  41. import java.util.Iterator;
  42. import java.util.List;

  43. public class EditCustomFieldAction extends ItrackerBaseAction {
  44.     private static final Logger log = Logger.getLogger(EditCustomFieldAction.class);


  45.     /* (non-Javadoc)
  46.       * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
  47.       */
  48.     @SuppressWarnings("unchecked")
  49.     @Override
  50.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  51.         ActionMessages errors = new ActionMessages();

  52.         if (!isTokenValid(request)) {
  53.             log.debug("Invalid request token while editing configuration.");
  54.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  55.                     "itracker.web.error.transaction"));
  56.             saveErrors(request, errors);
  57.             saveToken(request);
  58.             return mapping.getInputForward();
  59.         }
  60.         resetToken(request);
  61.         try {
  62.             ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();
  63.             String action = (String) PropertyUtils.getSimpleProperty(form, "action");
  64.             if (action == null) {
  65.                 return mapping.findForward("listconfiguration");
  66.             }
  67.             CustomFieldForm customFieldForm = (CustomFieldForm) form;

  68.             CustomField customField;
  69.             if ("create".equals(action)) {
  70.                 customField = new CustomField();
  71.                 customField.setFieldType(CustomField.Type.valueOf(customFieldForm.getFieldType()));
  72.                 customField.setRequired(("true".equals(PropertyUtils.getSimpleProperty(form, "required")) ? true : false));
  73.                 customField.setSortOptionsByName(("true".equals((String) PropertyUtils.getSimpleProperty(form, "sortOptionsByName")) ? true : false));
  74.                 customField.setDateFormat((String) PropertyUtils.getSimpleProperty(form, "dateFormat"));
  75.                 customField = configurationService.createCustomField(customField);
  76.             } else if ("update".equals(action)) {
  77.                 Integer id = (Integer) PropertyUtils.getSimpleProperty(form, "id");
  78.                 customField = configurationService.getCustomField(id);
  79.                 if (customField == null) {
  80.                     throw new SystemConfigurationException("Invalid custom field id " + id);
  81.                 }
  82.                 List<CustomFieldValue> customFieldValues = customField.getOptions();
  83.                 customField.setFieldType(CustomField.Type.valueOf(customFieldForm.getFieldType()));
  84.                 customField.setRequired(("true".equals((String) PropertyUtils.getSimpleProperty(form, "required")) ? true : false));
  85.                 customField.setSortOptionsByName(("true".equals((String) PropertyUtils.getSimpleProperty(form, "sortOptionsByName")) ? true : false));
  86.                 customField.setDateFormat((String) PropertyUtils.getSimpleProperty(form, "dateFormat"));
  87.                 customField.setOptions(customFieldValues);
  88.                 customField = configurationService.updateCustomField(customField);
  89.             } else {
  90.                 throw new SystemConfigurationException("Invalid action " + action + " while editing custom field.");
  91.             }

  92.             if (customField == null) {
  93.                 throw new SystemConfigurationException(
  94.                         "Unable to create new custom field model.");
  95.             }

  96.             HashMap<String, String> translations = (HashMap<String, String>) PropertyUtils
  97.                     .getSimpleProperty(form, "translations");
  98.             String key = CustomFieldUtilities
  99.                     .getCustomFieldLabelKey(customField.getId());
  100.             log.debug("Processing label translations for custom field "
  101.                     + customField.getId() + " with key " + key);
  102.             if (translations != null && key != null && !key.equals("")) {
  103.                 configurationService.removeLanguageKey(key);
  104.                 Iterator<String> iter = translations.keySet().iterator();
  105.                 while (iter.hasNext()) {
  106.                     String locale = iter.next();
  107.                     if (locale != null) {
  108.                         String translation = translations.get(locale);
  109.                         if (translation != null && !translation.trim().equals("")) {
  110.                             log.debug("Adding new translation for locale "
  111.                                     + locale + " for "
  112.                                     + customField.getId());
  113.                             configurationService
  114.                                     .updateLanguageItem(new Language(locale,
  115.                                             key, translation));
  116.                         }
  117.                     }
  118.                 }
  119.             }
  120.             if (key != null) {
  121.                 ITrackerResources.clearKeyFromBundles(key, true);
  122.             }
  123.             try {
  124.                 // Now reset the cached versions in IssueUtilities
  125.                 configurationService.resetConfigurationCache(Configuration.Type.customfield);
  126.             } catch (Exception e) {
  127.                 log.info("execute: resetConfigurationCache trowed exception, caught", e);
  128.             }

  129.             HttpSession session = request.getSession();
  130.             if (customField.getFieldType() == CustomField.Type.LIST && "create".equals(action)) {
  131.                 session.setAttribute(Constants.CUSTOMFIELD_KEY, customField);
  132.                 customFieldForm.setRequestEnv(request);
  133.                 saveToken(request);
  134.                 return new ActionForward(mapping.findForward("editcustomfield").getPath() + "?id=" + customField.getId() + "&action=update");
  135.             }

  136.             session.removeAttribute(Constants.CUSTOMFIELD_KEY);
  137.         } catch (SystemConfigurationException sce) {
  138.             log.error("Exception processing form data: " + sce.getMessage(), sce);
  139.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(sce.getKey()));
  140.         } catch (Exception e) {
  141.             log.error("Exception processing form data", e);
  142.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
  143.         }

  144.         if (!errors.isEmpty()) {
  145.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  146.                     "itracker.web.error.transaction"));
  147.             saveErrors(request, errors);
  148.             return mapping.findForward("error");
  149.         }

  150.         return mapping.findForward("listconfiguration");
  151.     }
  152. }