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  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import javax.servlet.http.HttpSession;
30  
31  import org.apache.commons.beanutils.PropertyUtils;
32  import org.apache.log4j.Logger;
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  import org.apache.struts.action.ActionMessage;
37  import org.apache.struts.action.ActionMessages;
38  import org.itracker.core.resources.ITrackerResources;
39  import org.itracker.model.CustomField;
40  import org.itracker.model.CustomFieldValue;
41  import org.itracker.model.Language;
42  import org.itracker.services.ConfigurationService;
43  import org.itracker.services.exceptions.SystemConfigurationException;
44  import org.itracker.services.util.CustomFieldUtilities;
45  import org.itracker.services.util.SystemConfigurationUtilities;
46  import org.itracker.web.actions.base.ItrackerBaseAction;
47  import org.itracker.web.forms.CustomFieldValueForm;
48  import org.itracker.web.util.Constants;
49  
50  public class EditCustomFieldValueAction extends ItrackerBaseAction {
51  	private static final Logger log = Logger.getLogger(EditCustomFieldValueAction.class);
52  
53      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
54          ActionMessages errors = new ActionMessages();
55  
56  
57          if(! isTokenValid(request)) {
58              log.debug("Invalid request token while editing configuration.");
59  			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
60  				"itracker.web.error.transaction"));
61  			saveErrors(request, errors);
62  //			return mapping.getInputForward();
63              return mapping.findForward("listconfiguration");
64          }
65          resetToken(request);
66          HttpSession session = request.getSession(true);
67          CustomField customField = (CustomField) session.getAttribute(Constants.CUSTOMFIELD_KEY);
68          if(customField == null) {
69              return mapping.findForward("listconfiguration");
70          }
71  
72          CustomFieldValueForm customFieldValueForm = (CustomFieldValueForm) form;
73          
74          try {
75  
76              String action = customFieldValueForm.getAction();
77              
78              if (action == null) {
79                  return mapping.findForward("listconfiguration");
80              }
81          
82              ConfigurationService configurationService = getITrackerServices().getConfigurationService();
83              CustomFieldValue customFieldValue = null;
84              
85              if ("create".equals(action)) {
86                  List<CustomFieldValue> currOptions = customField.getOptions();
87                  int highestSortOrder = (currOptions.size() == 0 ? 1 : currOptions.get(currOptions.size() - 1).getSortOrder());
88                  customFieldValue = new CustomFieldValue();
89                  customFieldValue.setCustomField(customField);
90                  customFieldValue.setValue(customFieldValueForm.getValue());
91                  customFieldValue.setSortOrder(customFieldValueForm.getSortOrder());
92                  customFieldValue = configurationService.createCustomFieldValue(customFieldValue);
93              } else if("update".equals(action)) {
94                  Integer id = (Integer) PropertyUtils.getSimpleProperty(form, "id");
95                  customFieldValue = configurationService.getCustomFieldValue(id);
96  
97                  customFieldValue.setValue(customFieldValueForm.getValue());
98                  customFieldValue.setSortOrder(customFieldValueForm.getSortOrder());
99  //                String name = CustomFieldUtilities.getCustomFieldOptionName(customFieldValue.getCustomField().getId(), id);
100 //                customFieldValue.setName(name);
101                 customFieldValue = configurationService.updateCustomFieldValue(customFieldValue);
102             } else {
103                 throw new SystemConfigurationException("Invalid action " + action + " while editing custom field value.");
104             }
105 
106             if(customFieldValue == null) {
107                 throw new SystemConfigurationException("Unable to create new custom field value model.");
108             }
109             
110             HashMap<String, String> translations = customFieldValueForm.getTranslations();
111             String key = CustomFieldUtilities.getCustomFieldOptionLabelKey(customField.getId(), customFieldValue.getId());
112             log.debug("Processing label translations for custom field value " + customFieldValue.getId() + " with key " + key);
113             if(translations != null && key != null && ! key.equals("")) {
114                 for(Iterator<String> iter = translations.keySet().iterator(); iter.hasNext(); ) {
115                     String locale = (String) iter.next();
116                     if(locale != null) {
117                         String translation = (String) translations.get(locale);
118                         if(translation != null && ! translation.equals("")) {
119                             log.debug("Adding new translation for locale " + locale + " for " + String.valueOf(customFieldValue.getId()));
120                             configurationService.updateLanguageItem(new Language(locale, key, translation));
121                         }
122                     }
123                 }
124                 String baseValue = (String) translations.get(ITrackerResources.BASE_LOCALE);
125                 configurationService.updateLanguageItem(new Language(ITrackerResources.BASE_LOCALE, key, baseValue));
126             }
127             if ( key != null )
128                 ITrackerResources.clearKeyFromBundles(key, true);
129             // Now reset the cached versions in IssueUtilities
130             configurationService.resetConfigurationCache(SystemConfigurationUtilities.TYPE_CUSTOMFIELD);
131             request.setAttribute("action",action);
132             String pageTitleKey = "";
133             String pageTitleArg = "";
134             pageTitleKey = "itracker.web.admin.editcustomfield.title.create";
135             if (action == "update") {
136             	 pageTitleKey = "itracker.web.admin.editcustomfield.title.update";
137             }
138             
139             request.setAttribute("languages", configurationService.getAvailableLanguages());
140             request.setAttribute("pageTitleKey",pageTitleKey); 
141             request.setAttribute("pageTitleArg",pageTitleArg);     
142          
143 //            session.removeAttribute(Constants.CUSTOMFIELDVALUE_KEY);
144             saveToken(request);
145             return new ActionForward(mapping.findForward("editcustomfield").getPath() + "?id=" + customField.getId() + "&action=update");
146         } catch(SystemConfigurationException sce) {
147             log.error("Exception processing form data: " + sce.getMessage(), sce);
148             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(sce.getKey()));
149         } catch(Exception e) {
150             log.error("Exception processing form data", e);
151             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
152         }
153 
154         if(! errors.isEmpty()) {
155             saveErrors(request, errors);
156             saveToken(request);
157             request.setAttribute("customFieldValueForm", form);
158             return mapping.getInputForward();
159         }
160 
161         return mapping.findForward("error");
162     }
163 }
164