web.DirslidesController.java Source code

Java tutorial

Introduction

Here is the source code for web.DirslidesController.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.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;

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

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

    // These properties are set from the spring config file
    // private BaseDaoModel daoMapper;

    /**
     * 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
     */
    private final int MSEC = 1000;

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

        logger.info("came 1, DirslidesController");

        if (!GlobalConst.hddomain.contains(GlobalConst.PortalServer) && (!rbDirectoryExists.equals("1"))) {
            return handleError("Cannot manage directory feature in deluxe version.");
        }
        logger.info("came 2, DirslidesController");

        if (getDaoMapper() == null) {
            return handleError("DaoMapper is null in DirslidesController");
        }
        logger.info("came 3, DirslidesController");

        /** 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 DirslidesController ");
        }
        logger.info("came 4, DirslidesController");
        if (directoryid.length() > GlobalConst.directoryidSize) {
            return handleError("directoryid.length() > WebConstants.directoryidSize, for DirslidesController ");
        }
        directoryid = RegexStrUtil.goodNameStr(directoryid);

        logger.info("came 5, DirslidesController");

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

        logger.info("came 6, DirslidesController");

        DirectoryStreamBlobDao dirBlobDao = (DirectoryStreamBlobDao) daoMapper.getDao(DbConstants.DIR_BLOB);
        if (dirBlobDao == null) {
            return handleError("dirBlobDao is null in DirslidesController directory");
        }

        logger.info("came 7, DirslidesController");
        List photos = null;
        List tagList = null;
        Directory dirBean = null;
        String dirName = null;
        logger.info("came 8, DirslidesController");
        try {
            photos = directoryDao.getBlobsByCategory(directoryid, DbConstants.PHOTO_CATEGORY,
                    DbConstants.BLOB_READ_FROM_SLAVE);
            tagList = dirBlobDao.getAllTags(directoryid, DbConstants.BLOB_READ_FROM_SLAVE);
            dirBean = directoryDao.viewDirectory(directoryid);
            if (dirBean != null) {
                dirName = dirBean.getValue(DbConstants.DIRNAME);
            }
        } catch (BaseDaoException e) {
            return handleError("Exception occured in getStreamBlobs() DirslidesController, directory() ", e);
        }
        logger.info("came 9, DirslidesController");
        if (photos == null) {
            return handleError("photos null, DirslidesController, directoryid = " + directoryid);
        }

        /** 
         * check if this photo exists 
              */
        logger.info("came 10, DirslidesController");
        int photoNum = 0;
        int nextPhotoNum = 0;
        int prevPhotoNum = 0;

        String goPhoto = request.getParameter(DbConstants.GOTO_PHOTO);
        if (!RegexStrUtil.isNull(goPhoto)) {
            String showphoto = request.getParameter(DbConstants.SHOW_PHOTO);
            if (!RegexStrUtil.isNull(showphoto)) {
                photoNum = new Integer(showphoto).intValue();
            }
        } else {
            String photoNumStr = request.getParameter(DbConstants.PHOTO_NUM);
            if (!RegexStrUtil.isNull(photoNumStr)) {
                photoNum = new Integer(photoNumStr).intValue();
            }
        }

        logger.info("came 11, DirslidesController");

        String timerStr = request.getParameter(DbConstants.TIMER);
        if (RegexStrUtil.isNull(timerStr)) {
            timerStr = "0";
        }

        logger.info("came 12, DirslidesController");

        if (photos.size() == 1) {
            photoNum = nextPhotoNum = prevPhotoNum = 0;
        } else {
            if (photoNum >= photos.size()) {
                photoNum = 0;
                nextPhotoNum = photoNum + 1;
                prevPhotoNum = photos.size() - 1;
            } else {
                nextPhotoNum = photoNum + 1;
                if (photoNum != 0) {
                    prevPhotoNum = photoNum - 1;
                    ;
                } else {
                    prevPhotoNum = photos.size() - 1;
                }
            }
        }
        logger.info("came 13, DirslidesController");

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

        String url = "";
        String fn = request.getParameter(DbConstants.FNDOTX);
        Map myModel = new HashMap();
        if (!RegexStrUtil.isNull(fn)) {
            String beg = request.getParameter("count");
            if (beg != null) {
                int count = new Integer(beg).intValue();

                /*
                photos.size = 3;
                photonum = 0, count = 2;  
                photonum = 1, count = 1;  into play
                photonum = 2, count = 0;
                photonum = 3, count = -1;
                */

                if ((count > 0) && count <= photos.size()) {
                    count--;
                    String newBeg = "-1";
                    if (count >= 0) {
                        newBeg = new Integer(count).toString();
                    }

                    logger.info("came 21 DirslidesController");

                    StringBuffer myUrl = new StringBuffer("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"");
                    myUrl.append(timerStr);
                    myUrl.append("; URL=");
                    myUrl.append("dirslides?directoryid=");
                    myUrl.append(directoryid);
                    myUrl.append("&dirname=");
                    myUrl.append(dirName);
                    myUrl.append("&photonum=");
                    myUrl.append(nextPhotoNum);
                    myUrl.append("&timer=");
                    myUrl.append(timerStr);
                    myUrl.append("&count=");
                    myUrl.append(newBeg);
                    myUrl.append("&fn.x=");
                    myUrl.append(DbConstants.PLAY);
                    myUrl.append("\">");
                    url = myUrl.toString();
                    logger.info("came 15, DirslidesController");
                } else {
                    //lastone 
                    myModel.put("count", null);
                }
            } // beg != null 
        } else {
            myModel.put("count", null);
        }
        logger.info("came 16, DirslidesController");

        StringBuffer usertags = new StringBuffer();
        if (tagList != null && tagList.size() > 0) {
            for (int i = 0; i < tagList.size(); i++) {
                if ((Photo) tagList.get(i) != null) {
                    usertags.append(((Photo) tagList.get(i)).getValue(DbConstants.USER_TAGS));
                }
            }
        }
        logger.info("came 17, DirslidesController");

        /**
        * views resolved to the name of the jsp using ViewResolver
        */
        String viewName = DbConstants.DIR_SLIDES;
        myModel.put(viewName, photos);
        myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists);
        myModel.put(DbConstants.DIRNAME, dirName);
        myModel.put(DbConstants.DIRECTORY_ID, directoryid);
        myModel.put(DbConstants.USER_PAGE, userpage);
        myModel.put(DbConstants.VISITOR_PAGE, memberUserpage);
        myModel.put(DbConstants.LOGIN_INFO, loginInfo);
        myModel.put(DbConstants.SHARE_INFO, shareInfo);
        myModel.put(DbConstants.USER_TAGS, usertags.toString());
        logger.info("came 18, DirslidesController");

        StringBuffer sb = new StringBuffer();
        sb.append(photoNum);
        myModel.put(DbConstants.PHOTO_NUM, sb.toString());

        logger.info("came 20, DirslidesController");

        sb.delete(0, sb.length());
        sb.append(nextPhotoNum);
        myModel.put(DbConstants.NEXT_NUM, sb.toString());

        sb.delete(0, sb.length());
        sb.append(prevPhotoNum);
        myModel.put(DbConstants.PREV_NUM, sb.toString());

        myModel.put(DbConstants.COBRAND, cobrand);
        myModel.put(DbConstants.TIMER, timerStr);
        myModel.put(DbConstants.URL, url);
        myModel.put(DbConstants.BUSINESS_EXISTS, isBizExists(login));
        logger.info("came 19, DirslidesController");
        return new ModelAndView(viewName, "model", myModel);

        //myModel.put(DbConstants.FN, fn);
    }

    /**
     * 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;
    }
}