web.DirldapshowadduserController.java Source code

Java tutorial

Introduction

Here is the source code for web.DirldapshowadduserController.java

Source

/**
* 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 $
 *
 * Directories can be added only the login users who have permissions to add
 *
 */
public class DirldapshowadduserController extends BaseController implements Controller {

    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    /**
     * 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 will initialize common data in the abstract class and the return result is of no value.
        // The abstract class initializes protected variables, login, logininfo.
        // Which can be accessed in all controllers if they want to.
        // ***************************************************************************

        try {
            ModelAndView m = super.handleRequest(request, response);
        } catch (Exception e) {
            return handleError("error in handleRequest", e);
        }

        if (!WebUtil.isLicenseProfessional(login)) {
            return handleError("Cannot manage directory feature in deluxe version.");
        }

        outOfSession(request, response);
        String directoryId = request.getParameter(DbConstants.DIRECTORY_ID);
        if (RegexStrUtil.isNull(login) || RegexStrUtil.isNull(directoryId)) {
            return handleUserpageError("Login/directoryId is null in DirldapshowadduserController.");
        }

        if (!RegexStrUtil.isNull(directoryId)) {
            if (directoryId.length() > GlobalConst.directoryidSize) {
                return handleError(
                        "directoryId.length() > WebConstants.directoryIdSize DirldapshowadduserController ");
            }
            directoryId = RegexStrUtil.goodNameStr(directoryId);
        }

        String loginId = null;
        if (loginInfo == null) {
            return handleUserpageError("LoginInfo is null in DirldapshowadduserController.");
        } else {
            loginId = loginInfo.getValue(DbConstants.LOGIN_ID);
        }

        String pageSizeStr = request.getParameter(DbConstants.PAGE_SIZE);
        String pageNumStr = request.getParameter(DbConstants.PAGE_NUM);

        int pageSize = GlobalConst.defaultPageSize;
        int pageNum = GlobalConst.defaultPageNum;

        if (!RegexStrUtil.isNull(pageSizeStr)) {
            pageSize = new Integer(pageSizeStr).intValue();
        } else {
            pageSizeStr = new Integer(pageSize).toString();
        }

        if (!RegexStrUtil.isNull(pageNumStr)) {
            pageNum = new Integer(pageNumStr).intValue();
        } else {
            pageNumStr = new Integer(pageNum).toString();
        }

        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);

        /**
         *  add users for a directory
         *  list users for a directory
         */
        DirectoryUserDao dirUserDao = (DirectoryUserDao) daoMapper.getDao(DbConstants.DIR_USER);
        DirectoryDao dirDao = (DirectoryDao) daoMapper.getDao(DbConstants.DIRECTORY);
        if ((dirDao == null) || (dirUserDao == null)) {
            return handleError("DirectoryDao/dirUserDao is null in DirldapshowadduserController  " + login);
        }

        DirectoryAuthorDao authorDao = (DirectoryAuthorDao) daoMapper.getDao(DbConstants.DIRECTORY_AUTHOR);
        if (authorDao == null) {
            return handleError("authorDao is null in DirldapshowadduserController  " + login);
        }

        List userDirList = null;
        Directory directory = null;
        List userLoginList = null;
        List authorsLoginList = null;
        try {
            userDirList = dirUserDao.listUsers(directoryId, DbConstants.READ_FROM_SLAVE);
            userLoginList = dirUserDao.getLoginsOfUsers(userDirList);
            directory = dirDao.viewDirectory(directoryId, loginId, login, DbConstants.READ_FROM_SLAVE,
                    DbConstants.BLOB_READ_FROM_SLAVE, DbConstants.WEBSITE_READ_FROM_SLAVE);
            HashSet authorSet = authorDao.listAuthorsOfDirectory(directoryId, loginId, login,
                    DbConstants.READ_FROM_SLAVE);
            logger.info("authorSet = " + authorSet.toString());
            authorsLoginList = authorDao.getLoginsOfAuthors(authorSet);
        } catch (BaseDaoException e) {
            return handleError(
                    "Exception occured in listUsers/getLoginsOfUsers()/viewDirectory()/listAuthorsOfDirectory()/getLoginsOfAuthors() for login "
                            + login + " directoryId =" + directoryId,
                    e);
        }

