KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > auth > MVNForumPermissionWebHelper


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/auth/MVNForumPermissionWebHelper.java,v 1.6 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.6 $
5  * $Date: 2006/04/14 17:05:26 $
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.auth;
42
43 import java.sql.*;
44 import java.util.ArrayList JavaDoc;
45
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48 import com.mvnforum.MVNForumConstant;
49 import com.mvnforum.db.*;
50 import net.myvietnam.mvncore.db.DBUtils;
51 import net.myvietnam.mvncore.exception.DatabaseException;
52
53 /** @todo support table prefix */
54 class MVNForumPermissionWebHelper {
55
56     private static Log log = LogFactory.getLog(MVNForumPermissionWebHelper.class);
57
58     private static final String JavaDoc MemberGroup = MemberGroupDAO.TABLE_NAME;
59     private static final String JavaDoc GroupPermission = GroupPermissionDAO.TABLE_NAME;
60     private static final String JavaDoc GroupForum = GroupForumDAO.TABLE_NAME;
61     private static final String JavaDoc MemberPermission= MemberPermissionDAO.TABLE_NAME;
62     private static final String JavaDoc MemberForum = MemberForumDAO.TABLE_NAME;
63
64     private MVNForumPermissionWebHelper() {
65     }
66
67     static ArrayList JavaDoc getMemberPermissions(int memberID)
68         throws DatabaseException {
69
70         Connection connection = null;
71         PreparedStatement statement = null;
72         ResultSet resultSet = null;
73         ArrayList JavaDoc retValue = new ArrayList JavaDoc();
74         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
75         sql.append("SELECT DISTINCT Permission");
76         sql.append(" FROM ").append(MemberPermission);
77         sql.append(" WHERE MemberID = ?");
78
79         //for testing
80
//log.debug("getMemberPermissions sql = " + sql.toString());
81

82         try {
83             connection = DBUtils.getConnection();
84             statement = connection.prepareStatement(sql.toString());
85             statement.setInt(1, memberID);
86             resultSet = statement.executeQuery();
87             while (resultSet.next()) {
88                 Integer JavaDoc perm = new Integer JavaDoc(resultSet.getInt("Permission"));
89                 retValue.add(perm);
90             }
91             return retValue;
92         } catch(SQLException sqle) {
93             log.error("Sql Execution Error!", sqle);
94             throw new DatabaseException("Error executing SQL in MVNForumPermissionWebHelper.getMemberPermissions.");
95         } finally {
96             DBUtils.closeResultSet(resultSet);
97             DBUtils.closeStatement(statement);
98             DBUtils.closeConnection(connection);
99         }
100     }
101
102     static ArrayList JavaDoc getGroupPermissions(int memberID)
103         throws DatabaseException {
104
105         Connection connection = null;
106         PreparedStatement statement = null;
107         ResultSet resultSet = null;
108         ArrayList JavaDoc retValue = new ArrayList JavaDoc();
109         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
110         sql.append("SELECT DISTINCT Permission");
111         sql.append(" FROM ").append(GroupPermission).append(", ").append(MemberGroup);
112         sql.append(" WHERE ( (").append(GroupPermission).append(".GroupID = ").append(MemberGroup).append(".GroupID) AND (").append(MemberGroup).append(".MemberID = ?) )");
113         if ((memberID!=0) && (memberID!=MVNForumConstant.MEMBER_ID_OF_GUEST)) {
114             sql.append(" OR ").append(GroupPermission).append(".GroupID = ").append(MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS);
115         }
116
117         //for testing
118
//log.debug("getGroupPermissions sql = " + sql.toString());
119

120         try {
121             connection = DBUtils.getConnection();
122             statement = connection.prepareStatement(sql.toString());
123             statement.setInt(1, memberID);
124             resultSet = statement.executeQuery();
125             while (resultSet.next()) {
126                 Integer JavaDoc perm = new Integer JavaDoc(resultSet.getInt("Permission"));
127                 retValue.add(perm);
128             }
129             return retValue;
130         } catch(SQLException sqle) {
131             log.error("Sql Execution Error!", sqle);
132             throw new DatabaseException("Error executing SQL in MVNForumPermissionWebHelper.getGroupPermissions.");
133         } finally {
134             DBUtils.closeResultSet(resultSet);
135             DBUtils.closeStatement(statement);
136             DBUtils.closeConnection(connection);
137         }
138     }
139
140     static ArrayList JavaDoc getMemberPermissionsInForums(int memberID)
141         throws DatabaseException {
142
143         Connection connection = null;
144         PreparedStatement statement = null;
145         ResultSet resultSet = null;
146         ArrayList JavaDoc retValue = new ArrayList JavaDoc();
147         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
148         sql.append("SELECT DISTINCT ForumID, Permission");
149         sql.append(" FROM ").append(MemberForum);
150         sql.append(" WHERE MemberID = ?");
151
152         //for testing
153
//log.debug("getMemberPermissionsInForums sql = " + sql.toString());
154

155         try {
156             connection = DBUtils.getConnection();
157             statement = connection.prepareStatement(sql.toString());
158             statement.setInt(1, memberID);
159             resultSet = statement.executeQuery();
160             while (resultSet.next()) {
161                 ForumPermission forumPermission = new ForumPermission();
162                 forumPermission.setForumID(resultSet.getInt("ForumID"));
163                 forumPermission.setPermission(resultSet.getInt("Permission"));
164                 retValue.add(forumPermission);
165             }
166             return retValue;
167         } catch(SQLException sqle) {
168             log.error("Sql Execution Error!", sqle);
169             throw new DatabaseException("Error executing SQL in MVNForumPermissionWebHelper.getMemberPermissionsInForums.");
170         } finally {
171             DBUtils.closeResultSet(resultSet);
172             DBUtils.closeStatement(statement);
173             DBUtils.closeConnection(connection);
174         }
175     }
176
177     static ArrayList JavaDoc getGroupPermissionsInForums(int memberID)
178         throws DatabaseException {
179
180         Connection connection = null;
181         PreparedStatement statement = null;
182         ResultSet resultSet = null;
183         ArrayList JavaDoc retValue = new ArrayList JavaDoc();
184         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
185         sql.append("SELECT DISTINCT ForumID, Permission");// belong to table GroupForum
186
sql.append(" FROM ").append(GroupForum).append(", ").append(MemberGroup);
187         sql.append(" WHERE ( (").append(GroupForum).append(".GroupID = ").append(MemberGroup).append(".GroupID) AND (").append(MemberGroup).append(".MemberID = ?) )");
188         if ((memberID!=0) && (memberID!=MVNForumConstant.MEMBER_ID_OF_GUEST)) {
189             sql.append(" OR ").append(GroupForum).append(".GroupID = ").append(MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS);
190         }
191
192         //for testing
193
//log.debug("getGroupPermissionsInForums sql = " + sql.toString());
194

195         try {
196             connection = DBUtils.getConnection();
197             statement = connection.prepareStatement(sql.toString());
198             statement.setInt(1, memberID);
199             resultSet = statement.executeQuery();
200             while (resultSet.next()) {
201                 ForumPermission forumPermission = new ForumPermission();
202                 forumPermission.setForumID(resultSet.getInt("ForumID"));
203                 forumPermission.setPermission(resultSet.getInt("Permission"));
204                 retValue.add(forumPermission);
205             }
206             return retValue;
207         } catch(SQLException sqle) {
208             log.error("Sql Execution Error!", sqle);
209             throw new DatabaseException("Error executing SQL in MVNForumPermissionWebHelper.getGroupPermissionsInForums.");
210         } finally {
211             DBUtils.closeResultSet(resultSet);
212             DBUtils.closeStatement(statement);
213             DBUtils.closeConnection(connection);
214         }
215     }
216
217     static ArrayList JavaDoc getPermissionsForGroupGuest()
218         throws DatabaseException {
219
220         Connection connection = null;
221         PreparedStatement statement = null;
222         ResultSet resultSet = null;
223         ArrayList JavaDoc retValue = new ArrayList JavaDoc();
224         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
225         sql.append("SELECT Permission");
226         sql.append(" FROM ").append(GroupPermission);
227         sql.append(" WHERE GroupID = ").append(MVNForumConstant.GROUP_ID_OF_GUEST);
228         try {
229             connection = DBUtils.getConnection();
230             statement = connection.prepareStatement(sql.toString());
231             resultSet = statement.executeQuery();
232             while (resultSet.next()) {
233                 Integer JavaDoc perm = new Integer JavaDoc(resultSet.getInt("Permission"));
234                 retValue.add(perm);
235             }
236             return retValue;
237         } catch(SQLException sqle) {
238             log.error("Sql Execution Error!", sqle);
239             throw new DatabaseException("Error executing SQL in MVNForumPermissionWebHelper.getPermissionsForGroupGuest.");
240         } finally {
241             DBUtils.closeResultSet(resultSet);
242             DBUtils.closeStatement(statement);
243             DBUtils.closeConnection(connection);
244         }
245     }
246
247     static ArrayList JavaDoc getPermissionsForGroupGuestInForums()
248         throws DatabaseException {
249
250         ArrayList JavaDoc retValue = new ArrayList JavaDoc();//getMemberPermissionsInForums(MVNForumConstant.MEMBER_ID_OF_GUEST);
251

252         Connection connection = null;
253         PreparedStatement statement = null;
254         ResultSet resultSet = null;
255         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
256         sql.append("SELECT ForumID, Permission");
257         sql.append(" FROM ").append(GroupForum);
258         sql.append(" WHERE GroupID = ").append(MVNForumConstant.GROUP_ID_OF_GUEST);
259
260         //for testing
261
//log.debug("getPermissionsForGroupGuestInForums sql = " + sql.toString());
262

263         try {
264             connection = DBUtils.getConnection();
265             statement = connection.prepareStatement(sql.toString());
266             resultSet = statement.executeQuery();
267             while (resultSet.next()) {
268                 ForumPermission forumPermission = new ForumPermission();
269                 forumPermission.setForumID(resultSet.getInt("ForumID"));
270                 forumPermission.setPermission(resultSet.getInt("Permission"));
271                 retValue.add(forumPermission);
272             }
273             return retValue;
274         } catch(SQLException sqle) {
275             log.error("Sql Execution Error!", sqle);
276             throw new DatabaseException("Error executing SQL in MVNForumPermissionWebHelper.getPermissionsForGroupGuestInForum.");
277         } finally {
278             DBUtils.closeResultSet(resultSet);
279             DBUtils.closeStatement(statement);
280             DBUtils.closeConnection(connection);
281         }
282     }
283
284 }
285
Popular Tags