web.SavecontactController.java Source code

Java tutorial

Introduction

Here is the source code for web.SavecontactController.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.CobrandDao;
import dao.ContactDao;
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.DbConstants;
import util.GlobalConst;
import util.RegexStrUtil;

/**
 *
 * @author Smitha Gudur (smitha@redbasin.com)
 * @version $Revision: 1.1 $
 */
public class SavecontactController 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)) {
            return handleUserpageError("Login is null in SavecontactController.");
        }

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

        ContactDao contactDao = (ContactDao) getDaoMapper().getDao(DbConstants.CONTACT);
        if (contactDao == null) {
            return handleError("ContactDao is null, in SavecontactController." + login);
        }

        String contactId = request.getParameter(DbConstants.CONTACT_ID);
        if (RegexStrUtil.isNull(contactId)) {
            return handleError("contactId is null in savecontactController");
        }

        if (contactId.length() > GlobalConst.contactIdSize) {
            return handleError("contactId.length > WebConstants.contactIdSize, SavecontactController");
        }
        contactId = RegexStrUtil.goodNameStr(contactId);

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

        String fname = request.getParameter(DbConstants.FIRST_NAME);
        if (!RegexStrUtil.isNull(fname)) {
            if (fname.length() > GlobalConst.firstNameSize) {
                fname = fname.substring(0, GlobalConst.firstNameSize);
            }
            fname = RegexStrUtil.goodNameStr(fname);
        }
        String lname = request.getParameter(DbConstants.LAST_NAME);
        if (!RegexStrUtil.isNull(lname)) {
            if (lname.length() > GlobalConst.lastNameSize) {
                lname = lname.substring(0, GlobalConst.lastNameSize);
            }
            lname = RegexStrUtil.goodNameStr(lname);
        }

        String mname = request.getParameter(DbConstants.MIDDLE_NAME);
        if (!RegexStrUtil.isNull(mname)) {
            if (mname.length() > GlobalConst.middleNameSize) {
                mname = mname.substring(0, GlobalConst.middleNameSize);
            }
            mname = RegexStrUtil.goodNameStr(mname);
        }

        String nickname = request.getParameter(DbConstants.NICKNAME);
        if (!RegexStrUtil.isNull(nickname)) {
            if (nickname.length() > GlobalConst.nickNameSize) {
                nickname = nickname.substring(0, GlobalConst.nickNameSize);
            }
            nickname = RegexStrUtil.goodText(nickname);
        }

        String byear = request.getParameter(DbConstants.BYEAR);
        if (!RegexStrUtil.isNull(byear) && (byear.length() > GlobalConst.yearSize)) {
            byear = byear.substring(0, GlobalConst.yearSize);
        }

        String bmonth = request.getParameter(DbConstants.BMONTH);
        if (!RegexStrUtil.isNull(bmonth) && (bmonth.length() > GlobalConst.monthSize)) {
            bmonth = bmonth.substring(0, GlobalConst.monthSize);
        }

        String bday = request.getParameter(DbConstants.BDAY);
        if (!RegexStrUtil.isNull(bday) && (bday.length() > GlobalConst.daySize)) {
            bday = bday.substring(0, GlobalConst.daySize);
        }

        if (RegexStrUtil.isNull(bday)) {
            bday = "01";
        }
        String dob = byear + "-" + bmonth + "-" + bday;

        String email = request.getParameter(DbConstants.EMAIL);
        if (!RegexStrUtil.isNull(email)) {
            if (email.length() > GlobalConst.emailSize) {
                email = email.substring(0, GlobalConst.emailSize);
            }
            email = RegexStrUtil.goodText(email);
        }

        String designation = request.getParameter(DbConstants.DESIGNATION);
        if (!RegexStrUtil.isNull(designation)) {
            if (designation.length() > GlobalConst.designationSize) {
                designation = designation.substring(0, GlobalConst.designationSize);
            }
            designation = RegexStrUtil.goodText(designation);
        }

        String title = request.getParameter(DbConstants.TITLE);
        if (!RegexStrUtil.isNull(title)) {
            if (title.length() > GlobalConst.titleSize) {
                title = title.substring(0, GlobalConst.titleSize);
            }
            title = RegexStrUtil.goodText(title);
        }

        String industry = request.getParameter(DbConstants.INDUSTRY);
        if (!RegexStrUtil.isNull(industry)) {
            if (industry.length() > GlobalConst.industrySize) {
                industry = industry.substring(0, GlobalConst.industrySize);
            }
            industry = RegexStrUtil.goodText(industry);
        }

        String company = request.getParameter(DbConstants.COMPANY);
        if (!RegexStrUtil.isNull(company)) {
            if (company.length() > GlobalConst.companySize) {
                company = company.substring(0, GlobalConst.companySize);
            }
            company = RegexStrUtil.goodText(company);
        }

        String pwebsite = request.getParameter(DbConstants.PWEBSITE);
        if (!RegexStrUtil.isNull(pwebsite)) {
            if (pwebsite.length() > GlobalConst.pWebsiteSize) {
                pwebsite = pwebsite.substring(0, GlobalConst.pWebsiteSize);
            }
            pwebsite = RegexStrUtil.goodText(pwebsite);
        }

        String cwebsite = request.getParameter(DbConstants.CWEBSITE);
        if (!RegexStrUtil.isNull(cwebsite)) {
            if (cwebsite.length() > GlobalConst.cWebsiteSize) {
                cwebsite = cwebsite.substring(0, GlobalConst.cWebsiteSize);
            }
            cwebsite = RegexStrUtil.goodText(cwebsite);
        }

        String blogsite = request.getParameter(DbConstants.BLOGSITE);
        if (!RegexStrUtil.isNull(blogsite)) {
            if (blogsite.length() > GlobalConst.blogSiteSize) {
                blogsite = blogsite.substring(0, GlobalConst.blogSiteSize);
            }
            blogsite = RegexStrUtil.goodText(blogsite);
        }

        String city = request.getParameter(DbConstants.CITY);
        if (!RegexStrUtil.isNull(city)) {
            if (city.length() > GlobalConst.citySize)
                city = city.substring(0, GlobalConst.citySize);
            city = RegexStrUtil.goodText(city);
        }

        String state = request.getParameter(DbConstants.STATE);
        if (!RegexStrUtil.isNull(state)) {
            if (state.length() > GlobalConst.stateSize)
                state = state.substring(0, GlobalConst.stateSize);
            state = RegexStrUtil.goodText(state);
        }

        String zipcode = request.getParameter(DbConstants.ZIPCODE);
        if (!RegexStrUtil.isNull(zipcode)) {
            if (zipcode.length() > GlobalConst.zipcodeSize) {
                zipcode = zipcode.substring(0, GlobalConst.zipcodeSize);
            }
            zipcode = RegexStrUtil.goodText(zipcode);
        }

        String country = request.getParameter(DbConstants.COUNTRY);
        if (!RegexStrUtil.isNull(country)) {
            if (country.length() > GlobalConst.countrySize) {
                country = country.substring(0, GlobalConst.countrySize);
            }
            country = RegexStrUtil.goodText(country);
        }

        String desc = request.getParameter(DbConstants.DESCRIPTION);
        if (!RegexStrUtil.isNull(desc)) {
            if (desc.length() > GlobalConst.descriptionSize) {
                desc = desc.substring(0, GlobalConst.descriptionSize);
            }
            desc = RegexStrUtil.goodText(desc);
        }

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

        String bstate = request.getParameter(DbConstants.BSTATE);
        if (!RegexStrUtil.isNull(bstate)) {
            if (bstate.length() > GlobalConst.bStateSize) {
                bstate = bstate.substring(0, GlobalConst.bStateSize);
            }
            bstate = RegexStrUtil.goodNameStr(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);
        }

        String hphone = request.getParameter(DbConstants.HPHONE);
        if (!RegexStrUtil.isNull(hphone)) {
            if (hphone.length() > GlobalConst.hPhoneSize) {
                hphone = hphone.substring(0, GlobalConst.hPhoneSize);
            }
            hphone = RegexStrUtil.goodText(hphone);
        }

        String cphone = request.getParameter(DbConstants.CPHONE);
        if (!RegexStrUtil.isNull(cphone)) {
            if (cphone.length() > GlobalConst.cPhoneSize) {
                cphone = cphone.substring(0, GlobalConst.cPhoneSize);
            }
            cphone = RegexStrUtil.goodText(cphone);
        }

        String bphone = request.getParameter(DbConstants.BPHONE);
        if (!RegexStrUtil.isNull(bphone)) {
            if (bphone.length() > GlobalConst.bPhoneSize) {
                bphone = bphone.substring(0, GlobalConst.bPhoneSize);
            }
            bphone = RegexStrUtil.goodText(bphone);
        }

        String netphone = request.getParameter(DbConstants.NETPHONE);
        if (!RegexStrUtil.isNull(netphone)) {
            if (netphone.length() > GlobalConst.netPhoneSize) {
                netphone = netphone.substring(0, GlobalConst.netPhoneSize);
            }
            netphone = RegexStrUtil.goodText(netphone);
        }

        String fax = request.getParameter(DbConstants.FAX);
        if (!RegexStrUtil.isNull(fax)) {
            if (fax.length() > GlobalConst.faxSize) {
                fax = fax.substring(0, GlobalConst.faxSize);
            }
            fax = RegexStrUtil.goodText(fax);
        }

        String yim = request.getParameter(DbConstants.YIM);
        if (!RegexStrUtil.isNull(yim)) {
            if (yim.length() > GlobalConst.yimSize) {
                yim = yim.substring(0, GlobalConst.yimSize);
            }
            yim = RegexStrUtil.goodText(yim);
        }

        String aim = request.getParameter(DbConstants.AIM);
        if (!RegexStrUtil.isNull(aim)) {
            if (aim.length() > GlobalConst.aimSize) {
                aim = aim.substring(0, GlobalConst.aimSize);
            }
            aim = RegexStrUtil.goodText(aim);
        }

        String msn = request.getParameter(DbConstants.MSN);
        if (!RegexStrUtil.isNull(msn)) {
            if (msn.length() > GlobalConst.msnSize) {
                msn = msn.substring(0, GlobalConst.msnSize);
            }
            msn = RegexStrUtil.goodText(msn);
        }

        String icq = request.getParameter(DbConstants.ICQ);
        if (!RegexStrUtil.isNull(icq)) {
            if (icq.length() > GlobalConst.icqSize) {
                icq = icq.substring(0, GlobalConst.icqSize);
            }
            icq = RegexStrUtil.goodText(icq);
        }

        String relation = request.getParameter(DbConstants.RELATION);
        if (!RegexStrUtil.isNull(relation)) {
            if (relation.length() > GlobalConst.relationSize) {
                relation = relation.substring(0, GlobalConst.relationSize);
            }
            relation = RegexStrUtil.goodText(relation);
        }

        String published = "0";
        if ((!RegexStrUtil.isNull(request.getParameter(DbConstants.PUBLISHED)))
                && (request.getParameter(DbConstants.PUBLISHED).equalsIgnoreCase("on"))) {
            published = "1";
        }

        String usertags = request.getParameter(DbConstants.USER_TAGS);
        if (!RegexStrUtil.isNull(usertags)) {
            if (usertags.length() > GlobalConst.usertagsSize)
                usertags = usertags.substring(0, GlobalConst.usertagsSize);
            usertags = RegexStrUtil.goodText(usertags);
        }

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

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

        }

        // default is male
        String mygender = "0";
        if ((!RegexStrUtil.isNull(request.getParameter(DbConstants.GENDER)))
                && (request.getParameter(DbConstants.GENDER).equalsIgnoreCase(DbConstants.FEMALE))) {
            mygender = "1";
        }

        List contacts = null;
        List allContacts = null;
        List sharedContacts = null;
        String alphabet = "a";

        try {
            if (!RegexStrUtil.isNull(fname)) {
                alphabet = fname.substring(0, 1);
            } else {
                if (!RegexStrUtil.isNull(lname)) {
                    alphabet = lname.substring(0, 1);
                }
            }
            contactDao.updateContact(contactId, loginInfo.getValue(DbConstants.LOGIN_ID), dob, title, industry,
                    company, pwebsite, cwebsite, blogsite, city, state, country, desc, zipcode, fname, lname, mname,
                    email, mygender, nickname, designation, bcity, bstate, bcountry, bzipcode, hphone, cphone,
                    bphone, yim, aim, msn, icq, fax, netphone, relation, published, usertags, alphabet, street,
                    bstreet);
            contacts = contactDao.getContacts(loginInfo.getValue(DbConstants.LOGIN_ID),
                    DbConstants.READ_FROM_MASTER, alphabet, "");
            allContacts = 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 updateContact/getAllContacts()/getContacts/getSharedContacts(), SavecontactController, for login "
                            + login,
                    e);
        }

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

        String viewName = "showcontacts";
        Map myModel = new HashMap();
        /**
        * set this to published mode to zero because user cannot add contact in published mode
        */
        myModel.put(DbConstants.PUBLISHED, "0");
        myModel.put(DbConstants.COBRAND, cobrand);
        myModel.put(DbConstants.LOGIN_INFO, loginInfo);
        myModel.put(DbConstants.CONTACTS_ALPHABET, contacts);
        myModel.put(DbConstants.CONTACTS, allContacts);
        myModel.put(DbConstants.SHARED_CONTACTS, sharedContacts);
        myModel.put(DbConstants.ALPHABET, alphabet);
        myModel.put(DbConstants.DIR_EXISTS, rbDirectoryExists);
        myModel.put(DbConstants.USER_PAGE, userpage);
        myModel.put(DbConstants.SHARE_INFO, shareInfo);
        myModel.put(DbConstants.BUSINESS_EXISTS, isBizExists(login));
        //  myModel.put(DbConstants.VISITOR_PAGE, memberUserpage);
        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;
    }
}