Java tutorial
/** * 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.CobrandDao; import dao.CollabrumDao; 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.Collabrum; 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; import util.WebUtil; /** * * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ public class CollslideshowController 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); } if (!WebUtil.isLicenseProfessional(login)) { return handleError("Cannot access collabrum features in deluxe version." + login); } if (getDaoMapper() == null) { return handleError("DaoMapper is null in CollslideshowController"); } /** users should be allowed to view the collabrums blobs without the session **/ String collabrumid = request.getParameter(DbConstants.COLLABRUM_ID); if (RegexStrUtil.isNull(collabrumid)) { return handleError("request, collabrum is null, for CollslideshowController "); } if (collabrumid.length() > GlobalConst.collabrumidSize) { return handleError("collabrumid.length() > WebConstants.collabrumidSize, for CollslideshowController "); } collabrumid = RegexStrUtil.goodNameStr(collabrumid); String colName = request.getParameter(DbConstants.COL_NAME); if (!RegexStrUtil.isNull(colName) && colName.length() > GlobalConst.collabrumNameSize) { return handleError("colName.length() > WebConstants.collabrumNameSize, CollslideshowController"); } colName = RegexStrUtil.goodNameStr(colName); CollabrumDao collabrumDao = (CollabrumDao) daoMapper.getDao(DbConstants.COLLABRUM); if (collabrumDao == null) { return handleError("CollabrumDao is null in CollslideshowController collabrum"); } List photos = null; try { photos = collabrumDao.getBlobsByCategory(collabrumid, DbConstants.PHOTO_CATEGORY, DbConstants.BLOB_READ_FROM_SLAVE); } catch (BaseDaoException e) { return handleError( "Exception occured in getStreamBlobs() CollslideshowController collabrumid =" + collabrumid, e); } if (photos == null) { return handleError("photos null, CollslideshowController, collabrumid = " + collabrumid); } /** * 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"; } 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; } } } Collabrum cobrand = null; CobrandDao cobrandDao = (CobrandDao) getDaoMapper().getDao(DbConstants.COBRAND); if (cobrandDao == null) { return handleUserpageError("CobrandDao is null, CollslideshowController "); } try { cobrand = cobrandDao.getCollCobrand(collabrumid); } catch (Exception e) { return handleError("exception getCollCobrand(), CollslideshowController ", e); } 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(); if ((count > 0) && count <= photos.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("collslideshow?collabrumid="); myUrl.append(collabrumid); myUrl.append("&colname="); myUrl.append(colName); 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); } /** * views resolved to the name of the jsp using ViewResolver */ String viewName = DbConstants.COLL_SLIDE_SHOW; myModel.put(DbConstants.STREAMS, photos); myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists); myModel.put(DbConstants.COL_NAME, colName); myModel.put(DbConstants.COLLABRUM_ID, collabrumid); 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.BUSINESS_EXISTS, isBizExists(login)); 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; } }