web.SearchphotosController.java Source code

Java tutorial

Introduction

Here is the source code for web.SearchphotosController.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.*;
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.DbConstants;
import util.DiaryAdmin;
import util.RegexStrUtil;
import util.WebUtil;

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

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

        //outOfSession(request, response);

        String searchText = request.getParameter(DbConstants.SEARCH);
        if (RegexStrUtil.isNull(searchText)) {
            Map myModel = new HashMap();
            String viewName = DbConstants.SEARCH;
            myModel.put(DbConstants.LOGIN_INFO, loginInfo);
            myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists);
            myModel.put(DbConstants.USER_PAGE, userpage);
            myModel.put(DbConstants.SHARE_INFO, shareInfo);
            myModel.put(DbConstants.VISITOR_PAGE, memberUserpage);
            myModel.put(DbConstants.BUSINESS_EXISTS, isBizExists(login));
            return new ModelAndView(viewName, "model", myModel);
        }
        searchText = RegexStrUtil.goodText(searchText);

        SearchDao searchDao = (SearchDao) daoMapper.getDao(DbConstants.SEARCH);
        if (searchDao == null) {
            return handleError("searchDao is null in SearchphotosController");
        }

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

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

        HashSet photoSet = null;
        CarryonDao carryonDao = (CarryonDao) daoMapper.getDao(DbConstants.CARRYON);
        if (carryonDao == null) {
            return handleError("carryonDao is null in SearchphotosController");
        }

        HashSet tagList = null;
        HashSet dirBlobResult = null;
        List carryonResult = null;
        try {
            if (isBizAware && bid != null) {
                photoSet = searchDao.bizSearchCarryon(DbConstants.READ_FROM_MASTER, searchText, bid, login);
            } else {
                photoSet = searchDao.searchCarryon(DbConstants.READ_FROM_MASTER, searchText);
            }
            if (rbDirectoryExists.equals("1")) {
                HashSet dirTags = searchDao.searchDirectoryBlobs(DbConstants.READ_FROM_SLAVE, searchText);
                if (dirTags != null && dirTags.size() > 0) {
                    dirBlobResult = dirDao.getDirBlobsFromTags(dirTags);
                }
            }

            searchDao.addTags(searchText);

            if (photoSet != null) {
                carryonResult = new ArrayList(photoSet);
                tagList = carryonDao.getUniqueTags(carryonResult);
                if (tagList != null) {
                    if (tagList.size() < 10) {
                        List hitList = null;
                        if (isBizAware && bid != null) {
                            hitList = carryonDao.getCarryonHitsBizAware(bid, DbConstants.READ_FROM_SLAVE);
                        } else {
                            hitList = carryonDao.getCarryonHits();
                        }
                        HashSet hitSet = carryonDao.getUniqueTags(hitList);
                        if (hitSet != null) {
                            Iterator it1 = hitSet.iterator();
                            for (int i = 0; i < hitSet.size(); i++) {
                                tagList.add((String) it1.next());
                            }
                        }
                    } // tagList.size() < 10
                }
            }
        } catch (BaseDaoException e) {
            return handleError("Exception in either searchDirectory" + searchText, e);
        }

        Map myModel = new HashMap();
        String viewName = DbConstants.PHOTOS;

        if (dirBlobResult == null && carryonResult == null) {
            if (tagList != null) {
                viewName = DbConstants.TAGS;
            }
        }
        if (tagList != null) {
            myModel.put(DbConstants.USER_TAGS, RegexStrUtil.goodText(tagList.toString()));
        }
        myModel.put(DbConstants.TOP_CARRYON, carryonResult);
        myModel.put(DbConstants.TAG_LIST, tagList);
        myModel.put(DbConstants.LOGIN_INFO, loginInfo);
        myModel.put(DbConstants.USER_PAGE, userpage);
        myModel.put(DbConstants.SHARE_INFO, shareInfo);
        myModel.put(DbConstants.VISITOR_PAGE, memberUserpage);
        myModel.put(DbConstants.BUSINESS_EXISTS, isBizExists(login));
        myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists);
        if (rbDirectoryExists.equals("1") && dirBlobResult != null) {
            myModel.put(DbConstants.DIR_BLOB, dirBlobResult);
        }
        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;
    }
}