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.*; 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.Photo; 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.GlobalConst; import util.RegexStrUtil; import util.WebUtil; /** * * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ public class CaptionupdateController 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", e); } outOfSession(request, response); String category = request.getParameter(DbConstants.CATEGORY); String entryid = request.getParameter(DbConstants.ENTRYID); String btitle = request.getParameter(DbConstants.BTITLE); if (RegexStrUtil.isNull(entryid) || RegexStrUtil.isNull(btitle) || RegexStrUtil.isNull(category)) { return handleError("entryid/btitle/category is null. Forwarding to " + login + "in edit photos, CaptionupdateController"); } boolean isFile = false; if (category.equals(DbConstants.FILE_CATEGORY)) { if (!WebUtil.isLicenseProfessional(login)) { return handleError( "Cannot access user file features (caption update for files) in deluxe version." + login); } isFile = true; } String caption = request.getParameter(DbConstants.CAPTION); String zoom = request.getParameter(DbConstants.ZOOM); String usertags = request.getParameter(DbConstants.USER_TAGS); if (!RegexStrUtil.isNull(category)) { if (!category.equals(DbConstants.FILE_CATEGORY) && !category.equals(DbConstants.PHOTO_CATEGORY)) { return handleError("category is neither a file nor a photo, CaptionUpdateController" + category); } } if (entryid.length() > GlobalConst.entryIdSize) { return handleError("entryid.length() > WebConstants.entryIdSize, CaptionUpdateController, " + entryid); } if (btitle.length() > GlobalConst.blobTitleSize) { btitle = btitle.substring(0, GlobalConst.blobTitleSize); } if (!RegexStrUtil.isNull(usertags)) { if (usertags.length() > GlobalConst.blobTitleSize) { usertags = usertags.substring(0, GlobalConst.blobTitleSize); } usertags = RegexStrUtil.goodText(usertags); } boolean def = request.getParameter(DbConstants.DEF) != null && request.getParameter(DbConstants.DEF).equalsIgnoreCase("on"); btitle = RegexStrUtil.goodStr(btitle); /** * update blob stream */ if (getDaoMapper() == null) { return handleError("DaoMapper is null in edit photos for login " + login); } CarryonDao carryonDao = (CarryonDao) getDaoMapper().getDao(DbConstants.CARRYON); if (carryonDao == null) { return handleError("carryonDao is null in edit photos for login " + login); } String loginId = null; if (loginInfo != null) { loginId = loginInfo.getValue(DbConstants.LOGIN_ID); } if (!RegexStrUtil.isNull(caption)) { if (caption.length() > GlobalConst.refererSize) { caption = caption.substring(0, GlobalConst.refererSize); } caption = RegexStrUtil.goodText(caption); } if (!RegexStrUtil.isNull(zoom)) { zoom = RegexStrUtil.goodNameStr(zoom); } try { carryonDao.updateCaption(RegexStrUtil.goodText(btitle), zoom, RegexStrUtil.goodNameStr(entryid), loginId, RegexStrUtil.goodNameStr(category), login, def, usertags, caption); } catch (BaseDaoException e) { return handleError("Exception occurred in updateCaption in carryon for login" + login, e); } /** * retrieve the update blob stream and other blobs */ List carryon = null; HashSet tagSet = null; try { carryon = carryonDao.getCarryonByCategory(loginId, category, DbConstants.READ_FROM_MASTER); List tagList = carryonDao.getTags(loginId, DbConstants.READ_FROM_MASTER); tagSet = carryonDao.getUniqueTags(tagList); } catch (BaseDaoException e) { return handleError("Exception occurred in getCarryon() in carryon edit for login " + login, e); } if (carryon == null) { return handleError("carryon is null."); } else { for (int i = 0; i < carryon.size(); i++) { Photo photo = (Photo) carryon.get(i); String keywords = photo.getValue(DbConstants.USER_TAGS); } } /* * display information about the files. */ DisplaypageDao displayDao = (DisplaypageDao) getDaoMapper().getDao(DbConstants.DISPLAY_PAGE); if (displayDao == null) { return handleError("displayDao is null in carryon edit for login " + login); } Displaypage displaypage = null; try { displaypage = displayDao.getDisplaypage(login, DbConstants.READ_FROM_SLAVE); } catch (BaseDaoException e) { return handleError("Exception occurred in getCarryon() in carryon edit for login " + login, e); } CobrandDao cobrandDao = (CobrandDao) getDaoMapper().getDao(DbConstants.COBRAND); if (cobrandDao == null) { return handleUserpageError("CobrandDao is null, CaptionupdateController"); } Userpage cobrand = cobrandDao.getUserCobrand(loginInfo.getValue(DbConstants.LOGIN_ID)); /** * editphotos or editfiles - views resolved to the name of * the jsp using ViewResolver */ String viewName = DbConstants.EDIT_PHOTOS; if (isFile) { viewName = DbConstants.EDIT_FILES; } Map myModel = new HashMap(); myModel.put(viewName, carryon); if (tagSet != null) { myModel.put(DbConstants.USER_TAGS, RegexStrUtil.goodText(tagSet.toString())); } myModel.put(DbConstants.DISPLAY_PAGE, displaypage); myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists); myModel.put(DbConstants.USER_PAGE, userpage); myModel.put(DbConstants.VISITOR_PAGE, memberUserpage); myModel.put(DbConstants.SHARE_INFO, shareInfo); myModel.put(DbConstants.LOGIN_INFO, loginInfo); myModel.put(DbConstants.COBRAND, cobrand); 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; } }