001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/auth/MVNForumPermissionWebHelper.java,v 1.9 2007/10/09 11:09:12 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.9 $
005: * $Date: 2007/10/09 11:09:12 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Minh Nguyen
039: * @author: Mai Nguyen
040: */
041: package com.mvnforum.auth;
042:
043: import java.sql.*;
044: import java.util.ArrayList;
045:
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048: import com.mvnforum.MVNForumConstant;
049: import com.mvnforum.db.*;
050: import net.myvietnam.mvncore.db.DBUtils;
051: import net.myvietnam.mvncore.exception.DatabaseException;
052:
053: /** @todo support table prefix */
054: class MVNForumPermissionWebHelper {
055:
056: private static Log log = LogFactory
057: .getLog(MVNForumPermissionWebHelper.class);
058:
059: private static final String MemberGroup = MemberGroupDAO.TABLE_NAME;
060: private static final String GroupPermission = GroupPermissionDAO.TABLE_NAME;
061: private static final String GroupForum = GroupForumDAO.TABLE_NAME;
062: private static final String MemberPermission = MemberPermissionDAO.TABLE_NAME;
063: private static final String MemberForum = MemberForumDAO.TABLE_NAME;
064:
065: private MVNForumPermissionWebHelper() {
066: }
067:
068: static ArrayList getMemberPermissions(int memberID)
069: throws DatabaseException {
070:
071: Connection connection = null;
072: PreparedStatement statement = null;
073: ResultSet resultSet = null;
074: ArrayList retValue = new ArrayList();
075: StringBuffer sql = new StringBuffer(512);
076: sql.append("SELECT DISTINCT Permission");
077: sql.append(" FROM ").append(MemberPermission);
078: sql.append(" WHERE MemberID = ?");
079:
080: //for testing
081: //log.debug("getMemberPermissions sql = " + sql.toString());
082:
083: try {
084: connection = DBUtils.getConnection();
085: statement = connection.prepareStatement(sql.toString());
086: statement.setInt(1, memberID);
087: resultSet = statement.executeQuery();
088: while (resultSet.next()) {
089: Integer perm = new Integer(resultSet
090: .getInt("Permission"));
091: retValue.add(perm);
092: }
093: return retValue;
094: } catch (SQLException sqle) {
095: log.error("Sql Execution Error!", sqle);
096: throw new DatabaseException(
097: "Error executing SQL in MVNForumPermissionWebHelper.getMemberPermissions.");
098: } finally {
099: DBUtils.closeResultSet(resultSet);
100: DBUtils.closeStatement(statement);
101: DBUtils.closeConnection(connection);
102: }
103: }
104:
105: static ArrayList getGroupPermissions(int memberID)
106: throws DatabaseException {
107:
108: Connection connection = null;
109: PreparedStatement statement = null;
110: ResultSet resultSet = null;
111: ArrayList retValue = new ArrayList();
112: StringBuffer sql = new StringBuffer(512);
113: sql.append("SELECT DISTINCT Permission");
114: sql.append(" FROM ").append(GroupPermission).append(
115: " groupperm, ").append(MemberGroup).append(" memgroup");
116: sql
117: .append(" WHERE ( (groupperm.GroupID = memgroup.GroupID) AND (memgroup.MemberID = ?) )");
118: if ((memberID != 0)
119: && (memberID != MVNForumConstant.MEMBER_ID_OF_GUEST)) {
120: sql.append(" OR groupperm.GroupID = ").append(
121: MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS);
122: }
123:
124: //for testing
125: //log.debug("getGroupPermissions sql = " + sql.toString());
126:
127: try {
128: connection = DBUtils.getConnection();
129: statement = connection.prepareStatement(sql.toString());
130: statement.setInt(1, memberID);
131: resultSet = statement.executeQuery();
132: while (resultSet.next()) {
133: Integer perm = new Integer(resultSet
134: .getInt("Permission"));
135: retValue.add(perm);
136: }
137: return retValue;
138: } catch (SQLException sqle) {
139: log.error("Sql Execution Error!", sqle);
140: throw new DatabaseException(
141: "Error executing SQL in MVNForumPermissionWebHelper.getGroupPermissions.");
142: } finally {
143: DBUtils.closeResultSet(resultSet);
144: DBUtils.closeStatement(statement);
145: DBUtils.closeConnection(connection);
146: }
147: }
148:
149: static ArrayList getMemberPermissionsInForums(int memberID)
150: throws DatabaseException {
151:
152: Connection connection = null;
153: PreparedStatement statement = null;
154: ResultSet resultSet = null;
155: ArrayList retValue = new ArrayList();
156: StringBuffer sql = new StringBuffer(512);
157: sql.append("SELECT DISTINCT ForumID, Permission");
158: sql.append(" FROM ").append(MemberForum);
159: sql.append(" WHERE MemberID = ?");
160:
161: //for testing
162: //log.debug("getMemberPermissionsInForums sql = " + sql.toString());
163:
164: try {
165: connection = DBUtils.getConnection();
166: statement = connection.prepareStatement(sql.toString());
167: statement.setInt(1, memberID);
168: resultSet = statement.executeQuery();
169: while (resultSet.next()) {
170: ForumPermission forumPermission = new ForumPermission();
171: forumPermission.setForumID(resultSet.getInt("ForumID"));
172: forumPermission.setPermission(resultSet
173: .getInt("Permission"));
174: retValue.add(forumPermission);
175: }
176: return retValue;
177: } catch (SQLException sqle) {
178: log.error("Sql Execution Error!", sqle);
179: throw new DatabaseException(
180: "Error executing SQL in MVNForumPermissionWebHelper.getMemberPermissionsInForums.");
181: } finally {
182: DBUtils.closeResultSet(resultSet);
183: DBUtils.closeStatement(statement);
184: DBUtils.closeConnection(connection);
185: }
186: }
187:
188: static ArrayList getGroupPermissionsInForums(int memberID)
189: throws DatabaseException {
190:
191: Connection connection = null;
192: PreparedStatement statement = null;
193: ResultSet resultSet = null;
194: ArrayList retValue = new ArrayList();
195: StringBuffer sql = new StringBuffer(512);
196: sql.append("SELECT DISTINCT ForumID, Permission");// belong to table GroupForum
197: sql.append(" FROM ").append(GroupForum).append(" grpforum, ")
198: .append(MemberGroup).append(" memgroup");
199: sql
200: .append(" WHERE ( (grpforum.GroupID = memgroup.GroupID) AND (memgroup.MemberID = ?) )");
201: if ((memberID != 0)
202: && (memberID != MVNForumConstant.MEMBER_ID_OF_GUEST)) {
203: sql.append(" OR grpforum.GroupID = ").append(
204: MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS);
205: }
206:
207: //for testing
208: //log.debug("getGroupPermissionsInForums sql = " + sql.toString());
209:
210: try {
211: connection = DBUtils.getConnection();
212: statement = connection.prepareStatement(sql.toString());
213: statement.setInt(1, memberID);
214: resultSet = statement.executeQuery();
215: while (resultSet.next()) {
216: ForumPermission forumPermission = new ForumPermission();
217: forumPermission.setForumID(resultSet.getInt("ForumID"));
218: forumPermission.setPermission(resultSet
219: .getInt("Permission"));
220: retValue.add(forumPermission);
221: }
222: return retValue;
223: } catch (SQLException sqle) {
224: log.error("Sql Execution Error!", sqle);
225: throw new DatabaseException(
226: "Error executing SQL in MVNForumPermissionWebHelper.getGroupPermissionsInForums.");
227: } finally {
228: DBUtils.closeResultSet(resultSet);
229: DBUtils.closeStatement(statement);
230: DBUtils.closeConnection(connection);
231: }
232: }
233:
234: static ArrayList getPermissionsForGroupGuest()
235: throws DatabaseException {
236:
237: Connection connection = null;
238: PreparedStatement statement = null;
239: ResultSet resultSet = null;
240: ArrayList retValue = new ArrayList();
241: StringBuffer sql = new StringBuffer(512);
242: sql.append("SELECT Permission");
243: sql.append(" FROM ").append(GroupPermission);
244: sql.append(" WHERE GroupID = ").append(
245: MVNForumConstant.GROUP_ID_OF_GUEST);
246: try {
247: connection = DBUtils.getConnection();
248: statement = connection.prepareStatement(sql.toString());
249: resultSet = statement.executeQuery();
250: while (resultSet.next()) {
251: Integer perm = new Integer(resultSet
252: .getInt("Permission"));
253: retValue.add(perm);
254: }
255: return retValue;
256: } catch (SQLException sqle) {
257: log.error("Sql Execution Error!", sqle);
258: throw new DatabaseException(
259: "Error executing SQL in MVNForumPermissionWebHelper.getPermissionsForGroupGuest.");
260: } finally {
261: DBUtils.closeResultSet(resultSet);
262: DBUtils.closeStatement(statement);
263: DBUtils.closeConnection(connection);
264: }
265: }
266:
267: static ArrayList getPermissionsForGroupGuestInForums()
268: throws DatabaseException {
269:
270: ArrayList retValue = new ArrayList();//getMemberPermissionsInForums(MVNForumConstant.MEMBER_ID_OF_GUEST);
271:
272: Connection connection = null;
273: PreparedStatement statement = null;
274: ResultSet resultSet = null;
275: StringBuffer sql = new StringBuffer(512);
276: sql.append("SELECT ForumID, Permission");
277: sql.append(" FROM ").append(GroupForum);
278: sql.append(" WHERE GroupID = ").append(
279: MVNForumConstant.GROUP_ID_OF_GUEST);
280:
281: //for testing
282: //log.debug("getPermissionsForGroupGuestInForums sql = " + sql.toString());
283:
284: try {
285: connection = DBUtils.getConnection();
286: statement = connection.prepareStatement(sql.toString());
287: resultSet = statement.executeQuery();
288: while (resultSet.next()) {
289: ForumPermission forumPermission = new ForumPermission();
290: forumPermission.setForumID(resultSet.getInt("ForumID"));
291: forumPermission.setPermission(resultSet
292: .getInt("Permission"));
293: retValue.add(forumPermission);
294: }
295: return retValue;
296: } catch (SQLException sqle) {
297: log.error("Sql Execution Error!", sqle);
298: throw new DatabaseException(
299: "Error executing SQL in MVNForumPermissionWebHelper.getPermissionsForGroupGuestInForum.");
300: } finally {
301: DBUtils.closeResultSet(resultSet);
302: DBUtils.closeStatement(statement);
303: DBUtils.closeConnection(connection);
304: }
305: }
306:
307: }
|