web.AddsiteblobController.java Source code

Java tutorial

Introduction

Here is the source code for web.AddsiteblobController.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.BaseDaoException;
import dao.BaseDaoModel;
import dao.CobrandSiteDao;
import java.io.IOException;
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 org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
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.DiaryAdmin;
import util.GlobalConst;
import util.RegexStrUtil;

/**
 *
 * @author Smitha Gudur (smitha@redbasin.com)
 * @version $Revision: 1.1 $
 */
public class AddsiteblobController extends BaseController implements Controller {

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

    /**
     * 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 {

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

        if (!DiaryAdmin.isDiaryAdmin(login)) {
            return handleError("Cannot add site cobrand as login is not Admin.");
        }

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

        // remember any request parameters have to be added thru the field list.
        // cannot use request.getParameter in this program.

        String vhostid = request.getParameter(DbConstants.VHOST_ID);
        if (RegexStrUtil.isNull(vhostid)) {
            return handleError("vhostid is null in AddsiteblobController");
        }
        vhostid = RegexStrUtil.goodNameStr(vhostid);

        Integer blobtype = new Integer(request.getParameter(DbConstants.BLOBTYPE));
        if (blobtype < GlobalConst.categoryMinSize || blobtype > GlobalConst.categoryMaxSize) {
            return handleUserpageError("category or blobtype is not appropriate type in AddsiteblobController");
        }

        byte[] blob = null;
        String mtype = null;
        String btitle = null;
        int zoom = 100;
        List fileList = null;
        DiskFileUpload upload = null;
        try {
            boolean isMultipart = FileUpload.isMultipartContent(request);
            // Create a new file upload handler
            upload = new DiskFileUpload();
            //upload.setSizeMax((long)10000000);
            //upload.setSizeMax(webConstants.getFileuploadsize());
            upload.setSizeMax(GlobalConst.fileUploadSize);

            // Parse the request
            fileList = upload.parseRequest(request);
        } catch (Exception e) {
            return handleError("Exception occurred in uploading the photo file.", e);
        }

        long fieldsize = 0;

        for (int i = 0; i < fileList.size(); i++) {
            FileItem fileItem = (FileItem) fileList.get(i);
            if (!fileItem.isFormField()) {
                blob = fileItem.get();
                mtype = fileItem.getContentType();
                long maxSize = upload.getSizeMax();
                fieldsize = fileItem.getSize();
            }
        }

        boolean addBlob = true;
        if ((fieldsize <= 0) || (RegexStrUtil.isNull(mtype))) {
            addBlob = false;
        }

        String loginid = loginInfo.getValue(DbConstants.LOGIN_ID);
        if (getDaoMapper() == null) {
            return handleError("DaoMapper is null in AddsiteblobController");
        }

        CobrandSiteDao cobrandSiteDao = (CobrandSiteDao) getDaoMapper().getDao(DbConstants.COBRAND_SITE);
        if (cobrandSiteDao == null) {
            return handleError("cobrandSiteDao is null for AddsiteblobController");
        }

        String ftype = request.getParameter(DbConstants.TYPE);

        if (RegexStrUtil.isNull(ftype)) {
            return handleError("ftype is null for AddsiteblobController");
        }

        if (!ftype.equals(DbConstants.COBRAND_HEADER) && !ftype.equals(DbConstants.COBRAND_FOOTER)) {
            return handleError("cobrand filetype is neither a header nor footer AddsiteblobController");
        }

        if (vhostid.length() > GlobalConst.entryIdSize) {
            return handleError("vhostid.length() > WebConstants.entryIdSize,in AddsiteblobController");
        }

        /**
         *  If the blob information is provided by the user, add the blob
         */
        String blobtypeStr = request.getParameter(DbConstants.BLOBTYPE);
        List sites = null;
        if (addBlob) {
            try {
                cobrandSiteDao.addCobrandStreamBlob(blob, vhostid, ftype, loginid, login);
                sites = cobrandSiteDao.getSites(DbConstants.READ_FROM_MASTER, login, loginid);
            } catch (BaseDaoException e) {
                return handleError("Exception occurred in addBlob() addSiteCobrandStream ", e);
            }
        }

        Map myModel = new HashMap();
        String viewName = DbConstants.EDIT_SITE_COBRAND;
        myModel.put(DbConstants.COBRAND, sites);
        myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists);
        myModel.put(DbConstants.USER_PAGE, userpage);
        myModel.put(DbConstants.LOGIN_INFO, loginInfo);
        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;
    }

}