KickJava   Java API By Example, From Geeks To Geeks.

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


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 EditCustomFieldAction extends ITrackerAction {
44
45     public EditCustomFieldAction() {
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         if(! isTokenValid(request)) {
55             Logger.logDebug("Invalid request token while editing configuration.");
56             return mapping.findForward("listconfiguration");
57         }
58         resetToken(request);
59         HttpSession session = request.getSession(true);
60
61         try {
62             InitialContext ic = new InitialContext();
63
64             Object JavaDoc scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME);
65             SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class);
66             SystemConfiguration sc = scHome.create();
67
68             String JavaDoc action = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "action");
69
70             if(action == null) {
71                 return mapping.findForward("listconfiguration");
72             }
73
74             CustomFieldModel customField = null;
75             if("create".equals(action)) {
76                 customField = new CustomFieldModel();
77                 customField.setFieldType(((Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "fieldType")).intValue());
78                 customField.setRequired(("true".equals((String JavaDoc) PropertyUtils.getSimpleProperty(form, "required")) ? true : false));
79                 customField.setSortOptionsByName(("true".equals((String JavaDoc) PropertyUtils.getSimpleProperty(form, "sortOptionsByName")) ? true : false));
80                 customField.setDateFormat((String JavaDoc) PropertyUtils.getSimpleProperty(form, "dateFormat"));
81                 customField = sc.createCustomField(customField);
82             } else if("update".equals(action)) {
83                 Integer JavaDoc id = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "id");
84                 customField = sc.getCustomField(id);
85                 if(customField == null) {
86                     throw new SystemConfigurationException("Invalid custom field id " + id);
87                 }
88                 customField.setFieldType(((Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "fieldType")).intValue());
89                 customField.setRequired(("true".equals((String JavaDoc) PropertyUtils.getSimpleProperty(form, "required")) ? true : false));
90                 customField.setSortOptionsByName(("true".equals((String JavaDoc) PropertyUtils.getSimpleProperty(form, "sortOptionsByName")) ? true : false));
91                 customField.setDateFormat((String JavaDoc) PropertyUtils.getSimpleProperty(form, "dateFormat"));
92                 // Set options to null so they don't get updated.
93
customField.setOptions(null);
94                 customField = sc.updateCustomField(customField);
95             } else {
96                 throw new SystemConfigurationException("Invalid action " + action + " while editing custom field.");
97             }
98
99             if(customField == null) {
100                 throw new SystemConfigurationException("Unable to create new custom field model.");
101             }
102
103             HashMap translations = (HashMap) PropertyUtils.getSimpleProperty(form, "translations");
104             String JavaDoc key = CustomFieldUtilities.getCustomFieldLabelKey(customField.getId());
105             Logger.logDebug("Processing label translations for custom field " + customField.getId() + " with key " + key);
106             if(translations != null && key != null && ! key.equals("")) {
107                 for(Iterator iter = translations.keySet().iterator(); iter.hasNext(); ) {
108                     String JavaDoc locale = (String JavaDoc) iter.next();
109                     if(locale != null) {
110                         String JavaDoc translation = (String JavaDoc) translations.get(locale);
111                         if(translation != null && ! translation.equals("")) {
112                             Logger.logDebug("Adding new translation for locale " + locale + " for " + customField);
113                             sc.updateLanguageItem(new LanguageModel(locale, key, translation));
114                         }
115                     }
116                 }
117                 String JavaDoc baseValue = (String JavaDoc) translations.get(ITrackerResources.BASE_LOCALE);
118                 sc.updateLanguageItem(new LanguageModel(ITrackerResources.BASE_LOCALE, key, baseValue));
119                 ITrackerResources.clearKeyFromBundles(key, true);
120             }
121
122             // Now reset the cached versions in IssueUtilities
123
sc.resetConfigurationCache(SystemConfigurationUtilities.TYPE_CUSTOMFIELD);
124             return mapping.findForward("listconfiguration");
125         } catch(SystemConfigurationException sce) {
126             Logger.logError("Exception processing form data: " + sce.getMessage(), sce);
127             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(sce.getKey()));
128         } catch(Exception JavaDoc e) {
129             Logger.logError("Exception processing form data", e);
130             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
131         }
132
133         if(! errors.isEmpty()) {
134             saveErrors(request, errors);
135             saveToken(request);
136             return mapping.getInputForward();
137         }
138
139         return mapping.findForward("error");
140     }
141 }
142   
Popular Tags