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.Iterator; import java.util.List; import javax.sql.DataSource; import model.DirAuthor; import model.Directory; 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.DirectoryScopeConstants; import util.RegexStrUtil; /** * <B>DirectoryAuthorDaoDb</B> <BR> * This object implements DirectoryAuthorDao interface * Busines folksonomy access control and author management * * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ public class DirectoryAuthorDaoDb extends DirectoryAbstractDao implements DirectoryAuthorDao { protected final Log logger = LogFactory.getLog(getClass()); private volatile DirectoryScopeConstants dirScope; /** * these objects are set by spring */ private volatile DirectoryAuthorQuery directoryAuthorQuery; private volatile DirectoryDeleteAuthorQuery deleteAuthorQuery; private volatile DirAdminAddQuery addAdminQuery; // private volatile DirAdminExistsQuery diradminexistsQuery; private volatile DirectoryListAuthorQuery listAuthorQuery; private volatile DirectoryBlockQuery blockDirectoryQuery; private volatile DirectoryBlockDeleteQuery deleteBlockQuery; private volatile DirectoryAddBlockQuery addBlockQuery; private volatile DirAuthorChildrenQuery dirAuthorChildrenQuery; private volatile DirectoryDeleteAutoAuthorQuery deleteAutoAuthorQuery; // private volatile DirectoryAuthorScopeQuery listDirAuthorScopeQuery; private volatile DirectoryListAllQuery listDirsQuery; private volatile DirectoryListUsersQuery listDirUsersQuery; private volatile BasicQuery listDirAuthorScopeQuery; private volatile BasicQuery showUsersQuery; private volatile BasicQuery showAllUsersAlphabetQuery; /* private volatile DirectoryWebsiteExistsQuery directoryWebsiteExistsQuery; private volatile DirectoryChildrenExistQuery directorychildrenexistQuery; private volatile ShowusersQuery showUsersQuery; private volatile ShowAllUsersAlphabetQuery showAllUsersAlphabetQuery; */ /** * This methods lists all the blocked directories for this user, reads from a slave * @param userId the user Id * @param userLogin the user Login * @return List - list of all directories that this user belongs to * @throws BaseDaoException */ public List listBlockedDirectories(String userId, String userLogin) throws BaseDaoException { return listBlockedDirectories(userId, userLogin, DbConstants.READ_FROM_SLAVE); } /** * This methods lists all the blocked directories of this userLogin * @param userId the user Id * @param userLogin the user Login * @param accessFlag the acess flag to read from either slave(0) or master (1) * @return List - list of all directories that this user belongs to * @throws BaseDaoException */ public List listBlockedDirectories(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.AUTHOR_BLOCKED_DIRS); Object obj = treeCache.get(fqn, userId); if (obj != null) { return (List) obj; } /** * Get scalability datasource for dirblock partitioned on userId */ String sourceName = null; if (accessFlag == 1) { sourceName = scalabilityManager.getWriteScalability(userId); } else { sourceName = scalabilityManager.getReadScalability(userId); } ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, listBlockedDirectories() " + sourceName + " userId = " + userId); } /** * get the list of directories that are blocked */ List result = null; Object[] params = { (Object) userId }; try { result = blockDirectoryQuery.execute(params); } catch (Exception e) { throw new BaseDaoException("error in blocklist directories, " + blockDirectoryQuery.getSql() + " userlogin = " + userLogin); } /** * add it in the jboss cache, userId is the key */ if ((result != null) && (result.size() > 0)) { treeCache.put(fqn, userId, result); } return result; } /** * This methods lists all the directories of this userLogin as Author, reads from a slave * @param userId the user Id * @param userLogin the user Login * @return List - list of all directories that this user belongs to * @throws BaseDaoException */ public List listDirectories(String userId, String userLogin) throws BaseDaoException { return listDirectories(userId, userLogin, DbConstants.READ_FROM_SLAVE); } /** * This methods lists all the directories of this userLogin as Author * @param userId the user Id * @param userLogin the user Login * @param accessFlag the acess flag to read from either slave(0) or master (1) * @return List - list of all directories that this user belongs to * @throws BaseDaoException */ public List listDirectories(String userId, String userLogin, int accessFlag) throws BaseDaoException { if (RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(userLogin)) { throw new BaseDaoException("params are null"); } /** * check if this user has permission to add authors */ /* boolean isDiaryAdmin = false; Fqn fqn = cacheUtil.fqn(DbConstants.AUTHORS_DIRECTORIES); if ( diaryAdmin.isDiaryAdmin(userLogin)) { fqn = cacheUtil.fqn(DbConstants.ADMIN_DIRS); Object obj = treeCache.get(fqn, DbConstants.ADMIN_DIRS); if (obj != null) { return (List)obj; } isDiaryAdmin = true; } */ /** Jboss methods * fqn - full qualified name */ Fqn fqn = cacheUtil.fqn(DbConstants.AUTHORS_DIRECTORIES); Object obj = treeCache.get(fqn, userLogin); if (obj != null) { return (List) obj; } /** * Get scalability datasource for directory, diradmin, not partitioned */ String sourceName = null; if (accessFlag == 1) { sourceName = scalabilityManager.getWriteZeroScalability(); } else { sourceName = scalabilityManager.getReadZeroScalability(); } ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, listDirectories() " + sourceName + " userId = " + userId); } /** * Get the list of directories for this author */ List result = null; try { Object params[] = { (Object) userId }; result = directoryAuthorQuery.execute(params); } catch (Exception e) { throw new BaseDaoException("exception in getting directories for this author " + directoryAuthorQuery.getSql() + " userId = " + userId + " userlogin = " + userLogin); } if ((result != null) && (result.size() > 0)) { for (int i = 0; i < result.size(); i++) { String directoryId = ((Directory) result.get(i)).getValue(DbConstants.DIRECTORY_ID); /** * children exist, so not a leafnode, set to zero, to check if members can be blocked */ if (childrenExist(directoryId)) { ((Directory) result.get(i)).setValue(DbConstants.IS_LEAFNODE, "0"); } else { ((Directory) result.get(i)).setValue(DbConstants.IS_LEAFNODE, "1"); } } treeCache.put(fqn, userLogin, result); } return result; } /** * This methods lists all the users of directories with * user specific scope * @param loginId - loginId * @param accessFlag - slave(0) or master (1) * @return List - list all users for directories * @throws BaseDaoException */ public HashSet listDirUsers(String loginId, String login, int accessFlag) throws BaseDaoException { if (RegexStrUtil.isNull(loginId) || RegexStrUtil.isNull(login)) { throw new BaseDaoException("params are null"); } /** Jboss methods * fqn - full qualified name */ Fqn fqn = cacheUtil.fqn(DbConstants.DIR_USERS_SCOPE); Object obj = treeCache.get(fqn, loginId); if (obj != null) { return (HashSet) obj; } List dirList = listDirectoriesWithScope(loginId, login, DbConstants.READ_FROM_SLAVE); if (dirList == null) { throw new BaseDaoException("dirList is null, listDirUsers() login = " + login); } /** * Get scalability datasource, not partitioned */ String sourceName = scalabilityManager.getReadZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, listDirUsers() " + sourceName + " loginId = " + loginId); } /** * list users for directory user scope access */ Connection conn = null; HashSet users = new HashSet(); try { conn = ds.getConnection(); for (int i = 0; i < dirList.size(); i++) { if (dirList.get(i) == null) { throw new BaseDaoException("dirList.get(i) is null, i= " + i + "dirAuthor = " + login); } else { String directoryId = ((Directory) dirList.get(i)).getValue(DbConstants.DIRECTORY_ID); HashSet result = null; Object params[] = { (Object) directoryId }; result = listDirUsersQuery.run(conn, directoryId); if ((result != null) && (result.size() > 0)) { users.add(result); } } } // for } catch (Exception e) { try { if (conn != null) { conn.close(); } } catch (Exception e1) { throw new BaseDaoException("error listDirUsers()", e1); } throw new BaseDaoException("error in listDirUsers() ", e); } try { if (conn != null) { conn.close(); } } catch (Exception e) { throw new BaseDaoException("error, listDirUsers() conn.close(), login = " + login, e); } if (users != null) { treeCache.put(fqn, loginId, users); } return users; } /** * This methods lists all directories with user specific scope * @param loginId - loginId * @param login - login * @param accessFlag - accessFlag * @param accessFlag - slave(0) or master (1) * @return List - list all directories with scope for this user * @throws BaseDaoException */ public List listDirectoriesWithScope(String loginId, String login, int accessFlag) throws BaseDaoException { if (RegexStrUtil.isNull(loginId)) { throw new BaseDaoException("params are null"); } /** Jboss methods * fqn - full qualified name */ Fqn fqn = cacheUtil.fqn(DbConstants.AUTHOR_DIRS_WITH_SCOPE); Object obj = treeCache.get(fqn, loginId); if (obj != null) { return (List) obj; } /** * Get scalability datasource not partitioned */ String queryName = null; if (accessFlag == 1) { queryName = scalabilityManager.getWriteZeroScalability("listdirauthorscopequery"); } else { queryName = scalabilityManager.getReadZeroScalability("listdirauthorscopequery"); } listDirAuthorScopeQuery = getQueryMapper().getQuery(queryName); List result = null; try { Object params[] = { (Object) dirScope.getUserspecificscopeid(), (Object) loginId }; result = listDirAuthorScopeQuery.execute(params); } catch (Exception e) { throw new BaseDaoException("error in listing dirs with user access scope " + listDirAuthorScopeQuery.getSql() + " loginId = " + loginId, e); } if ((result != null) && (result.size() > 0)) { treeCache.put(fqn, loginId, result); } return result; } /** * Checks if this directory has any children. * Checks in the dirtree, dircoll, dirwebsites * If children exist, return true otherwise false. * @param directoryId * @return boolean * @throws BaseDaoException */ /* private boolean childrenExist(String directoryId) throws BaseDaoException { if (RegexStrUtil.isNull(directoryId)) { throw new BaseDaoException("directoryId params are null, childrenExist"); } */ /** Jboss methods * Fqn - full qualified name * Check if the directory already set in the cache * If directory exists, get leaf_node, * Return the value of childrenExist from directory that is cached */ /* Fqn fqn = cacheUtil.fqn(DbConstants.DIRECTORY); Object obj = treeCache.get(fqn, directoryId); if (obj != null) { Directory dir = (Directory)obj; String leafNode = dir.getValue(DbConstants.IS_LEAFNODE); // children exist is true, if IS_LEAFNODE == 0 if (!RegexStrUtil.isNull(leafNode) && leafNode.equals("0")) { return true; } else { return false; } } */ /** * Get scalability datasource for directory - not partitioned */ /* String sourceName = scalabilityManager.getReadZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, childrenExist() " + sourceName); } */ /** * Check if the children exist for this directoryId. tables dircoll, dirtree and dirwebsites. * */ /* List result = null; try { Object[] params = {(Object)directoryId, (Object)directoryId}; result = directorychildrenexistQuery.execute(params); } catch (Exception e) { throw new BaseDaoException("error getting children for directory, " + directorychildrenexistQuery.getSql() + " directoryId = " + directoryId, e); } if ((result != null) && (result.size() > 0)) { return true; } */ /** * Get scalability datasource for dirwebsites - partitioned on directoryId */ /* List websiteResult = null; sourceName = scalabilityManager.getReadScalability(directoryId); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, childrenExist() " + sourceName + " directoryId " + directoryId); } try { Object[] myparams = {(Object)directoryId}; websiteResult = directoryWebsiteExistsQuery.execute(myparams); } catch (Exception e) { throw new BaseDaoException("error getting children for dirwebsites, " + directoryWebsiteExistsQuery.getSql() + " directoryId = " + directoryId, e); } if ((websiteResult != null) && (websiteResult.size() > 0)) { return true; } return false; } */ /** * getBlockedDirectories - gets the list of blocked directories * @param authorResult - list of author directories * @param memberResult - list of member directories * @return boolean - returns true or false if the author can block any this member */ public boolean getBlockedDirectories(List authorResult, List memberResult) { HashSet hs1 = new HashSet(authorResult); HashSet hs2 = new HashSet(memberResult); boolean isblocked = hs1.removeAll(hs2); return isblocked; } /** * This method lists the authors for each directory that is in the directory list, reads from slave * This is a list of authors that belong to a directory * userId the user id * userLogin the user login * @return HashSet set is the list of authors * @throws BaseDaoException **/ public HashSet listAuthors(String userId, String userLogin) throws BaseDaoException { return listAuthors(userId, userLogin, DbConstants.READ_FROM_SLAVE); } /** * This method lists all the authors for each directory that is in the directory list of this user * @param userId the user id - who is the author of all these directories * @param userLogin the user login - who is the author of all these directories * @param accessFlag the access flag that specifies the datasource master(1) or slave(0) * @return HashSet set is the list of authors * @throws BaseDaoException */ public HashSet listAuthors(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.AUTHORS_LIST); Object obj = treeCache.get(fqn, userLogin); if (obj != null) { return (HashSet) obj; } /** * lists the directories given the userId */ List result = listDirectories(userId, userLogin); /** * Get scalability datasource for directory, diradmin, hdlogin not partitioned */ String sourceName = scalabilityManager.getReadZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, listAuthors() " + sourceName + " userId = " + userId); } /** * lists the authors for each directory that this user adminsters or is author of * eg: Directory_1 * Authors: * eg: Directory_2 * Authors: */ HashSet authors = new HashSet(); HashSet myResult = null; Connection conn = null; try { conn = ds.getConnection(); for (int i = 0; i < result.size(); i++) { myResult = listAuthorQuery.run(conn, ((Directory) result.get(i)).getValue(DbConstants.DIRECTORY_ID)); authors.add(myResult); } } catch (Exception e) { try { if (conn != null) { conn.close(); } } catch (Exception e1) { throw new BaseDaoException( "error while listing author query, conn.close()exception in listAuthorQuery " + userLogin, e1); } throw new BaseDaoException("exception in list authors, " + userLogin, e); } try { if (conn != null) { conn.close(); } } catch (Exception e2) { throw new BaseDaoException("conn.close exception after completing listAuthorQuery " + userLogin, e2); } if ((authors != null) && (!authors.isEmpty())) { treeCache.put(fqn, userLogin, authors); } return authors; } /** * Adds the author for this directory in the same connection * @param directoryId - directory id * @param userId - user login is used to check if this user has the permission to add authors * @param userLogin - user login is used to check if this user has the permission to add authors * @param authorList author list */ public void addAuthors(String directoryId, String userId, String userLogin, List authorList) throws BaseDaoException { if (RegexStrUtil.isNull(directoryId) || RegexStrUtil.isNull(userLogin) || (authorList == null) || RegexStrUtil.isNull(userId)) { throw new BaseDaoException("addAuthors() params are null"); } logger.info("authorList = " + authorList.size()); /** * check if this user has permission to add authors */ if (!diaryAdmin.isDiaryAdmin(userLogin) && !isAuthor(directoryId, userId)) { throw new BaseDaoException("addAuthors(), User does not have permission to add authors to directory, " + directoryId + " userId = " + userId); } /** * Get scalability datasource for diradmin, not partitioned */ String sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, addAuthors() " + sourceName + " userId = " + userId); } /** * Add author to this directory */ String memberId = null; Connection conn = null; try { logger.info("authorList = " + authorList.size()); conn = ds.getConnection(); conn.setAutoCommit(false); for (int i = 0; i < authorList.size(); i++) { String member = (String) authorList.get(i); logger.info("member = " + authorList.get(i)); if (!RegexStrUtil.isNull(member)) { Hdlogin hdlogin = getLoginid(member); if (hdlogin == null) { throw new BaseDaoException( "addAuthors(), Hdlogin for member is null, addAuthor(), member= " + member); } else { memberId = hdlogin.getValue(DbConstants.LOGIN_ID); logger.info("memberId = " + memberId); addAdminQuery.run(conn, directoryId, memberId); deleteBlockQuery.run(conn, directoryId, memberId); } } } } catch (Exception e) { try { conn.rollback(); } catch (Exception e1) { try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e2) { throw new BaseDaoException( "conn.close() error, addAuthors(), addAdminQuery()/deleteBlockQuery(), memberId" + memberId, e2); } throw new BaseDaoException( " rollback() exception, for addAuthors(), addAdminQuery(), deleteBlockQuery() userId = " + userId, e1); } throw new BaseDaoException( " addAuthors(), addAdminQuery/deleteBlockQuery, exception, userId = " + userId, e); } // connection commit try { conn.commit(); } catch (Exception e3) { throw new BaseDaoException( " commit() exception, for addAuthors(), addAdminQuery/deleteBlockQuery, userId = " + userId, e3); } try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e4) { throw new BaseDaoException( " conn.close() exception, for commit(), addAuthors(), addAdminQuery/deleteBlockQuery(), userId = " + userId, e4); } /** * member's list needs to be updated with the new query, * remove the old entries for this member */ for (int i = 0; i < authorList.size(); i++) { String member = (String) authorList.get(i); if (!RegexStrUtil.isNull(member)) { Hdlogin hdlogin = getLoginid(member); if (hdlogin == null) { throw new BaseDaoException( "addAuthors(), Hdlogin for member is null, addAuthor(), member= " + member); } else { memberId = hdlogin.getValue(DbConstants.LOGIN_ID); } } Fqn fqn = cacheUtil.fqn(DbConstants.AUTHORS_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); } fqn = cacheUtil.fqn(DbConstants.AUTHORS_DIRECTORIES); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } StringBuffer sb = new StringBuffer(directoryId); sb.append("-"); sb.append(memberId); fqn = cacheUtil.fqn(DbConstants.DIR_AUTHOR); if (treeCache.exists(fqn, sb.toString())) { treeCache.remove(fqn, sb.toString()); } fqn = cacheUtil.fqn(DbConstants.AUTHOR_BLOCKED_DIRS); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } } Fqn fqn = cacheUtil.fqn(DbConstants.AUTHORS_LIST); if (treeCache.exists(fqn, userLogin)) { treeCache.remove(fqn, userLogin); } fqn = cacheUtil.fqn(DbConstants.DIRECTORY); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.DIR_AUTHORS); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.DIR_ADMINS); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.ADMIN_DIRS); if (treeCache.exists(fqn, DbConstants.ADMIN_DIRS)) { treeCache.remove(fqn, DbConstants.ADMIN_DIRS); } } /** * Adds the author for this directory. * @param directoryId the directory id * @param member the member who is added as author to this directory * @param userId the user Login is used to check if this user has the permission to add authors * @param userLogin the user Login is used to check if this user has the permission to add authors */ public void addAuthor(String directoryId, String member, String userId, String userLogin) throws BaseDaoException { if (RegexStrUtil.isNull(directoryId) || RegexStrUtil.isNull(userLogin) || RegexStrUtil.isNull(member) || RegexStrUtil.isNull(userId)) { throw new BaseDaoException("params are null"); } /** * check if this user has permission to add authors */ if (!diaryAdmin.isDiaryAdmin(userLogin) && !isAuthor(directoryId, userId)) { throw new BaseDaoException("User does not have permission to add authors to directory, " + directoryId + " userId = " + userId); } /** * Get scalability datasource for diradmin, not partitioned */ String sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, addAuthor() " + sourceName + " userId = " + userId); } /** * Add author to this directory */ String memberId = null; Connection conn = null; try { Hdlogin hdlogin = getLoginid(member); if (hdlogin == null) { throw new BaseDaoException("Hdlogin for member is null, addAuthor(), member= " + member); } else { memberId = hdlogin.getValue(DbConstants.LOGIN_ID); conn = ds.getConnection(); conn.setAutoCommit(false); addAdminQuery.run(conn, directoryId, memberId); deleteBlockQuery.run(conn, directoryId, memberId); } } catch (Exception e) { try { conn.rollback(); } catch (Exception e1) { try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e2) { throw new BaseDaoException("conn.close() error, addAuthor(), memberId" + memberId, e2); } throw new BaseDaoException(" rollback() exception, for addAuthor(), userId = " + userId, e1); } throw new BaseDaoException(" addAuthor exception, userId = " + userId, e); } // connection commit try { conn.commit(); } catch (Exception e3) { throw new BaseDaoException(" commit() exception, for addAuthor() userId = " + userId, e3); } try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e4) { throw new BaseDaoException(" conn.close() exception, for commit(), addAuthor() userId = " + userId, e4); } /** * member's list needs to be updated with the new query, remove the old entries for this member */ Fqn fqn = cacheUtil.fqn(DbConstants.AUTHORS_LIST); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.AUTHORS_LIST); if (treeCache.exists(fqn, userLogin)) { treeCache.remove(fqn, userLogin); } fqn = cacheUtil.fqn(DbConstants.USER_PAGE); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.DIRECTORY); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.DIR_AUTHORS); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.DIR_ADMINS); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.ADMIN_DIRS); if (treeCache.exists(fqn, DbConstants.ADMIN_DIRS)) { treeCache.remove(fqn, DbConstants.ADMIN_DIRS); } fqn = cacheUtil.fqn(DbConstants.AUTHORS_DIRECTORIES); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } StringBuffer sb = new StringBuffer(directoryId); sb.append("-"); sb.append(memberId); fqn = cacheUtil.fqn(DbConstants.DIR_AUTHOR); if (treeCache.exists(fqn, sb.toString())) { treeCache.remove(fqn, sb.toString()); } fqn = cacheUtil.fqn(DbConstants.AUTHOR_BLOCKED_DIRS); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } } /** * Adds the users in directory as dir authors * @param memberList memberList * @param directoryId directoryId * @param userId the user Login is used to check if this user has the permission to add authors * @param userLogin the user Login is used to check if this user has the permission to add authors * @return List - list of users who are not members * @throws BaseDaoException */ public String addAuthors(List memberList, String directoryId, String userId, String userLogin) throws BaseDaoException { if ((memberList == null) || RegexStrUtil.isNull(directoryId) || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(userLogin)) { throw new BaseDaoException("params are null"); } /* get existing authors */ HashSet authorList = listAuthorsOfDirectory(directoryId, userId, userLogin, DbConstants.READ_FROM_SLAVE); List userList = new ArrayList(authorList); List existingUserList = null; if (userList != null) { existingUserList = new ArrayList(); for (int i = 0; i < userList.size(); i++) { if ((Directory) userList.get(i) != null) { existingUserList.add(((Directory) userList.get(i)).getValue(DbConstants.LOGIN)); } } } /* remove existing authors from the memberList */ List idList = null; if ((existingUserList != null) && (existingUserList.size() > 0)) { HashSet hs1 = new HashSet(memberList); HashSet hs2 = new HashSet(existingUserList); if (hs1.removeAll(hs2)) { idList = new ArrayList(hs1); } else { idList = memberList; } } else { idList = memberList; } /* add only new valid users in the directory */ StringBuffer notMembers = new StringBuffer(); try { if (idList != null && idList.size() > 0) { for (int i = 0; i < idList.size(); i++) { if (idList.get(i) != null) { String mLogin = (String) idList.get(i); Hdlogin hdlogin = getLoginid(mLogin); if (hdlogin == null) { notMembers.append(mLogin); notMembers.append(" "); } else { addAuthor(directoryId, mLogin, userId, userLogin); } } } } } catch (BaseDaoException e) { throw new BaseDaoException( "Exception occured in addAuthor(), DirectoryAuthorDao for userLogin " + userLogin, e); } return notMembers.toString(); } /** * Adds the author to multiple directories * @param idList the list of directory ids to which the author is added. * @param member the member who is added as author to this directory * @param userId the user Login is used to check if this user has the permission to add authors * @param userLogin the user Login is used to check if this user has the permission to add authors */ public void addAuthor(ArrayList idList, String member, String userId, String userLogin) throws BaseDaoException { if ((idList == null) || RegexStrUtil.isNull(userLogin) || RegexStrUtil.isNull(member) || RegexStrUtil.isNull(userId)) { throw new BaseDaoException("params are null"); } /** * Get scalability datasource for directory, diradmin, not partitioned */ String sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException( "ds null, addAuthor(ArrayList) " + sourceName + " author to add = " + member); } /** * Add this author to multiple directories */ Connection conn = null; String memberId = null; try { Hdlogin hdlogin = getLoginid(member); memberId = hdlogin.getValue(DbConstants.LOGIN_ID); conn = ds.getConnection(); conn.setAutoCommit(false); for (int i = 0; i < idList.size(); i++) { addAdminQuery.run(conn, (String) idList.get(i), memberId); deleteBlockQuery.run(conn, (String) idList.get(i), memberId); } } catch (Exception e) { try { conn.rollback(); } catch (Exception e1) { try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e2) { throw new BaseDaoException("conn.close() error, addAuthor(ArrayList), memberId" + memberId, e2); } throw new BaseDaoException(" rollback() exception, for addAuthor(ArrayList), userId = " + userId, e1); } throw new BaseDaoException(" addAuthor(ArrayList) exception, userId = " + userId, e); } // connection commit try { conn.commit(); } catch (Exception e3) { throw new BaseDaoException(" commit() exception, for addAuthor(ArrayList) userId = " + userId, e3); } try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e4) { throw new BaseDaoException( " conn.close() exception, for commit(), addAuthor(ArrayList) userId = " + userId, e4); } /** * member's list needs to be updated with the new query, remove the old entries for this member */ Fqn fqn = cacheUtil.fqn(DbConstants.AUTHORS_LIST); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.AUTHORS_LIST); if (treeCache.exists(fqn, userLogin)) { treeCache.remove(fqn, userLogin); } fqn = cacheUtil.fqn(DbConstants.USER_PAGE); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.AUTHORS_DIRECTORIES); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } for (int i = 0; i < idList.size(); i++) { // remove directory from cache fqn = cacheUtil.fqn(DbConstants.DIRECTORY); String directoryId = (String) idList.get(i); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.DIR_AUTHORS); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.DIR_ADMINS); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.ADMIN_DIRS); if (treeCache.exists(fqn, DbConstants.ADMIN_DIRS)) { treeCache.remove(fqn, DbConstants.ADMIN_DIRS); } StringBuffer sb = new StringBuffer(directoryId); sb.append("-"); sb.append(memberId); fqn = cacheUtil.fqn(DbConstants.DIR_AUTHOR); if (treeCache.exists(fqn, sb.toString())) { treeCache.remove(fqn, sb.toString()); } } fqn = cacheUtil.fqn(DbConstants.AUTHOR_BLOCKED_DIRS); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } } /** * Deletes the author for this directory. * @param directoryId the directory id * @param member the member who is removed as author from this directory * @param userId the user Login is used to check if this user has the permission to delete authors * @param userLogin the user Login is used to check if this user has the permission to delete authors */ public void deleteAuthor(String directoryId, String member, String userId, String userLogin) throws BaseDaoException { if (RegexStrUtil.isNull(directoryId) || RegexStrUtil.isNull(userLogin) || RegexStrUtil.isNull(member) || RegexStrUtil.isNull(userId)) { throw new BaseDaoException("params are null"); } /** * check if this user has permission to delete authors */ if (!diaryAdmin.isDiaryAdmin(userLogin) && !isAuthor(directoryId, userId)) { throw new BaseDaoException("User does not have permission to delete authors to directory, " + directoryId + " userId = " + userId); } /** * Get scalability datasource for diradmin, not partitioned */ String sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, deleteAuthor() " + sourceName + " author to delete = " + member); } /** * Delete this author from a directory */ String memberId = null; Connection conn = null; try { Hdlogin hdlogin = getLoginid(member); memberId = hdlogin.getValue(DbConstants.LOGIN_ID); conn = ds.getConnection(); deleteAuthorQuery.run(conn, directoryId, memberId); } catch (Exception e) { try { if (conn != null) { conn.close(); } } catch (Exception e1) { throw new BaseDaoException("exception closing connection in deleting author, " + memberId + " directoryId = " + directoryId, e1); } throw new BaseDaoException( "exception while deleting author, " + memberId + " directoryId = " + directoryId, e); } /** * close the connection */ try { if (conn != null) { conn.close(); } } catch (Exception e) { throw new BaseDaoException("exception while closing the connection for deleteAuthor ", e); } /** * key = directoryid + userId * remove dirauthor from the cache */ StringBuffer sb = new StringBuffer(directoryId); sb.append("-"); sb.append(memberId); String key = sb.toString(); Fqn fqn = cacheUtil.fqn(DbConstants.DIR_AUTHOR); if (treeCache.exists(fqn, key)) { treeCache.remove(fqn, key); } fqn = cacheUtil.fqn(DbConstants.AUTHORS_LIST); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.AUTHORS_LIST); if (treeCache.exists(fqn, userLogin)) { treeCache.remove(fqn, userLogin); } fqn = cacheUtil.fqn(DbConstants.USER_PAGE); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.DIRECTORY); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.DIR_AUTHORS); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.DIR_ADMINS); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.ADMIN_DIRS); if (treeCache.exists(fqn, DbConstants.ADMIN_DIRS)) { treeCache.remove(fqn, DbConstants.ADMIN_DIRS); } fqn = cacheUtil.fqn(DbConstants.AUTHORS_DIRECTORIES); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.DIR_MOVE); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } fqn = cacheUtil.fqn(DbConstants.DIR_COPY); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } fqn = cacheUtil.fqn(DbConstants.DIR_USERS_SCOPE); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } } /** * Deletes the author from multiple directories. * @param idList the list of directory ids * @param member the member to remove as author from this directory * @param userId the user Login is used to check if this user has the permission to delete authors * @param userLogin the user Login is used to check if this user has the permission to delete authors */ public void deleteAuthor(ArrayList idList, String member, String userId, String userLogin) throws BaseDaoException { if ((idList == null) || RegexStrUtil.isNull(userLogin) || RegexStrUtil.isNull(member) || RegexStrUtil.isNull(userId)) { throw new BaseDaoException("params are null"); } /** * Get scalability datasource for diradmin, not partitioned */ String sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, deleteAuthor() " + sourceName + " userId = " + userId); } /** * delete author from a given list of directories (idList) */ String memberId = null; Connection conn = null; try { Hdlogin hdlogin = getLoginid(member); memberId = hdlogin.getValue(DbConstants.LOGIN_ID); conn = ds.getConnection(); conn.setAutoCommit(false); for (int i = 0; i < idList.size(); i++) { deleteAuthorQuery.run(conn, (String) idList.get(i), memberId); } } catch (Exception e) { try { conn.rollback(); } catch (Exception e1) { try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e2) { throw new BaseDaoException("conn.close() error, deleteAuthor, memberId" + memberId, e2); } throw new BaseDaoException(" rollback() exception, for deleteAuthor(), userId = " + userId, e1); } } /** * commit the transaction */ try { conn.commit(); } catch (Exception e3) { throw new BaseDaoException(" commit() exception, for deleteAuthor() userId = " + userId, e3); } /** * autocommit close */ try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e4) { throw new BaseDaoException(" conn.close() exception, for commit(), deleteAuthor() userId = " + userId, e4); } /** Jboss methods * fqn - full qualified name * check if the author already set in the cache * If it exists, remove the authors */ Fqn fqn = cacheUtil.fqn(DbConstants.AUTHORS_DIRECTORIES); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.AUTHORS_LIST); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.AUTHORS_LIST); if (treeCache.exists(fqn, userLogin)) { treeCache.remove(fqn, userLogin); } fqn = cacheUtil.fqn(DbConstants.USER_PAGE); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.DIR_MOVE); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } fqn = cacheUtil.fqn(DbConstants.DIR_COPY); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } fqn = cacheUtil.fqn(DbConstants.DIR_USERS_SCOPE); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } for (int i = 0; i < idList.size(); i++) { fqn = cacheUtil.fqn(DbConstants.DIRECTORY); String directoryId = (String) idList.get(i); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } removeUsersFromDirAuthorsCache(directoryId); fqn = cacheUtil.fqn(DbConstants.DIR_AUTHORS); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.DIR_ADMINS); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.ADMIN_DIRS); if (treeCache.exists(fqn, DbConstants.ADMIN_DIRS)) { treeCache.remove(fqn, DbConstants.ADMIN_DIRS); } StringBuffer sb = new StringBuffer(directoryId); sb.append("-"); sb.append(memberId); fqn = cacheUtil.fqn(DbConstants.DIR_AUTHOR); if (treeCache.exists(fqn, sb.toString())) { treeCache.remove(fqn, sb.toString()); } } } /** * isAuthor() method * checks if this user is the author of this directory and returns the value of either true or false * @param myparams * @param adminResult * @return boolean * @throws BaseDaoException */ /* private boolean isAuthor(String directoryId, String userId) { if (RegexStrUtil.isNull(directoryId) || RegexStrUtil.isNull(userId)) { throw new BaseDaoException("params are null"); } */ /** * key = directoryid + userId * remove dirauthor from the cache */ /* StringBuffer sb = new StringBuffer(directoryId); sb.append("-"); sb.append(userId); String key = sb.toString(); Fqn fqn = cacheUtil.fqn(DbConstants.DIR_AUTHOR); Object obj = treeCache.get(fqn, key); if (obj != null) { return ((DirAuthor)obj).getValue(DbConstants.IS_AUTHOR).equals("1"); } */ /** * Get scalability datasource for diradmin, not partitioned */ /* String sourceName = scalabilityManager.getReadZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, isAuthor() " + sourceName + " userId = " + userId); } try { Object[] myparams = {(Object)directoryId, (Object)userId}; List adminResult = diradminexistsQuery.execute(myparams); if ( (adminResult != null) && (adminResult.size() > 0)) { ((DirAuthor)adminResult.get(0)).setValue(DbConstants.IS_AUTHOR, "1"); treeCache.put(fqn, key, (DirAuthor)adminResult.get(0)); return true; } } catch (BaseDaoException e) { throw new BaseDaoException("exception for isauthor, " + diradminexistsQuery.getSql(), e); } */ /** * put it in the jboss cache */ /* DirAuthor dirAuthor = (DirAuthor)eop.newObject(DbConstants.DIR_AUTHOR); if (dirAuthor != null) { dirAuthor.setValue(DbConstants.IS_AUTHOR, "0"); treeCache.put(fqn, key, dirAuthor); } return false; } */ /** * unblock the blocked directories for this member * @param idList the list of the directory ids * @param memberId memberId who is to be unblocked or deleted from block list. * @param userId administrator of directory * @param userLogin administrator's login of directory * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ public void unBlockDirectories(ArrayList idList, String memberId, String userId, String userLogin) throws BaseDaoException { if ((idList == null) || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(memberId) || RegexStrUtil.isNull(userLogin)) { throw new BaseDaoException("params are null"); } /** * check user permissions */ /* if (!diaryAdmin.isDiaryAdmin(userLogin) && !isAuthor(directoryId, userId) ) { throw new BaseDaoException("User does not have permission to list moderators, " + directoryId + " userId = " + userId); } */ /** * Get scalability datasource for dirblock, partitioned on loginid */ String sourceName = scalabilityManager.getWriteScalability(memberId); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, unBlockDirectories() " + sourceName + " memberId = " + memberId); } Hdlogin hdlogin = getLogin(memberId); List blockedDirs = null; if (hdlogin != null) { blockedDirs = listBlockedDirectories(memberId, hdlogin.getValue(DbConstants.LOGIN), DbConstants.READ_FROM_MASTER); } /** * unblock the directories for this member */ Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); for (int i = 0; i < idList.size(); i++) { /** * delete the blocked member, only if this member has been blocked on this directory */ if (blockedDirs != null) { for (int j = 0; j < blockedDirs.size(); j++) { if ((DirAuthor) blockedDirs.get(j) == null) continue; String dirId = (String) ((DirAuthor) blockedDirs.get(j)).getValue(DbConstants.DIRECTORY_ID); if (dirId.equals((String) idList.get(i))) { deleteBlockQuery.run(conn, (String) idList.get(i), memberId); } } } } } catch (Exception e) { try { conn.rollback(); } catch (Exception e1) { try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e2) { throw new BaseDaoException("conn.close() error, unBlockDirectories, memberId" + memberId, e2); } throw new BaseDaoException(" rollback() exception, for unBlockDirectories(), userId = " + userId, e1); } } /** * commit the transaction */ try { conn.commit(); } catch (Exception e3) { throw new BaseDaoException(" commit() exception, for unBlockDirectories() userId = " + userId, e3); } try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e4) { throw new BaseDaoException( " conn.close() exception, for commit(), unBlockDirectories() userId = " + userId, e4); } Fqn fqn = cacheUtil.fqn(DbConstants.AUTHOR_BLOCKED_DIRS); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } fqn = cacheUtil.fqn(DbConstants.DIR_MOVE); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } fqn = cacheUtil.fqn(DbConstants.DIR_COPY); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } for (int i = 0; i < idList.size(); i++) { fqn = cacheUtil.fqn(DbConstants.DIRECTORY); if (treeCache.exists(fqn, (String) idList.get(i))) { treeCache.remove(fqn, (String) idList.get(i)); } removeUsersFromDirAuthorsCache((String) idList.get(i)); fqn = cacheUtil.fqn(DbConstants.DIR_AUTHORS); if (treeCache.exists(fqn, (String) idList.get(i))) { treeCache.remove(fqn, (String) idList.get(i)); } fqn = cacheUtil.fqn(DbConstants.DIR_ADMINS); if (treeCache.exists(fqn, (String) idList.get(i))) { treeCache.remove(fqn, (String) idList.get(i)); } } fqn = cacheUtil.fqn(DbConstants.ADMIN_DIRS); if (treeCache.exists(fqn, DbConstants.ADMIN_DIRS)) { treeCache.remove(fqn, DbConstants.ADMIN_DIRS); } } /** * Block multiple directories for this member * @param idList the list of the directory ids * @param memberId memberId who is to be blocked. * @param userId administrator of directory * @param userLogin administrator's login of directory * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ public void blockDirectories(ArrayList idList, String memberId, String userId, String userLogin) throws BaseDaoException { if ((idList == null) || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(memberId) || RegexStrUtil.isNull(userLogin)) { throw new BaseDaoException("params are null"); } /** * check user permissions */ /* if (!diaryAdmin.isDiaryAdmin(userLogin) && !isAuthor(directoryId, userId) ) { throw new BaseDaoException("User does not have permission to list moderators, " + directoryId + " userId = " + userId); } */ /** * Get scalability datasource for dirblock, partitioned on loginid **/ String sourceName = scalabilityManager.getWriteScalability(memberId); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, blockDirectories() " + sourceName + " memberId = " + memberId); } Hdlogin hdlogin = getLogin(memberId); List blockedDirs = null; if (hdlogin != null) { blockedDirs = listBlockedDirectories(memberId, hdlogin.getValue(DbConstants.LOGIN), DbConstants.READ_FROM_MASTER); } /** * block the directories for this member */ Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); for (int i = 0; i < idList.size(); i++) { /** * if the user is already blocked, dont block it again */ if (blockedDirs != null) { for (int j = 0; j < blockedDirs.size(); j++) { if ((DirAuthor) blockedDirs.get(j) == null) continue; String dirId = (String) ((DirAuthor) blockedDirs.get(j)).getValue(DbConstants.DIRECTORY_ID); if (dirId.equals((String) idList.get(i))) { continue; } } } addBlockQuery.run(conn, (String) idList.get(i), memberId); deleteAutoAuthorQuery.run(conn, (String) idList.get(i), memberId); } } catch (Exception e) { try { conn.rollback(); } catch (Exception e1) { try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e2) { throw new BaseDaoException("conn.close() error, addBlockDirectories, memberId" + memberId, e2); } throw new BaseDaoException(" rollback() exception, for addBlockDirectories(), userId = " + userId, e1); } } /** * commit the transaction */ try { conn.commit(); } catch (Exception e3) { throw new BaseDaoException(" commit() exception, for addBlockDirectories() userId = " + userId, e3); } try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e4) { throw new BaseDaoException( " conn.close() exception, for commit(), addBlockDirectories() userId = " + userId, e4); } Fqn fqn = cacheUtil.fqn(DbConstants.AUTHOR_BLOCKED_DIRS); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } fqn = cacheUtil.fqn(DbConstants.DIR_MOVE); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } fqn = cacheUtil.fqn(DbConstants.DIR_COPY); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } for (int i = 0; i < idList.size(); i++) { fqn = cacheUtil.fqn(DbConstants.DIRECTORY); if (treeCache.exists(fqn, (String) idList.get(i))) { treeCache.remove(fqn, (String) idList.get(i)); } removeUsersFromDirAuthorsCache((String) idList.get(i)); fqn = cacheUtil.fqn(DbConstants.DIR_AUTHORS); if (treeCache.exists(fqn, (String) idList.get(i))) { treeCache.remove(fqn, (String) idList.get(i)); } fqn = cacheUtil.fqn(DbConstants.DIR_ADMINS); if (treeCache.exists(fqn, (String) idList.get(i))) { treeCache.remove(fqn, (String) idList.get(i)); } } fqn = cacheUtil.fqn(DbConstants.ADMIN_DIRS); if (treeCache.exists(fqn, DbConstants.ADMIN_DIRS)) { treeCache.remove(fqn, DbConstants.ADMIN_DIRS); } } /** * @param directoryId the id of the directory * @param memberId memberId who is to be blocked. * @param userId administrator of directory * @param userLogin administrator's login of directory * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ public void blockDirectory(String directoryId, String memberId, String userId, String userLogin) throws BaseDaoException { if (RegexStrUtil.isNull(directoryId) || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(memberId) || RegexStrUtil.isNull(userLogin)) { throw new BaseDaoException("params are null"); } /** * check user permissions */ if (!diaryAdmin.isDiaryAdmin(userLogin) && !isAuthor(directoryId, userId)) { throw new BaseDaoException( "User does not have permission to list moderators, " + directoryId + " userId = " + userId); } /** * Get scalability datasource for dirblock, partitioned on loginid */ String sourceName = scalabilityManager.getWriteScalability(memberId); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, blockDirectory() " + sourceName + " memberId = " + memberId); } /** * block this directory for this member */ Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); addBlockQuery.run(conn, directoryId, memberId); deleteAuthorQuery.run(conn, directoryId, memberId); } catch (Exception e) { try { conn.rollback(); } catch (Exception e1) { try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e2) { throw new BaseDaoException("conn.close() error, blockDirectory(), memberId" + memberId, e2); } throw new BaseDaoException(" rollback() exception, for blockDirectory(), memberId = " + memberId, e1); } throw new BaseDaoException(" blockDirectory exception, memberId = " + memberId, e); } // connection commit try { conn.commit(); } catch (Exception e3) { throw new BaseDaoException(" commit() exception, for blockDirectory() memberId = " + memberId, e3); } try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e4) { throw new BaseDaoException( " conn.close() exception, for commit(), blockDirectory() memberId = " + memberId, e4); } /** * remove from cache */ Fqn fqn = cacheUtil.fqn(DbConstants.AUTHOR_BLOCKED_DIRS); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } fqn = cacheUtil.fqn(DbConstants.DIRECTORY); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.DIR_MOVE); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } fqn = cacheUtil.fqn(DbConstants.DIR_COPY); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } removeUsersFromDirAuthorsCache(directoryId); fqn = cacheUtil.fqn(DbConstants.DIR_AUTHORS); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.DIR_ADMINS); if (treeCache.exists(fqn, directoryId)) { treeCache.remove(fqn, directoryId); } fqn = cacheUtil.fqn(DbConstants.ADMIN_DIRS); if (treeCache.exists(fqn, DbConstants.ADMIN_DIRS)) { treeCache.remove(fqn, DbConstants.ADMIN_DIRS); } } /** * Given a directoryid, returns HashSet of authors * @param directoryId the id of the directory * @param userId administrator of directory * @param userLogin administrator's login of directory * @param accessFlag access Flag * @return HashSet - list of authors with the login and member name * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ public HashSet listAuthorsOfDirectory(String directoryId, String userId, String userLogin, int accessFlag) { if (RegexStrUtil.isNull(directoryId) || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(userLogin)) { throw new BaseDaoException("params are null"); } /** * check user permissions */ if (!diaryAdmin.isDiaryAdmin(userLogin) && !isAuthor(directoryId, userId)) { throw new BaseDaoException( "User does not have permission to list authors, " + directoryId + " userId = " + userId); } Fqn fqn = cacheUtil.fqn(DbConstants.DIR_AUTHORS); Object obj = treeCache.get(fqn, directoryId); if (obj != null) { return (HashSet) obj; } /** * Get scalability datasource for directory, diradmin, * hdlogin not partitioned */ String sourceName = scalabilityManager.getReadZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null, listAuthors() " + sourceName + " userId = " + userId); } HashSet myResult = null; Connection conn = null; try { conn = ds.getConnection(); myResult = listAuthorQuery.run(conn, directoryId); } catch (Exception e) { try { if (conn != null) { conn.close(); } } catch (Exception e1) { throw new BaseDaoException( "error while listing author query, conn.close()exception in listAuthorQuery " + userLogin, e1); } throw new BaseDaoException("exception in list authors, " + userLogin, e); } try { if (conn != null) { conn.close(); } } catch (Exception e2) { throw new BaseDaoException("conn.close exception after completing listAuthorQuery " + userLogin, e2); } if ((myResult != null) && (!myResult.isEmpty())) { treeCache.put(fqn, directoryId, myResult); } logger.info("myResult = " + myResult.toString()); return myResult; } /** * This methods lists all users * @param directoryId - directoryId * @param userId - userId * @param userLogin - userLogin * @param accessFlag the acess flag to read slave(0) or master (1) * @return List - list of all users alphabets (first,last,login) * @throws BaseDaoException */ public List getAllUsersAlphabet(String directoryId, String userId, String userLogin, int accessFlag) throws BaseDaoException { if (RegexStrUtil.isNull(userLogin) || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(directoryId)) { throw new BaseDaoException("params are null"); } /** * check user permissions */ if (!diaryAdmin.isDiaryAdmin(userLogin) && !isAuthor(directoryId, userId)) { throw new BaseDaoException("User does not have permission to list users for this directory, " + directoryId + " userId = " + userId); } List result = null; Fqn fqn = cacheUtil.fqn(DbConstants.ALPHABET_ALL_USERS); if (treeCache.exists(fqn, DbConstants.ALPHABET_ALL_USERS)) { // return (List)treeCache.get(fqn, DbConstants.ALPHABET_ALL_USERS); result = (List) treeCache.get(fqn, DbConstants.ALPHABET_ALL_USERS); } else { String queryName = null; if (accessFlag == 1) { queryName = scalabilityManager.getWriteZeroScalability("showallusersalphabetquery"); } else { queryName = scalabilityManager.getReadZeroScalability("showallusersalphabetquery"); } showAllUsersAlphabetQuery = getQueryMapper().getQuery(queryName); try { result = showAllUsersAlphabetQuery.execute(); } catch (Exception e) { throw new BaseDaoException("error in getAllUsersAlphabet() " + showAllUsersAlphabetQuery.getSql(), e); } } if (result == null) { return null; } else { HashSet authorSet = listAuthorsOfDirectory(directoryId, userId, userLogin, accessFlag); HashSet dirauthors = new HashSet(); if (authorSet != null && authorSet.size() > 0) { Iterator it = authorSet.iterator(); while (it.hasNext()) { Directory author = (Directory) it.next(); if (author != null) { dirauthors.add(author.getValue(DbConstants.LOGIN)); } } if (result != null && result.size() > 0 && dirauthors.size() > 0) { List newUsers = new ArrayList(); for (int i = 0; i < result.size(); i++) { Hdlogin hdlogin = (Hdlogin) result.get(i); if (hdlogin != null) { if (!RegexStrUtil.isNull(hdlogin.getValue(DbConstants.LOGIN))) { if (!dirauthors.contains(hdlogin.getValue(DbConstants.LOGIN))) { newUsers.add(result.get(i)); // hdlogin.setValue(DbConstants.AUTHOR, "0"); } } } } treeCache.put(fqn, DbConstants.ALPHABET_ALL_USERS, newUsers); return newUsers; } } } return null; } /** * This methods lists all users that match with this alphabet * @param alphabet - alphabet * @param directoryId - directoryId * @param userId - userId * @param userLogin - userLogin * @param accessFlag the acess flag to read slave(0) or master (1) * @return List - list of all users * @throws BaseDaoException */ public List getUsers(String alphabet, String directoryId, String userId, String userLogin, int accessFlag) throws BaseDaoException { if (RegexStrUtil.isNull(directoryId) || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(userLogin)) { throw new BaseDaoException("params are null"); } /** alphabet default is "a" */ if (RegexStrUtil.isNull(alphabet)) { alphabet = "A"; } /** * check user permissions */ if (!diaryAdmin.isDiaryAdmin(userLogin) && !isAuthor(directoryId, userId)) { throw new BaseDaoException("User does not have permission to list users for this directory, " + directoryId + " userId = " + userId); } Fqn fqn = cacheUtil.fqn(DbConstants.USERS_ALPHABET); if (treeCache.exists(fqn, alphabet)) { HashSet dirauthors = new HashSet(); List result = (List) treeCache.get(fqn, alphabet); if (result != null && result.size() > 0) { HashSet authorSet = listAuthorsOfDirectory(directoryId, userId, userLogin, accessFlag); if (authorSet != null && authorSet.size() > 0) { Iterator it = authorSet.iterator(); while (it.hasNext()) { Directory author = (Directory) it.next(); if (author != null) { dirauthors.add(author.getValue(DbConstants.LOGIN)); } } } } if (result != null && result.size() > 0 && dirauthors.size() > 0) { List newUsers = new ArrayList(); for (int i = 0; i < result.size(); i++) { Hdlogin hdlogin = (Hdlogin) result.get(i); if (hdlogin != null) { if (!RegexStrUtil.isNull(hdlogin.getValue(DbConstants.LOGIN))) { if (!dirauthors.contains(hdlogin.getValue(DbConstants.LOGIN))) { newUsers.add(result.get(i)); // hdlogin.setValue(DbConstants.AUTHOR, "0"); } } } } treeCache.put(fqn, alphabet, newUsers); return newUsers; } } String queryName = null; if (accessFlag == 1) { queryName = scalabilityManager.getWriteZeroScalability("showusersquery"); } else { queryName = scalabilityManager.getReadZeroScalability("showusersquery"); } showUsersQuery = getQueryMapper().getQuery(queryName); StringBuffer alphabetString = new StringBuffer("%"); alphabetString.append(alphabet); alphabetString.append("%"); List result = null; try { Object[] params = { (Object) alphabetString.toString(), (Object) alphabetString.toString(), (Object) alphabetString.toString() }; result = showUsersQuery.execute(params); } catch (Exception e) { throw new BaseDaoException("error in getUsers() " + showUsersQuery.getSql(), e); } HashSet authorSet = listAuthorsOfDirectory(directoryId, userId, userLogin, accessFlag); if (authorSet != null && authorSet.size() > 0) { HashSet dirauthors = new HashSet(); Iterator it = authorSet.iterator(); while (it.hasNext()) { Directory author = (Directory) it.next(); if (author != null) { dirauthors.add(author.getValue(DbConstants.LOGIN)); } } if (result != null && result.size() > 0 && dirauthors.size() > 0) { List newUsers = new ArrayList(); for (int i = 0; i < result.size(); i++) { Hdlogin hdlogin = (Hdlogin) result.get(i); if (hdlogin != null) { if (!RegexStrUtil.isNull(hdlogin.getValue(DbConstants.LOGIN))) { if (!dirauthors.contains(hdlogin.getValue(DbConstants.LOGIN))) { newUsers.add(result.get(i)); // hdlogin.setValue(DbConstants.AUTHOR, "0"); } } } } treeCache.put(fqn, alphabet, newUsers); return newUsers; } } return null; } /** * getAlphabetSet - returns the list of alphabet contained for each user * @param result - List of users * @return HashSet - HashSet of Alphabet List of users * @throws BaseDaoException */ public HashSet getAlphabetSet(List result) { if (result == null) { return null; } HashSet hs = new HashSet(); for (int i = 0; i < result.size(); i++) { Hdlogin hdlogin = (Hdlogin) result.get(i); if (hdlogin != null) { // login as E (typically email is used as // login for LDAP/Active Directory if (!RegexStrUtil.isNull(hdlogin.getValue(DbConstants.E))) { hs.add(hdlogin.getValue(DbConstants.E)); } if (!RegexStrUtil.isNull(hdlogin.getValue(DbConstants.F))) { hs.add(hdlogin.getValue(DbConstants.F)); } if (!RegexStrUtil.isNull(hdlogin.getValue(DbConstants.L))) { hs.add(hdlogin.getValue(DbConstants.L)); } } } return hs; } /** * getLoginsOfAuthors - returns the list of authors with their logins * @param authorSet - HashSet of authors with logins and member info * @return List -list of authors with logins * @throws BaseDaoException -when error occurs */ public List getLoginsOfAuthors(HashSet authorSet) throws BaseDaoException { if (authorSet == null) { return null; } List authorList = new ArrayList(); if (authorList != null) { Iterator iterator = authorSet.iterator(); while (iterator.hasNext()) { Directory dir = (Directory) iterator.next(); if (dir != null) { authorList.add(dir.getValue(DbConstants.LOGIN)); } } return authorList; } return null; } /** * 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 setdirectoryauthorQuery(DirectoryAuthorQuery daq) { directoryAuthorQuery = daq; } public void setdiraddadminQuery(DirAdminAddQuery daq) { this.addAdminQuery = daq; } public void setdirectorydeleteauthorQuery(DirectoryDeleteAuthorQuery daq) { this.deleteAuthorQuery = daq; } // directory adminquery /* public void setdiradminexistsQuery(DirAdminExistsQuery daq ) { this.diradminexistsQuery = daq; } */ public void setdirlistauthorQuery(DirectoryListAuthorQuery daq) { this.listAuthorQuery = daq; } public void setdirectoryblockQuery(DirectoryBlockQuery daq) { this.blockDirectoryQuery = daq; } public void setdirectoryaddblockQuery(DirectoryAddBlockQuery daq) { this.addBlockQuery = daq; } public void setdirauthorchildrenQuery(DirAuthorChildrenQuery daq) { this.dirAuthorChildrenQuery = daq; } public void setdirectorydeleteautoauthorQuery(DirectoryDeleteAutoAuthorQuery daq) { this.deleteAutoAuthorQuery = daq; } public void setdirectoryblockdeleteQuery(DirectoryBlockDeleteQuery daq) { this.deleteBlockQuery = daq; } public void setdirlistusersQuery(DirectoryListUsersQuery daq) { this.listDirUsersQuery = daq; } public void setdirscopeconstants(DirectoryScopeConstants dirscope) { this.dirScope = dirscope; } }