OrderConfigurationItemAction.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.Configuration.Type;
  25. import org.itracker.model.util.UserUtilities;
  26. import org.itracker.services.ConfigurationService;
  27. import org.itracker.web.actions.base.ItrackerBaseAction;
  28. import org.itracker.web.util.LoginUtilities;
  29. import org.itracker.web.util.ServletContextUtils;

  30. import javax.servlet.ServletException;
  31. import javax.servlet.http.HttpServletRequest;
  32. import javax.servlet.http.HttpServletResponse;
  33. import java.io.IOException;
  34. import java.util.ArrayList;
  35. import java.util.List;


  36. public class OrderConfigurationItemAction extends ItrackerBaseAction {
  37.     private static final Logger log = Logger.getLogger(OrderConfigurationItemAction.class);

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


  40.         if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
  41.             return mapping.findForward("unauthorized");
  42.         }

  43.         try {
  44.             ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();

  45.             Integer configId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
  46.             String action = (String) PropertyUtils.getSimpleProperty(form, "action");
  47.             if (configId == null || configId.intValue() <= 0) {
  48.                 throw new SystemConfigurationException("Invalid configuration id.");
  49.             }

  50.             Configuration configItem = configurationService.getConfigurationItem(configId);
  51.             if (configItem == null) {
  52.                 throw new SystemConfigurationException("Invalid configuration id.");
  53.             }

  54.             Type configType = configItem.getType();
  55.             List<Configuration> configItems = configurationService.getConfigurationItemsByType(configType);
  56.             List<Configuration> newConfigItems = new ArrayList<Configuration>();

  57.             for (int i = 0; i < configItems.size(); i++) {
  58.                 newConfigItems.add(configItems.get(i));
  59.             }
  60.             for (int i = 0; i < configItems.size(); i++) {
  61.                 if (configItems.get(i) != null) {
  62.                     Configuration firstConfiguration = new Configuration();
  63.                     Configuration secondConfiguration = new Configuration();
  64.                     Configuration curConfiguration = configItems.get(i);
  65.                     int todo_i = -1;
  66.                     if (curConfiguration.getId().equals(configId)) {
  67.                         if ("up".equals(action)) {
  68.                             todo_i = i - 1;
  69.                         } else {
  70.                             todo_i = i + 1;
  71.                         }
  72.                         Configuration todoConfiguration = configItems.get(todo_i);

  73.                         firstConfiguration.setId(todoConfiguration.getId());
  74.                         firstConfiguration.setCreateDate(todoConfiguration.getCreateDate());
  75.                         firstConfiguration.setLastModifiedDate(todoConfiguration.getLastModifiedDate());
  76.                         firstConfiguration.setName(todoConfiguration.getName());
  77.                         firstConfiguration.setOrder(curConfiguration.getOrder());
  78.                         firstConfiguration.setType(todoConfiguration.getType());
  79.                         firstConfiguration.setValue(todoConfiguration.getValue());
  80.                         firstConfiguration.setVersion(todoConfiguration.getVersion());


  81.                         secondConfiguration.setId(curConfiguration.getId());
  82.                         secondConfiguration.setCreateDate(curConfiguration.getCreateDate());
  83.                         secondConfiguration.setLastModifiedDate(curConfiguration.getLastModifiedDate());
  84.                         secondConfiguration.setName(curConfiguration.getName());
  85.                         secondConfiguration.setOrder(todoConfiguration.getOrder());
  86.                         secondConfiguration.setType(curConfiguration.getType());
  87.                         secondConfiguration.setValue(curConfiguration.getValue());
  88.                         secondConfiguration.setVersion(curConfiguration.getVersion());

  89.                         newConfigItems.set(todo_i, firstConfiguration);
  90.                         newConfigItems.set(i, secondConfiguration);
  91.                     }
  92.                 }
  93.             }

  94.             newConfigItems = configurationService.updateConfigurationItems(newConfigItems, configType);

  95.             // Only resolutions and severities can be reordered at this point.  Statuses
  96.             // and some basic workflow depend on the actual value of the status, so
  97.             // the order must equal the value of the status for it to work correctly.
  98.             switch (configType) {
  99.                 case resolution:
  100.                 case severity:
  101.                     configurationService.resetConfigurationCache(configType);
  102.             }


  103.             return mapping.findForward("listconfiguration");
  104.         } catch (SystemConfigurationException nfe) {
  105.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidconfiguration"));
  106.             log.debug("Invalid configuration item id " + request.getParameter("id") + " specified.");
  107.         } catch (NumberFormatException nfe) {
  108.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidconfiguration"));
  109.             log.debug("Invalid configuration item id " + request.getParameter("id") + " specified.");
  110.         } catch (Exception e) {
  111.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
  112.             log.error("System Error.", e);
  113.         }
  114.         if (!errors.isEmpty()) {
  115.             saveErrors(request, errors);
  116.         }
  117.         return mapping.findForward("error");
  118.     }

  119. }