web.AddauthorController.java Source code

Java tutorial

Introduction

Here is the source code for web.AddauthorController.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.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 AddauthorController 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, collabrum, 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 AddAuthors() feature in deluxe version.");
        }

        // ***************************************************************************
        // 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 recollabrum to extend SessionObject.
        // ***************************************************************************
        outOfSession(request, response);

        if (RegexStrUtil.isNull(login) || (loginInfo == null)) {
            return handleUserpageError("Login/loginInfo is null in AddauthorController.");
        }

        boolean checkall = false;
        if ((!RegexStrUtil.isNull(request.getParameter(DbConstants.CHECKALL)))
                && (request.getParameter(DbConstants.CHECKALL).equalsIgnoreCase("on"))) {
            checkall = true;
        }

        String alphabet = request.getParameter(DbConstants.ALPHABET);
        if (RegexStrUtil.isNull(alphabet)) {
            alphabet = "A";
        }

        String directoryId = request.getParameter(DbConstants.DIRECTORY_ID);
        String size = request.getParameter(DbConstants.SIZE);
        if (RegexStrUtil.isNull(size)) {
            return handleError("list size is null, AddauthorController for login " + login);
        }
        logger.info("size = " + size);

        if (!RegexStrUtil.isNull(directoryId)) {
            if (directoryId.length() > GlobalConst.directoryidSize) {
                return handleError("directoryId.length() > WebConstants.directoryIdSize AddauthorController ");
            }
            directoryId = RegexStrUtil.goodNameStr(directoryId);
        } else {
            return handleError("directoryId is null, AddauthorController ");
        }

        /**
         *  adds members as authors for this directory
         */
        DirectoryAuthorDao authorDao = (DirectoryAuthorDao) daoMapper.getDao(DbConstants.DIRECTORY_AUTHOR);
        if ((authorDao == null)) {
            return handleError("DirectoryAuthorDao is null in AddauthorController collabrum, " + login);
        }

        DirectoryDao dirDao = (DirectoryDao) daoMapper.getDao(DbConstants.DIRECTORY);
        if ((dirDao == null)) {
            return handleError("dirDao is null in AddauthorController collabrum, " + login);
        }

        int colListSize = new Integer(size).intValue();
        ArrayList idList = new ArrayList();
        String myString = null;
        String val = "";

        if (checkall) {
            for (int i = 0; i < colListSize; i++) {
                val = new Integer(i).toString();
                myString = "col" + val;
                idList.add(request.getParameter(myString));
            }
        } else {
            for (int i = 0; i < colListSize; i++) {
                val = new Integer(i).toString();
                if ((!RegexStrUtil.isNull(request.getParameter(val)))
                        && (request.getParameter(val).equalsIgnoreCase("on"))) {
                    myString = "col" + val;
                    idList.add(request.getParameter(myString));
                }
            }
        }

        logger.info("idList = " + idList.toString() + ", size=" + idList.size());

        /**
         *  lists the authors of this directory 
         */
        HashSet authorSet = null;
        List users = null;
        HashSet alphabetUsers = null;
        Directory directory = null;
        try {
            if (loginInfo != null) {
                String userId = loginInfo.getValue(DbConstants.LOGIN_ID);
                authorDao.addAuthors(directoryId, userId, login, idList);
                if (checkall) {
                    users = authorDao.getAllUsersAlphabet(directoryId, userId, login, DbConstants.READ_FROM_SLAVE);
                    alphabetUsers = authorDao.getAlphabetSet(users);
                } else {
                    users = authorDao.getUsers(alphabet, directoryId, userId, login, DbConstants.READ_FROM_SLAVE);
                }
                authorSet = authorDao.listAuthorsOfDirectory(directoryId, userId, login,
                        DbConstants.READ_FROM_MASTER);
                directory = dirDao.viewDirectory(directoryId, userId, login, DbConstants.READ_FROM_SLAVE,
                        DbConstants.BLOB_READ_FROM_SLAVE, DbConstants.WEBSITE_READ_FROM_SLAVE);
            }
        } catch (BaseDaoException e) {
            return handleError(
                    "Exception occured, listAuthorsOfDirectory()/viewDirectory()/getUsers/getAllUsersAlphabet()  AddauthorController for login "
                            + login + " ErrorMsg=" + e.getMessage(),
                    e);
        }

        if (authorSet == null) {
            return handleError("authorSet, AddauthorController login " + login);
        }

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

        //if (!WebUtil.isLdapActive()) {
        String viewName = DbConstants.SHOW_USERS;
        Map myModel = new HashMap();
        if (checkall) {
            myModel.put(DbConstants.HIGHLIGHT, "all");
            viewName = DbConstants.SHOW_ALL_USERS_DIR;
        } else {
            viewName = DbConstants.SHOW_USERS;
            myModel.put(DbConstants.HIGHLIGHT, alphabet);
        }
        myModel.put(DbConstants.LOGIN_INFO, loginInfo);
        myModel.put(DbConstants.USERS_ALPHABET, alphabetUsers);
        myModel.put(DbConstants.MEMBERS, authorSet);
        myModel.put(DbConstants.USERS, users);
        myModel.put(DbConstants.COBRAND, cobrand);
        myModel.put(DbConstants.DIRECTORY, directory);
        myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists);
        myModel.put(DbConstants.USER_PAGE, userpage);
        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;
    }
}