KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > admin > CategoryWebHandler


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/CategoryWebHandler.java,v 1.23 2006/04/14 17:05:25 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.23 $
5  * $Date: 2006/04/14 17:05:25 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  */

41 package com.mvnforum.admin;
42
43 import java.sql.Timestamp JavaDoc;
44 import java.util.Collection JavaDoc;
45 import java.util.Locale JavaDoc;
46
47 import com.mvnforum.MVNForumResourceBundle;
48 import com.mvnforum.MyUtil;
49 import com.mvnforum.auth.*;
50 import com.mvnforum.db.CategoryBean;
51 import com.mvnforum.db.CategoryCache;
52 import com.mvnforum.db.DAOFactory;
53 import net.myvietnam.mvncore.exception.*;
54 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
55 import net.myvietnam.mvncore.util.*;
56 import net.myvietnam.mvncore.web.GenericRequest;
57 import net.myvietnam.mvncore.web.GenericResponse;
58
59 public class CategoryWebHandler {
60
61     private OnlineUserManager onlineUserManager = OnlineUserManager.getInstance();
62
63     public CategoryWebHandler() {
64     }
65
66     /*
67      * @todo: check permission
68      */

69     public void processAdd(GenericRequest request, GenericResponse response)
70         throws BadInputException, CreateException, DatabaseException, DuplicateKeyException,
71         ForeignKeyNotFoundException, AuthenticationException, AssertionException {
72
73         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
74         MVNForumPermission permission = onlineUser.getPermission();
75         permission.ensureCanAddCategory();
76
77         MyUtil.saveVNTyperMode(request, response);
78
79         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
80
81         int parentCategoryID = 0;
82         String JavaDoc categoryName = GenericParamUtil.getParameterSafe(request, "CategoryName", true);
83         categoryName = DisableHtmlTagFilter.filter(categoryName);
84         String JavaDoc categoryDesc = GenericParamUtil.getParameterSafe(request, "CategoryDesc", false);
85         categoryDesc = DisableHtmlTagFilter.filter(categoryDesc);
86         int categoryOption = 0;//@todo review and support it later
87
int categoryStatus = 0;//@todo review and support it later
88

89         DAOFactory.getCategoryDAO().create(parentCategoryID, categoryName, categoryDesc,
90                                  now/*categoryCreationDate*/, now/*categoryModifiedDate*/, 0/*categoryOrder*/,
91                                  categoryOption, categoryStatus);
92
93         // Now clear the cache
94
CategoryCache.getInstance().clear();
95         
96         request.setAttribute("CategoryName", categoryName);
97     }
98
99     public void prepareDelete(GenericRequest request)
100         throws ObjectNotFoundException, BadInputException, DatabaseException, AuthenticationException, AssertionException {
101
102         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
103         MVNForumPermission permission = onlineUser.getPermission();
104         permission.ensureCanDeleteCategory();
105
106         // primary key column(s)
107
int categoryID = GenericParamUtil.getParameterInt(request, "category");
108
109         Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
110
111         Collection JavaDoc forumsInCategory = DAOFactory.getForumDAO().getForums_inCategory(categoryID);
112         if (forumsInCategory.isEmpty() == false) {
113             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete_not_empty_category");
114             throw new BadInputException(localizedMessage);
115             //throw new BadInputException("Cannot delete a not-empty category. Please delete all forums in this category first.");
116
}
117
118         CategoryBean categoryBean = DAOFactory.getCategoryDAO().getCategory(categoryID);
119
120         request.setAttribute("CategoryBean", categoryBean);
121     }
122
123     public void processDelete(GenericRequest request)
124         throws BadInputException, ObjectNotFoundException, DatabaseException, AuthenticationException, AssertionException {
125
126         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
127         MVNForumPermission permission = onlineUser.getPermission();
128
129         // user must have been authenticated before he can delete
130
permission.ensureIsAuthenticated();
131
132         permission.ensureCanDeleteCategory();
133
134         Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
135
136         // primary key column(s)
137
int categoryID = GenericParamUtil.getParameterInt(request, "category");
138
139         Collection JavaDoc forumsInCategory = DAOFactory.getForumDAO().getForums_inCategory(categoryID);
140         if (forumsInCategory.isEmpty() == false) {
141             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete_not_empty_category");
142             throw new BadInputException(localizedMessage);
143             //throw new BadInputException("Cannot delete a not-empty category. Please delete all forums in this category first.");
144
}
145
146         // now check the password
147
MyUtil.ensureCorrectCurrentPassword(request);
148
149         /*
150         try {
151             // NOTE: implement it when we add table GroupCategory
152             GroupCategoryWebHelper.deleteGroupCategory_inCategory(categoryID);
153         } catch (ObjectNotFoundException ex) {}
154         */

155         DAOFactory.getWatchDAO().delete_inCategory(categoryID);
156
157         DAOFactory.getCategoryDAO().delete(categoryID);
158
159         // Now clear the cache
160
CategoryCache.getInstance().clear();
161     }
162
163     /*
164      * @todo: check permission
165      */

166     public void prepareEdit(GenericRequest request)
167         throws BadInputException, ObjectNotFoundException, DatabaseException, AuthenticationException, AssertionException {
168
169         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
170         MVNForumPermission permission = onlineUser.getPermission();
171         permission.ensureCanEditCategory();
172
173         // primary key column(s)
174
int categoryID = GenericParamUtil.getParameterInt(request, "category");
175
176         CategoryBean categoryBean = DAOFactory.getCategoryDAO().getCategory(categoryID);
177
178         request.setAttribute("CategoryBean", categoryBean);
179     }
180
181     /*
182      * @todo: check permission
183      */

184     public void processUpdate(GenericRequest request, GenericResponse response)
185         throws BadInputException, DatabaseException, DuplicateKeyException,
186         ObjectNotFoundException, AuthenticationException, AssertionException {
187
188         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
189         MVNForumPermission permission = onlineUser.getPermission();
190         permission.ensureCanEditCategory();
191
192         MyUtil.saveVNTyperMode(request, response);
193
194         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
195
196         // primary key column(s)
197
int categoryID = GenericParamUtil.getParameterInt(request, "CategoryID");
198
199         // column(s) to update
200
String JavaDoc categoryName = GenericParamUtil.getParameterSafe(request, "CategoryName", true);
201         categoryName = DisableHtmlTagFilter.filter(categoryName);
202         String JavaDoc categoryDesc = GenericParamUtil.getParameterSafe(request, "CategoryDesc", false);
203         categoryDesc = DisableHtmlTagFilter.filter(categoryDesc);
204         int categoryOrder = GenericParamUtil.getParameterUnsignedInt(request, "CategoryOrder");
205         int categoryOption = 0;// @todo review and support it later
206
int categoryStatus = 0;// @todo review and support it later
207

208         DAOFactory.getCategoryDAO().update(categoryID, // primary key
209
categoryName, categoryDesc, now/*categoryModifiedDate*/,
210                                  categoryOrder, categoryOption, categoryStatus);
211
212         // Now clear the cache
213
CategoryCache.getInstance().clear();
214     }
215
216     /*
217      * @todo: check permission
218      */

219     public void processUpdateCategoryOrder(GenericRequest request)
220         throws BadInputException, DatabaseException,
221         ObjectNotFoundException, AuthenticationException, AssertionException {
222
223         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
224         MVNForumPermission permission = onlineUser.getPermission();
225         permission.ensureCanEditCategory();
226
227         Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
228
229         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
230
231         // primary key column(s)
232
int categoryID = GenericParamUtil.getParameterInt(request, "category");
233
234         String JavaDoc action = GenericParamUtil.getParameterSafe(request, "action", true);
235         if (action.equals("up")) {
236             DAOFactory.getCategoryDAO().decreaseCategoryOrder(categoryID, now);
237         } else if (action.equals("down")) {
238             DAOFactory.getCategoryDAO().increaseCategoryOrder(categoryID, now);
239         } else {
240             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_update_category.unknown_action", new Object JavaDoc[] {action});
241             throw new BadInputException(localizedMessage);
242             //throw new BadInputException("Cannot update CategoryOrder: unknown action: " + action);
243
}
244
245         // Now clear the cache
246
CategoryCache.getInstance().clear();
247     }
248 }
249
Popular Tags