KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > actions > EditCustomFieldValueAction


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 cowsultants.itracker.web.actions;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.util.*;
24 import javax.ejb.*;
25 import javax.rmi.*;
26 import javax.naming.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import org.apache.commons.beanutils.*;
31 import org.apache.struts.action.*;
32 import org.apache.struts.upload.*;
33 import org.apache.struts.util.*;
34
35 import cowsultants.itracker.ejb.client.exceptions.*;
36 import cowsultants.itracker.ejb.client.interfaces.*;
37 import cowsultants.itracker.ejb.client.models.*;
38 import cowsultants.itracker.ejb.client.resources.*;
39 import cowsultants.itracker.ejb.client.util.*;
40 import cowsultants.itracker.web.util.*;
41
42
43 public class EditCustomFieldValueAction extends ITrackerAction {
44
45     public EditCustomFieldValueAction() {
46     }
47
48     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49         ActionErrors errors = new ActionErrors();
50
51         if(! isLoggedIn(request, response)) {
52             return mapping.findForward("login");
53         }
54
55         HttpSession session = request.getSession(true);
56         CustomFieldModel customField = (CustomFieldModel) session.getAttribute(Constants.CUSTOMFIELD_KEY);
57         if(customField == null) {
58             return mapping.findForward("listconfiguration");
59         }
60
61         if(! isTokenValid(request)) {
62             Logger.logDebug("Invalid request token while editing custom field value.");
63             return new ActionForward(mapping.findForward("editcustomfield").getPath() + "?id=" + customField.getId() + "&action=update");
64         }
65         resetToken(request);
66
67         try {
68             InitialContext ic = new InitialContext();
69
70             Object JavaDoc scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME);
71             SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class);
72             SystemConfiguration sc = scHome.create();
73
74             String JavaDoc action = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "action");
75
76             if(action == null) {
77                 return new ActionForward(mapping.findForward("editcustomfield").getPath() + "?id=" + customField.getId() + "&action=update");
78             }
79
80             CustomFieldValueModel customFieldValue = null;
81             if("create".equals(action)) {
82                 Integer JavaDoc id = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "id");
83                 CustomFieldValueModel[] currOptions = customField.getOptions();
84                 int highestSortOrder = (currOptions.length == 0 ? 1 : currOptions[currOptions.length - 1].getSortOrder());
85                 customFieldValue = new CustomFieldValueModel();
86                 customFieldValue.setCustomFieldId(customField.getId());
87                 customFieldValue.setValue((String JavaDoc) PropertyUtils.getSimpleProperty(form, "value"));
88                 customFieldValue.setSortOrder(highestSortOrder + 1);
89                 customFieldValue = sc.createCustomFieldValue(customFieldValue);
90             } else if("update".equals(action)) {
91                 Integer JavaDoc id = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "id");
92                 customFieldValue = sc.getCustomFieldValue(id);
93                 if(customField == null) {
94                     throw new SystemConfigurationException("Invalid custom field value id " + id);
95                 }
96                 customFieldValue.setValue((String JavaDoc) PropertyUtils.getSimpleProperty(form, "value"));
97                 customFieldValue = sc.updateCustomFieldValue(customFieldValue);
98             } else {
99                 throw new SystemConfigurationException("Invalid action " + action + " while editing custom field value.");
100             }
101
102             if(customFieldValue == null) {
103                 throw new SystemConfigurationException("Unable to create new custom field value model.");
104             }
105
106             HashMap translations = (HashMap) PropertyUtils.getSimpleProperty(form, "translations");
107             String JavaDoc key = CustomFieldUtilities.getCustomFieldOptionLabelKey(customField.getId(), customFieldValue.getId());
108             Logger.logDebug("Processing label translations for custom field value " + customFieldValue.getId() + " with key " + key);
109             if(translations != null && key != null && ! key.equals("")) {
110                 for(Iterator iter = translations.keySet().iterator(); iter.hasNext(); ) {
111                     String JavaDoc locale = (String JavaDoc) iter.next();
112                     if(locale != null) {
113                         String JavaDoc translation = (String JavaDoc) translations.get(locale);
114                         if(translation != null && ! translation.equals("")) {
115                             Logger.logDebug("Adding new translation for locale " + locale + " for " + customFieldValue);
116                             sc.updateLanguageItem(new LanguageModel(locale, key, translation));
117                         }
118                     }
119                 }
120                 String JavaDoc baseValue = (String JavaDoc) translations.get(ITrackerResources.BASE_LOCALE);
121                 sc.updateLanguageItem(new LanguageModel(ITrackerResources.BASE_LOCALE, key, baseValue));
122                 ITrackerResources.clearKeyFromBundles(key, true);
123             }
124
125             // Now reset the cached versions in IssueUtilities
126
sc.resetConfigurationCache(SystemConfigurationUtilities.TYPE_CUSTOMFIELD);
127             return new ActionForward(mapping.findForward("editcustomfield").getPath() + "?id=" + customField.getId() + "&action=update");
128         } catch(SystemConfigurationException sce) {
129             Logger.logError("Exception processing form data: " + sce.getMessage(), sce);
130             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(sce.getKey()));
131         } catch(Exception JavaDoc e) {
132             Logger.logError("Exception processing form data", e);
133             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
134         }
135
136         if(! errors.isEmpty()) {
137             saveErrors(request, errors);
138             saveToken(request);
139             request.setAttribute("customFieldValueForm", form);
140             return mapping.getInputForward();
141         }
142
143         return mapping.findForward("error");
144     }
145 }
146   
Popular Tags