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 web; import dao.*; import java.io.IOException; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import ldap.LdapApi; import model.Directory; import model.Userpage; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import util.*; /** * * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ public class ShowldapauthorsfordirController extends BaseController implements Controller { /** Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass()); // These properties are set from the spring config file // private BaseDaoModel daoMapper; /** * This method is called by the spring framework. The configuration * for this controller to be invoked is based on the pagetype and * is set in the urlMapping property in the spring config file. * * @param request the <code>HttpServletRequest</code> * @param response the <code>HttpServletResponse</code> * @throws ServletException * @throws IOException * @return ModelAndView this instance is returned to spring */ public synchronized ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // *************************************************************************** // This is the only line of code you need to get all session info initialized! // Always be the first line before anything else is done. Add to each controller's // handlRequest method. Also remember to extend SessionObject. // *************************************************************************** try { ModelAndView m = super.handleRequest(request, response); } catch (Exception e) { return handleError("error in handleRequest"); } logger.info("ShowldapauthorsfordirController entered"); if (RegexStrUtil.isNull(login)) { return handleUserpageError("Login is null, ShowldapauthorsfordirController"); } String pageNumStr = request.getParameter(DbConstants.PAGE_NUM); String pageSizeStr = request.getParameter(DbConstants.PAGE_SIZE); int pageSize = GlobalConst.defaultPageSize; int pageNum = GlobalConst.defaultPageNum; if (!RegexStrUtil.isNull(pageSizeStr)) { pageSize = new Integer(pageSizeStr).intValue(); } else { pageSizeStr = new Integer(pageSize).toString(); } logger.info("pageSizeStr = " + pageSizeStr); if (!RegexStrUtil.isNull(pageNumStr)) { pageNum = new Integer(pageNumStr).intValue(); } else { pageNumStr = new Integer(pageNum).toString(); } logger.info("pageNumStr = " + pageNumStr); String startStr = GlobalConst.startStr; String endStr = GlobalConst.endStr; String alphabets = request.getParameter(DbConstants.ALPHABET); String[] arrStr = null; if (!RegexStrUtil.isNull(alphabets)) { arrStr = alphabets.split("-"); } else { alphabets = startStr + "-" + endStr; } char startChar = GlobalConst.startChar; char endChar = GlobalConst.endChar; if (arrStr != null && arrStr.length > 1) { logger.info("arrStr = " + arrStr); startChar = ((String) arrStr[0]).charAt(0); startStr = arrStr[0]; endChar = ((String) arrStr[1]).charAt(0); endStr = arrStr[1]; } logger.info("startChar = " + startChar + " endChar = " + endChar); String directoryId = request.getParameter(DbConstants.DIRECTORY_ID); if (RegexStrUtil.isNull(directoryId)) { return handleError("directoryId is null, ShowldapauthorsfordirController"); } if (getDaoMapper() == null) { return handleError("DaoMapper is null, in ShowldapauthorsfordirController." + login); } DirectoryAuthorDao authorDao = (DirectoryAuthorDao) getDaoMapper().getDao(DbConstants.DIRECTORY_AUTHOR); if (authorDao == null) { return handleError("DirectoryAuthorDao is null, in ShowldapauthorsfordirController." + login); } DirectoryDao dirDao = (DirectoryDao) getDaoMapper().getDao(DbConstants.DIRECTORY); if (dirDao == null) { return handleError("dirDao is null, in ShowldapauthorsfordirController." + login); } List users = null; HashSet authorSet = null; Directory directory = null; String loginId = null; List authorLoginList = null; try { if (loginInfo != null) { loginId = loginInfo.getValue(DbConstants.LOGIN_ID); directory = dirDao.viewDirectory(directoryId, loginId, login, DbConstants.READ_FROM_SLAVE, DbConstants.BLOB_READ_FROM_SLAVE, DbConstants.WEBSITE_READ_FROM_SLAVE); authorSet = authorDao.listAuthorsOfDirectory(directoryId, loginId, login, DbConstants.READ_FROM_SLAVE); logger.info("authorSet = " + authorSet.toString()); authorLoginList = authorDao.getLoginsOfAuthors(authorSet); } } catch (BaseDaoException e) { return handleError( "Exception occurred in viewDirectory()/listAuthorsOfDirectory(), ShowldapauthorsfordirController, for login " + login, e); } /** * cobrand */ Userpage cobrand = null; CobrandDao cobrandDao = (CobrandDao) getDaoMapper().getDao(DbConstants.COBRAND); if (cobrandDao == null) { return handleError("CobrandDao is null, ShowldapauthorsfordirController"); } try { cobrand = cobrandDao.getUserCobrand(loginId); } catch (BaseDaoException e) { return handleError("cobrand is null, ShowldapauthorsfordirController.", e); } /** * get all ldap users based on certain attributes * get all ldap group users based on the user grouptype for * this directory * Includes name, email, group */ List allLdapUsersName, allLdapUsersGroup, allLdapUsersMail, allLdapUsersUid; List groupUsersGroup, groupUsersMail, groupUsersUid; allLdapUsersName = allLdapUsersGroup = allLdapUsersMail = null; groupUsersGroup = groupUsersMail = null; groupUsersUid = allLdapUsersUid = null; List totalLdapUsers = null; String maxPageNumStr = null; String maxPageSizeStr = null; String ldapGroup = null; boolean enableUserGroup = false; boolean enableShareGroup = false; if (WebUtil.isLdapActive()) { int userMailIndex = 0; int userGroupIndex = 1; int userUidIndex = 2; try { enableUserGroup = dirDao.isUserGroupEnabled(login, loginId, directoryId); if (!enableUserGroup) { enableShareGroup = dirDao.isShareGroupEnabled(login, loginId, directoryId); } LdapApi ldapConnection = new LdapApi(); if (ldapConnection == null) { return handleError("ldapConnection is null, ShowldapauthorsfordirController"); } else { /** * Returns a list of userlogins, list of emails(login), * Get the group of author, get the list of users who * who belong to this group */ String[] attrs = { LdapConstants.ldapAttrMail, LdapConstants.ldapGroup, LdapConstants.ldapAttrUid }; List groupUsers = null; String groupArea = null; logger.info("enableUserGroup = " + enableUserGroup); logger.info("enableShareGroup = " + enableShareGroup); if (enableUserGroup || enableShareGroup) { groupArea = dirDao.getDirectoryGroupAndAreaInfo(login, loginId, directoryId); logger.info("groupArea = " + groupArea); ldapGroup = dirDao.getDirectoryGroup(login); if (!RegexStrUtil.isNull(groupArea)) { String searchDn = LdapUtil.getUserSearchDn(groupArea); logger.info("searchDn = " + searchDn); groupUsers = ldapConnection.getLdapUsers(searchDn, attrs, startChar, endChar, pageSize, pageNum); if (groupUsers == null) { return handleError("ldap groupUsers is null, groupArea= " + groupArea + " directoryid = " + directoryId); } else { logger.info("groupUsers = " + groupUsers.toString()); if (groupUsers.size() >= userMailIndex) { groupUsersMail = (List) groupUsers.get(userMailIndex); if (groupUsersMail != null) { logger.info("groupUsersMail = " + groupUsersMail.toString()); } } if (groupUsers.size() >= userGroupIndex) { groupUsersGroup = (List) groupUsers.get(userGroupIndex); if (groupUsersGroup != null) { logger.info("groupUsersGroup = " + groupUsersGroup.toString()); } } if (groupUsers.size() >= userUidIndex) { groupUsersUid = (List) groupUsers.get(userUidIndex); if (groupUsersUid != null) { logger.info("groupUsersUid = " + groupUsersUid.toString()); } } } } } /* * When the scope is !enableUserGroup */ List allLdapUsers = null; if (!enableUserGroup) { logger.info("!enableUserGroup "); allLdapUsers = ldapConnection.getLdapUsers(LdapConstants.ldapAdminRoleDn, attrs, startChar, endChar, pageSize, pageNum); totalLdapUsers = ldapConnection.getLdapUsers(LdapConstants.ldapAdminRoleDn, attrs, startChar, endChar); if (allLdapUsers == null) { return handleError("allLdapUsers is null, directoryid = " + directoryId); } else { logger.info("allLdapUsers = " + allLdapUsers.toString()); } List totalLdapUsersMail = null; List totalLdapUsersGroup = null; List totalLdapUsersUid = null; if (totalLdapUsers != null) { if (totalLdapUsers.size() > userMailIndex) { totalLdapUsersMail = (List) totalLdapUsers.get(userMailIndex); } if (totalLdapUsers.size() > userGroupIndex) { totalLdapUsersGroup = (List) totalLdapUsers.get(userGroupIndex); } if (totalLdapUsers.size() > userUidIndex) { totalLdapUsersUid = (List) totalLdapUsers.get(userUidIndex); } logger.info("totalLdapUsers.size() " + totalLdapUsers.size()); logger.info("totalLdapUsers= " + totalLdapUsers.toString()); if (totalLdapUsersMail != null) { int maxPageNum = WebUtil.getMaxPageNumber(totalLdapUsersMail.size(), pageSize); int maxPageSize = WebUtil.getMaxPageSize(totalLdapUsersMail.size(), pageSize); logger.info("maxPageSize = " + maxPageSize); logger.info("maxPageNum = " + maxPageNum); maxPageSizeStr = new Integer(maxPageSize).toString(); maxPageNumStr = new Integer(maxPageNum).toString(); logger.info("maxPageSizeStr = " + maxPageSizeStr); logger.info("maxPageNumStr = " + maxPageNumStr); } } } /** * List the users only when we want * to display enableShareGroup or show the list of users */ /* if (enableShareGroup && groupUsers != null) { allLdapUsersMail = LdapUtil.removeElements((List)allLdapUsers.get(userMailIndex), (List)groupUsers.get(userMailIndex)); allLdapUsersGroup = LdapUtil.removeElements((List)allLdapUsers.get(userGroupIndex), (List)groupUsers.get(userGroupIndex)); } else { } */ if (allLdapUsers != null) { if (allLdapUsers.size() > userMailIndex) { allLdapUsersMail = (List) allLdapUsers.get(userMailIndex); } if (allLdapUsers.size() > userGroupIndex) { allLdapUsersGroup = (List) allLdapUsers.get(userGroupIndex); } if (allLdapUsers.size() > userUidIndex) { allLdapUsersUid = (List) allLdapUsers.get(userUidIndex); } } if (!enableShareGroup && !enableUserGroup) { logger.info("enableShareGroup = " + !enableShareGroup); logger.info("enableUserGroup = " + !enableUserGroup); if (allLdapUsersMail == null || allLdapUsersUid == null || allLdapUsersGroup == null) { logger.info( "allLdapUserseMail/allLdapUsersUid/allldapUsersGroup is null, indicates that all users of ldap belong to the group " + login); return handleError("allLdapUsersMail/allLdapUsersUid/allLdapUsersGroup is null"); } else { if (allLdapUsersMail != null) { logger.info("allLdapUsersMail = " + allLdapUsersMail.toString()); } if (allLdapUsersGroup != null) { logger.info("allLdapUsersGroup = " + allLdapUsersGroup.toString()); } if (allLdapUsersUid != null) { logger.info("allLdapUsersUid = " + allLdapUsersUid.toString()); } } } } } catch (Exception e) { return handleError("ldap Exception getUsers()" + e.getMessage(), e); } } String viewName = DbConstants.SHOW_DB_AUTHORS; Map myModel = new HashMap(); myModel.put(DbConstants.LOGIN_INFO, loginInfo); myModel.put(DbConstants.PAGE_NUM, pageNumStr); myModel.put(DbConstants.DIRECTORY, directory); myModel.put(DbConstants.MEMBERS, authorSet); myModel.put(DbConstants.COBRAND, cobrand); myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists); myModel.put(DbConstants.START, startStr); myModel.put(DbConstants.END, endStr); myModel.put(DbConstants.USER_PAGE, userpage); myModel.put(DbConstants.BUSINESS_EXISTS, isBizExists(login)); myModel.put(DbConstants.MAX_PAGE_NUM, maxPageNumStr); myModel.put(DbConstants.MAX_PAGE_SIZE, maxPageSizeStr); myModel.put(DbConstants.PAGE_SIZE, pageSizeStr); myModel.put(DbConstants.ALPHABET, alphabets); // myModel.put(DbConstants.USERS, users); // myModel.put(DbConstants.USERS_ALPHABET, alphabetUsers); if (WebUtil.isLdapActive()) { if (enableUserGroup) { myModel.put(DbConstants.LDAP_GROUP, ldapGroup); myModel.put(DbConstants.ALL_LDAP_USERS_MAIL, groupUsersMail); myModel.put(DbConstants.ALL_LDAP_USERS_GROUP, groupUsersGroup); myModel.put(DbConstants.ALL_LDAP_USERS_UID, groupUsersUid); } else { myModel.put(DbConstants.ALL_LDAP_USERS_MAIL, allLdapUsersMail); myModel.put(DbConstants.ALL_LDAP_USERS_GROUP, allLdapUsersGroup); myModel.put(DbConstants.ALL_LDAP_USERS_UID, allLdapUsersUid); } if (enableUserGroup) { myModel.put(DbConstants.ENABLE_USER_GROUP, "1"); } else { myModel.put(DbConstants.ENABLE_USER_GROUP, "0"); } /* if (enableShareGroup) { myModel.put(DbConstants.ENABLE_SHARE_GROUP, "1"); } else { myModel.put(DbConstants.ENABLE_SHARE_GROUP, "0"); } */ myModel.put(DbConstants.TOTAL_LDAP_USERS, totalLdapUsers); } myModel.put(DbConstants.LOGIN_LIST, authorLoginList); return new ModelAndView(viewName, "model", myModel); } /** * BaseDaoModel set by the spring framework. It maps the DAO implementation * to the pagetype. * @param daoMapper set the BaseDaoModel instance */ public void setDaoMapper(BaseDaoModel daoMapper) { this.daoMapper = daoMapper; } /** * BaseDaoModel set by the spring framework. It maps the DAO implementation * to the pagetype. * @return BaseDaoModel get the BaseDaoModel instance */ public BaseDaoModel getDaoMapper() { return daoMapper; } }