View Javadoc

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  
19  package org.itracker.web.actions.admin.configuration;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.commons.beanutils.PropertyUtils;
28  import org.apache.log4j.Logger;
29  import org.apache.struts.action.ActionForm;
30  import org.apache.struts.action.ActionForward;
31  import org.apache.struts.action.ActionMapping;
32  import org.apache.struts.action.ActionMessage;
33  import org.apache.struts.action.ActionMessages;
34  import org.itracker.core.resources.ITrackerResources;
35  import org.itracker.model.CustomField;
36  import org.itracker.services.ConfigurationService;
37  import org.itracker.services.exceptions.SystemConfigurationException;
38  import org.itracker.services.util.CustomFieldUtilities;
39  import org.itracker.services.util.SystemConfigurationUtilities;
40  import org.itracker.services.util.UserUtilities;
41  import org.itracker.web.actions.base.ItrackerBaseAction;
42  
43  
44  
45  public class RemoveCustomFieldAction extends ItrackerBaseAction {
46  	private static final Logger log = Logger.getLogger(RemoveCustomFieldAction.class);
47  	
48  
49      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
50          ActionMessages errors = new ActionMessages();
51  
52          
53          if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
54              return mapping.findForward("unauthorized");
55          }
56          
57          try {
58              ConfigurationService configurationService = getITrackerServices().getConfigurationService();
59              
60              Integer valueId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
61              if(valueId == null || valueId.intValue() <= 0) {
62                  throw new SystemConfigurationException("Invalid custom field id.");
63              }
64              
65              CustomField customField = configurationService.getCustomField(valueId);
66              if(customField == null) {
67                  throw new SystemConfigurationException("Invalid custom field id.");
68              }
69              String key = CustomFieldUtilities.getCustomFieldLabelKey(customField.getId());
70              boolean status = configurationService.removeCustomField(customField.getId());
71              if ( status ) {
72                  if ( key != null )
73                      ITrackerResources.clearKeyFromBundles(key, false);
74              } else {
75                  errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
76              }
77              
78              configurationService.resetConfigurationCache(SystemConfigurationUtilities.TYPE_CUSTOMFIELD);
79              if (!errors.isEmpty()) {
80              	saveErrors(request, errors);
81              	return mapping.getInputForward();
82              }
83              return mapping.findForward("listconfiguration");
84          } catch(SystemConfigurationException sce) {
85              log.debug(sce.getMessage(), sce);
86              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidcustomfield"));
87          } catch(NumberFormatException nfe) {
88              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidcustomfield"));
89              log.debug("Invalid custom field  id " + request.getParameter("id") + " specified.");
90          } catch(Exception e) {
91              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
92              log.error("System Error.", e);
93          }
94          if(! errors.isEmpty()) {
95              saveMessages(request, errors);
96          }
97          return mapping.findForward("error");
98      }
99      
100 }