OrderCustomFieldValueAction.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.model.Configuration;
  24. import org.itracker.model.CustomField;
  25. import org.itracker.model.CustomFieldValue;
  26. import org.itracker.model.util.SystemConfigurationUtilities;
  27. import org.itracker.model.util.UserUtilities;
  28. import org.itracker.services.ConfigurationService;
  29. import org.itracker.web.actions.base.ItrackerBaseAction;
  30. import org.itracker.web.util.LoginUtilities;
  31. import org.itracker.web.util.ServletContextUtils;

  32. import javax.servlet.ServletException;
  33. import javax.servlet.http.HttpServletRequest;
  34. import javax.servlet.http.HttpServletResponse;
  35. import java.io.IOException;
  36. import java.util.Collections;
  37. import java.util.Iterator;
  38. import java.util.List;

  39. public class OrderCustomFieldValueAction extends ItrackerBaseAction {
  40.     private static final Logger log = Logger
  41.             .getLogger(OrderCustomFieldValueAction.class);

  42.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  43.                                  HttpServletRequest request, HttpServletResponse response)
  44.             throws ServletException, IOException {
  45.         ActionMessages errors = new ActionMessages();

  46.         if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request,
  47.                 response)) {
  48.             return mapping.findForward("unauthorized");
  49.         }

  50.         try {
  51.             ConfigurationService configurationService = ServletContextUtils.getItrackerServices()
  52.                     .getConfigurationService();

  53.             Integer customFieldValueId = (Integer) PropertyUtils
  54.                     .getSimpleProperty(form, "id");
  55.             String action = (String) PropertyUtils.getSimpleProperty(form,
  56.                     "action");
  57.             if (customFieldValueId == null
  58.                     || customFieldValueId.intValue() <= 0) {
  59.                 throw new SystemConfigurationException(
  60.                         "Invalid custom field value id.");
  61.             }

  62.             CustomFieldValue customFieldValue = configurationService
  63.                     .getCustomFieldValue(customFieldValueId);
  64.             if (customFieldValue == null) {
  65.                 throw new SystemConfigurationException(
  66.                         "Invalid custom field value id.");
  67.             }

  68.             CustomField customField = configurationService
  69.                     .getCustomField(customFieldValue.getCustomField().getId());
  70.             if (customField == null) {
  71.                 throw new SystemConfigurationException(
  72.                         "Invalid custom field id.");
  73.             }
  74.             List<CustomFieldValue> customFieldvalues = customField.getOptions();


  75.             Collections.sort(customFieldvalues,
  76.                     CustomFieldValue.SORT_ORDER_COMPARATOR);
  77.             Iterator<CustomFieldValue> valuesIt = customFieldvalues.iterator();
  78.             CustomFieldValue curCustomFieldValue, prevCustomFieldValue = null;
  79.             int i = 0;
  80.             while (valuesIt.hasNext()) {
  81.                 curCustomFieldValue = valuesIt.next();
  82.                 curCustomFieldValue.setSortOrder(i);

  83.                 if (curCustomFieldValue.getId() == customFieldValueId) {

  84.                     if ("up".equals(action)) {
  85.                         if (prevCustomFieldValue != null) {
  86.                             curCustomFieldValue.setSortOrder(i - 1);
  87.                             prevCustomFieldValue.setSortOrder(i);
  88.                         }
  89.                     } else {
  90.                         CustomFieldValue value = valuesIt.next();
  91.                         value.setSortOrder(i++);
  92.                         curCustomFieldValue.setSortOrder(i);
  93.                         curCustomFieldValue = value;
  94.                     }

  95.                 }
  96.                 prevCustomFieldValue = curCustomFieldValue;
  97.                 i++;
  98.             }

  99.             configurationService.updateCustomField(customField);

  100.             configurationService
  101.                     .resetConfigurationCache(Configuration.Type.customfield);
  102.             request.setAttribute("action", action);
  103.             return new ActionForward(mapping.findForward("editcustomfield")
  104.                     .getPath()
  105.                     + "?id=" + customField.getId() + "&action=update");
  106.         } catch (SystemConfigurationException nfe) {
  107.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  108.                     "itracker.web.error.invalidcustomfieldvalue"));
  109.             log.debug("Invalid custom field value id "
  110.                     + request.getParameter("id") + " specified.");
  111.         } catch (NumberFormatException nfe) {
  112.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  113.                     "itracker.web.error.invalidcustomfieldvalue"));
  114.             log.debug("Invalid custom field value id "
  115.                     + request.getParameter("id") + " specified.");
  116.         } catch (Exception e) {
  117.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  118.                     "itracker.web.error.system"));
  119.             log.error("System Error.", e);
  120.         }
  121.         if (!errors.isEmpty()) {
  122.             saveErrors(request, errors);
  123.         }
  124.         return mapping.findForward("error");
  125.     }

  126. }