web.ViewphotosController.java Source code

Java tutorial

Introduction

Here is the source code for web.ViewphotosController.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 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.*;
import model.Userpage;

/**
 *
 * @author Smitha Gudur (smitha@redbasin.com)
 * @version $Revision: 1.3 $
 *
 * Directories can be added only the login users who have permissions to add
 *
 */
public class ViewphotosController extends BaseController implements Controller {

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

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

        /**
         * For subscription model, don't show unless the user has session
         */
        try {
            ModelAndView m = super.handleRequest(request, response);
        } catch (Exception e) {
            return handleError("error in ViewtphotosController", e);
        }

        if (WebUtil.isProductPremiumSubscription()) {
            outOfSession(request, response);
        }

        if (getDaoMapper() == null) {
            handleError("DaoMapper is null, ViewtphotosController.");
        }

        List photoHitsList = null;
        CarryonDao carryonDao = (CarryonDao) daoMapper.getDao(DbConstants.CARRYON);
        if (carryonDao == null) {
            return handleError("carryonDao is null in ViewphotosController");
        }

        /**
         *  admin can search in any business.
         */
        boolean isBizAware = false;
        String bid = null;
        if (WebUtil.isProductPremiumSubscription()) {
            if (!RegexStrUtil.isNull(login)) {
                if (!DiaryAdmin.isDiaryAdmin(login)) {
                    isBizAware = true;
                }
            }
            if (userpage != null) {
                bid = userpage.getValue(DbConstants.BID);
            }
        }

        HashSet tagSet = null;
        if (isBizAware) {
            try {
                if (!RegexStrUtil.isNull(bid)) {
                    photoHitsList = carryonDao.getCarryonHitsBizAware(bid, DbConstants.READ_FROM_SLAVE);
                }
            } catch (BaseDaoException e) {
                return handleError(
                        "Exception in either getTopHitsBizAware()/getRecentBlogsBizAware/getPopularBlogsBizAware",
                        e);
            }
        } else {
            try {
                photoHitsList = carryonDao.getCarryonHits();
                if (photoHitsList != null) {
                    tagSet = carryonDao.getUniqueTags(photoHitsList);
                }
            } catch (BaseDaoException e) {
                return handleError("Exception in either getCarryonHits()/getUniqueTags() in carryonDao", e);
            }
        }

        DirectoryDao dirDao = (DirectoryDao) daoMapper.getDao(DbConstants.DIRECTORY);
        if (dirDao == null) {
            return handleError("dirDao is null in ViewphotosController");
        }

        DirectoryStreamBlobDao dirStreamBlobDao = (DirectoryStreamBlobDao) daoMapper.getDao(DbConstants.DIR_BLOB);
        if (dirStreamBlobDao == null) {
            return handleError("dirStreamBlobDao is null in ViewphotosController");
        }

        HashSet dirBlobResult = null;
        if (tagSet != null) {
            SearchDao searchDao = (SearchDao) daoMapper.getDao(DbConstants.SEARCH);
            if (searchDao == null) {
                return handleError("searchDao is null, ViewphotosController");
            }
            if (rbDirectoryExists.equals("1")) {
                try {
                    List dirBlobTags = dirStreamBlobDao.get10Tags(DbConstants.READ_FROM_SLAVE);
                    if (dirBlobTags != null && dirBlobTags.size() > 0) {
                        dirBlobResult = dirDao.getDirBlobsFromTags(new HashSet(dirBlobTags));
                        HashSet tagSet2 = carryonDao.getUniqueTags(dirBlobTags);
                        tagSet = sqlSearch.getUniqueTags(tagSet, tagSet2);
                    }
                } catch (BaseDaoException e) {
                    return handleError("Exception in either searchDirectoryBlobs, ViewphotosController", e);
                }
            }
        }

        Userpage cobrand = null;
        CobrandDao cobrandDao = (CobrandDao) getDaoMapper().getDao(DbConstants.COBRAND);
        if (cobrandDao == null) {
            return handleError("CobrandDao is null,  ViewphotosController");
        }

        if (loginInfo != null) {
            try {
                cobrand = cobrandDao.getUserCobrand(loginInfo.getValue(DbConstants.LOGIN_ID));

            } catch (BaseDaoException e) {
                return handleError("cobrand is null, ViewphotosController.", e);
            }
        }

        String viewName = DbConstants.PHOTOS;
        Map myModel = new HashMap();
        myModel.put(DbConstants.TOP_CARRYON, photoHitsList);
        myModel.put(DbConstants.USER_PAGE, userpage);
        myModel.put(DbConstants.SHARE_INFO, shareInfo);
        myModel.put(DbConstants.VISITOR_PAGE, memberUserpage);
        myModel.put(DbConstants.TAG_LIST, tagSet);
        myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists);
        myModel.put(DbConstants.COBRAND, cobrand);
        myModel.put(DbConstants.LOGIN_INFO, loginInfo);
        if (rbDirectoryExists.equals("1") && (dirBlobResult != null)) {
            myModel.put(DbConstants.DIR_BLOB, dirBlobResult);
        }
        if (tagSet != null) {
            myModel.put(DbConstants.USER_TAGS, RegexStrUtil.goodText(tagSet.toString()));
        }

        if (!RegexStrUtil.isNull(login)) {
            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;
    }

    /**
     *  This method is called by spring.
     * @param dbutils
     */
    public void setmysqlSearch(MysqlSearch mysqlSearch) {
        this.sqlSearch = mysqlSearch;
    }
}