        if (userDirList == null) {
            return handleError("userDirList is null DirldapshowadduserController, for directoryid " + directoryId
                    + " for login admin = " + login);
        }

        /**
        * 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 = allLdapUsersUid = null;
        groupUsersGroup = groupUsersMail = groupUsersUid = null;

        List totalLdapUsers = null;
        String maxPageNumStr = null;
        String maxPageSizeStr = null;
        boolean enableUserGroup = false;
        boolean enableShareGroup = false;

        if (WebUtil.isLdapActive()) {
            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, ShowldapusersfordirController");
                } 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 group = null;
                    int userMailIndex = 0;
                    int userGroupIndex = 1;
                    int userUidIndex = 2;

                    if (enableUserGroup || enableShareGroup) {
                        group = dirDao.getDirectoryGroup(login, loginId, directoryId);
                        logger.info("group = " + group);
                        if (!RegexStrUtil.isNull(group)) {
                            groupUsers = ldapConnection.getLdapUsers(LdapUtil.getUserSearchDn(group), attrs,
                                    startChar, endChar, pageSize, pageNum);
                        }
                        if (groupUsers == null) {
                            return handleError(
                                    "ldap groupUsers is null, group= " + group + " directoryid = " + directoryId);
                        } else {
                            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());
                                }
                            }
                            logger.info("group = " + group);
                            logger.info("groupUsers = " + groupUsers.toString());
                        }
                    }

                    /*
                    * When the scope is !enableUserGroup
                    */
                    List allLdapUsers = null;

                    if (!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());
                        }

                        if (totalLdapUsers != null) {
                            logger.info("totalLdapUsers.size() " + totalLdapUsers.size());
                            if (totalLdapUsers.size() > userMailIndex) {
                                List totalLdapUsersMail = (List) totalLdapUsers.get(userMailIndex);
                                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 (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);
            }
        }

        CobrandDao cobrandDao = (CobrandDao) getDaoMapper().getDao(DbConstants.COBRAND);
        if (cobrandDao == null) {
            return handleUserpageError("CobrandDao is null, DirldapshowadduserController");
        }
        Userpage cobrand = cobrandDao.getUserCobrand(loginInfo.getValue(DbConstants.LOGIN_ID));

        String viewName = DbConstants.SHOW_DB_DIR_ADD_USERS;
        Map myModel = new HashMap();
        myModel.put(DbConstants.LOGIN_INFO, loginInfo);
        myModel.put(DbConstants.LOGIN_LIST, userLoginList);
        String users_view = request.getParameter(DbConstants.USERS_VIEW);
        myModel.put(DbConstants.USERS_VIEW, users_view);
        // just the logins of the authors
        myModel.put(DbConstants.AUTHORS_LIST, authorsLoginList);
        myModel.put(DbConstants.MEMBERS, userDirList);
        myModel.put(DbConstants.PAGE_NUM, pageNumStr);
        myModel.put(DbConstants.DIRECTORY, directory);
        myModel.put(DbConstants.START, startStr);
        myModel.put(DbConstants.END, endStr);
        myModel.put(DbConstants.USER_PAGE, userpage);
        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.COBRAND, cobrand);
        myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists);

        if (WebUtil.isLdapActive()) {
            if (enableUserGroup) {
                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);
        }

        if (DiaryAdmin.isDiaryAdmin(login)) {
            myModel.put(DbConstants.BUSINESS_EXISTS, "1");
        } else {
            myModel.put(DbConstants.BUSINESS_EXISTS, isBizExists(login));
        }
        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;
    }
}