KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > user > FavoriteThreadWebHandler


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/FavoriteThreadWebHandler.java,v 1.16 2006/04/14 17:05:27 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.16 $
5  * $Date: 2006/04/14 17:05:27 $
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.user;
42
43 import java.sql.Timestamp JavaDoc;
44 import java.util.Locale JavaDoc;
45
46 import com.mvnforum.MVNForumConfig;
47 import com.mvnforum.MVNForumResourceBundle;
48 import com.mvnforum.auth.*;
49 import com.mvnforum.db.*;
50 import net.myvietnam.mvncore.exception.*;
51 import net.myvietnam.mvncore.util.*;
52 import net.myvietnam.mvncore.web.GenericRequest;
53
54 public class FavoriteThreadWebHandler {
55
56     private OnlineUserManager userManager = OnlineUserManager.getInstance();
57
58     public FavoriteThreadWebHandler() {
59     }
60
61     public void processAdd(GenericRequest request)
62         throws BadInputException, CreateException, DatabaseException, ForeignKeyNotFoundException,
63         ObjectNotFoundException, AuthenticationException, AssertionException {
64
65         OnlineUser onlineUser = userManager.getOnlineUser(request);
66         MVNForumPermission permission = onlineUser.getPermission();
67         permission.ensureIsAuthenticated();
68
69         int memberID = onlineUser.getMemberID();
70
71         // check to make sure that this user doesnt exceed his favorite max
72
int currentFavoriteCount = DAOFactory.getFavoriteThreadDAO().getNumberOfFavoriteThreads_inMember(memberID);
73         int maxFavorites = MVNForumConfig.getMaxFavoriteThread();
74         if (currentFavoriteCount >= maxFavorites) {
75             //@todo: choose a better exception class
76
Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
77             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.over_favorite_quota", new Object JavaDoc[] {new Integer JavaDoc(maxFavorites)});
78             throw new BadInputException(localizedMessage);
79             //throw new BadInputException("You have already use all your favorite quota (" + maxFavorites + ").");
80
}
81
82         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
83         int threadID = GenericParamUtil.getParameterInt(request, "thread");
84         Timestamp JavaDoc favoriteCreationDate = now;
85         int favoriteType = 0;//@todo implement it later
86
int favoriteOption = 0;//@todo implement it later
87
int favoriteStatus = 0;//@todo implement it later
88
Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
89
90         ThreadBean threadBean = null;
91         try {
92             threadBean = DAOFactory.getThreadDAO().getThread(threadID);
93         } catch (ObjectNotFoundException e) {
94             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.threadid_not_exists", new Object JavaDoc[] {new Integer JavaDoc(threadID)});
95             throw new ObjectNotFoundException(localizedMessage);
96         }
97         int forumID = threadBean.getForumID();
98
99         ForumCache.getInstance().getBean(forumID).ensureNotDisabledForum();
100
101         // now check permission the this user have the readPost permission
102
permission.ensureCanReadPost(forumID);
103
104         // has the permission now, then insert to database
105
try {
106             DAOFactory.getFavoriteThreadDAO().create(memberID, threadID, forumID,
107                                                favoriteCreationDate, favoriteType, favoriteOption,
108                                                favoriteStatus);
109         } catch (DuplicateKeyException ex) {
110             // already add favorite thread, just ignore
111
}
112     }
113
114     public void processDelete(GenericRequest request)
115         throws BadInputException, DatabaseException, ObjectNotFoundException,
116         AuthenticationException, AssertionException {
117
118         OnlineUser onlineUser = userManager.getOnlineUser(request);
119         MVNForumPermission permission = onlineUser.getPermission();
120         permission.ensureIsAuthenticated();
121
122         // primary key column(s)
123
int memberID = onlineUser.getMemberID();
124         int threadID = GenericParamUtil.getParameterInt(request, "thread");
125
126         DAOFactory.getFavoriteThreadDAO().delete(memberID, threadID);
127     }
128 }
129
Popular Tags