web.UpdatebusinessController.java Source code

Java tutorial

Introduction

Here is the source code for web.UpdatebusinessController.java

Source

/**
* 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.BusinessDao;
import dao.CobrandDao;
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.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.*;

/**
 *
 * @author Smitha Gudur (smitha@redbasin.com)
 * @version $Revision: 1.1 $
 */
public class UpdatebusinessController 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 (!WebUtil.isProductPremiumSubscription()) {
            return handleError("Cannot access this feature in non premimu subscription version.");
        }

        if (!DiaryAdmin.isDiaryAdmin(login)) {
            return handleError("User is not diaryAdmin, cannot updateBusiness" + login);
        }

        if (getDaoMapper() == null) {
            return handleError("DaoMapper is null, in UpdatebusinessController." + login);
        }

        String bid = request.getParameter(DbConstants.BID);
        if (RegexStrUtil.isNull(bid)) {
            return handleError("bid is null in UpdatebusinessController");
        }

        String bsearch = request.getParameter(DbConstants.BSEARCH);
        if (!RegexStrUtil.isNull(bsearch)) {
            if (!bsearch.equals(DbConstants.BSEARCH_ALLOW) && (!bsearch.equals(DbConstants.BSEARCH_DISALLOW))) {
                return handleError("bsearch is invalid in UpdatebusinessController");
            }
        } else {
            return handleError("bsearch is null in UpdatebusinessController");
        }

        if (bid.length() > GlobalConst.bidSize) {
            return handleError("bid.length > WebConstants.bidSize, UpdatebusinessController");
        }

        try {
            NumberFormat nf = NumberFormat.getInstance();
            Number myNum = nf.parse(bid);
        } catch (ParseException e) {
            return handleError("bid has other than [0-9] characters, UpdatebusinessController.", e);
        }
        bid = RegexStrUtil.goodNameStr(bid);

        String pname = request.getParameter(DbConstants.PNAME);
        if (!RegexStrUtil.isNull(pname)) {
            if (pname.length() > GlobalConst.pNameSize) {
                pname = pname.substring(0, GlobalConst.pNameSize);
            }
            pname = RegexStrUtil.goodNameStr(pname);
        }

        String bname = request.getParameter(DbConstants.BNAME);
        if (!RegexStrUtil.isNull(bname)) {
            if (bname.length() > GlobalConst.bNameSize) {
                bname = bname.substring(0, GlobalConst.bNameSize);
            }
            bname = RegexStrUtil.goodText(bname);
        }

        // check cWebsite
        String bwebsite = request.getParameter(DbConstants.BWEBSITE);
        if (!RegexStrUtil.isNull(bwebsite)) {
            if (bwebsite.length() > GlobalConst.cWebsiteSize)
                bwebsite = bwebsite.substring(0, GlobalConst.cWebsiteSize);
            bwebsite = RegexStrUtil.goodText(bwebsite);
        }

        String bstreet = request.getParameter(DbConstants.BSTREET);
        if (!RegexStrUtil.isNull(bstreet)) {
            if (bstreet.length() > GlobalConst.streetSize)
                bstreet = bstreet.substring(0, GlobalConst.streetSize);
            bstreet = RegexStrUtil.goodText(bstreet);
        }

        String bcity = request.getParameter(DbConstants.BCITY);
        if (!RegexStrUtil.isNull(bcity)) {
            if (bcity.length() > GlobalConst.bCitySize) {
                bcity = bcity.substring(0, GlobalConst.bCitySize);
            }
            bcity = RegexStrUtil.goodText(bcity);
        }

        String bstate = request.getParameter(DbConstants.BSTATE);
        if (!RegexStrUtil.isNull(bstate)) {
            if (bstate.length() > GlobalConst.bStateSize) {
                bstate = bstate.substring(0, GlobalConst.bStateSize);
            }
            bstate = RegexStrUtil.goodText(bstate);
        }
        String bzipcode = request.getParameter(DbConstants.BZIPCODE);
        if (!RegexStrUtil.isNull(bzipcode)) {
            if (bzipcode.length() > GlobalConst.bZipcodeSize) {
                bzipcode = bzipcode.substring(0, GlobalConst.bZipcodeSize);
            }
            bzipcode = RegexStrUtil.goodText(bzipcode);
        }

        String bcountry = request.getParameter(DbConstants.BCOUNTRY);
        if (!RegexStrUtil.isNull(bcountry)) {
            if (bcountry.length() > GlobalConst.bCountrySize) {
                bcountry = bcountry.substring(0, GlobalConst.bCountrySize);
            }
            bcountry = RegexStrUtil.goodText(bcountry);
        }

        // check bPhoneSize
        String pphone = request.getParameter(DbConstants.PPHONE);
        if (!RegexStrUtil.isNull(pphone)) {
            if (pphone.length() > GlobalConst.bPhoneSize) {
                pphone = pphone.substring(0, GlobalConst.bPhoneSize);
            }
            pphone = RegexStrUtil.goodText(pphone);
        }

        String bein = request.getParameter(DbConstants.BEIN);
        if (!RegexStrUtil.isNull(bein)) {
            if (bein.length() > GlobalConst.bEinSize) {
                bein = bein.substring(0, GlobalConst.bEinSize);
            }
            bein = RegexStrUtil.goodText(bein);
        }

        List bizList = null;
        try {
            BusinessDao businessDao = (BusinessDao) getDaoMapper().getDao(DbConstants.BUSINESS);
            if (businessDao == null) {
                return handleError("businessDao is null, in UpdatebusinessController." + login);
            }
            businessDao.updateBusiness(login, bid, bname, pname, pphone, bwebsite, bein, bstreet, bcity, bstate,
                    bcountry, bzipcode, bsearch);
            bizList = businessDao.getBizList(login, DbConstants.READ_FROM_MASTER);
        } catch (BaseDaoException e) {
            return handleError("Exception in updateBusiness() login " + login, e);
        }

        if (bizList == null) {
            return handleError("bizList is null, login " + login);
        }

        /** 
         * cobrand
         */
        Userpage cobrand = null;
        CobrandDao cobrandDao = (CobrandDao) getDaoMapper().getDao(DbConstants.COBRAND);
        if (cobrandDao == null) {
            return handleError("CobrandDao is null, UpdatebusinessController");
        }
        try {
            cobrand = cobrandDao.getUserCobrand(loginInfo.getValue(DbConstants.LOGIN_ID));
        } catch (BaseDaoException e) {
            return handleError("cobrand is null, UpdatebusinessController.", e);
        }

        String viewName = DbConstants.LIST_BUSINESS;
        Map myModel = new HashMap();
        myModel.put(DbConstants.USER_PAGE, userpage);
        myModel.put(DbConstants.COBRAND, cobrand);
        myModel.put(DbConstants.LOGIN_INFO, loginInfo);
        myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists);
        myModel.put(DbConstants.BUSINESS_EXISTS, isBizExists(login));
        myModel.put(DbConstants.BIZ_LIST, bizList);
        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;
    }
}