KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/GroupsWebHandler.java,v 1.19 2006/04/14 17:05:25 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.19 $
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.*;
45
46 import com.mvnforum.*;
47 import com.mvnforum.auth.*;
48 import com.mvnforum.db.DAOFactory;
49 import com.mvnforum.db.GroupsBean;
50 import net.myvietnam.mvncore.exception.*;
51 import net.myvietnam.mvncore.util.*;
52 import net.myvietnam.mvncore.web.GenericRequest;
53 import net.myvietnam.mvncore.web.GenericResponse;
54
55 public class GroupsWebHandler {
56
57     private OnlineUserManager onlineUserManager = OnlineUserManager.getInstance();
58
59     public GroupsWebHandler() {
60     }
61
62     public void processAdd(GenericRequest request, GenericResponse response)
63         throws ObjectNotFoundException, BadInputException, CreateException, DatabaseException, DuplicateKeyException,
64         ForeignKeyNotFoundException, AuthenticationException, AssertionException {
65
66         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
67         MVNForumPermission permission = onlineUser.getPermission();
68         permission.ensureCanAdminSystem();
69
70         MyUtil.saveVNTyperMode(request, response);
71         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
72
73         String JavaDoc groupName = GenericParamUtil.getParameterSafe(request, "GroupName", true);
74         String JavaDoc groupDesc = GenericParamUtil.getParameterSafe(request, "GroupDesc", false);
75         int groupOption = 0;//GenericParamUtil.getParameterInt(request, "GroupOption");
76

77         // now check the name should not be the default name of group of company
78
if (groupName.toLowerCase().startsWith(MVNForumGlobal.COMPANY_GROUP_FREFIX.toLowerCase())) {
79             throw new BadInputException("Cannot add group with the name reserved for company.");
80         }
81
82         DAOFactory.getGroupsDAO().create(""/*groupOwnerName*/, groupName, groupDesc,
83                                groupOption, now/*groupCreationDate*/, now/*groupModifiedDate*/);
84
85         //now add owner to group if there is owner for this group
86
String JavaDoc groupOwnerName = GenericParamUtil.getParameterSafe(request, "GroupOwnerName", false);
87         if (groupOwnerName.length() > 0) {
88             int groupID = DAOFactory.getGroupsDAO().getGroupIDFromGroupName(groupName);
89             int privilege = 0;//@todo review and support it later, should be GroupAdmin
90
try {
91                 DAOFactory.getGroupsDAO().updateOwner(groupID, // primary key
92
groupOwnerName, now/*groupModifiedDate*/);
93                 DAOFactory.getMemberGroupDAO().create(groupID, groupOwnerName, privilege, now, now);
94             } catch (ForeignKeyNotFoundException ex) {
95                 // what should do when member not found ???
96
// now, I just do nothing
97
} catch (DuplicateKeyException ex) {
98                 // do nothing, it is not an error (member already in this group)
99
}
100         }
101     }
102
103     public void prepareDelete(GenericRequest request)
104         throws ObjectNotFoundException, BadInputException, DatabaseException, AuthenticationException, AssertionException {
105
106         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
107         MVNForumPermission permission = onlineUser.getPermission();
108         permission.ensureCanAdminSystem();
109
110         Locale locale = I18nUtil.getLocaleInRequest(request);
111
112         // primary key column(s)
113
int groupID = GenericParamUtil.getParameterInt(request, "group");
114
115         //make sure reserved groups are never deleted (like "Registered Members" group)
116
if (groupID <= MVNForumConstant.LAST_RESERVED_GROUP_ID) {
117             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete_group.id_less_than");
118             throw new BadInputException(localizedMessage);
119             //throw new BadInputException("Cannot delete group with id <= " + Integer.toString(MVNForumConstant.LAST_RESERVED_GROUP_ID));
120
}
121
122         GroupsBean groupsBean = DAOFactory.getGroupsDAO().getGroup(groupID);
123
124         request.setAttribute("GroupsBean", groupsBean);
125     }
126
127     public void processDelete(GenericRequest request)
128         throws BadInputException, ObjectNotFoundException, DatabaseException, AuthenticationException, AssertionException {
129
130         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
131         MVNForumPermission permission = onlineUser.getPermission();
132         permission.ensureCanAdminSystem();
133
134         Locale locale = I18nUtil.getLocaleInRequest(request);
135
136         // primary key column(s)
137
int groupID = GenericParamUtil.getParameterInt(request, "group");
138
139         //make sure reserved groups are never deleted (like "Registered Members" group)
140
if (groupID <= MVNForumConstant.LAST_RESERVED_GROUP_ID) {
141             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete_group.id_less_than", new Object JavaDoc[]{Integer.toString(MVNForumConstant.LAST_RESERVED_GROUP_ID)});
142             throw new BadInputException(localizedMessage);
143             //throw new BadInputException("Cannot delete group with id <= " + Integer.toString(MVNForumConstant.LAST_RESERVED_GROUP_ID));
144
}
145
146         // now check the name should not be the default name of group of company
147
GroupsBean groupsBean = DAOFactory.getGroupsDAO().getGroup(groupID);
148         if (groupsBean.getGroupName().toLowerCase().startsWith(MVNForumGlobal.COMPANY_GROUP_FREFIX.toLowerCase())) {
149             throw new BadInputException("Cannot delete group with the name reserved for company.");
150         }
151
152         // now check the password
153
MyUtil.ensureCorrectCurrentPassword(request);
154
155         DAOFactory.getGroupForumDAO().delete_inGroup(groupID);
156
157         DAOFactory.getGroupPermissionDAO().delete_inGroup(groupID);
158
159         DAOFactory.getMemberGroupDAO().delete_inGroup(groupID);
160
161         DAOFactory.getGroupsDAO().delete(groupID);
162     }
163
164     public void processUpdate(GenericRequest request, GenericResponse response)
165         throws BadInputException, ObjectNotFoundException, DatabaseException, DuplicateKeyException,
166         AuthenticationException, AssertionException {
167
168         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
169         MVNForumPermission permission = onlineUser.getPermission();
170         permission.ensureCanAdminSystem();
171
172         MyUtil.saveVNTyperMode(request, response);
173         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
174
175         // primary key column(s)
176
int groupID = GenericParamUtil.getParameterInt(request, "group");
177
178         // column(s) to update
179
String JavaDoc groupName = GenericParamUtil.getParameterSafe(request, "GroupName", true);
180         String JavaDoc groupDesc = GenericParamUtil.getParameterSafe(request, "GroupDesc", true);
181
182         // now check the name should not be the default name of group of company
183
GroupsBean groupBean = DAOFactory.getGroupsDAO().getGroup(groupID);
184         if (groupBean.getGroupName().toLowerCase().startsWith(MVNForumGlobal.COMPANY_GROUP_FREFIX.toLowerCase())) {
185             throw new BadInputException("Cannot edit auto-generated group for company.");
186         }
187
188         DAOFactory.getGroupsDAO().update(groupID, // primary key
189
groupName, groupDesc, now/*groupModifiedDate*/);
190     }
191
192     public void processUpdateGroupOwner(GenericRequest request)
193         throws ObjectNotFoundException, BadInputException, DatabaseException, ForeignKeyNotFoundException,
194         CreateException, AuthenticationException, AssertionException {
195
196         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
197         MVNForumPermission permission = onlineUser.getPermission();
198         permission.ensureCanAdminSystem();
199
200         Locale locale = I18nUtil.getLocaleInRequest(request);
201
202         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
203
204         // primary key column(s)
205
int groupID = GenericParamUtil.getParameterInt(request, "group");
206
207         //make sure group owners for reserved groups can't be changed
208
if (groupID <= MVNForumConstant.LAST_RESERVED_GROUP_ID) {
209             String JavaDoc localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete_group_owner_for_reserved_groups");
210             throw new BadInputException(localizedMessage);
211             //throw new AssertionException("Cannot update group owner for reserved (virtual) groups.");
212
}
213         //@todo: Igor: Why don't we allow changing of GroupOwner for reserved groups? I think we have no reason to disallow that.
214

215         // column(s) to update
216
String JavaDoc groupOwnerName = GenericParamUtil.getParameterSafe(request, "GroupOwnerName", false);
217
218         DAOFactory.getGroupsDAO().updateOwner(groupID, // primary key
219
groupOwnerName, now/*groupModifiedDate*/);
220
221         /*
222          * now add owner to group if there is owner for this group
223          * if member already in the group, we dont throw Exception (DuplicateKeyException)
224          */

225         if (groupOwnerName.length() > 0) {
226             int privilege = 0;//@todo review and support it later
227
try {
228                 DAOFactory.getMemberGroupDAO().create(groupID, groupOwnerName, privilege, now, now);
229             } catch (DuplicateKeyException ex) {
230                 // do nothing, it is not an error (member already in this group)
231
}
232         }
233     }
234
235     public void prepareView(GenericRequest request)
236         throws BadInputException, ObjectNotFoundException, DatabaseException, AuthenticationException, AssertionException {
237
238         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
239         MVNForumPermission permission = onlineUser.getPermission();
240         permission.ensureCanAdminSystem();
241
242         // primary key column(s)
243
int groupID = GenericParamUtil.getParameterInt(request, "group");
244
245         GroupsBean groupsBean = DAOFactory.getGroupsDAO().getGroup(groupID);
246
247         request.setAttribute("GroupsBean", groupsBean);
248     }
249
250     public void prepareList(GenericRequest request)
251         throws DatabaseException, AuthenticationException, AssertionException {
252
253         OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
254         MVNForumPermission permission = onlineUser.getPermission();
255         permission.ensureCanAdminSystem();
256
257         Collection groupsBeans = DAOFactory.getGroupsDAO().getGroups();
258
259         // now count the number of members in each group
260
Iterator iterator = groupsBeans.iterator();
261         while(iterator.hasNext()) {
262             GroupsBean groupsBean = (GroupsBean)iterator.next();
263             int groupID = groupsBean.getGroupID();
264             if (groupID == MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS) {
265                 int memberCount = DAOFactory.getMemberDAO().getNumberOfMembers();
266                 if (MVNForumConfig.isGuestUserInDatabase()) {
267                     //"Registered Members" group. Exclude virtual guest from count.
268
memberCount--;
269                 }
270                 groupsBean.setGroupMemberCount(memberCount);
271             } else if (groupID <= MVNForumConstant.LAST_RESERVED_GROUP_ID) {
272                 //other reserved groups
273
groupsBean.setGroupMemberCount(0);
274             } else {
275                 groupsBean.setGroupMemberCount(DAOFactory.getMemberGroupDAO().getNumberOfBeans_inGroup(groupID));
276             }
277         }
278
279         request.setAttribute("GroupsBeans", groupsBeans);
280     }
281
282 }
283
Popular Tags