Java tutorial
/** * Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org] * * This file is part of Redbasin OpenDocShare community project. * * Redbasin OpenDocShare is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package dao; import java.sql.Connection; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import javax.sql.DataSource; import model.Collabrum; import model.Hdlogin; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jboss.cache.Fqn; import util.DbConstants; import util.RegexStrUtil; /** * <B>CollModeratorDaoDb</B> * <BR> * This object implements CollModeratorDao interface * CollModeratorDao provides methods to manage collabrum accesscontrol * (blocking members, assigning moderators, removing moderators etc) * * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ public class CollModeratorDaoDb extends DirectoryAbstractDao implements CollModeratorDao { protected final Log logger = LogFactory.getLog(getClass()); //private volatile DiaryAdmin diaryAdmin; /** * these obejcts are set by spring */ private volatile CollabrumListQuery listCollabrumsQuery; private volatile CollModeratorsListQuery listModeratorQuery; private volatile CollModeratorDeleteQuery deleteModeratorQuery; private volatile CollModeratorAddQuery addModeratorQuery; private volatile CollMemberBlockQuery listBlockedCollabrumsQuery; private volatile CollMembersAddQuery addMemberQuery; private volatile CollMemberExistsQuery isMemberQuery; private volatile CollBlockDeleteQuery deleteBlockedMember; // private volatile CollabrumAdminExistsQuery adminExistsQuery; /** * This method checks if this member is blocked in any social networks * This method is used to display blocked member * userId - the user id * userLogin - the user login * @return List of collabrums that are blocked for this user * @throws BaseDaoException */ public List listBlockedCollabrums(String userId, String userLogin) throws BaseDaoException { if (RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(userLogin)) { throw new BaseDaoException("params are null"); } /** Jboss methods * fqn - full qualified name * check if the blocked collabrums already set in the cache * If it exists, return the blocked collabrums from the cache. */ Fqn fqn = cacheUtil.fqn(DbConstants.BLOCKED_COLLABRUM_LIST); Object obj = treeCache.get(fqn, userId); if (obj != null) { return (List) obj; } /** * Get scalability datasource for collblock partitioned on loginid */ String sourceName = scalabilityManager.getReadScalability(userId); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, listBlockedCollabrums() " + sourceName + " userId = " + userId); } List result = null; try { Object[] params = { (Object) userId }; result = listBlockedCollabrumsQuery.execute(params); } catch (Exception e) { throw new BaseDaoException("error in blocklist collabrums, " + listBlockedCollabrumsQuery.getSql() + " userlogin = " + userLogin); } if ((result != null) && (result.size() > 0)) { treeCache.put(fqn, userId, result); } return result; } /** * This method lists the collabrums that this user belongs, reads from slave datasource * @param userId - the user id, * @param userLogin - the user login * @return List - list of collabrums that this user belongs to * @throws BaseDaoException */ public List listCollabrums(String userId, String userLogin) { if (RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(userLogin)) { throw new BaseDaoException("params are null"); } /** Jboss methods * fqn - full qualified name * check if the organizer already set in the cache * If it exists, return the organizer from the cache. */ Fqn fqn = cacheUtil.fqn(DbConstants.MEM_AS_ORGANIZER_LIST); Object obj = treeCache.get(fqn, userLogin); if (obj != null) { return (List) obj; } /** * Get scalability datasource for collabrum, colladmin (not partitioned) */ String sourceName = scalabilityManager.getReadZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, listCollabrums() " + sourceName + " userId = " + userId); } List result; //Object[] params = {(Object)userId, (Object)userId}; Object[] params = { (Object) userId }; try { result = listCollabrumsQuery.execute(params); } catch (Exception e) { throw new BaseDaoException( "error listing collabrums, " + listCollabrumsQuery.getSql() + " userlogin = " + userLogin); } if ((result != null) && (result.size() > 0)) { treeCache.put(fqn, userLogin, result); } return result; } /** * This method retrieves the list of moderators on slave datasource. * @param userId - the user id, check if this user is Organizer for collabrums * @param userLogin - the user login, checks if this user is diaryAdmin * @return HashSet - the hashSet that contains the list of moderators */ public HashSet listModerators(String userId, String userLogin) throws BaseDaoException { return listModerators(userId, userLogin, DbConstants.READ_FROM_SLAVE); } /** * This method retrieves the list of moderators from a specified datasource * @param userId - the user id, check if this user is Organizer for collabrums * @param userLogin - the user login, checks if this user is diaryAdmin * @param accessFlag - the access flag indicates the datasource master(0) or slave(1) * @return HashSet - the hashSet that contains the list of moderators */ public HashSet listModerators(String userId, String userLogin, int accessFlag) throws BaseDaoException { if (RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(userLogin)) { throw new BaseDaoException("params are null"); } /** Jboss methods * fqn - full qualified name */ Fqn fqn = cacheUtil.fqn(DbConstants.MEM_AS_MODERATOR_LIST); Object obj = treeCache.get(fqn, userLogin); if (obj != null) { return (HashSet) obj; } /** * Get scalability datasource for collabrum, colladmin (not partitioned) */ String sourceName = null; if (accessFlag == DbConstants.READ_FROM_MASTER) { sourceName = scalabilityManager.getWriteZeroScalability(); } else { sourceName = scalabilityManager.getReadZeroScalability(); } ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, listModerators() " + sourceName + " userId = " + userId); } List result = listCollabrums(userId, userLogin); HashSet moderators = new HashSet(); HashSet myResult = null; Connection conn = null; try { conn = ds.getConnection(); for (int i = 0; i < result.size(); i++) { myResult = listModeratorQuery.run(conn, ((Collabrum) result.get(i)).getValue(DbConstants.COLLABRUM_ID)); moderators.add(myResult); } } catch (Exception e) { try { if (conn != null) { conn.close(); } } catch (Exception e1) { throw new BaseDaoException( "error while listing moderator query, conn.close()exception in listModerquery " + userLogin, e1); } throw new BaseDaoException("exception in list moderators, " + userLogin, e); } try { if (conn != null) { conn.close(); } } catch (Exception e2) { throw new BaseDaoException("conn.close exception after completing listmoderatorQuery " + userLogin, e2); } if (moderators != null) { treeCache.put(fqn, userLogin, moderators); } return moderators; } /** * Unblock a member from collabrums * @param colIdList the list of the collabrumid's * @param member member who is to be unblocked * @param userId admin of collabrum * @param userLogin login of the admin of collabrum * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ public void unBlockMemberFromCollabrums(ArrayList colIdList, String member, String userId, String userLogin) throws BaseDaoException { if ((colIdList == null) || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(member) || RegexStrUtil.isNull(userLogin)) { throw new BaseDaoException("params are null"); } Hdlogin hdlogin = getLoginid(member); String memberId = hdlogin.getValue(DbConstants.LOGIN_ID); if (RegexStrUtil.isNull(memberId)) { throw new BaseDaoException("memberId is null"); } /** * check if this user has the permission to add the moderator - isDairyAdmin or Organizer */ for (int i = 0; i < colIdList.size(); i++) { if (!isOrganizer((String) colIdList.get(i), userLogin, userId)) { throw new BaseDaoException("User does not have permission to add moderators, collabrumId =" + colIdList.get(i) + " userId = " + userId); } } /** * Get scalability datasource for collabrum, colladmin (not partitioned) */ String sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, unBlockMember() " + sourceName + " userId = " + userId); } Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); for (int i = 0; i < colIdList.size(); i++) { if (!isMember((String) colIdList.get(i), memberId)) { deleteBlockedMember.run(conn, (String) colIdList.get(i), memberId); } } } catch (Exception e) { try { conn.rollback(); } catch (Exception e1) { try { if (conn != null) { conn.close(); } } catch (Exception e2) { throw new BaseDaoException("conn.close() error, unBlockMember, memberid = " + memberId, e2); } throw new BaseDaoException("error occured while addModertor, memberId = " + memberId, e1); } } // connection commit try { conn.commit(); } catch (Exception e3) { throw new BaseDaoException(" commit() exception, for unBlockMember() " + "memberId = " + memberId, e3); } try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e4) { throw new BaseDaoException(" conn.close() exception, for commit(), unBlockMember() " + "memberId = " + memberId + " userId = " + userId, e4); } /** Jboss methods * fqn - full qualified name * check if the userpage already set in the cache * If it exists, return the userpage from the cache. */ Fqn fqn = cacheUtil.fqn(DbConstants.MEM_AS_MODERATOR_LIST); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.MEM_AS_ORGANIZER_LIST); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.USER_PAGE); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } /** * remove this from the userlogin also */ fqn = cacheUtil.fqn(DbConstants.MEM_AS_MODERATOR_LIST); if (treeCache.exists(fqn, userLogin)) { treeCache.remove(fqn, userLogin); } StringBuffer sb = new StringBuffer(); for (int i = 0; i < colIdList.size(); i++) { sb.append(colIdList.get(i)); sb.append("-"); sb.append(memberId); fqn = cacheUtil.fqn(DbConstants.ORGANIZER); if (treeCache.exists(fqn, sb.toString())) { treeCache.remove(fqn, sb.toString()); } sb.delete(0, sb.length()); fqn = cacheUtil.fqn(DbConstants.COLLABRUM); if (treeCache.exists(fqn, colIdList.get(i))) { treeCache.remove(fqn, colIdList.get(i)); } } fqn = cacheUtil.fqn(DbConstants.BLOCKED_COLLABRUM_LIST); Object obj = treeCache.get(fqn, memberId); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } } /** * Add a member to the collabrum moderator list * @param colIdList the list of the collabrumid's * @param member member who is to be added as moderator * @param userId admin of collabrum * @param userLogin login of the admin of collabrum * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ public void addModerator(ArrayList colIdList, String member, String userId, String userLogin) throws BaseDaoException { if ((colIdList == null) || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(member) || RegexStrUtil.isNull(userLogin)) { throw new BaseDaoException("params are null"); } Hdlogin hdlogin = getLoginid(member); String memberId = hdlogin.getValue(DbConstants.LOGIN_ID); if (RegexStrUtil.isNull(memberId)) { throw new BaseDaoException("memberId is null"); } /** * check if this user has the permission to add the moderator - diaryAdmin or Organizer */ for (int i = 0; i < colIdList.size(); i++) { if (!isOrganizer((String) colIdList.get(i), userLogin, userId)) { throw new BaseDaoException("User does not have permission to add moderators, collabrumId =" + colIdList.get(i) + " userId = " + userId); } } /** * Get scalability datasource for collabrum, colladmin (not partitioned) */ String sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, addModerator() " + sourceName + " userId = " + userId); } Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); addModeratorQuery.run(conn, colIdList, memberId); for (int i = 0; i < colIdList.size(); i++) { if (!isMember((String) colIdList.get(i), memberId)) { deleteBlockedMember.run(conn, (String) colIdList.get(i), memberId); addMemberQuery.run(conn, (String) colIdList.get(i), memberId); } } } catch (Exception e) { try { conn.rollback(); } catch (Exception e1) { try { if (conn != null) { conn.close(); } } catch (Exception e2) { throw new BaseDaoException("conn.close() error, addModerator, memberid = " + memberId, e2); } throw new BaseDaoException("error occured while addModertor, memberId = " + memberId, e1); } } // connection commit try { conn.commit(); } catch (Exception e3) { throw new BaseDaoException(" commit() exception, for addModerator() " + "memberId = " + memberId, e3); } try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e4) { throw new BaseDaoException(" conn.close() exception, for commit(), addModerator() " + "memberId = " + memberId + " userId = " + userId, e4); } /** Jboss methods * fqn - full qualified name * check if the userpage already set in the cache * If it exists, return the userpage from the cache. */ Fqn fqn = cacheUtil.fqn(DbConstants.MEM_AS_MODERATOR_LIST); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.MEM_AS_ORGANIZER_LIST); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.USER_PAGE); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } /** * remove this from the userlogin also */ fqn = cacheUtil.fqn(DbConstants.MEM_AS_MODERATOR_LIST); if (treeCache.exists(fqn, userLogin)) { treeCache.remove(fqn, userLogin); } StringBuffer sb = new StringBuffer(); for (int i = 0; i < colIdList.size(); i++) { sb.append(colIdList.get(i)); sb.append("-"); sb.append(memberId); fqn = cacheUtil.fqn(DbConstants.ORGANIZER); if (treeCache.exists(fqn, sb.toString())) { treeCache.remove(fqn, sb.toString()); } sb.delete(0, sb.length()); fqn = cacheUtil.fqn(DbConstants.COLLABRUM); if (treeCache.exists(fqn, colIdList.get(i))) { treeCache.remove(fqn, colIdList.get(i)); } } fqn = cacheUtil.fqn(DbConstants.BLOCKED_COLLABRUM_LIST); Object obj = treeCache.get(fqn, memberId); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } } /** * This deletes the member from the collabrum organizers/moderators list * @param idList collabrum ids list * @param member member who is to be deleted. * @param userId administrator of collabrum * @param userLogin administrator's login of collabrum * @param isAdmin true if this user is the administrator * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ public void deleteModerator(ArrayList idList, String member, String userId, String userLogin, boolean isAdmin) throws BaseDaoException { if ((idList == null) || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(member) || RegexStrUtil.isNull(userLogin)) { throw new BaseDaoException("params are null"); } /** * check if the userLogin is diaryAdmin or userId an organizer for each of the collabrum's */ if (!isAdmin) { for (int i = 0; i < idList.size(); i++) { if (!isOrganizer((String) idList.get(i), userLogin, userId)) { throw new BaseDaoException("User does not have permission to deleteModerators(), " + idList.get(i) + " userId = " + userId); } } } /** * get the member id */ Hdlogin hdlogin = getLoginid(member); String memberId = hdlogin.getValue(DbConstants.LOGIN_ID); if (RegexStrUtil.isNull(memberId)) { throw new BaseDaoException("memberId is null"); } /** * Get scalability datasource for collabrum, colladmin (not partitioned) */ String sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, in deleteModerators() " + sourceName + " userId = " + userId); } /** * Delete the moderator from colladmin */ Connection conn = null; try { conn = ds.getConnection(); for (int i = 0; i < idList.size(); i++) { deleteModeratorQuery.run(conn, (String) idList.get(i), memberId); } } catch (Exception e) { try { if (conn != null) { conn.close(); } } catch (Exception e1) { throw new BaseDaoException("conn.close() error, deleteModerator, " + memberId, e1); } throw new BaseDaoException("error occured while deleteModertor, " + memberId, e); } try { if (conn != null) { conn.close(); } } catch (Exception e1) { throw new BaseDaoException("conn.close() error, deleteModerator, " + memberId, e1); } Fqn fqn = cacheUtil.fqn(DbConstants.MEM_AS_MODERATOR_LIST); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.MEM_AS_ORGANIZER_LIST); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } /** * remove this from the userlogin also */ fqn = cacheUtil.fqn(DbConstants.MEM_AS_MODERATOR_LIST); if (treeCache.exists(fqn, userLogin)) { treeCache.remove(fqn, userLogin); } StringBuffer sb = new StringBuffer(); String key = null; for (int i = 0; i < idList.size(); i++) { sb.append(idList.get(i)); sb.append("-"); sb.append(memberId); key = sb.toString(); fqn = cacheUtil.fqn(DbConstants.ORGANIZER); if (treeCache.exists(fqn, key)) { treeCache.remove(fqn, key); } fqn = cacheUtil.fqn(DbConstants.IS_ORGANIZER); if (treeCache.exists(fqn, key)) { treeCache.remove(fqn, key); } sb.delete(0, sb.length()); fqn = cacheUtil.fqn(DbConstants.COLLABRUM); if (treeCache.exists(fqn, idList.get(i))) { treeCache.remove(fqn, idList.get(i)); } } fqn = cacheUtil.fqn(DbConstants.USER_PAGE); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.BLOCKED_COLLABRUM_LIST); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } } /** * @param collabrumId the id of the collabrum * @param userId the userId who does the query to get the userIds, need to be organizer. * @return boolean * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ /* private boolean isOrganizer(String collabrumId, String userId) { return collOrganizer(collabrumId, "", userId); Fqn fqn = cacheUtil.fqn(DbConstants.ORGANIZER); StringBuffer sb = new StringBuffer(collabrumId); sb.append("-"); sb.append(userId); String key = sb.toString(); Object obj = treeCache.get(fqn, key); if (obj != null) { if ( ((Member)obj).getValue(DbConstants.IS_ORGANIZER) != null) { return ((Member)obj).getValue(DbConstants.IS_ORGANIZER).equals("1"); } } String sourceName = scalabilityManager.getReadZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, isOrganizer() " + sourceName + " collabrumId = " + collabrumId + " userId = " + userId); } List result = null; try { Object[] params = {(Object)collabrumId, (Object)userId}; result = adminExistsQuery.execute(params); } catch (Exception e) { throw new BaseDaoException("exception error in adminExistsQuery , " + adminExistsQuery.getSql() + " collabrumId = " + collabrumId + " userId = " + userId, e); } boolean isOrganizer = false; if ((result != null) && (result.size() > 0)) { isOrganizer = true; } Member organizer = (Member)eop.newObject(DbConstants.MEMBER); if (isOrganizer) { organizer.setValue(DbConstants.IS_ORGANIZER, "1"); } else { organizer.setValue(DbConstants.IS_ORGANIZER, "0"); } treeCache.put(fqn, key, organizer); return isOrganizer; } */ /** * Is this user a member of collabrum */ private boolean isMember(String collabrumId, String memberId) { try { Object[] params = { (Object) memberId, (Object) collabrumId }; List result = isMemberQuery.execute(params); if ((result != null) && (result.size() > 0)) { return true; } } catch (Exception e) { throw new BaseDaoException("isMemberExists for collabrum, " + isMemberQuery.getSql(), e); } return false; } /** * This property is setby spring automatically at web.xml startup * @param ds - this is JDBC datasource bean that is connection from the pool */ public void setJdbcSource(DataSource ds) { this.ds = ds; } /* public void setDiaryadmin(DiaryAdmin diaryAdmin ) { this.diaryAdmin = diaryAdmin; } */ public void setlistcollabrumsQuery(CollabrumListQuery daq) { this.listCollabrumsQuery = daq; } public void setlistmoderatorQuery(CollModeratorsListQuery daq) { this.listModeratorQuery = daq; } public void setcollmoderatoraddQuery(CollModeratorAddQuery daq) { this.addModeratorQuery = daq; } public void setcollmoderatordeleteQuery(CollModeratorDeleteQuery daq) { this.deleteModeratorQuery = daq; } public void setcollmemberblockQuery(CollMemberBlockQuery daq) { this.listBlockedCollabrumsQuery = daq; } /* public void setcollabrumadminexistsQuery(CollabrumAdminExistsQuery daq) { this.adminExistsQuery = daq; } */ /* public void setEop(ExpiringObjectPool eop) { this.eop = eop; } */ public void setismemberQuery(CollMemberExistsQuery daq) { this.isMemberQuery = daq; } public void setcollmembersaddQuery(CollMembersAddQuery daq) { this.addMemberQuery = daq; } public void setcollblockdeleteQuery(CollBlockDeleteQuery daq) { this.deleteBlockedMember = daq; } }