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.Userpage; import org.apache.commons.fileupload.DiskFileUpload; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUpload; 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.*; /** * * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ public class CarryonupdateController 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); if (RegexStrUtil.isNull(login) && RegexStrUtil.isNull(member)) { return handleUserpageError("Login & member are null."); } String category = request.getParameter(DbConstants.CATEGORY); boolean isCobrand = false; if (!RegexStrUtil.isNull(request.getParameter(DbConstants.IS_COBRAND))) { isCobrand = request.getParameter(DbConstants.IS_COBRAND).equals((Object) "1"); } if ((!RegexStrUtil.isNull(category) && category.equals(DbConstants.FILE_CATEGORY)) || isCobrand) { if (!GlobalConst.hddomain.contains(DbConstants.INDIA_CENTURY)) { if (!WebUtil.isLicenseProfessional(login)) { return handleError( "Cannot access user carryon features or cobrand user in deluxe version." + login); } } } if (RegexStrUtil.isNull(category)) { return handleError("category is null in CarryonupdateController. " + login); } // *************************************************************************** // This is the only line of code you need to get all session info initialized! // Always be the first line before anything else is done. Add to each controller's // handlRequest method. Also remember to extend SessionObject. // *************************************************************************** /** * map blob dao */ if (getDaoMapper() == null) { return handleError("DaoMapper is null in carryon update."); } CarryonDao carryonDao = (CarryonDao) getDaoMapper().getDao(DbConstants.CARRYON); if (carryonDao == null) { return handleError("CarryonDao is null for carryon update."); } byte[] blob = null; String mtype = null; if (!RegexStrUtil.isNull(category)) { int catVal = new Integer(category).intValue(); if (catVal < GlobalConst.categoryMinSize || catVal > GlobalConst.categoryMaxSize) { return handleError("category values are not correct" + catVal); } } List fileList = null; DiskFileUpload upload = null; try { boolean isMultipart = FileUpload.isMultipartContent(request); // Create a new file upload handler upload = new DiskFileUpload(); /** originally set to 10MB **/ if (!DiaryAdmin.isDiaryAdmin(login)) { upload.setSizeMax((long) 10000000); } else { upload.setSizeMax(GlobalConst.maxUploadSize); } // Parse the request fileList = upload.parseRequest(request); } catch (Exception e) { return handleError("Exception occurred in uploading the photo file.", e); } long fieldsize = 0; String fieldname, fieldvalue; fieldname = fieldvalue = null; // educate the fieldnames to this form by using the setFieldName() String label = "btitle"; String caption = ""; String tagsLabel = DbConstants.USER_TAGS; String fileName = null; String usertags = null; String btitle = null; for (int i = 0; i < fileList.size(); i++) { FileItem fileItem = (FileItem) fileList.get(i); if (fileItem.isFormField()) { fileItem.setFieldName(label); fieldname = fileItem.getFieldName(); if (fieldname.equalsIgnoreCase(DbConstants.USER_TAGS)) { usertags = fileItem.getString(); //logger.info("usertags = " + usertags); label = ""; } else { if (fieldname.equalsIgnoreCase("btitle")) { btitle = fileItem.getString(); label = DbConstants.CAPTION; //logger.info("btitle = " + btitle); //fileItem.setFieldName(tagsLabel); } else { if (fieldname.equalsIgnoreCase("caption")) { caption = fileItem.getString(); label = DbConstants.USER_TAGS; } else { fieldvalue = fileItem.getString(); } } } /* set the filename */ } else { blob = fileItem.get(); mtype = fileItem.getContentType(); long maxSize = upload.getSizeMax(); /* filename */ fileName = fileItem.getName(); fieldsize = fileItem.getSize(); } } if (RegexStrUtil.isNull(btitle)) { btitle = fileName; } if ((fieldsize <= 0) || (RegexStrUtil.isNull(mtype)) || (RegexStrUtil.isNull(btitle)) || (blob == null)) { return handleError("fieldsize/mtype/btitle/blob one of them is empty, cannot upload files."); } CobrandDao cobrandDao = (CobrandDao) getDaoMapper().getDao(DbConstants.COBRAND); if (cobrandDao == null) { return handleError("cobrandDao is null for CarryonupdateController"); } DisplaypageDao displayDao = (DisplaypageDao) getDaoMapper().getDao(DbConstants.DISPLAY_PAGE); if (displayDao == null) { return handleError("displayDao is null for CarryonupdateController"); } try { if (isCobrand) { String ftype = request.getParameter(DbConstants.TYPE); if (RegexStrUtil.isNull(ftype)) { return handleError("ftype is null, CarryonUpdateController() "); } if (ftype.equals(DbConstants.COBRAND_HEADER) || ftype.equals(DbConstants.COBRAND_FOOTER)) { cobrandDao.addUserCobrand(blob, ftype, loginInfo.getValue(DbConstants.LOGIN_ID), login); } else { return handleError("cobrand type is not a header or footer in CarryonUpdateController "); } } else { if (btitle.length() > GlobalConst.blobTitleSize) { btitle = btitle.substring(0, GlobalConst.blobTitleSize); } int zoom = 100; if (!RegexStrUtil.isNull(usertags)) { if (usertags.length() > GlobalConst.usertagsSize) { usertags = usertags.substring(0, GlobalConst.usertagsSize); } usertags = RegexStrUtil.goodText(usertags); } if (!RegexStrUtil.isNull(caption)) { if (caption.length() > GlobalConst.refererSize) { caption = caption.substring(0, GlobalConst.refererSize); } caption = RegexStrUtil.goodText(caption); } boolean publishPhoto = displayDao.getDisplayPhotos(login, DbConstants.READ_FROM_SLAVE); carryonDao.addCarryon(fieldsize, category, mtype, RegexStrUtil.goodText(btitle), blob, zoom, loginInfo.getValue(DbConstants.LOGIN_ID), login, usertags, caption, publishPhoto); } } catch (BaseDaoException e) { return handleError("Exception occurred in addCarryon/addCobrandUserStreamBlo()", e); } /** * list the files */ String loginId = loginInfo.getValue(DbConstants.LOGIN_ID); List carryon = null; List tagList = null; HashSet tagSet = null; try { carryon = carryonDao.getCarryonByCategory(loginId, category, DbConstants.READ_FROM_MASTER); tagList = carryonDao.getTags(loginId, DbConstants.READ_FROM_MASTER); tagSet = carryonDao.getUniqueTags(tagList); } catch (BaseDaoException e) { return handleError( "Exception occurred in getCarryonByCategory()/getTags carryon update for login " + login, e); } /** * this is resolved to the name of the jsp using ViewResolver * if not blob type is images, display files */ if (carryon == null) { return handleError("carryon is null."); } /** * display information about the files, if the category of the blobs is files category(1) */ String viewName = DbConstants.EDIT_PHOTOS; if (category.equals(DbConstants.FILE_CATEGORY)) { viewName = DbConstants.EDIT_FILES; } Displaypage displaypage = null; Userpage cobrand = null; try { displaypage = displayDao.getDisplaypage(login, DbConstants.READ_FROM_SLAVE); cobrand = cobrandDao.getUserCobrand(loginInfo.getValue(DbConstants.LOGIN_ID)); } catch (BaseDaoException e) { return handleError("Exception occurred in getDisplaypage() for login " + login, e); } Map myModel = new HashMap(); myModel.put(viewName, carryon); myModel.put(DbConstants.COBRAND, cobrand); if (tagSet != null) { myModel.put(DbConstants.USER_TAGS, RegexStrUtil.goodText(tagSet.toString())); } myModel.put(DbConstants.LOGIN_INFO, loginInfo); myModel.put(DbConstants.DISPLAY_PAGE, displaypage); myModel.put(DbConstants.USER_PAGE, userpage); myModel.put(DbConstants.SHARE_INFO, shareInfo); myModel.put(DbConstants.VISITOR_PAGE, memberUserpage); myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists); 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; } }