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.forms;
20  
21  import org.apache.log4j.Logger;
22  import org.apache.struts.action.ActionErrors;
23  import org.apache.struts.action.ActionMapping;
24  import org.apache.struts.action.ActionMessage;
25  import org.apache.struts.action.ActionMessages;
26  import org.apache.struts.validator.ValidatorForm;
27  import org.itracker.core.resources.ITrackerResources;
28  import org.itracker.model.CustomField;
29  import org.itracker.model.CustomFieldValue;
30  import org.itracker.model.NameValuePair;
31  import org.itracker.model.util.CustomFieldUtilities;
32  import org.itracker.services.ConfigurationService;
33  import org.itracker.web.util.Constants;
34  import org.itracker.web.util.LoginUtilities;
35  import org.itracker.web.util.ServletContextUtils;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpSession;
39  import java.util.*;
40  
41  /**
42   * This is the Struts Form. It is used by action.
43   *
44   * @author ready
45   */
46  @SuppressWarnings("serial")
47  public class CustomFieldForm extends ValidatorForm {
48      String action = null;
49      Integer id = null;
50      Integer fieldType = null;
51      String required = null;
52      String dateFormat = null;
53      String sortOptionsByName = null;
54      String value = null;
55      //	private String base_locale;
56      Map<String, String> translations = new HashMap<String, String>();
57  
58      private static final Logger log = Logger.getLogger(CustomFieldForm.class);
59  
60      public final void setRequestEnv(HttpServletRequest request) {
61  
62          ConfigurationService configurationService = ServletContextUtils
63                  .getItrackerServices().getConfigurationService();
64          Locale currentLocale = LoginUtilities.getCurrentLocale(request);
65          CustomField customField = (CustomField) request.getSession().getAttribute(Constants.CUSTOMFIELD_KEY);
66  
67          Map<String, List<String>> languages_map = configurationService.getAvailableLanguages();
68          String[] languagesArray = new String[languages_map.size()];
69          int idx = 0;
70         for (String language : languages_map.keySet()) {
71            languagesArray[idx] = language;
72            idx++;
73         }
74  
75          String pageTitleKey = "itracker.web.admin.editcustomfield.title.create";
76          String pageTitleArg = "";
77  
78          String action = getAction();
79  
80          if ("update".equals(action)) {
81              pageTitleKey = "itracker.web.admin.editcustomfield.title.update";
82              pageTitleArg = ITrackerResources.getString(CustomFieldUtilities.getCustomFieldLabelKey(customField.getId()));
83          }
84          request.setAttribute("pageTitleKey", pageTitleKey);
85          request.setAttribute("pageTitleArg", pageTitleArg);
86  
87          request.setAttribute("customFieldForm", this);
88          request.setAttribute("languages", languagesArray);
89          request.setAttribute("action", action);
90  
91          Map<String, List<String>> languages = configurationService.getAvailableLanguages();
92          Map<NameValuePair, List<NameValuePair>> languagesNameValuePair = new TreeMap<>(NameValuePair.KEY_COMPARATOR);
93          for (Map.Entry<String, List<String>> entry : languages.entrySet()) {
94              String language = entry.getKey();
95              List<String> locales = entry.getValue();
96              List<NameValuePair> localesNameValuePair = new ArrayList<NameValuePair>();
97              for (String locale : locales) {
98                  NameValuePair localeNameValuePair = new NameValuePair(locale, ITrackerResources.getString("itracker.locale.name", locale));
99                  localesNameValuePair.add(localeNameValuePair);
100             }
101             NameValuePair languageNameValuePair = new NameValuePair(language, ITrackerResources.getString("itracker.locale.name", language));
102             languagesNameValuePair.put(languageNameValuePair, localesNameValuePair);
103         }
104         HttpSession session = request.getSession();
105         String baseLocaleKey = "translations(" + ITrackerResources.BASE_LOCALE + ")";
106 
107         List<CustomFieldValue> options = customField.getOptions();
108 
109         Collections.sort(options, CustomFieldValue.SORT_ORDER_COMPARATOR);
110         if (log.isDebugEnabled()) {
111             log.debug("setRequestEnv: sorted values by sort order comparator: " + options);
112         }
113 
114         Map<Integer, String> optionsMap = new TreeMap<>();
115         for (CustomFieldValue option : options) {
116             String optionName = CustomFieldUtilities.getCustomFieldOptionName(customField.getId(), option.getId(), currentLocale);
117             optionsMap.put(option.getId(), optionName);
118         }
119 
120         String fieldTypeString = Integer.toString(CustomField.Type.STRING.getCode());
121         String fieldTypeInteger = Integer.toString(CustomField.Type.INTEGER.getCode());
122         String fieldTypeDate = Integer.toString(CustomField.Type.DATE.getCode());
123         String fieldTypeList = Integer.toString(CustomField.Type.LIST.getCode());
124 
125         request.setAttribute("fieldTypeString", fieldTypeString);
126         request.setAttribute("fieldTypeInteger", fieldTypeInteger);
127         request.setAttribute("fieldTypeDate", fieldTypeDate);
128         request.setAttribute("fieldTypeList", fieldTypeList);
129         String dateFormatDateOnly = CustomFieldUtilities.DATE_FORMAT_DATEONLY;
130         String dateFormatTimeOnly = CustomFieldUtilities.DATE_FORMAT_TIMEONLY;
131         String dateFormatFull = CustomFieldUtilities.DATE_FORMAT_FULL;
132 
133         request.setAttribute("dateFormatDateOnly", dateFormatDateOnly);
134         request.setAttribute("dateFormatTimeOnly", dateFormatTimeOnly);
135         request.setAttribute("dateFormatFull", dateFormatFull);
136 
137         session.setAttribute("CustomFieldType_List", Integer.toString(CustomField.Type.LIST.getCode()));
138         request.setAttribute("baseLocaleKey", baseLocaleKey);
139         request.setAttribute("field", customField);
140         request.setAttribute("languagesNameValuePair", languagesNameValuePair);
141         request.setAttribute("options", options);
142         request.setAttribute("optionsMap", optionsMap);
143 
144     }
145 
146 
147     /*
148       * public void reset(ActionMapping mapping, HttpServletRequest request) {
149       * action = null; id = null; fieldType = null; required= null; dateFormat=
150       * null; sortOptionsByName= null; value= null; translations = null;
151       *  }
152       */
153     @Override
154     public ActionErrors validate(ActionMapping mapping,
155                                  HttpServletRequest request) {
156         ActionErrors errors = super.validate(mapping, request);
157 
158         if (null == getBaseTranslation() || "".equals(getBaseTranslation().trim())) {
159             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.validate.required",
160                     ITrackerResources.getString("itracker.web.attr.baselocale", LoginUtilities.getCurrentLocale(request))));
161         }
162 
163         setRequestEnv(request);
164         return errors;
165     }
166 
167     @Override
168     public void reset(ActionMapping mapping, HttpServletRequest request) {
169         super.reset(mapping, request);
170     }
171 
172     public String getAction() {
173         return action;
174     }
175 
176     public void setAction(String action) {
177         this.action = action;
178     }
179 
180     public String getDateFormat() {
181         return dateFormat;
182     }
183 
184     public void setDateFormat(String dateFormat) {
185         this.dateFormat = dateFormat;
186     }
187 
188     public Integer getFieldType() {
189         return fieldType;
190     }
191 
192     public void setFieldType(Integer fieldType) {
193         this.fieldType = fieldType;
194     }
195 
196     public Integer getId() {
197         return id;
198     }
199 
200     public void setId(Integer id) {
201         this.id = id;
202     }
203 
204     public String getRequired() {
205         return required;
206     }
207 
208     public void setRequired(String required) {
209         this.required = required;
210     }
211 
212     public String getSortOptionsByName() {
213         return sortOptionsByName;
214     }
215 
216     public void setSortOptionsByName(String sortOptionsByName) {
217         this.sortOptionsByName = sortOptionsByName;
218     }
219 
220     public Map<String, String> getTranslations() {
221         return translations;
222     }
223 
224     public void setTranslations(Map<String, String> translations) {
225         this.translations = translations;
226     }
227 
228     public String getValue() {
229         return value;
230     }
231 
232     public void setValue(String value) {
233         this.value = value;
234     }
235 
236     /**
237      * get localization in base locale
238      */
239     private String getBaseTranslation() {
240         return translations.get(ITrackerResources.BASE_LOCALE);
241     }
242 
243 
244 }