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.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.Hdlogin; 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; /** * * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ public class SharecontactController 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 { // *************************************************************************** // 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. // *************************************************************************** try { ModelAndView m = super.handleRequest(request, response); } catch (Exception e) { return handleError("error in handleRequest"); } outOfSession(request, response); if (RegexStrUtil.isNull(login) || RegexStrUtil.isNull(member) || (loginInfo == null)) { return handleUserpageError("Login/member/loginInfo is null, SharecontactController."); } String contactId = request.getParameter(DbConstants.CONTACT_ID); if (RegexStrUtil.isNull(contactId)) { return handleError("contactId is null, SharecontactController"); } if (contactId.length() > GlobalConst.contactIdSize) { return handleError("contactId.length > WebConstants.contactIdSize, SharecontactController"); } try { NumberFormat nf = NumberFormat.getInstance(); Number myNum = nf.parse(contactId); } catch (ParseException e) { return handleError("contactId has other than [0-9] characters, SharecontactController", e); } contactId = RegexStrUtil.goodNameStr(contactId); String fname = request.getParameter(DbConstants.FIRST_NAME); if (!RegexStrUtil.isNull(fname) && (fname.length() > GlobalConst.firstNameSize)) { fname = fname.substring(0, GlobalConst.firstNameSize); } String lname = request.getParameter(DbConstants.LAST_NAME); if (!RegexStrUtil.isNull(lname) && (lname.length() > GlobalConst.lastNameSize)) { lname = lname.substring(0, GlobalConst.lastNameSize); } String alphabet = ""; if (!RegexStrUtil.isNull(fname)) { alphabet = fname.substring(0, 1); } else { if (!RegexStrUtil.isNull(lname)) { alphabet = lname.substring(0, 1); } } if (getDaoMapper() == null) { return handleError("DaoMapper is null, SharecontactController." + login); } ContactDao contactDao = (ContactDao) getDaoMapper().getDao(DbConstants.CONTACT); if (contactDao == null) { return handleError("ContactDao is null, in SharecontactController." + login); } List contacts = null; List sharedContacts = null; try { contactDao.shareContact(contactId, loginInfo.getValue(DbConstants.LOGIN_ID), member, alphabet); contacts = contactDao.getAllContacts(loginInfo.getValue(DbConstants.LOGIN_ID), DbConstants.READ_FROM_MASTER); sharedContacts = contactDao.getSharedContacts(loginInfo.getValue(DbConstants.LOGIN_ID)); } catch (BaseDaoException e) { return handleError("Exception in shareOneContact(), SharecontactController, for member " + member, e); } /** * cobrand */ Userpage cobrand = null; CobrandDao cobrandDao = (CobrandDao) getDaoMapper().getDao(DbConstants.COBRAND); if (cobrandDao == null) { return handleError("CobrandDao is null, SharecontactController"); } try { cobrand = cobrandDao.getUserCobrand(loginInfo.getValue(DbConstants.LOGIN_ID)); } catch (BaseDaoException e) { return handleError("cobrand is null, SharecontactController", e); } /** * Get the members login information */ MemberDao myMemberDao = (MemberDao) daoMapper.getDao(DbConstants.MEMBER); if (myMemberDao == null) { return handleError("MemberDao is null in ShareuserController member, " + member); } Hdlogin memberInfo = null; try { memberInfo = myMemberDao.getMemberInfo(member); } catch (BaseDaoException e) { return handleError("Exception occured in getMemberInfo(), of ShareuserController for member " + member, e); } if (memberInfo == null) { return handleError("memberInfo is null."); } String viewName = "showcontacts"; Map myModel = new HashMap(); myModel.put(DbConstants.COBRAND, cobrand); myModel.put(DbConstants.PUBLISHED, "0"); myModel.put(DbConstants.LOGIN_INFO, loginInfo); myModel.put(DbConstants.CONTACTS, contacts); myModel.put(DbConstants.MEMBER_INFO, memberInfo); myModel.put(DbConstants.SHARED_CONTACTS, sharedContacts); myModel.put(DbConstants.HIGHLIGHT, "all"); 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; } }