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.log4j.Logger;
22  import org.apache.struts.action.*;
23  import org.itracker.SystemConfigurationException;
24  import org.itracker.core.resources.ITrackerResources;
25  import org.itracker.model.CustomField;
26  import org.itracker.model.CustomFieldValue;
27  import org.itracker.model.Language;
28  import org.itracker.model.NameValuePair;
29  import org.itracker.model.util.CustomFieldUtilities;
30  import org.itracker.model.util.UserUtilities;
31  import org.itracker.services.ConfigurationService;
32  import org.itracker.web.actions.base.ItrackerBaseAction;
33  import org.itracker.web.forms.CustomFieldValueForm;
34  import org.itracker.web.util.Constants;
35  import org.itracker.web.util.LoginUtilities;
36  import org.itracker.web.util.ServletContextUtils;
37  
38  import javax.servlet.ServletException;
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  import javax.servlet.http.HttpSession;
42  import java.io.IOException;
43  import java.util.ArrayList;
44  import java.util.List;
45  import java.util.Map;
46  import java.util.TreeMap;
47  
48  public class EditCustomFieldValueFormAction extends ItrackerBaseAction {
49      private static final Logger log = Logger
50              .getLogger(EditCustomFieldValueFormAction.class);
51  
52      public ActionForward execute(ActionMapping mapping, ActionForm form,
53                                   HttpServletRequest request, HttpServletResponse response)
54              throws ServletException, IOException {
55          ActionMessages errors = new ActionMessages();
56          //  TODO: Action Cleanup
57  
58          if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request,
59                  response)) {
60              return mapping.findForward("unauthorized");
61          }
62          ConfigurationService configurationService = ServletContextUtils.getItrackerServices()
63                  .getConfigurationService();
64  
65          try {
66  
67              HttpSession session = request.getSession(true);
68              Map<String, List<String>> languages = configurationService
69                      .getAvailableLanguages();
70  
71              CustomFieldValueFormer/web/forms/CustomFieldValueForm.html#CustomFieldValueForm">CustomFieldValueForm customFieldValueForm = (CustomFieldValueForm) form;
72  
73              if (customFieldValueForm == null) {
74                  customFieldValueForm = new CustomFieldValueForm();
75              }
76  
77              CustomFieldValue customFieldValue = new CustomFieldValue();
78  
79              String action = customFieldValueForm.getAction();
80  
81              String messageKey;
82              if ("update".equals(action)) {
83                  Integer id = customFieldValueForm.getId();
84                  customFieldValue = configurationService.getCustomFieldValue(id);
85                  if (customFieldValue == null) {
86                      throw new SystemConfigurationException(
87                              "Invalid custom field value id " + id);
88                  }
89  
90                  customFieldValueForm.setId(id);
91                  customFieldValueForm.setValue(customFieldValue.getValue());
92  
93                  customFieldValueForm.setSortOrder(customFieldValue.getSortOrder());
94  
95                  Map<String, String> translations = new TreeMap<>();
96                  messageKey = CustomFieldUtilities
97                          .getCustomFieldOptionLabelKey(customFieldValue
98                                          .getCustomField().getId(),
99                                  customFieldValue.getId());
100                 List<Language> languageItems = configurationService
101                         .getLanguageItemsByKey(messageKey);
102 
103                 for (Language languageItem : languageItems) {
104                     translations.put(languageItem.getLocale(),
105                             languageItem.getResourceValue());
106                 }
107                 customFieldValueForm.setTranslations(translations);
108             } else {
109                 customFieldValue.setCustomField((CustomField) session.getAttribute(Constants.CUSTOMFIELD_KEY));
110                 messageKey = "";
111             }
112 
113             CustomField field = customFieldValue.getCustomField();
114 
115             String pageTitleKey = "";
116             String pageTitleArg = "";
117             pageTitleKey = "itracker.web.admin.editcustomfieldvalue.title.create";
118             if ("update".equals(action)) {
119                 pageTitleKey = "itracker.web.admin.editcustomfieldvalue.title.update";
120             }
121 
122             request.setAttribute("languages", configurationService.getAvailableLanguages());
123             request.setAttribute("pageTitleKey", pageTitleKey);
124             request.setAttribute("pageTitleArg", pageTitleArg);
125 
126             request.setAttribute("messageKey", messageKey);
127             request.setAttribute("languages", languages);
128             request.setAttribute("customFieldValueForm", customFieldValueForm);
129             request.setAttribute("action", action);
130             session.setAttribute(Constants.CUSTOMFIELDVALUE_KEY,
131                     customFieldValue);
132             session.setAttribute("field", field);
133             saveToken(request);
134             setRequestEnvironment(request, configurationService);
135             return mapping.getInputForward();
136 
137 
138 
139         } catch (SystemConfigurationException sce) {
140             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
141                     "itracker.web.error.invalidcustomfieldvalue"));
142         } catch (Exception e) {
143             log.error("Exception while creating edit custom field value form.",
144                     e);
145             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
146                     "itracker.web.error.system"));
147         }
148         if (!errors.isEmpty()) {
149             saveErrors(request, errors);
150             setRequestEnvironment(request, configurationService);
151             return mapping.getInputForward();
152         }
153 
154         return mapping.findForward("error");
155     }
156 
157     public static void setRequestEnvironment(HttpServletRequest request, ConfigurationService configurationService) {
158         Map<String, List<String>> languages = configurationService.getAvailableLanguages();
159         Map<NameValuePair, List<NameValuePair>> languagesNameValuePair = new TreeMap<>(NameValuePair.KEY_COMPARATOR);
160         for (Map.Entry<String, List<String>> entry : languages.entrySet()) {
161             String language = entry.getKey();
162             List<String> locales = entry.getValue();
163 
164             List<NameValuePair> localesNameValuePair = new ArrayList<>();
165             for (String locale : locales) {
166                 NameValuePair localeNameValuePair = new NameValuePair(locale, ITrackerResources.getString("itracker.locale.name", locale));
167                 localesNameValuePair.add(localeNameValuePair);
168             }
169             NameValuePair languageNameValuePair = new NameValuePair(language, ITrackerResources.getString("itracker.locale.name", language));
170             languagesNameValuePair.put(languageNameValuePair, localesNameValuePair);
171         }
172         request.setAttribute("languagesNameValuePair", languagesNameValuePair);
173         request.setAttribute("baseLocale", ITrackerResources.BASE_LOCALE);
174     }
175 
176 }