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.preferences;
20  
21  import org.apache.log4j.Logger;
22  import org.apache.struts.action.*;
23  import org.itracker.core.resources.ITrackerResources;
24  import org.itracker.model.User;
25  import org.itracker.model.UserPreferences;
26  import org.itracker.model.util.UserUtilities;
27  import org.itracker.services.ConfigurationService;
28  import org.itracker.services.UserService;
29  import org.itracker.web.actions.base.ItrackerBaseAction;
30  import org.itracker.web.forms.UserForm;
31  import org.itracker.web.util.Constants;
32  import org.itracker.web.util.LoginUtilities;
33  import org.itracker.web.util.ServletContextUtils;
34  
35  import javax.servlet.ServletException;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  import javax.servlet.http.HttpSession;
39  import java.io.IOException;
40  import java.util.List;
41  import java.util.Locale;
42  import java.util.Map;
43  import java.util.Set;
44  
45  public class EditPreferencesFormAction extends ItrackerBaseAction {
46      private static final Logger log = Logger
47              .getLogger(EditPreferencesFormAction.class);
48  
49      public EditPreferencesFormAction() {
50      }
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          try {
59              UserService userService = ServletContextUtils.getItrackerServices().getUserService();
60              request.setAttribute("uh", userService);
61              ConfigurationService configurationService = ServletContextUtils.getItrackerServices()
62                      .getConfigurationService();
63              request.setAttribute("sc", configurationService);
64              Map<String, List<String>> languagesMap = configurationService
65                      .getAvailableLanguages();
66              request.setAttribute("languagesMap", languagesMap);
67              Set<String> languageCodes = languagesMap.keySet();
68              request.setAttribute("languageCodes", languageCodes);
69  
70              Map<String, String> languagesList = ITrackerResources.getLocaleNamesMap(
71                      LoginUtilities.getCurrentLocale(request),
72                      languageCodes,
73                      languagesMap);
74  //			
75  //			if (languagesList.size() == 0) {
76  //				languagesList.put("en_US", ITrackerResources.getString(
77  //						"itracker.locale.name", "en_US"));
78  //			}
79              request.setAttribute("languagesList", languagesList);
80              Integer languagesListSize = languagesList.size();
81              request.setAttribute("languagesListSize", languagesListSize);
82              HttpSession session = request.getSession();
83              User user = (User) session.getAttribute(Constants.USER_KEY);
84              if (user == null) {
85                  return mapping.findForward("login");
86              }
87  
88              UserPreferences userPrefs = user.getPreferences();
89              // TODO: remove this hack, this should be handled central, there are other
90              // instances of this hack
91              if (userPrefs == null) {
92                  userPrefs = new UserPreferences();
93                  user.setPreferences(userPrefs);
94                  userPrefs.setUser(user);
95              }
96  
97              UserForm./../../../org/itracker/web/forms/UserForm.html#UserForm">UserForm userForm = (UserForm) form;
98              if (userForm == null) {
99                  userForm = new UserForm();
100             }
101             userForm.setLogin(user.getLogin());
102             userForm.setEmail(user.getEmail());
103             userForm.setFirstName(user.getFirstName());
104             userForm.setLastName(user.getLastName());
105 
106             Locale userLocale = ITrackerResources.getLocale(userPrefs
107                     .getUserLocale());
108             userForm.setUserLocale((userLocale == null ? ITrackerResources
109                     .getDefaultLocale() : userLocale.toString()));
110             userForm
111                     .setNumItemsOnIndex((userPrefs.getNumItemsOnIndex() < 1 ? ITrackerResources
112                             .getString("itracker.web.generic.all", userLocale)
113                             : Integer.toString(userPrefs.getNumItemsOnIndex())));
114             userForm
115                     .setNumItemsOnIssueList((userPrefs.getNumItemsOnIssueList() < 1 ? ITrackerResources
116                             .getString("itracker.web.generic.all", userLocale)
117                             : Integer.toString(userPrefs
118                             .getNumItemsOnIssueList())));
119             userForm.setShowClosedOnIssueList((userPrefs
120                     .getShowClosedOnIssueList() ? "true" : "false"));
121             userForm.setSortColumnOnIssueList(userPrefs
122                     .getSortColumnOnIssueList());
123             userForm
124                     .setHiddenIndexSections(UserUtilities
125                             .getHiddenIndexSections(userPrefs
126                                     .getHiddenIndexSections()));
127             userForm
128                     .setRememberLastSearch((userPrefs.getRememberLastSearch() ? "true"
129                             : "false"));
130             userForm.setUseTextActions((userPrefs.getUseTextActions() ? "true"
131                     : "false"));
132 
133             Locale locale = getLocale(request);
134             String statusName = UserUtilities.getStatusName(user.getStatus(),
135                     locale);
136             request.setAttribute("statusName", statusName);
137             String userLocaleAsString = ITrackerResources.getString(
138                     "itracker.locale.name", userPrefs.getUserLocale());
139             request.setAttribute("userLocaleAsString", userLocaleAsString);
140 
141             String showClosedOnIssueListLocalized = ITrackerResources
142                     .getString(
143                             (userPrefs.getShowClosedOnIssueList() ? "itracker.web.generic.yes"
144                                     : "itracker.web.generic.no"), locale);
145             request.setAttribute("showClosedOnIssueListLocalized",
146                     showClosedOnIssueListLocalized);
147             String getRememberLastSearchLocalized = ITrackerResources
148                     .getString(
149                             (userPrefs.getRememberLastSearch() ? "itracker.web.generic.yes"
150                                     : "itracker.web.generic.no"), locale);
151             request.setAttribute("getRememberLastSearchLocalized",
152                     getRememberLastSearchLocalized);
153 
154            String getUseTextActionsLocalized = ITrackerResources
155                    .getString(
156                            (userPrefs.getUseTextActions() ? "itracker.web.generic.yes"
157                                    : "itracker.web.generic.no"), locale);
158            request.setAttribute("getUseTextActionsLocalized",
159                    getUseTextActionsLocalized);
160 
161             request.setAttribute("preferencesForm", userForm);
162             session.setAttribute(Constants.EDIT_USER_KEY, user);
163             session.setAttribute(Constants.EDIT_USER_PREFS_KEY, userPrefs);
164             saveToken(request);
165 
166             Boolean allowPreferenceUpdate = false;
167             Boolean allowProfileUpdate = false;
168             Boolean allowPasswordUpdate = false;
169 
170             if (user != null) {
171                 allowProfileUpdate = userService.allowProfileUpdates(
172                         user, null, UserUtilities.AUTH_TYPE_UNKNOWN,
173                         UserUtilities.REQ_SOURCE_WEB);
174                 allowPasswordUpdate = userService.allowPasswordUpdates(
175                         user, null, UserUtilities.AUTH_TYPE_UNKNOWN,
176                         UserUtilities.REQ_SOURCE_WEB);
177 
178                 allowPreferenceUpdate = userService
179                         .allowPreferenceUpdates(user, null,
180                                 UserUtilities.AUTH_TYPE_UNKNOWN,
181                                 UserUtilities.REQ_SOURCE_WEB);
182 
183             }
184 
185             request.setAttribute("allowProfileUpdate", allowProfileUpdate);
186             request.setAttribute("allowPasswordUpdate", allowPasswordUpdate);
187             request.setAttribute("allowPreferenceUpdate", allowPreferenceUpdate);
188 
189             if (!allowPreferenceUpdate) {
190                 StringBuilder hiddenSectionsString = new StringBuilder();
191                 userPrefs = (UserPreferences) session
192                         .getAttribute("edituserprefs");
193                 Integer[] hiddenSections = UserUtilities
194                         .getHiddenIndexSections(userPrefs.getHiddenIndexSections());
195                 for (int i = 0; i < hiddenSections.length; i++) {
196                     int sectionNumber = (hiddenSections[i] != null ? hiddenSections[i]
197                             .intValue()
198                             : -1);
199                     if (sectionNumber == UserUtilities.PREF_HIDE_ASSIGNED) {
200                         hiddenSectionsString.append((hiddenSectionsString.length() == 0 ? ""
201                                 : ", "));
202                         hiddenSectionsString.append(", ").append(ITrackerResources.getString(
203                                 "itracker.web.editprefs.section.assigned",
204                                 locale));
205                     } else if (sectionNumber == UserUtilities.PREF_HIDE_UNASSIGNED) {
206                         hiddenSectionsString.append((hiddenSectionsString.length() == 0 ? ""
207                                 : ", "));
208                         hiddenSectionsString.append(", ").append(
209                                 ITrackerResources
210                                         .getString(
211                                                 "itracker.web.editprefs.section.unassigned",
212                                                 locale));
213                     } else if (sectionNumber == UserUtilities.PREF_HIDE_CREATED) {
214                         hiddenSectionsString.append((hiddenSectionsString.length() == 0 ? ""
215                                 : ", "));
216                         hiddenSectionsString.append(", ").append(
217                                 ITrackerResources.getString(
218                                         "itracker.web.editprefs.section.created",
219                                         locale));
220                     } else if (sectionNumber == UserUtilities.PREF_HIDE_WATCHED) {
221                         hiddenSectionsString.append((hiddenSectionsString.length() == 0 ? ""
222                                 : ", "));
223                         hiddenSectionsString.append(", ").append(
224                                 ITrackerResources.getString(
225                                         "itracker.web.editprefs.section.watched",
226                                         locale));
227                     }
228 
229                 }
230                 request.setAttribute("hiddenSectionsString", hiddenSectionsString.toString());
231             }
232 
233             request
234                     .setAttribute("pageTitleKey",
235                             "itracker.web.editprefs.title");
236             request.setAttribute("pageTitleArg", "");
237 
238             request.setAttribute("PREF_HIDE_ASSIGNED", UserUtilities.PREF_HIDE_ASSIGNED);
239             request.setAttribute("PREF_HIDE_UNASSIGNED", UserUtilities.PREF_HIDE_UNASSIGNED);
240             request.setAttribute("PREF_HIDE_CREATED", UserUtilities.PREF_HIDE_CREATED);
241             request.setAttribute("PREF_HIDE_WATCHED", UserUtilities.PREF_HIDE_WATCHED);
242 
243             return mapping.findForward("editpreferencesform");
244 
245         } catch (Exception e) {
246             log.error("Exception while creating edit issue form.", e);
247             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
248                     "itracker.web.error.system"));
249         }
250 
251         return mapping.findForward("error");
252     }
253 
254 }