web.DirectoryupdateblobController.java Source code

Java tutorial

Introduction

Here is the source code for web.DirectoryupdateblobController.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 $
 *
 * Directories can be added only the login users who have permissions to add
 *
 */
public class DirectoryupdateblobController 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, directory, 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.");
        }

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

        if (RegexStrUtil.isNull(login)) {
            return handleUserpageError("Login is null in DirectoryupdateblobController.");
        }

        String directoryid = request.getParameter(DbConstants.DIRECTORY_ID);
        String entryid = request.getParameter(DbConstants.ENTRYID);
        String btitle = request.getParameter(DbConstants.BTITLE);
        String zoom = request.getParameter(DbConstants.ZOOM);
        String usertags = request.getParameter(DbConstants.USER_TAGS);
        String caption = request.getParameter(DbConstants.CAPTION);

        if (RegexStrUtil.isNull(directoryid) || RegexStrUtil.isNull(entryid) || RegexStrUtil.isNull(btitle)) {
            return handleError(
                    "request, directoryid/entryid/btitle is null, directoryupdateblobController " + login);
        }

        if (directoryid.length() > GlobalConst.directoryidSize) {
            return handleError(
                    "directoryid.length > WebConstants.directoryidSize,  directoryupdateblobController " + login);
        }

        if (entryid.length() > GlobalConst.entryIdSize) {
            return handleError(
                    "entryid.length > WebConstants.entryIdSize,  directoryupdateblobController " + login);
        }

        if (btitle.length() > GlobalConst.blobTitleSize) {
            btitle = btitle.substring(0, GlobalConst.blobTitleSize);
        }

        if (!RegexStrUtil.isNull(caption)) {
            if (caption.length() > GlobalConst.refererSize) {
                caption = caption.substring(0, GlobalConst.refererSize);
            }
            caption = RegexStrUtil.goodText(caption);
        }

        if (!RegexStrUtil.isNull(usertags)) {
            if (usertags.length() > GlobalConst.usertagsSize) {
                usertags = usertags.substring(0, GlobalConst.usertagsSize);
            }
            usertags = RegexStrUtil.goodText(usertags);
        }

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

        DirectoryStreamBlobDao dirStreamBlobDao = (DirectoryStreamBlobDao) daoMapper.getDao(DbConstants.DIR_BLOB);
        if (dirStreamBlobDao == null) {
            return handleError("DirectoryDao is null in DirectoryupdateblobController directory, " + login);
        }
        if (!RegexStrUtil.isNull(zoom)) {
            int zoomVal = new Integer(zoom).intValue();
            if (zoomVal < GlobalConst.zoomMinSize || zoomVal > GlobalConst.zoomMaxSize) {
                return handleError(
                        "zoomVal is either < zoomMinSize or > zoomMaxSize, DirectoryupdateblobController directory, login="
                                + login);
            }
        }

        String loginId = null;
        if (loginInfo != null) {
            loginId = loginInfo.getValue(DbConstants.LOGIN_ID);
        }

        boolean def = request.getParameter(DbConstants.DEF) != null
                && request.getParameter(DbConstants.DEF).equalsIgnoreCase("ON");

        directoryid = RegexStrUtil.goodNameStr(directoryid);
        entryid = RegexStrUtil.goodNameStr(entryid);
        btitle = RegexStrUtil.goodText(btitle);

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

        List tagList = null;
        Directory directory = null;
        try {
            if (login != null && loginInfo != null) {
                directory = directoryDao.viewDirectory(directoryid, loginInfo.getValue(DbConstants.LOGIN_ID), login,
                        DbConstants.READ_FROM_SLAVE, DbConstants.BLOB_READ_FROM_SLAVE,
                        DbConstants.WEBSITE_READ_FROM_SLAVE);
            } else {
                directory = directoryDao.viewDirectory(directoryid);
            }
            tagList = blobDao.getAllTags(directoryid, DbConstants.READ_FROM_SLAVE);
        } catch (BaseDaoException e) {
            return handleError("Exception occured in viewDirectory()/getAllTags() for directory() " + directoryid,
                    e);
        }

        /** 
              * dirpath and dirname are required for sans
              * renameSanFile
              */
        try {
            directoryDao.updateStreamBlob(entryid, directoryid, loginId, login, zoom, btitle, def, caption,
                    directory.getValue(DbConstants.DIRPATH), directory.getValue(DbConstants.DIRNAME));
            dirStreamBlobDao.updateTags(entryid, directoryid, usertags);
            // get the latest directory with the blobs updated information
            directory = directoryDao.viewDirectory(directoryid, loginInfo.getValue(DbConstants.LOGIN_ID), login,
                    DbConstants.READ_FROM_SLAVE, DbConstants.BLOB_READ_FROM_MASTER,
                    DbConstants.WEBSITE_READ_FROM_SLAVE);
        } catch (BaseDaoException e) {
            return handleError("Exception occured in updateStreamBlob()/updateTags()/viewDirectory() for member "
                    + login + " directoryid = " + directoryid, e);
        }

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

        StringBuffer sb = new StringBuffer();
        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, DbConstants.DATEDESC);
        myModel.put(DbConstants.SELECTION, DbConstants.DATE_DESC_STR);
        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;
    }
}