KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > actions > my > EditSystemConfigurationExecAction


1 /*
2  * $Id: EditSystemConfigurationExecAction.java,v 1.9 2005/01/17 21:35:45 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.web.actions.my;
27
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Locale JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.apache.commons.lang.StringUtils;
36
37 import com.j2biz.blogunity.BlogunityManager;
38 import com.j2biz.blogunity.dao.SystemConfigurationDAO;
39 import com.j2biz.blogunity.exception.BlogunityException;
40 import com.j2biz.blogunity.i18n.I18N;
41 import com.j2biz.blogunity.i18n.I18NStatusFactory;
42 import com.j2biz.blogunity.pojo.SystemConfiguration;
43 import com.j2biz.blogunity.web.ActionResultFactory;
44 import com.j2biz.blogunity.web.FormError;
45 import com.j2biz.blogunity.web.FormErrorList;
46 import com.j2biz.blogunity.web.IActionResult;
47
48 /**
49  * @author michelson
50  * @version $$
51  * @since 0.1
52  *
53  *
54  */

55 public class EditSystemConfigurationExecAction extends MyAbstractAction {
56     private static final IActionResult SYSCONFIG_FORM_FORWARD = ActionResultFactory
57             .buildForward("/jsp/my/sysconfigForm.jsp");
58
59     private static final IActionResult SYSCONFIG_FORM_REDIRECT = ActionResultFactory
60             .buildRedirect("/my/editSystemConfigurationForm");
61
62     /*
63      * (non-Javadoc)
64      *
65      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
66      * javax.servlet.http.HttpServletResponse)
67      */

68     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
69             throws BlogunityException {
70
71         if (!user.isAdministrator())
72                 throw new BlogunityException(I18NStatusFactory
73                         .create(I18N.ERRORS.USER_NOT_AUTHORIZED_FOR_EXECUTION));
74
75         FormErrorList errors = new FormErrorList();
76         SystemConfiguration config = BlogunityManager.getSystemConfiguration();
77
78         // siteTitle
79
String JavaDoc siteTitle = request.getParameter("siteTitle");
80         if (StringUtils.isNotEmpty(siteTitle)) {
81             config.setSiteTitle(siteTitle);
82         } else {
83             errors.add(new FormError("siteTitle", "Site title cannot be empty!"));
84         }
85
86         // siteDescription
87
String JavaDoc siteDescription = request.getParameter("siteDescription");
88         if (StringUtils.isNotEmpty(siteDescription)) {
89             config.setSiteDescription(siteDescription);
90         } else {
91             config.setSiteDescription(null);
92         }
93
94         // siteKeywords
95
String JavaDoc siteKeywords = request.getParameter("siteKeywords");
96         if (StringUtils.isNotEmpty(siteKeywords)) {
97             config.setSiteKeywords(siteKeywords);
98         } else {
99             config.setSiteKeywords(null);
100         }
101
102         // systemLocale
103
String JavaDoc systemLocaleStr = request.getParameter("systemLocale");
104         if (StringUtils.isNotEmpty(systemLocaleStr)) {
105             config.setSystemLocale(new Locale JavaDoc(systemLocaleStr));
106         } else {
107             errors.add(new FormError("systemLocale", "System's locale cannot be empty!"));
108         }
109
110         // datetimeFormat
111
String JavaDoc datetimeFormat = request.getParameter("datetimeFormat");
112         if (StringUtils.isNotEmpty(datetimeFormat)) {
113             try {
114                 // check, is format valid?
115
SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc(datetimeFormat);
116                 config.setDatetimeFormat(datetimeFormat);
117             } catch (Exception JavaDoc e) {
118                 errors.add(new FormError("datetimeFormat", e.getMessage()));
119             }
120
121         } else {
122             errors.add(new FormError("datetimeFormat", "Date/Time Format cannot be empty!"));
123         }
124
125         // dateFormat
126
String JavaDoc dateFormat = request.getParameter("dateFormat");
127         if (StringUtils.isNotEmpty(dateFormat)) {
128             try {
129                 // check, is format valid?
130
SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc(dateFormat);
131                 config.setDateFormat(dateFormat);
132             } catch (Exception JavaDoc e) {
133                 errors.add(new FormError("dateFormat", e.getMessage()));
134             }
135
136         } else {
137             errors.add(new FormError("dateFormat", "Date Format cannot be empty!"));
138         }
139
140         // timeFormat
141
String JavaDoc timeFormat = request.getParameter("timeFormat");
142         if (StringUtils.isNotEmpty(timeFormat)) {
143             try {
144                 // check, is format valid?
145
SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc(timeFormat);
146                 config.setTimeFormat(timeFormat);
147             } catch (Exception JavaDoc e) {
148                 errors.add(new FormError("timeFormat", e.getMessage()));
149             }
150
151         } else {
152             errors.add(new FormError("timeFormat", "Time Format cannot be empty!"));
153         }
154
155         // individualBlogsPerUser
156
String JavaDoc individualBlogsPerUser = request.getParameter("individualBlogsPerUser");
157         if (StringUtils.isNotEmpty(individualBlogsPerUser)) {
158             try {
159                 int _individualBlogsPerUser = Integer.parseInt(individualBlogsPerUser);
160                 if (_individualBlogsPerUser < -1 || _individualBlogsPerUser > 10) {
161                     errors.add(new FormError("individualBlogsPerUser",
162                             "Number of individual blogs per user must be between -1 and 10!"));
163                 } else
164                     config.setIndividualBlogsPerUser(_individualBlogsPerUser);
165
166             } catch (NumberFormatException JavaDoc e) {
167                 errors.add(new FormError("individualBlogsPerUser",
168                         "You must specify number of individual blogs per user!"));
169             }
170         } else {
171             errors.add(new FormError("individualBlogsPerUser",
172                     "You must specify number of individual blogs per user!"));
173         }
174
175         // communityBlogsPerUser
176
String JavaDoc communityBlogsPerUser = request.getParameter("communityBlogsPerUser");
177         if (StringUtils.isNotEmpty(communityBlogsPerUser)) {
178             try {
179                 int _communityBlogsPerUser = Integer.parseInt(communityBlogsPerUser);
180                 if (_communityBlogsPerUser < -1 || _communityBlogsPerUser > 10) {
181                     errors.add(new FormError("communityBlogsPerUser",
182                             "Number of community blogs per user must be between -1 and 10!"));
183                 } else
184                     config.setCommunityBlogsPerUser(_communityBlogsPerUser);
185
186             } catch (NumberFormatException JavaDoc e) {
187                 errors.add(new FormError("communityBlogsPerUser",
188                         "You must specify number of community blogs per user!"));
189             }
190         } else {
191             errors.add(new FormError("communityBlogsPerUser",
192                     "You must specify number of community blogs per user!"));
193         }
194
195         // userpicsPerUser
196
String JavaDoc userpicsPerUser = request.getParameter("userpicsPerUser");
197         if (StringUtils.isNotEmpty(userpicsPerUser)) {
198             try {
199                 int _userpicsPerUser = Integer.parseInt(userpicsPerUser);
200                 if (_userpicsPerUser < -1 || _userpicsPerUser > 10) {
201                     errors.add(new FormError("userpicsPerUser",
202                             "Number of userpics per user must be between -1 and 10!"));
203                 } else
204                     config.setUserpicsPerUser(_userpicsPerUser);
205
206             } catch (NumberFormatException JavaDoc e) {
207                 errors.add(new FormError("userpicsPerUser",
208                         "You must specify number of userpics per user!"));
209             }
210         } else {
211             errors.add(new FormError("userpicsPerUser",
212                     "You must specify number of userpics per user!"));
213         }
214
215         // max. userpic size
216
String JavaDoc userpicMaxSize = request.getParameter("userpicMaxSize");
217         if (StringUtils.isNotEmpty(userpicMaxSize)) {
218
219             try {
220                 int _userpicMaxSize = Integer.parseInt(userpicMaxSize);
221                 if (_userpicMaxSize < 1024) {
222                     errors.add(new FormError("userpicMaxSize",
223                             "Size of the uploaded userpic can not be smaller than 1024 bytes!"));
224                 } else
225                     config.setMaxUserpicSize(_userpicMaxSize);
226
227             } catch (NumberFormatException JavaDoc e) {
228                 errors.add(new FormError("userpicMaxSize",
229                         "Size of the uploaded userpic must be a number!"));
230             }
231
232         } else {
233             errors.add(new FormError("userpicMaxSize",
234                     "You must specify max. size of the uploaded userpic!"));
235         }
236
237         // max. userpic width
238
String JavaDoc userpicMaxWidth = request.getParameter("userpicMaxWidth");
239         if (StringUtils.isNotEmpty(userpicMaxWidth)) {
240
241             try {
242                 int _userpicMaxWidth = Integer.parseInt(userpicMaxWidth);
243                 if (_userpicMaxWidth < 10) {
244                     errors.add(new FormError("userpicMaxWidth",
245                             "Width of the uploaded userpic can not be smaller than 10 pixels!"));
246                 } else
247                     config.setMaxUserpicWidth(_userpicMaxWidth);
248
249             } catch (NumberFormatException JavaDoc e) {
250                 errors.add(new FormError("userpicMaxWidth",
251                         "Width of the uploaded userpic must be a number!"));
252             }
253
254         } else {
255             errors.add(new FormError("userpicMaxWidth",
256                     "You must specify max. width of the uploaded userpic!"));
257         }
258
259         // max. userpic height
260
String JavaDoc userpicMaxHeight = request.getParameter("userpicMaxHeight");
261         if (StringUtils.isNotEmpty(userpicMaxHeight)) {
262
263             try {
264                 int _userpicMaxHeight = Integer.parseInt(userpicMaxHeight);
265                 if (_userpicMaxHeight < 10) {
266                     errors.add(new FormError("userpicMaxHeight",
267                             "Height of the uploaded userpic can not be smaller than 10 pixels!"));
268                 } else
269                     config.setMaxUserpicHeight(_userpicMaxHeight);
270
271             } catch (NumberFormatException JavaDoc e) {
272                 errors.add(new FormError("userpicMaxHeight",
273                         "Height of the uploaded userpic must be a number!"));
274             }
275
276         } else {
277             errors.add(new FormError("userpicMaxHeight",
278                     "You must specify max. width of the uploaded userpic!"));
279         }
280
281         // allowNewUsers
282
String JavaDoc allowNewUsers = request.getParameter("allowNewUsers");
283         config.setAllowNewUsers(("on".equalsIgnoreCase(allowNewUsers)) ? true : false);
284
285         // validateNewUsers
286
String JavaDoc validateNewUsers = request.getParameter("validateNewUsers");
287         config.setValidateNewUsers(("on".equalsIgnoreCase(validateNewUsers)) ? true : false);
288
289         // rankingOn
290
String JavaDoc rankingOn = request.getParameter("rankingOn");
291         config.setRankingOn(("on".equalsIgnoreCase(rankingOn)) ? true : false);
292
293         // themeEditingAllowed
294
String JavaDoc themeEditingAllowed = request.getParameter("themeEditingAllowed");
295         config.setThemeEditingAllowed(("on".equalsIgnoreCase(themeEditingAllowed)) ? true : false);
296
297         // accesslogEnabled?
298
String JavaDoc accesslogEnabled = request.getParameter("accesslogEnabled");
299         config.setAccessLoggingEnabled(("on".equalsIgnoreCase(accesslogEnabled)) ? true : false);
300
301         // smtpHost
302
String JavaDoc smtpHost = request.getParameter("smtpHost");
303         if (StringUtils.isNotEmpty(smtpHost)) {
304             config.setSmtpHost(smtpHost);
305         } else {
306             config.setSmtpHost(null);
307         }
308
309         // smtpUser
310
String JavaDoc smtpUser = request.getParameter("smtpUser");
311         if (StringUtils.isNotEmpty(smtpUser)) {
312             config.setSmtpUser(smtpUser);
313         } else {
314             config.setSmtpUser(null);
315         }
316
317         // smtpPassword
318
String JavaDoc smtpPassword = request.getParameter("smtpPassword");
319         if (StringUtils.isNotEmpty(smtpPassword)) {
320             config.setSmtpPassword(smtpPassword);
321         } else {
322             config.setSmtpPassword(null);
323         }
324
325         request.setAttribute("configuration", config);
326         if (errors.size() > 0) {
327             request.setAttribute("errors", errors);
328             request.setAttribute("configuration", BlogunityManager.getSystemConfiguration());
329
330             String JavaDoc[] localeArray = Locale.getISOLanguages();
331             ArrayList JavaDoc locales = new ArrayList JavaDoc();
332             for (int i = 0; i < localeArray.length; i++) {
333                 locales.add(new Locale JavaDoc(localeArray[i]));
334             }
335             request.setAttribute("locales", locales);
336
337             return SYSCONFIG_FORM_FORWARD;
338         }
339
340         (new SystemConfigurationDAO()).updateSystemConfiguration(config);
341
342         return SYSCONFIG_FORM_REDIRECT;
343
344     }
345 }
Popular Tags