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.text.NumberFormat; import java.text.ParseException; import java.util.ArrayList; 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.Directory; 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 $ * * Directories can be added only the login users who have permissions to add * */ public class DirectorydeleteallController 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 { // *************************************************************************** // This will initialize common data in the abstract class and the return result is of no value. // The abstract class initializes protected variables, login, directory, logininfo. // Which can be accessed in all controllers if they want to. // *************************************************************************** try { ModelAndView m = super.handleRequest(request, response); } catch (Exception e) { return handleError("error in handleRequest", e); } if (!WebUtil.isLicenseProfessional(login)) { return handleError("Cannot access manage (delete) directory feature in deluxe version."); } // *************************************************************************** // 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 redirectory to extend SessionObject. // *************************************************************************** outOfSession(request, response); if (RegexStrUtil.isNull(login)) { return handleUserpageError("Login is null in DirectorydeleteallController."); } String directoryId = request.getParameter(DbConstants.DIRECTORY_ID); if (RegexStrUtil.isNull(directoryId)) { return handleError("request, directoryId is null, for directorydeleteController " + login); } if (directoryId.length() > GlobalConst.directoryidSize) { return handleError("directoryId.length() > WebConstants.directoryIdSize DirectoryController "); } directoryId = RegexStrUtil.goodNameStr(directoryId); String deleteAll = request.getParameter("Delete All"); String delete = request.getParameter(DbConstants.DELETE); if (RegexStrUtil.isNull(delete) && RegexStrUtil.isNull(deleteAll)) { return handleError("Delete && deleteAll are null, DirectorydeleteallController() for login " + login); } String size = request.getParameter(DbConstants.SIZE); if (RegexStrUtil.isNull(size)) { return handleError("collabrum list size is null, DirectorydeleteallController() for login " + login); } String urlCnt = request.getParameter(DbConstants.URL_CNT); if (RegexStrUtil.isNull(urlCnt)) { return handleError("url list size is null, DirectorydeleteallController() for login " + login); } int colListSize = new Integer(size).intValue(); String val = ""; ArrayList collabrumIdList = new ArrayList(); for (int i = 0; i < colListSize; i++) { val = new Integer(i).toString(); if (!RegexStrUtil.isNull(request.getParameter(val))) { if ((delete != null) && delete.equals(DbConstants.DELETE)) { if (request.getParameter(val).equalsIgnoreCase("on")) { StringBuffer myString = new StringBuffer("col"); myString.append(val); collabrumIdList.add(RegexStrUtil.goodNameStr(request.getParameter(myString.toString()))); } } else { StringBuffer myString = new StringBuffer("col"); myString.append(val); collabrumIdList.add(RegexStrUtil.goodNameStr(request.getParameter(myString.toString()))); } } } try { NumberFormat nf = NumberFormat.getInstance(); Number myNum = nf.parse(urlCnt); } catch (ParseException e) { return handleError("urlCnt has other than [0-9] characters, DirectorydeleteallController.", e); } int webListSize = new Integer(urlCnt).intValue(); ArrayList webList = new ArrayList(); for (int i = 0; i < webListSize; i++) { val = new Integer(i).toString(); if (!RegexStrUtil.isNull(request.getParameter(val))) { if ((delete != null) && delete.equals(DbConstants.DELETE)) { if (request.getParameter(val).equalsIgnoreCase("on")) { StringBuffer myString = new StringBuffer(DbConstants.URL); myString.append(val); webList.add(RegexStrUtil.goodNameStr(request.getParameter(myString.toString()))); } } else { StringBuffer myString = new StringBuffer(DbConstants.URL); myString.append(val); webList.add(RegexStrUtil.goodNameStr(request.getParameter(myString.toString()))); } } } DirectoryDao directoryDao = (DirectoryDao) daoMapper.getDao(DbConstants.DIRECTORY); if (directoryDao == null) { return handleError("DirectoryDao is null in DirectorydeleteallController directory, " + login); } String loginId = null; if (loginInfo != null) { loginId = loginInfo.getValue(DbConstants.LOGIN_ID); } try { if (!directoryDao.isUserAuthor(directoryId, loginId, login)) { return handleError( "User is not an author, cannot delete directory, DirectorydeleteallController directory, " + login); } } catch (BaseDaoException e) { return handleError("isUserAuthor() is null, in DirectorydeleteallController directory, " + login, e); } CollabrumDao collabrumDao = (CollabrumDao) daoMapper.getDao(DbConstants.COLLABRUM); if (collabrumDao == null) { return handleError("CollabrumDao null, DirectorydeleteallController, login " + login); } try { collabrumDao.deleteDirCollabrums(directoryId, collabrumIdList, loginId, login); } catch (BaseDaoException e) { return handleError("Exception occured in DirectorydeleteallController() for member " + login, e); } DirWebsiteDao websiteDirDao = (DirWebsiteDao) daoMapper.getDao(DbConstants.DIR_WEBSITE); if (websiteDirDao == null) { return handleError("DirWebsiteDao is null in DirwebsitedeleteController directory, " + login); } try { websiteDirDao.deleteAllWebsites(webList, loginId, login, directoryId); } catch (BaseDaoException e) { return handleError( "Exception occured in DirectorydeleteallController() for login " + login + "login = " + login, e); } List parentResult = null; try { parentResult = directoryDao.deleteDirectory(directoryId, loginId, login); } catch (BaseDaoException e) { return handleError( "Exception occured in deleteDirectory() for member " + login + " directoryId = " + directoryId, e); } Directory dir = null; if (parentResult == null) { return handleError("Exception occured in retrieving the parent in deleteDirectory, login " + login + " directoryId = " + directoryId); } else { if (parentResult.size() > 0) { String parentid = ((Directory) parentResult.get(0)).getValue(DbConstants.PARENT_ID); if (RegexStrUtil.isNull(parentid)) { return handleError( "parentid is null, deleteDirectory, login " + login + " directoryId = " + directoryId); } try { dir = directoryDao.viewDirectory(parentid, loginId, login, DbConstants.READ_FROM_MASTER, DbConstants.BLOB_READ_FROM_SLAVE, DbConstants.READ_FROM_SLAVE); } catch (BaseDaoException e) { return handleError("Exception occured in directoryDeleteController() for member " + login + " ErrorMsg=" + e.getMessage(), e); } } } String viewName = DbConstants.VIEW_DIRECTORY; Map myModel = new HashMap(); myModel.put(DbConstants.LOGIN_INFO, loginInfo); myModel.put(DbConstants.DIRECTORY, dir); 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); } /** * 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; } }