KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/ForumWebHandler.java,v 1.34 2006/04/14 17:05:25 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.34 $
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.Locale JavaDoc;
45
46 import com.mvnforum.*;
47 import com.mvnforum.auth.*;
48 import com.mvnforum.common.AttachmentUtil;
49 import com.mvnforum.db.*;
50 import com.mvnforum.search.post.DeletePostIndexTask;
51 import com.mvnforum.search.post.PostIndexer;
52 import net.myvietnam.mvncore.exception.*;
53 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
54 import net.myvietnam.mvncore.util.*;
55 import net.myvietnam.mvncore.web.GenericRequest;
56 import net.myvietnam.mvncore.web.GenericResponse;
57
58 public class ForumWebHandler {
59
60     private OnlineUserManager onlineUserManager = OnlineUserManager.getInstance();
61
62     public ForumWebHandler() {
63     }
64
65     public void processAdd(GenericRequest request, GenericResponse response)
66         throws BadInputException, CreateException, DatabaseException, DuplicateKeyException,
67         ForeignKeyNotFoundException, AuthenticationException, AssertionException {
68
69         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
70         MVNForumPermission permission = onlineUser.getPermission();
71         permission.ensureCanAddForum();
72
73         MyUtil.saveVNTyperMode(request, response);
74         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
75
76         int categoryID = GenericParamUtil.getParameterInt(request, "CategoryID");
77         String JavaDoc forumName = GenericParamUtil.getParameterSafe(request, "ForumName", true);
78         forumName = DisableHtmlTagFilter.filter(forumName);
79         String JavaDoc forumDesc = GenericParamUtil.getParameterSafe(request, "ForumDesc", false);
80         forumDesc = DisableHtmlTagFilter.filter(forumDesc);
81         int forumType = GenericParamUtil.getParameterInt(request, "ForumType");
82         int forumFormatOption = 0;//@todo review and support it later
83
int forumOption = 0;//@todo review and support it later
84
int forumStatus = GenericParamUtil.getParameterInt(request, "ForumStatus");
85         int forumModerationMode = GenericParamUtil.getParameterInt(request, "ForumModerationMode");
86         String JavaDoc forumPassword = "";//@todo review and support it later
87

88         // check valid
89
ForumBean.validateForumType(forumType);
90         ForumBean.validateForumFormatOption(forumFormatOption);
91         ForumBean.validateForumOption(forumOption);
92         ForumBean.validateForumStatus(forumStatus);
93         ForumBean.validateForumModerationMode(forumModerationMode);
94
95         int forumID = DAOFactory.getForumDAO().createForum(categoryID, ""/*lastPostMemberName*/, forumName,
96                                   forumDesc, now/*forumCreationDate*/, now/*forumModifiedDate*/,
97                                   now/*forumLastPostDate*/, 0/*forumOrder*/, forumType,
98                                   forumFormatOption, forumOption, forumStatus,
99                                   forumModerationMode, forumPassword, 0/*forumThreadCount*/,
100                                   0/*forumPostCount*/);
101
102         // Check if the user created forum should be the owner (ForumAdmin) of that forum
103
// This is used for KG
104
if (MVNForumConfig.getEnableAutoForumOwner()) {
105             int memberID = onlineUser.getMemberID();
106             DAOFactory.getMemberForumDAO().create(memberID, forumID, MVNForumPermission.PERMISSION_FORUM_ADMIN);
107             onlineUser.reloadPermission();
108         }
109
110         // For Company, we auto add PERMISSION_NORMAL_USER of the relation of forum and group of this company
111
try {
112             int companyID = GenericParamUtil.getParameterInt(request, "companyid");
113             CompanyBean companyBean = DAOFactory.getCompanyDAO().getCompany(companyID);
114             int groupID = companyBean.getGroupID();
115             DAOFactory.getGroupForumDAO().create(groupID, forumID, MVNForumPermission.PERMISSION_NORMAL_USER);
116         } catch (Exception JavaDoc ex) {
117             // catch all Exception and just ignore
118
}
119         
120         // Now clear the cache
121
ForumCache.getInstance().clear();
122
123         request.setAttribute("ForumName", forumName);
124     }
125     
126     public void prepareDelete(GenericRequest request)
127         throws BadInputException, ObjectNotFoundException, DatabaseException, AuthenticationException, AssertionException {
128
129         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
130         MVNForumPermission permission = onlineUser.getPermission();
131
132         // primary key column(s)
133
int forumID = GenericParamUtil.getParameterInt(request, "forum");
134
135         permission.ensureCanDeleteForum(forumID);
136
137         ForumBean forumBean = null;
138         try {
139             forumBean = DAOFactory.getForumDAO().getForum(forumID);
140         } catch (ObjectNotFoundException e) {
141             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
142             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.forumid_not_exists", new Object JavaDoc[] {new Integer JavaDoc(forumID)});
143             throw new ObjectNotFoundException(localizedMessage);
144         }
145         int numberOfThreads = DAOFactory.getThreadDAO().getNumberOfEnableThreads_inForum(forumID);
146         int numberOfPosts = DAOFactory.getPostDAO().getNumberOfEnablePosts_inForum(forumID);
147
148         int numberOfPendingThreads = DAOFactory.getThreadDAO().getNumberOfDisableThreads_inForum(forumID);
149         int numberOfPendingPosts = DAOFactory.getPostDAO().getNumberOfDisablePosts_inForum(forumID);
150
151         request.setAttribute("ForumBean", forumBean);
152         request.setAttribute("NumberOfThreads", new Integer JavaDoc(numberOfThreads));
153         request.setAttribute("NumberOfPosts", new Integer JavaDoc(numberOfPosts));
154         request.setAttribute("NumberOfPendingThreads", new Integer JavaDoc(numberOfPendingThreads));
155         request.setAttribute("NumberOfPendingPosts", new Integer JavaDoc(numberOfPendingPosts));
156     }
157
158     public void processDelete(GenericRequest request)
159         throws BadInputException, ObjectNotFoundException, DatabaseException, AuthenticationException, AssertionException {
160
161         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
162         MVNForumPermission permission = onlineUser.getPermission();
163
164         // primary key column(s)
165
int forumID = GenericParamUtil.getParameterInt(request, "forum");
166
167         // user must have been authenticated before he can delete
168
permission.ensureIsAuthenticated();
169
170         permission.ensureCanDeleteForum(forumID);
171
172         // now check the password
173
MyUtil.ensureCorrectCurrentPassword(request);
174
175         // Delete all attachments in this forum,
176
// we must call this before any attempt to delete the post/thread/forum
177
// That is, the order when delete is VERY IMPORTANT
178
AttachmentUtil.deleteAttachments_inForum(forumID);
179
180         DAOFactory.getGroupForumDAO().delete_inForum(forumID);
181         DAOFactory.getMemberForumDAO().delete_inForum(forumID);
182
183         DAOFactory.getFavoriteThreadDAO().delete_inForum(forumID);
184
185         DAOFactory.getWatchDAO().delete_inForum(forumID);
186
187         DAOFactory.getPostDAO().delete_inForum(forumID);
188         DAOFactory.getThreadDAO().delete_inForum(forumID);
189
190         // now delete the forum, note that we delete it after delete all child objects
191
DAOFactory.getForumDAO().delete(forumID);
192
193         // now update the search index
194
PostIndexer.scheduleDeletePostTask(forumID, DeletePostIndexTask.OBJECT_TYPE_FORUM);
195
196         // Clear cache
197
PostCache.getInstance().clear();
198         ThreadCache.getInstance().clear();
199         ForumCache.getInstance().clear();
200     }
201
202     public void prepareEdit(GenericRequest request)
203         throws BadInputException, ObjectNotFoundException, DatabaseException, AuthenticationException, AssertionException {
204
205         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
206         MVNForumPermission permission = onlineUser.getPermission();
207
208         // primary key column(s)
209
int forumID = GenericParamUtil.getParameterInt(request, "forum");
210
211         permission.ensureCanEditForum(forumID);
212         ForumBean forumBean = null;
213         Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
214         try {
215             forumBean = DAOFactory.getForumDAO().getForum(forumID);
216         } catch (ObjectNotFoundException e) {
217             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.forumid_not_exists", new Object JavaDoc[] {new Integer JavaDoc(forumID)});
218             throw new ObjectNotFoundException(localizedMessage);
219         }
220
221         request.setAttribute("ForumBean", forumBean);
222     }
223
224     public void processUpdate(GenericRequest request, GenericResponse response)
225         throws BadInputException, ObjectNotFoundException, DatabaseException, DuplicateKeyException,
226         ForeignKeyNotFoundException, AuthenticationException, AssertionException {
227
228         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
229         MVNForumPermission permission = onlineUser.getPermission();
230
231         // primary key column(s)
232
int forumID = GenericParamUtil.getParameterInt(request, "ForumID");
233
234         permission.ensureCanEditForum(forumID);
235
236         MyUtil.saveVNTyperMode(request, response);
237
238         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
239
240         // column(s) to update
241
int categoryID = GenericParamUtil.getParameterInt(request, "CategoryID");
242         String JavaDoc forumName = GenericParamUtil.getParameterSafe(request, "ForumName", true);
243         forumName = DisableHtmlTagFilter.filter(forumName);
244         String JavaDoc forumDesc = GenericParamUtil.getParameterSafe(request, "ForumDesc", false);
245         forumDesc = DisableHtmlTagFilter.filter(forumDesc);
246         Timestamp JavaDoc forumModifiedDate = now;
247         int forumOrder = GenericParamUtil.getParameterUnsignedInt(request, "ForumOrder");
248         int forumType = GenericParamUtil.getParameterInt(request, "ForumType");
249         int forumFormatOption = 0;//GenericParamUtil.getParameterInt(request, "ForumFormatOption");
250
int forumOption = 0;//GenericParamUtil.getParameterInt(request, "ForumOption");
251
int forumStatus = GenericParamUtil.getParameterInt(request, "ForumStatus");
252         int forumModerationMode = GenericParamUtil.getParameterInt(request, "ForumModerationMode");
253
254         // check valid
255
ForumBean.validateForumType(forumType);
256         ForumBean.validateForumFormatOption(forumFormatOption);
257         ForumBean.validateForumOption(forumOption);
258         ForumBean.validateForumStatus(forumStatus);
259         ForumBean.validateForumModerationMode(forumModerationMode);
260
261         DAOFactory.getForumDAO().update(forumID, // primary key
262
categoryID, forumName, forumDesc,
263                                   forumModifiedDate, forumOrder, forumType,
264                                   forumFormatOption, forumOption, forumStatus,
265                                   forumModerationMode);
266
267         // Now clear the cache
268
ForumCache.getInstance().clear();
269     }
270
271     /*
272      * @todo: check permission
273      */

274     public void processUpdateForumOrder(GenericRequest request)
275         throws BadInputException, DatabaseException,
276         ObjectNotFoundException, AuthenticationException, AssertionException {
277
278         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
279         MVNForumPermission permission = onlineUser.getPermission();
280
281         Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
282
283         // primary key column(s)
284
int forumID = GenericParamUtil.getParameterInt(request, "forum");
285
286         permission.ensureCanEditForum(forumID);
287
288         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
289
290         String JavaDoc action = GenericParamUtil.getParameterSafe(request, "action", true);
291         if (action.equals("up")) {
292             DAOFactory.getForumDAO().decreaseForumOrder(forumID, now);
293         } else if (action.equals("down")) {
294             DAOFactory.getForumDAO().increaseForumOrder(forumID, now);
295         } else {
296             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_update_order.unknown_action", new Object JavaDoc[] {action});
297             throw new BadInputException(localizedMessage);
298             //throw new BadInputException("Cannot update ForumOrder: unknown action: " + action);
299
}
300
301         // Now clear the cache
302
ForumCache.getInstance().clear();
303     }
304
305     public void prepareForumManagement(GenericRequest request)
306         throws AssertionException, DatabaseException, AuthenticationException {
307         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
308         MVNForumPermission permission = onlineUser.getPermission();
309         if ((permission.canEditAnyForum() == false) && (permission.canAddForum() == false)) {
310             permission.ensureCanEditAnyForum(); // is this the correct permission
311
permission.ensureCanAddForum();
312         }
313     }
314 }
315
Popular Tags