web.DirectorygetblobController.java Source code

Java tutorial

Introduction

Here is the source code for web.DirectorygetblobController.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.text.NumberFormat;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Directory;
import model.Photo;
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.DbConstants;
import util.GlobalConst;
import util.RegexStrUtil;
import util.WebUtil;

/**
 *
 * @author Smitha Gudur (smitha@redbasin.com)
 * @version $Revision: 1.1 $
 *
 * Get the blob tags
 *
 */
public class DirectorygetblobController 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 
        // return result is of no value.
        // The abstract class initializes protected variables,
        // login, directoryid, logininfo.
        // Which can be accessed in all controllers if they want to.
        // ***************************************************************************

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

        if (!WebUtil.isProductRedbasinPortalDataCenter()) {
            if (!WebUtil.isLicenseProfessional(login)) {
                return handleError("Cannot access directory feature in deluxe version.");
            }
            if (WebUtil.isProductSecureBusinessDataCenter()) {
                outOfSession(request, response);
                if (RegexStrUtil.isNull(login) || loginInfo == null) {
                    return handleUserpageError("Login or loginInfo is null, DirectorygetblobController");
                }
            }
        } else {
            /* ok to access this in some portal product */
            if (!WebUtil.isLicenseProfessional(login)
                    && (!GlobalConst.hddomain.contains(DbConstants.INDIA_CENTURY))) {
                return handleError("Cannot access directory feature in deluxe version.");
            }
        }

        logger.info("login after checks = " + login);

        /**
         * users should be allowed to view the directory blobs without the session 
         */
        String directoryid = request.getParameter(DbConstants.DIRECTORY_ID);
        if (RegexStrUtil.isNull(directoryid)) {
            return handleError("request, directory is null, for directorygetblobController ");
        }
        if (directoryid.length() > GlobalConst.directoryidSize) {
            return handleError(
                    "directoryid.length() > WebConstants.directoryidSize, for directorygetblobController ");
        }
        directoryid = RegexStrUtil.goodNameStr(directoryid);

        String blobtype = request.getParameter(DbConstants.BLOBTYPE);
        if (!RegexStrUtil.isNull(blobtype)) {
            int catValue = new Integer(blobtype).intValue();
            if (catValue < GlobalConst.categoryMinSize || catValue > GlobalConst.categoryMaxSize) {
                return handleError("category/blobtype is > categoryMin/MaxSize, DirectorygetblobController");
            }
        }

        DirectoryDao directoryDao = (DirectoryDao) daoMapper.getDao(DbConstants.DIRECTORY);
        if (directoryDao == null) {
            return handleError("DirectoryDao is null in DirectorygetblobController directory");
        }

        DirectoryStreamBlobDao blobDao = (DirectoryStreamBlobDao) daoMapper.getDao(DbConstants.DIR_BLOB);
        if (blobDao == null) {
            return handleError("DirectoryStreamBlobDao is null, DirectorygetblobController");
        }

        String sortByStr = request.getParameter(DbConstants.SORT_BY);
        int sortBy = DbConstants.INTDATEDESC;
        if (!RegexStrUtil.isNull(sortByStr)) {
            logger.info("sortBy = " + sortByStr);
            sortBy = new Integer(sortByStr).intValue();
        }

        String selection = DbConstants.DATE_DESC_STR;
        List tagList = null;
        Directory directory = null;
        List files = null;
        try {
            if (login != null && loginInfo != null) {
                // default is DATEASC
                directory = directoryDao.viewDirectory(directoryid, loginInfo.getValue(DbConstants.LOGIN_ID), login,
                        DbConstants.READ_FROM_SLAVE, DbConstants.BLOB_READ_FROM_SLAVE,
                        DbConstants.WEBSITE_READ_FROM_SLAVE);
                switch (sortBy) {
                case DbConstants.INTSIZEASC:
                    files = blobDao.getFilesSortBySizeAsc(directoryid, blobtype,
                            loginInfo.getValue(DbConstants.LOGIN_ID));
                    selection = DbConstants.SIZE_ASC_STR;
                    break;
                case DbConstants.INTSIZEDESC:
                    files = blobDao.getFilesSortBySizeDesc(directoryid, blobtype,
                            loginInfo.getValue(DbConstants.LOGIN_ID));
                    selection = DbConstants.SIZE_DESC_STR;
                    break;
                case DbConstants.INTNAMEASC:
                    files = blobDao.getFilesSortByNameAsc(directoryid, blobtype,
                            loginInfo.getValue(DbConstants.LOGIN_ID));
                    selection = DbConstants.NAME_ASC_STR;
                    break;
                case DbConstants.INTNAMEDESC:
                    files = blobDao.getFilesSortByNameDesc(directoryid, blobtype,
                            loginInfo.getValue(DbConstants.LOGIN_ID));
                    selection = DbConstants.NAME_DESC_STR;
                    break;
                case DbConstants.INTDATEASC:
                    files = blobDao.getFilesSortByDateAsc(directoryid, blobtype,
                            loginInfo.getValue(DbConstants.LOGIN_ID));
                    selection = DbConstants.DATE_ASC_STR;
                    break;
                default:
                    selection = DbConstants.DATE_DESC_STR;
                    break;

                }
            } else {
                directory = directoryDao.viewDirectory(directoryid);
            }
            tagList = blobDao.getAllTags(directoryid, DbConstants.READ_FROM_SLAVE);
        } catch (BaseDaoException e) {
            return handleError("Exception occured in viewDirectory()/getAllTags/getFilesSortBy..() for directory() "
                    + e.getMessage(), e);
        }

        if (directory == null) {
            return handleError("directory is null for directoryid = " + directoryid);
        }

        /**
         * collphotofolder or editblobfile - views
         */
        String viewName = DbConstants.DIR_PHOTO_FOLDER;
        StringBuffer sb = new StringBuffer();
        if (blobtype.equals(DbConstants.FILE_CATEGORY)) {
            viewName = DbConstants.EDIT_DIR_BLOB_FILE;
        } /* else { */
        if (tagList != null && tagList.size() > 0) {
            for (int i = 0; i < tagList.size(); i++) {
                Photo photo = (Photo) tagList.get(i);
                if (photo != null) {
                    sb.append(photo.getValue(DbConstants.USER_TAGS));
                    sb.append(",");
                }
            }
        }
        /* } */

        Directory cobrand = null;
        CobrandDao cobrandDao = (CobrandDao) getDaoMapper().getDao(DbConstants.COBRAND);
        if (cobrandDao == null) {
            return handleUserpageError("CobrandDao is null, DirectorygetblobController ");
        }
        try {
            cobrand = cobrandDao.getDirCobrand(directoryid);
        } catch (Exception e) {
            return handleError("exception getDirCobrand(), DirectorygetblobController ", e);
        }

        String pageNum = request.getParameter(DbConstants.PAGE_NUM);
        try {
            if (!RegexStrUtil.isNull(pageNum)) {
                NumberFormat nf = NumberFormat.getInstance();
                Number mypageNum = nf.parse(pageNum);
            }
        } catch (ParseException e) {
            return handleError("pageNum has other than [0-9] characters, DirectorygetblobController.", e);
        }

        Map myModel = new HashMap();
        myModel.put(DbConstants.LOGIN_INFO, loginInfo);
        if (sb != null) {
            myModel.put(DbConstants.USER_TAGS, sb.toString());
        } else {
            myModel.put(DbConstants.USER_TAGS, " ");
        }
        myModel.put(DbConstants.DIRECTORY, directory);
        myModel.put(DbConstants.DIRNAME, directory.getValue(DbConstants.DIRNAME));
        myModel.put(DbConstants.DIRECTORY_ID, directoryid);
        myModel.put(DbConstants.PAGE_NUM, pageNum);
        myModel.put(DbConstants.COBRAND, cobrand);
        myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists);
        myModel.put(DbConstants.USER_PAGE, userpage);
        myModel.put(DbConstants.SHARE_INFO, shareInfo);
        myModel.put(DbConstants.IS_DIRECTORY, "1");
        myModel.put(DbConstants.VISITOR_PAGE, memberUserpage);
        myModel.put(DbConstants.BUSINESS_EXISTS, isBizExists(login));
        myModel.put(DbConstants.SORT_BY, sortByStr);
        myModel.put(DbConstants.PHOTOS, files);
        myModel.put(DbConstants.SELECTION, selection);
        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;
    }
}