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 org.apache.commons.beanutils.PropertyUtils;
22  import org.apache.log4j.Logger;
23  import org.apache.struts.action.*;
24  import org.itracker.SystemConfigurationException;
25  import org.itracker.model.CustomField;
26  import org.itracker.model.Language;
27  import org.itracker.model.util.CustomFieldUtilities;
28  import org.itracker.model.util.UserUtilities;
29  import org.itracker.services.ConfigurationService;
30  import org.itracker.web.actions.base.ItrackerBaseAction;
31  import org.itracker.web.forms.CustomFieldForm;
32  import org.itracker.web.util.Constants;
33  import org.itracker.web.util.LoginUtilities;
34  import org.itracker.web.util.ServletContextUtils;
35  
36  import javax.servlet.ServletException;
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  import javax.servlet.http.HttpSession;
40  import java.io.IOException;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.TreeMap;
44  
45  //  TODO: Action Cleanup
46  
47  public class EditCustomFieldFormAction extends ItrackerBaseAction {
48      private static final Logger log = Logger.getLogger(EditCustomFieldFormAction.class);
49  
50      /* (non-Javadoc)
51        * @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)
52        */
53      @Override
54      public ActionForward execute(ActionMapping mapping, ActionForm form,
55                                   HttpServletRequest request, HttpServletResponse response)
56              throws ServletException, IOException {
57          ActionMessages errors = new ActionMessages();
58  
59          if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
60              return mapping.findForward("unauthorized");
61          }
62  
63          try {
64  
65              HttpSession session = request.getSession(true);
66  
67              CustomFieldFormorg/itracker/web/forms/CustomFieldForm.html#CustomFieldForm">CustomFieldForm customFieldForm = (CustomFieldForm) form;
68              if (customFieldForm == null)
69                  customFieldForm = new CustomFieldForm();
70  
71              String action = (String) PropertyUtils.getSimpleProperty(customFieldForm, "action");
72              CustomField customField = null;
73              ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();
74              if ("create".equals(action)) {
75                  customField = new CustomField();
76              } else if ("update".equals(action)) {
77                  Integer id = (Integer) PropertyUtils.getSimpleProperty(customFieldForm, "id");
78                  customField = configurationService.getCustomField(id);
79                  if (customField == null)
80                      throw new SystemConfigurationException("Invalid custom field id " + id);
81  
82                  customFieldForm.setId(id);
83                  customFieldForm.setFieldType(customField.getFieldType().getCode());
84                  customFieldForm.setRequired(Boolean.toString(customField.isRequired()));
85                  customFieldForm.setDateFormat(customField.getDateFormat());
86                  customFieldForm.setSortOptionsByName(Boolean.toString(customField.isSortOptionsByName()));
87  
88                  Map<String, String> translations = new TreeMap<>();
89                  List<Language> languageItems = configurationService.getLanguageItemsByKey(CustomFieldUtilities.getCustomFieldLabelKey(customField.getId()));
90                 for (Language languageItem : languageItems) {
91                    translations.put(languageItem.getLocale(), languageItem.getResourceValue());
92                 }
93                  customFieldForm.setTranslations(translations);
94              }
95  
96              /*
97                  * Check whether customField is null or not, if null redirect the
98                  * page to error, otherwise put required objects in request
99                  * object to render output
100                 */
101             if (customField == null) {
102                 log.error("EditCustomFieldFormAction#execute: customField was null!");
103                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidproject"));
104                 return mapping.findForward("error");
105             }
106             session.setAttribute(Constants.CUSTOMFIELD_KEY, customField);
107             customFieldForm.setRequestEnv(request);
108             saveToken(request);
109 
110         } catch (SystemConfigurationException sce) {
111             log.error("execute: Exception system configuration exception caught.", sce);
112             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
113                     "itracker.web.error.invalidcustomfield"));
114         } catch (Exception e) {
115             log.error("execute: Exception while creating edit custom field form.", e);
116             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
117                     "itracker.web.error.system"));
118         }
119 
120         if (!errors.isEmpty()) {
121             saveErrors(request, errors);
122             return mapping.findForward("error");
123         }
124 
125         return mapping.getInputForward();
126     }
127 
128 
129 }