web.SlidesController.java Source code

Java tutorial

Introduction

Here is the source code for web.SlidesController.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.HashSet;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Displaypage;
import model.Hdlogin;
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.DbConstants;
import util.RegexStrUtil;

/**
 *
 * @author Smitha Gudur (smitha@redbasin.com)
 * @version $Revision: 1.1 $
 */
public class SlidesController 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
     */
    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");
        }

        if (getDaoMapper() == null) {
            return handleError("DaoMapper is null in SlidesController, member " + member);
        }

        String member = request.getParameter(DbConstants.MEMBER);
        if (RegexStrUtil.isNull(member)) {
            return handleError("member is null, Forwarding to " + member + "SlidesController");
        }

        member = RegexStrUtil.goodId(member);

        Hdlogin memberInfo = null;
        MemberDao memberDao = (MemberDao) daoMapper.getDao(DbConstants.MEMBER);
        if (memberDao == null) {
            return handleError("MemberDao is null, SlidesController, login, " + login);
        }

        try {
            memberInfo = memberDao.getMemberInfo(member);
        } catch (BaseDaoException e) {
            return handleError("getMemberInfo(member) exception in SlidesController, member=" + member, e);
        }

        if (memberInfo == null) {
            return handleError("memberInfo is null, in SlidesController" + member);
        }

        DisplaypageDao displayDao = (DisplaypageDao) getDaoMapper().getDao(DbConstants.DISPLAY_PAGE);
        if (displayDao == null) {
            return handleError("DisplaypageDao is null, " + member);
        }
        Displaypage uDisplaypage = null;
        try {
            uDisplaypage = displayDao.getDisplaypage(member);
        } catch (BaseDaoException e) {
            return handleError("Exception occurred in getMyDisplayInfo(), for member, " + member, e);
        }
        if (uDisplaypage == null) {
            return handleUserpageError("displaypage is null" + member);
        }

        /**
         *  if the user has not published photos, check:
         *    1. Is this login same as member?
              *    2. Is the login in session?
         */
        String published = uDisplaypage.getValue(DbConstants.PHOTOS);
        if (!RegexStrUtil.isNull(published) && (published.equals("0"))) {
            if (!RegexStrUtil.isNull(login) && !login.equals(member)) {
                return handleUserpageError(login + " user cannot access slideshow of this member =" + member);
            }
            outOfSession(request, response);
        }

        /**
         *    start the slide show
         */
        CarryonDao carryonDao = (CarryonDao) getDaoMapper().getDao(DbConstants.CARRYON);
        if (carryonDao == null) {
            return handleError("carryonDao is null, SlidesController for member " + member);
        }

        /**
         *  retrieve the update blob stream and other blobs
         */
        List carryon = null;
        List tagList = null;
        HashSet tagSet = null;
        try {
            carryon = carryonDao.getCarryonByCategory(memberInfo.getValue(DbConstants.LOGIN_ID),
                    DbConstants.PHOTO_CATEGORY, DbConstants.READ_FROM_SLAVE);
            tagList = carryonDao.getTags(memberInfo.getValue(DbConstants.LOGIN_ID), DbConstants.READ_FROM_SLAVE);
            tagSet = carryonDao.getUniqueTags(tagList);
        } catch (BaseDaoException e) {
            return handleError("Exception occurred in getCarryonByCategory() member, " + member, e);
        }
        if (carryon == null) {
            return handleError("carryon is null for SlidesController, member = " + member);
        }

        /** 
         * check if this photo exists 
              */
        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();
            }
        }

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

        String fn = request.getParameter(DbConstants.FNDOTX);
        if (carryon.size() == 1) {
            photoNum = nextPhotoNum = prevPhotoNum = 0;
        } else {
            if (photoNum >= carryon.size()) {
                photoNum = 0;
                nextPhotoNum = photoNum + 1;
                prevPhotoNum = carryon.size() - 1;
            } else {
                nextPhotoNum = photoNum + 1;
                if (photoNum != 0) {
                    prevPhotoNum = photoNum - 1;
                    ;
                } else {
                    prevPhotoNum = carryon.size() - 1;
                }
            }
        }

        CobrandDao cobrandDao = (CobrandDao) getDaoMapper().getDao(DbConstants.COBRAND);
        if (cobrandDao == null) {
            return handleUserpageError("CobrandDao is null, SlidesController");
        }

        Userpage cobrand = cobrandDao.getUserCobrand(memberInfo.getValue(DbConstants.LOGIN_ID));
        String url = "";
        Map myModel = new HashMap();

        if (!RegexStrUtil.isNull(fn)) {
            String beg = request.getParameter("count");
            if (beg != null) {
                int count = new Integer(beg).intValue();
                if ((count > 0) && count <= carryon.size()) {
                    count--;
                    String newBeg = "-1";
                    if (count >= 0) {
                        newBeg = new Integer(count).toString();
                    }
                    StringBuffer myUrl = new StringBuffer("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"");
                    myUrl.append(timerStr);
                    myUrl.append("; URL=");
                    myUrl.append("slides?member=");
                    myUrl.append(member);
                    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();
                } else {
                    //lastone
                    myModel.put("count", null);
                }
            } // beg != null
        } else {
            myModel.put("count", null);
        }

        /**
        * editphotos or editfiles - views resolved to the name of the jsp using ViewResolver
        */
        String viewName = DbConstants.SLIDES;
        myModel.put(viewName, carryon);
        myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists);
        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.PUBLISHED, published);
        if (tagSet != null) {
            myModel.put(DbConstants.USER_TAGS, RegexStrUtil.goodText(tagSet.toString()));
        }

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

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