com.stoxx.portlet.controller.RegistrationController.java Source code

Java tutorial

Introduction

Here is the source code for com.stoxx.portlet.controller.RegistrationController.java

Source

package com.stoxx.portlet.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.mail.MessagingException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletMode;
import javax.portlet.PortletSession;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.WindowState;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;
import com.liferay.portal.kernel.captcha.CaptchaTextException;
import com.liferay.portal.kernel.captcha.CaptchaUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PrefsPropsUtil;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Layout;
import com.liferay.portal.model.User;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.PortletURLFactoryUtil;
import com.stoxx.digital.platform.NoSuchEmailDomainException;
import com.stoxx.digital.platform.model.EmailDomain;
import com.stoxx.digital.platform.model.SalesEntry;
import com.stoxx.digital.platform.service.EmailDomainLocalServiceUtil;
import com.stoxx.digital.platform.service.SalesEntryLocalServiceUtil;
import com.stoxx.portlet.delegate.RegistrationDelegator;
import com.stoxx.portlet.registration.model.RegistrationBean;
import com.stoxx.portlet.registration.model.UserPreferences;
import com.stoxx.portlet.util.RegistrationConstants;
import com.stoxx.portlet.util.RegistrationVOUtil;
import com.stoxx.service.constant.STOXXConstants;
import com.stoxx.service.exception.STOXXException;
import com.stoxx.service.util.STOXXDateUtil;
import com.stoxx.service.util.STOXXEncrypter;
import com.stoxx.service.util.STOXXMailUtil;
import com.stoxx.service.util.STOXXUtil;

@Controller
@RequestMapping("VIEW")
public class RegistrationController implements Serializable {

    private static final String THE_PACKAGE_DETAILS = "the package details";
    private static final String _58_REDIRECT = "_58_redirect";
    private static final String AND_BEAN_EMAIL_IS = "and bean email is";
    private static final String EXISTING_USER = "existingUser";
    private static final String USER_EXISTS = "userExists";
    private static final String USER_DELETED_KEY = "userDeleted";
    private static final String LINK_EXPIRED = "link-expired";
    private static final String USER_EXISTS_KEY = "user-exists-key";
    private static final String USER_EXISTS_MIGRATION_KEY = "user-exists-migration-key";
    private static final String INTERNET = "INTERNET";
    private static final String INTRANET = "INTRANET";
    private static final String EXISTING = "existingUser=true";
    private static final String CUST_USER_EXCEEDED = "customer-user-exceeded";
    private static final long serialVersionUID = 1L;
    private static final String ENCRYPTION_KEY = "URL-4A144BEBF7EVE7B7DCF26491AE";
    private static final String ACTION = "action";
    private static final String REGISTRATION_BEAN = "registrationBean";
    private static final String DELETED_USER = "deletedUser";
    private RegistrationDelegator registrationDelegator;
    private STOXXMailUtil stoxxMailUtil;

    @ModelAttribute(REGISTRATION_BEAN)
    public RegistrationBean getRegistrationBean() {
        return new RegistrationBean();
    }

    @ModelAttribute("userPreferences")
    public UserPreferences getUserPreferences() {
        return new UserPreferences();
    }

    @ResourceMapping("captcha")
    public void handleResource(@ModelAttribute(REGISTRATION_BEAN) RegistrationBean registrationBean,
            ResourceRequest request, ResourceResponse response) {

        try {
            CaptchaUtil.serveImage(request, response);
        } catch (IOException e) {
            log.error("IOException", e);
        }

    }

    @RenderMapping
    public String handleRenderRequest(RenderRequest renderRequest, RenderResponse renderResponse, Model model,
            @ModelAttribute(REGISTRATION_BEAN) RegistrationBean registrationBean) {
        log.info("STOXXRegistrationController.handleRenderRequest()");
        ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
        Boolean isExistingUser = Boolean.FALSE;
        Boolean isDeletedUser = Boolean.FALSE;
        try {
            log.info("the action parameter is " + renderRequest.getParameter(ACTION));
            String portalUrl = PortalUtil.getCurrentURL(renderRequest);
            log.info("current Portal url>>>> " + portalUrl);
            String businessEmailAddressEncrypted = null;
            registrationBean = getListDetails(registrationBean, themeDisplay);
            registrationBean.setCompanyId(themeDisplay.getCompanyId());
            registrationBean.setIsExistingUser(isExistingUser);
            registrationBean.setIsDeletedUser(isDeletedUser);
            if (null != renderRequest.getParameter(ACTION)
                    && renderRequest.getParameter(ACTION).equalsIgnoreCase("setupProfileTRANSLATOR")) {
                log.info("for Translator and the email address is $$$$" + businessEmailAddress + AND_BEAN_EMAIL_IS
                        + registrationBean.getBusinessEmailAddress());
                if (Validator.isNotNull(businessEmailAddress)) {
                    registrationBean.setBusinessEmailAddress(businessEmailAddress);
                }
                log.info("user type is" + registrationBean.getUserType());
                if (registrationBean.getUserType().equalsIgnoreCase(STOXXConstants.STOXX_VENDOR_USER)) {
                    registrationBean = registrationDelegator.fetchFromStoxxUser(registrationBean);
                    log.info("Company Name >>>>> is" + registrationBean.getCompanyName());
                    log.info("Translator agency ID >>>>> is" + registrationBean.getTranslatorAgencyId());
                }
                if (registrationBean.getUserType().equalsIgnoreCase(STOXXConstants.STOXX_STAFF_USER)) {
                    registrationBean.setCompanyName(RegistrationConstants.STAFF_COMPANY);
                    log.info("Company Name >>>>> is" + registrationBean.getCompanyName());
                }
                if (registrationDelegator.checkIfRegistered(registrationBean.getBusinessEmailAddress())) {
                    registrationBean = registrationDelegator.fetchFromStoxxUser(registrationBean);
                    if (null != registrationBean.getUserType() && registrationBean.getUserType()
                            .equalsIgnoreCase(STOXXConstants.STOXX_REGISTERED_USER)) {
                        String packageDetails = registrationDelegator
                                .getPackageDetails(registrationBean.getSalesEntryId());
                        log.info(THE_PACKAGE_DETAILS + packageDetails);
                        registrationBean.setPackageDetails(packageDetails);
                    }
                }
                registrationBean = getListDetails(registrationBean, themeDisplay);
                registrationBean.setIsExistingUser(isExistingUser);
                registrationBean.setIsDeletedUser(isDeletedUser);
                model.addAttribute(REGISTRATION_BEAN, registrationBean);
                return RegistrationConstants.SETUP_PROFILE;
            } else if (null != renderRequest.getParameter(ACTION)
                    && renderRequest.getParameter(ACTION).equalsIgnoreCase("userExists")
                    || null != renderRequest.getParameter(ACTION)
                            && renderRequest.getParameter(ACTION).equalsIgnoreCase("invalidEmail")
                    || null != renderRequest.getParameter(ACTION)
                            && renderRequest.getParameter(ACTION).equalsIgnoreCase("setupProfileGENERALLICENSED")
                    || null != renderRequest.getParameter(ACTION)
                            && renderRequest.getParameter(ACTION).equalsIgnoreCase("setupProfileSTAFF")
                    || null != renderRequest.getParameter(ACTION)
                            && renderRequest.getParameter(ACTION).equalsIgnoreCase("setupProfileGENERAL")
                    || null != renderRequest.getParameter(ACTION)
                            && renderRequest.getParameter(ACTION).equalsIgnoreCase("linkAlreadysent")) {
                log.info("email address is $$$$" + businessEmailAddress + AND_BEAN_EMAIL_IS
                        + registrationBean.getBusinessEmailAddress());
                businessEmailAddress = registrationBean.getBusinessEmailAddress();
                model.addAttribute(REGISTRATION_BEAN, registrationBean);
                return RegistrationConstants.INITIATE_REGISTRATION;
            } else if (null != renderRequest.getParameter(ACTION)
                    && renderRequest.getParameter(ACTION).equalsIgnoreCase("CaptchaException")) {
                log.info("email address is $$$$" + businessEmailAddress + AND_BEAN_EMAIL_IS
                        + registrationBean.getBusinessEmailAddress());
                model.addAttribute(REGISTRATION_BEAN, registrationBean);
                return RegistrationConstants.SETUP_PROFILE;
            } else if (portalUrl.contains(StringPool.QUESTION) && portalUrl.contains(StringPool.DOLLAR)) {
                log.info("inside registration link validation with dollar and questionmark");
                getUserTypeandEmalaIdfromEncrptedUrl(model, registrationBean, portalUrl);
                model.addAttribute(REGISTRATION_BEAN, registrationBean);
                if (Validator.isNotNull(businessEmailAddress) && !registrationBean.getIsExistingUser()) {
                    log.info("businessEmailAddress>>>>>>>>>>>>>>>>>>>>>>>>>>" + businessEmailAddress);
                    log.info("USER_>>>>>>>>>>>>>>>" + registrationDelegator
                            .checkIfUserExistsInLiferayDB(registrationBean, businessEmailAddress));
                    registrationBean.setCompanyId(themeDisplay.getCompanyId());
                    boolean isUserExistInCustomDB = registrationDelegator
                            .checkIfUserExistsInLiferay(registrationBean, businessEmailAddress);
                    log.info("STOXX_USER" + isUserExistInCustomDB);
                    if (registrationDelegator.checkIfUserExistsInLiferayDB(registrationBean, businessEmailAddress)
                            && registrationDelegator.checkIfUserExistsInLiferay(registrationBean,
                                    businessEmailAddress)
                            && Validator.isNotNull(registrationBean.getIsDeletedUser())
                            && !registrationBean.getIsDeletedUser()) {
                        log.info("inside dupplicate registration block with dollar an questionmark");
                        SessionErrors.add(renderRequest, USER_EXISTS_KEY);
                    } else {
                        if (Validator.isNotNull(registrationBean.getIsDeletedUser())
                                && registrationBean.getIsDeletedUser()) {
                            return RegistrationConstants.INITIATE_REGISTRATION;
                        }
                        if (!isUserExistInCustomDB) {
                            SessionErrors.add(renderRequest, USER_DELETED_KEY);
                        } else {
                            log.info("inside registration block for new registration with dollar an questionmark");
                            if (fetchActivationLinkCreateDateDiff(registrationBean.getBusinessEmailAddress())) {
                                SessionErrors.add(renderRequest, RegistrationConstants.REGISTRATION_LINK_EXPIRED);
                            }
                            if (registrationDelegator.checkIfUserExistsInLiferay(registrationBean)
                                    && registrationBean.getUserType()
                                            .equalsIgnoreCase(STOXXConstants.STOXX_STAFF_USER)
                                    && registrationBean.getUserType()
                                            .equalsIgnoreCase(STOXXConstants.STOXX_GENERAL_USER)) {
                                SessionMessages.add(renderRequest, "user-activation-link-already-sent");
                            }
                        }
                    }
                }
                SessionMessages.add(renderRequest, "user-validation-done");
                return RegistrationConstants.INITIATE_REGISTRATION;
            } else if (null != renderRequest.getParameter(ACTION)
                    && renderRequest.getParameter(ACTION).equalsIgnoreCase(EXISTING_USER)) {
                log.info("FOR EXISTING USER STEO 1 >>>>> email address is $$$$" + businessEmailAddress
                        + AND_BEAN_EMAIL_IS + registrationBean.getBusinessEmailAddress());
                registrationBean = registrationDelegator.fetchFromStoxxUser(registrationBean);
                if (Validator.isNotNull(registrationBean) && Validator.isNotNull(registrationBean.getStatus())
                        && registrationBean.getStatus() == 3) {
                    isExistingUser = Boolean.TRUE;
                    registrationBean.setIsExistingUser(isExistingUser);
                    registrationBean.setIsDeletedUser(isDeletedUser);
                }
                model.addAttribute(REGISTRATION_BEAN, registrationBean);
                SessionMessages.add(renderRequest, EXISTING_USER);
                return RegistrationConstants.INITIATE_REGISTRATION;
            }

            else if (null != renderRequest.getParameter(ACTION)
                    && renderRequest.getParameter(ACTION).equalsIgnoreCase(CUST_USER_EXCEEDED)) {
                log.info("FOR EXCEEDING CUSTOMER  USER STEO 1 >>>>> email address is $$$$"
                        + registrationBean.getBusinessEmailAddress());

                model.addAttribute(REGISTRATION_BEAN, registrationBean);
                SessionMessages.add(renderRequest, CUST_USER_EXCEEDED);
                return RegistrationConstants.INITIATE_REGISTRATION;
            }

            else if (portalUrl.contains(StringPool.QUESTION + EXISTING)) {
                log.info("new existing logic");
                registrationBean = registrationDelegator.fetchFromStoxxUser(registrationBean);
                if (Validator.isNotNull(registrationBean) && Validator.isNotNull(registrationBean.getStatus())
                        && registrationBean.getStatus() == 3) {
                    isExistingUser = Boolean.TRUE;
                    registrationBean.setIsExistingUser(isExistingUser);
                }
                model.addAttribute(REGISTRATION_BEAN, registrationBean);
                SessionErrors.add(renderRequest, USER_EXISTS_MIGRATION_KEY);
                return RegistrationConstants.INITIATE_REGISTRATION;
            } else if (null != renderRequest.getParameter(ACTION)
                    && renderRequest.getParameter(ACTION).equalsIgnoreCase("existingUserStep2")
                    || null != renderRequest.getParameter(ACTION)
                            && renderRequest.getParameter(ACTION).equalsIgnoreCase(DELETED_USER)) {
                log.info("FOR EXISTING USER STEP 2 >>>>> email address is $$$$" + businessEmailAddress
                        + AND_BEAN_EMAIL_IS + registrationBean.getBusinessEmailAddress());
                registrationBean = registrationDelegator.fetchFromStoxxUser(registrationBean, businessEmailAddress);
                log.info("Sales entry id is >>>>>>>>>" + registrationBean.getSalesEntryId()
                        + " and the usertype is >>>>>>" + registrationBean.getUserType());

                if (Validator.isNotNull(registrationBean.getStatus()) && registrationBean.getStatus() == 4
                        && registrationBean.getUserType().equalsIgnoreCase(STOXXConstants.STOXX_REGISTERED_USER)
                        && !Validator.isNotNull(registrationBean.getSalesEntryId())) {
                    registrationDelegator.updateUserType(registrationBean.getBusinessEmailAddress(),
                            STOXXConstants.STOXX_GENERAL_USER);
                    registrationBean.setUserType(STOXXConstants.STOXX_GENERAL_USER);
                }
                User liferayStoxxUser = UserLocalServiceUtil.fetchUserByEmailAddress(themeDisplay.getCompanyId(),
                        businessEmailAddress);

                if (Validator.isNotNull(registrationBean) && Validator.isNotNull(registrationBean.getStatus())
                        && registrationBean.getStatus() == 3) {
                    isExistingUser = Boolean.TRUE;
                    registrationBean.setIsExistingUser(isExistingUser);
                    registrationBean.setIsDeletedUser(isDeletedUser);
                    if (Validator.isNotNull(liferayStoxxUser)) {
                        registrationBean = populateFields(registrationBean, themeDisplay, liferayStoxxUser);
                    }
                    if (Validator.isNotNull(registrationBean.getSalesEntryId()) && registrationBean.getUserType()
                            .equalsIgnoreCase(STOXXConstants.STOXX_REGISTERED_USER)) {
                        String packageDetails = registrationDelegator
                                .getPackageDetails(registrationBean.getSalesEntryId());
                        log.info(THE_PACKAGE_DETAILS + packageDetails);
                        registrationBean.setPackageDetails(packageDetails);
                    }
                    getListDetails(registrationBean, themeDisplay);
                }
                if (Validator.isNotNull(registrationBean) && Validator.isNotNull(registrationBean.getStatus())
                        && registrationBean.getStatus() == 4) {
                    isDeletedUser = Boolean.TRUE;
                    isExistingUser = Boolean.FALSE;
                    registrationBean.setIsDeletedUser(isDeletedUser);
                    registrationBean.setIsExistingUser(isExistingUser);
                    if (Validator.isNotNull(registrationBean.getSalesEntryId()) && registrationBean.getUserType()
                            .equalsIgnoreCase(STOXXConstants.STOXX_REGISTERED_USER)) {
                        String packageDetails = registrationDelegator
                                .getPackageDetails(registrationBean.getSalesEntryId());
                        log.info(THE_PACKAGE_DETAILS + packageDetails);
                        registrationBean.setPackageDetails(packageDetails);
                    }
                    User liferayDeletedUser = UserLocalServiceUtil.fetchUserByEmailAddress(
                            themeDisplay.getCompanyId(), registrationBean.getBusinessEmailAddress());
                    if (Validator.isNotNull(liferayDeletedUser)) {
                        registrationBean = populateFields(registrationBean, themeDisplay, liferayStoxxUser);
                    }

                    registrationBean = getListDetails(registrationBean, themeDisplay);
                    registrationBean = registrationDelegator
                            .getUserPreferenceDetails(registrationBean.getBusinessEmailAddress(), registrationBean);

                }

                if (!Validator.isNotNull(registrationBean.getStatus()) && liferayStoxxUser.getStatus() == 1
                        && Validator.isNotNull(registrationBean.getSalesEntryId())) {
                    log.info("For customer who are going to add by admin into application after deletion.");
                    isDeletedUser = Boolean.TRUE;
                    isExistingUser = Boolean.FALSE;
                    registrationBean.setIsDeletedUser(isDeletedUser);
                    registrationBean.setIsExistingUser(isExistingUser);
                    if (Validator.isNotNull(registrationBean.getSalesEntryId()) && registrationBean.getUserType()
                            .equalsIgnoreCase(STOXXConstants.STOXX_REGISTERED_USER)) {
                        String packageDetails = registrationDelegator
                                .getPackageDetails(registrationBean.getSalesEntryId());
                        log.info(THE_PACKAGE_DETAILS + packageDetails);
                        registrationBean.setPackageDetails(packageDetails);
                    }
                    getListDetails(registrationBean, themeDisplay);
                }

                registrationBean.setIsExistingUser(isExistingUser);
                registrationBean.setIsDeletedUser(isDeletedUser);
                model.addAttribute(REGISTRATION_BEAN, registrationBean);
                return RegistrationConstants.SETUP_PROFILE;
            }

            else if (portalUrl.contains(StringPool.QUESTION)) {
                SessionMessages.add(renderRequest, "user-validation-done");
                registrationBean = new RegistrationBean();
                businessEmailAddressEncrypted = portalUrl.split(StringPool.BACK_SLASH + StringPool.QUESTION)[1];
                log.info("businessEmailAddress value>>>> " + businessEmailAddressEncrypted);
                businessEmailAddress = STOXXEncrypter.decrypt(businessEmailAddressEncrypted, ENCRYPTION_KEY);
                log.info("decrypted email id is>>> " + businessEmailAddress);
                RegistrationBean registrationBean2 = registrationDelegator.fetchFromStoxxUser(registrationBean,
                        businessEmailAddress);
                if (Validator.isNotNull(registrationBean2) && Validator.isNotNull(registrationBean2.getStatus())
                        && registrationBean2.getStatus() == 3) {
                    log.info("<<<<<<<<<the user is an eisting user>>>>>>>>");
                    isExistingUser = Boolean.TRUE;
                    isDeletedUser = Boolean.FALSE;
                }
                registrationBean.setIsExistingUser(isExistingUser);
                registrationBean.setIsDeletedUser(isDeletedUser);
                log.info("inside registration link validation with questionmark");
                registrationBean.setCompanyId(themeDisplay.getCompanyId());
                if (Validator.isNotNull(registrationBean2) && Validator.isNotNull(registrationBean2.getStatus())
                        && registrationBean2.getStatus() == 4 && registrationDelegator
                                .isInactiveUser(registrationBean.getCompanyId(), businessEmailAddress)) {
                    log.info("<<<<<<<<< the user has been  deleted previously >>>>>>>>");
                    isDeletedUser = Boolean.TRUE;
                    isExistingUser = Boolean.FALSE;
                }
                log.info("registrationBean.getIsDeletedUser() " + isDeletedUser);

                if (Validator.isNotNull(businessEmailAddress) && !registrationBean.getIsExistingUser()) {
                    if (registrationDelegator.checkIfUserExistsInLiferayDB(registrationBean, businessEmailAddress)
                            && registrationDelegator.checkIfUserExistsInLiferay(registrationBean,
                                    businessEmailAddress)
                            && !isDeletedUser) {
                        log.info("inside dupplicate registration block with questionmark");
                        SessionErrors.add(renderRequest, USER_EXISTS_KEY);
                    } else {
                        boolean isUserExistInCustomDB = registrationDelegator
                                .checkIfUserExistsInLiferay(registrationBean, businessEmailAddress);
                        log.info("isUserExistInCustomDB : " + isUserExistInCustomDB);
                        if (!isUserExistInCustomDB && !isDeletedUser) {
                            SessionErrors.add(renderRequest, USER_DELETED_KEY);
                        } else {
                            log.info("inside registration block for new registration with questionmark");
                            registrationBean.setBusinessEmailAddress(businessEmailAddress);
                            registrationBean = registrationDelegator.fetchFromStoxxUser(registrationBean);
                            registrationBean.setBusinessEmailAddress(businessEmailAddress);
                            log.info(
                                    "registartion bean email address" + registrationBean.getBusinessEmailAddress());
                            log.info("the user type is " + registrationBean.getUserType());
                            if (fetchActivationLinkCreateDateDiff(registrationBean.getBusinessEmailAddress())) {
                                SessionErrors.add(renderRequest, RegistrationConstants.REGISTRATION_LINK_EXPIRED);
                            }
                            if (registrationDelegator.checkIfUserExistsInLiferay(registrationBean)
                                    && registrationBean.getUserType()
                                            .equalsIgnoreCase(STOXXConstants.STOXX_STAFF_USER)
                                    && registrationBean.getUserType()
                                            .equalsIgnoreCase(STOXXConstants.STOXX_GENERAL_USER)) {
                                SessionMessages.add(renderRequest, "user-activation-link-already-sent");
                            }
                        }
                    }
                }
                registrationBean.setIsDeletedUser(isDeletedUser);
            }
            registrationBean.setIsExistingUser(isExistingUser);
            registrationBean.setIsDeletedUser(isDeletedUser);
            log.info("registrationBean.toString() : " + registrationBean.toString());
            model.addAttribute(REGISTRATION_BEAN, registrationBean);

        } catch (STOXXException e) {
            log.error("STOXXException in handleRenderRequest", e);
        } catch (Exception e) {
            log.error("Exception in handleRenderRequest", e);
        }
        registrationBean.setIsExistingUser(isExistingUser);
        registrationBean.setIsDeletedUser(isDeletedUser);
        return RegistrationConstants.INITIATE_REGISTRATION;
    }

    /**
     * @param registrationBean
     * @param themeDisplay
     * @param liferayStoxxUser
     * @throws SystemException
     * @throws PortalException
     * @throws STOXXException
     */
    private RegistrationBean populateFields(RegistrationBean registrationBean, ThemeDisplay themeDisplay,
            User liferayStoxxUser) throws SystemException, PortalException, STOXXException {
        registrationBean.setFirstName(liferayStoxxUser.getFirstName());
        registrationBean.setLastName(liferayStoxxUser.getLastName());
        registrationBean.setJobTitle(liferayStoxxUser.getJobTitle());
        registrationBean.setCity(liferayStoxxUser.getAddresses().get(0).getCity());
        registrationBean.setCountryId(liferayStoxxUser.getAddresses().get(0).getCountryId());
        Map<String, Integer> titleList = STOXXUtil.getTitleList();
        registrationBean.setTitleList(titleList);
        String gender = LanguageUtil.get(themeDisplay.getLocale(), "stoxx-registration-female");
        if (liferayStoxxUser.isMale()) {
            gender = LanguageUtil.get(themeDisplay.getLocale(), "stoxx-registration-male");
        }
        registrationBean.setGender(gender);
        Date dateToConvert = liferayStoxxUser.getBirthday();
        String dateOfBirth = STOXXDateUtil.convertDateToStringInStoxxApplicationDateFormat(dateToConvert);
        String defaultBitrhday = PropsUtil.get("stoxx-registration-default-date-of-birthday");
        if (StringUtils.isNotBlank(defaultBitrhday)) {
            defaultBitrhday = defaultBitrhday.replace(StringPool.COMMA, StringPool.COMMA + StringPool.SPACE);
        }
        if (StringUtils.isNotBlank(dateOfBirth)) {
            if (defaultBitrhday.equalsIgnoreCase(dateOfBirth.trim())) {
                dateOfBirth = StringPool.BLANK;
            } else {
                registrationBean.setDateOfBirth(dateOfBirth);
            }
        } else {
            dateOfBirth = StringPool.BLANK;
        }
        log.info("The date of birth is >>>>>>>>" + dateOfBirth);
        registrationBean.setDateOfBirth(dateOfBirth);
        if (StringUtils.isNotBlank(dateOfBirth)) {
            registrationBean.setDateOfBirth(dateOfBirth);
        }
        if (Validator.isNotNull(registrationBean) && Validator.isNotNull(registrationBean.isEmailContactPref())) {
            registrationBean.setEmailContactPref(registrationBean.isEmailContactPref());
        }
        liferayStoxxUser.getAddresses().get(0).getCountry().getName(themeDisplay.getLocale());
        registrationBean.setPhoneNumber(liferayStoxxUser.getPhones().get(0).getNumber());
        registrationBean.setUserZipCode(liferayStoxxUser.getAddresses().get(0).getZip());
        registrationBean.setUserAddress(liferayStoxxUser.getAddresses().get(0).getStreet1());
        registrationBean.setUserAddress1(liferayStoxxUser.getAddresses().get(0).getStreet2());
        return registrationBean;
    }

    private RegistrationBean getListDetails(RegistrationBean registrationBean, ThemeDisplay themeDisplay)
            throws SystemException, STOXXException, PortalException {
        registrationBean.setListProfessionalProfile(
                RegistrationVOUtil.loadProfessionalProfileForGeneralandRegisteredUser(themeDisplay.getLocale()));
        registrationBean.setCountryIdToCountryName(
                RegistrationVOUtil.getCountryList(registrationDelegator, themeDisplay.getLocale()));
        registrationBean.setDeptStaffList(registrationDelegator.getStaffDeparts());
        registrationBean.setMapSliderType(RegistrationVOUtil.loadSliderList(themeDisplay.getLocale()));
        registrationBean.setHobbiesList(RegistrationVOUtil.loadHobbiesList(themeDisplay.getLocale()));
        Map<String, Integer> titleList = STOXXUtil.getTitleList();
        registrationBean.setTitleList(titleList);
        registrationBean.setMapIndexType(RegistrationVOUtil.loadIndexList(themeDisplay.getLocale()));
        registrationBean.setMapUserRegion(RegistrationVOUtil.loadRegionList(themeDisplay.getLocale()));
        registrationBean.setMapNews(RegistrationVOUtil.loadNewsList(themeDisplay.getLocale()));
        registrationBean.setMapResearch(RegistrationVOUtil.loadResearchList(themeDisplay.getLocale()));
        registrationBean
                .setNotificationGeneralEvents(RegistrationVOUtil.getGeneralEventList(themeDisplay.getLocale()));
        registrationBean.setNotificationResources(RegistrationVOUtil.getResourcesList(themeDisplay.getLocale()));
        registrationBean.setNotificationIndex(RegistrationVOUtil.getIndexList(themeDisplay.getLocale()));
        return registrationBean;
    }

    private void getUserTypeandEmalaIdfromEncrptedUrl(Model model, RegistrationBean registrationBean,
            String portalUrl) {
        String businessEmailAddressEncrypted;
        String userTypEncrypted;
        Boolean isExistingUser = Boolean.FALSE;
        Boolean isDeletedUser = Boolean.FALSE;
        try {
            businessEmailAddressEncrypted = portalUrl.substring(portalUrl.indexOf(StringPool.QUESTION) + 1,
                    portalUrl.indexOf(StringPool.DOLLAR));
            businessEmailAddress = STOXXEncrypter.decrypt(businessEmailAddressEncrypted, ENCRYPTION_KEY);
            log.info("decrypted email id is&&&&& " + businessEmailAddress);
            registrationBean.setBusinessEmailAddress(businessEmailAddress);
            log.info("registartion bean email address" + registrationBean.getBusinessEmailAddress());
            userTypEncrypted = portalUrl.split(StringPool.BACK_SLASH + StringPool.DOLLAR)[1];
            userType = STOXXEncrypter.decrypt(userTypEncrypted, ENCRYPTION_KEY);
            registrationBean.setUserType(userType);
            RegistrationBean registrationBean2 = registrationDelegator.fetchFromStoxxUser(registrationBean);
            if (Validator.isNotNull(registrationBean2) && Validator.isNotNull(registrationBean2.getStatus())
                    && registrationBean2.getStatus() == 3) {
                log.info("<<<<<<<<<the user is an eisting user>>>>>>>>");
                isExistingUser = Boolean.TRUE;
            }
            if (Validator.isNotNull(registrationBean2) && Validator.isNotNull(registrationBean2.getStatus())
                    && registrationBean2.getStatus() == 4) {
                log.info("<<<<<<<<<the user has been  deleted previously>>>>>>>>");
                isDeletedUser = Boolean.TRUE;
            }
            registrationBean.setIsExistingUser(isExistingUser);
            registrationBean.setIsDeletedUser(isDeletedUser);
        } catch (STOXXException e) {
            log.error("STOXXException ", e);
        } catch (Exception e) {
            log.error("Exception", e);
        }
        log.info("the user type is " + registrationBean.getUserType());
        model.addAttribute(REGISTRATION_BEAN, registrationBean);
    }

    @ActionMapping(params = "action=reactivateLink")
    public void reactivateLink(ActionRequest actionRequest, ActionResponse actionResponse, Model model,
            @ModelAttribute(REGISTRATION_BEAN) RegistrationBean registrationBean, BindingResult bindingResult) {
        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
        try {
            log.info("***************************the business email address variable is" + businessEmailAddress);
            registrationBean.setBusinessEmailAddress(businessEmailAddress);
            registrationBean = registrationDelegator.fetchFromStoxxUser(registrationBean);
            log.info("the user type is " + registrationBean.getUserType() + " and the activationLink is "
                    + registrationBean.getActivationLink());
            sendEmailByTemplate(registrationBean, themeDisplay);

            registrationBean.setActivationLinkCreatedDate(new Date());
            registrationDelegator.addUserRegistration(registrationBean);
            SessionMessages.add(actionRequest, "mail-success-message");
        } catch (STOXXException e) {
            log.error("STOXXException in handleRenderRequest", e);
        } catch (MessagingException e) {
            log.error("MessagingException in handleRenderRequest", e);
        } catch (Exception e) {
            log.error("Exception", e);
        }

    }

    /**
     * @param registrationBean
     * @param themeDisplay
     * @param emailBodyMap
     * @param emailTemplateFolder
     * @throws SystemException
     * @throws MessagingException
     */
    protected void sendEmailByTemplate(RegistrationBean registrationBean, ThemeDisplay themeDisplay)
            throws SystemException, MessagingException {
        Map<String, Object> emailBodyMap = new HashMap<String, Object>();
        String emailTemplateFolder = "templates";
        try {
            String emailVelocityTemplate = StringPool.BLANK;
            emailVelocityTemplate = emailTemplateFolder + "/" + "registrationActivationMail.vm";
            emailBodyMap.put("toName", registrationBean.getBusinessEmailAddress());
            emailBodyMap.put("activationLink", registrationBean.getActivationLink());
            emailBodyMap.put("fromName", PrefsPropsUtil.getString("stoxx-reg-mail-from-name"));
            emailBodyMap.put("fromAddress", PropsUtil.get("stoxx-reg-mail-from-address"));
            emailBodyMap.put("portalURL", PrefsPropsUtil.getString("stoxx-reg-mail-from-url"));
            stoxxMailUtil.sendEmailNotification(registrationBean.getBusinessEmailAddress(),
                    PropsUtil.get("stoxx-reg-mail-from-address"), emailVelocityTemplate,
                    LanguageUtil.get(themeDisplay.getLocale(), "stoxx-reg-email-subject"), emailBodyMap);
        } catch (MessagingException e) {
            log.error("Exception in sendEmailByTemplate", e);
        }
    }

    @ActionMapping(params = "action=addUserRegistrationAction")
    public void addUserRegistrationAction(ActionRequest actionRequest, ActionResponse actionResponse, Model model,
            @ModelAttribute(REGISTRATION_BEAN) RegistrationBean registrationBean, BindingResult bindingResult) {
        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
        SessionMessages.add(actionRequest,
                PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
        String actionResponseParameter = null;
        try {
            log.info("***************************the business email address variable is" + businessEmailAddress);
            log.info("registrationBean.toString() : " + registrationBean.toString());
            log.info("***************************On Linked in click" + registrationBean.getLinkedinReg());
            log.info("registrationBean.getLinkedinReg() is" + registrationBean.getLinkedinReg());
            if (Validator.isNull(registrationBean.getLinkedinReg())) {
                registrationBean.setLinkedinReg(Boolean.FALSE);
            }
            registrationBean.setCompanyId(themeDisplay.getCompanyId());
            if (Validator.isNotNull(registrationBean) && registrationBean.getLinkedinReg()) {

                if (registrationDelegator.checkIfUserExistsInLiferayDB(registrationBean)) {
                    SessionErrors.add(actionRequest, USER_EXISTS_KEY);
                    actionResponse.setRenderParameter(ACTION, USER_EXISTS);
                    return;
                } else {
                    registrationBean.setUserType(STOXXConstants.STOXX_GENERAL_USER);
                    registrationBean.setIsExistingUser(Boolean.FALSE);
                    actionResponseParameter = "setupProfileTRANSLATOR";
                    createActivationLink(actionRequest, registrationBean, themeDisplay);
                    actionResponse.setRenderParameter(ACTION, actionResponseParameter);
                    return;
                }
            }
            if (Validator.isNotNull(registrationBean)) {
                registrationBean.setCompanyId(themeDisplay.getCompanyId());
                log.info("the company id  is" + registrationBean.getCompanyId());
                log.info("registrationBean.getIsDeletedUser() : " + registrationBean.getIsDeletedUser());
                if (Validator.isNotNull(registrationBean.getIsExistingUser())
                        && registrationBean.getIsExistingUser()
                        && !(Validator.isNotNull(registrationBean.getIsDeletedUser())
                                && registrationBean.getIsDeletedUser())) {
                    log.info("Return the user back to login page...");
                    actionResponse.setRenderParameter(ACTION, "existingUserStep2");
                    return;
                }
            }
            if (Validator.isNotNull(registrationBean) && null != registrationBean.getBusinessEmailAddress()) {
                log.info("the EXPECTED  emailAddress id  is" + registrationBean.getBusinessEmailAddress());
                RegistrationBean regBean = registrationDelegator.fetchFromStoxxUser(registrationBean);
                log.info("the status is" + regBean.getStatus());
                if (Validator.isNotNull(regBean) && Validator.isNotNull(regBean.getStatus())
                        && (regBean.getStatus() == 3 || regBean.getStatus() == 4)) {
                    log.info("the user with emailAddress id  is" + regBean.getBusinessEmailAddress()
                            + "is an existing user and needs to be migrated");
                    try {
                        if (regBean.getStatus() == 4
                                && regBean.getUserType().equalsIgnoreCase(STOXXConstants.STOXX_REGISTERED_USER)) {
                            String emailDomainName = registrationBean.getBusinessEmailAddress()
                                    .substring(registrationBean.getBusinessEmailAddress().lastIndexOf("@"));
                            EmailDomain emailDomain = EmailDomainLocalServiceUtil.getEmailDomain(emailDomainName);
                            regBean.setSalesEntryId(emailDomain.getSalesEntryId());
                            SalesEntry salesEntry = SalesEntryLocalServiceUtil
                                    .getSalesEntry(emailDomain.getSalesEntryId());
                            int allowedLicenses = salesEntry.getUsersAllowed();
                            int alreadyUsedLicenses = registrationDelegator
                                    .getCustomerCountByGroupId(salesEntry.getSalesEntryId());
                            log.info("alreadyUsedLicenses from DB for salesentryID: " + salesEntry.getSalesEntryId()
                                    + " is: " + alreadyUsedLicenses);
                            log.info("allowedLicenses: " + allowedLicenses + "for salesentry company: "
                                    + salesEntry.getCompanyName());
                            if (allowedLicenses <= alreadyUsedLicenses) {
                                SessionErrors.add(actionRequest, CUST_USER_EXCEEDED);
                                actionResponse.setRenderParameter(ACTION, CUST_USER_EXCEEDED);
                                return;
                            }
                        }
                    } catch (NoSuchEmailDomainException e) {
                        log.info("No domain exist for provided email address.");
                        regBean.setUserType(STOXXConstants.STOXX_GENERAL_USER);
                    }
                    createActivationLink(actionRequest, regBean, themeDisplay);
                    actionResponse.setRenderParameter(ACTION, EXISTING_USER);
                    return;
                }
                if (registrationDelegator.checkIfUserExistsInLiferayDB(registrationBean)) {
                    SessionErrors.add(actionRequest, USER_EXISTS_KEY);
                    actionResponse.setRenderParameter(ACTION, USER_EXISTS);
                    return;
                } else if (fetchActivationLinkCreateDateDiff(registrationBean.getBusinessEmailAddress())) {
                    SessionErrors.add(actionRequest, RegistrationConstants.REGISTRATION_LINK_EXPIRED);
                    actionResponse.setRenderParameter(ACTION, RegistrationConstants.REGISTRATION_LINK_ALREADY_SENT);
                    return;
                } else if (registrationDelegator.checkIfUserExistsInLiferay(registrationBean)) {
                    SessionErrors.add(actionRequest, "user-activation-link-already-sent");
                    actionResponse.setRenderParameter(ACTION, RegistrationConstants.REGISTRATION_LINK_ALREADY_SENT);
                    return;
                }
            } else if (null != businessEmailAddress) {
                log.info("Within else if : businessEmailAddress : " + businessEmailAddress);
                RegistrationBean regBean = registrationDelegator.fetchFromStoxxUser(registrationBean,
                        businessEmailAddress);
                if (Validator.isNotNull(regBean) && Validator.isNotNull(regBean.getStatus())
                        && regBean.getStatus() == 3) {
                    log.info("the user with emailAddress id  is" + regBean.getBusinessEmailAddress()
                            + "is an existing user and needs to be migrated");
                    actionResponse.setRenderParameter(ACTION, EXISTING_USER);
                    return;
                }
                if (Validator.isNotNull(regBean) && Validator.isNotNull(regBean.getStatus())
                        && regBean.getStatus() == 4) {
                    log.info("the user with emailAddress id  is" + regBean.getBusinessEmailAddress()
                            + "is an existing user and needs to be migrated");
                    actionResponse.setRenderParameter(ACTION, DELETED_USER);
                    return;
                }

                if (Validator.isNotNull(registrationBean.getIsDeletedUser())
                        && registrationBean.getIsDeletedUser()) {
                    log.info("User email address is : " + businessEmailAddress
                            + ". User added as customer after delition");
                    actionResponse.setRenderParameter(ACTION, DELETED_USER);
                    return;
                }

                if (registrationDelegator.checkIfUserExistsInLiferayDB(registrationBean, businessEmailAddress)) {
                    log.info("User exist in liferay DB");
                    SessionErrors.add(actionRequest, USER_EXISTS_KEY);
                    actionResponse.setRenderParameter(ACTION, USER_EXISTS);
                    return;
                } else if (fetchActivationLinkCreateDateDiff(businessEmailAddress)) {
                    log.info("The activation link is expired.");
                    SessionErrors.add(actionRequest, LINK_EXPIRED);
                    actionResponse.setRenderParameter(ACTION, RegistrationConstants.REGISTRATION_LINK_ALREADY_SENT);
                    return;
                }
            }

            if (null != registrationBean.getBusinessEmailAddress()) {
                log.info("Within If " + registrationBean.getBusinessEmailAddress());
                if (registrationBean.getBusinessEmailAddress()
                        .substring(registrationBean.getBusinessEmailAddress().indexOf(StringPool.AT) + 1)
                        .equalsIgnoreCase(RegistrationConstants.STAFF_DOMAIN_PATTERN)) {
                    registrationBean.setUserType(STOXXConstants.STOXX_STAFF_USER);
                    createActivationLink(actionRequest, registrationBean, themeDisplay);
                    actionResponseParameter = "setupProfileSTAFF";
                } else {
                    try {
                        String emailDomainName = registrationBean.getBusinessEmailAddress()
                                .substring(registrationBean.getBusinessEmailAddress().lastIndexOf("@"));
                        EmailDomain emailDomain = EmailDomainLocalServiceUtil.getEmailDomain(emailDomainName);

                        registrationBean.setSalesEntryId(emailDomain.getSalesEntryId());
                        SalesEntry salesEntry = SalesEntryLocalServiceUtil
                                .getSalesEntry(emailDomain.getSalesEntryId());

                        int allowedLicenses = salesEntry.getUsersAllowed();

                        int alreadyUsedLicenses = registrationDelegator
                                .getCustomerCountByGroupId(salesEntry.getSalesEntryId());
                        log.info("alreadyUsedLicenses from DB for salesentryID: " + salesEntry.getSalesEntryId()
                                + "is: " + alreadyUsedLicenses);
                        log.info("allowedLicenses: " + allowedLicenses + "for salesentry company: "
                                + salesEntry.getCompanyName());
                        if (allowedLicenses <= alreadyUsedLicenses) {
                            SessionErrors.add(actionRequest, CUST_USER_EXCEEDED);
                            actionResponse.setRenderParameter(ACTION, CUST_USER_EXCEEDED);
                            return;
                        }
                        registrationBean.setUserType(STOXXConstants.STOXX_REGISTERED_USER);
                        registrationBean.setCompanyName(salesEntry.getCompanyName());
                        actionResponseParameter = "setupProfileGENERALLICENSED";
                    } catch (NoSuchEmailDomainException e) {
                        log.info("No domain exist for provided email address.");
                        registrationBean.setUserType(STOXXConstants.STOXX_GENERAL_USER);
                        actionResponseParameter = "setupProfileGENERAL";
                    }
                    createActivationLink(actionRequest, registrationBean, themeDisplay);
                }
            } else {
                log.info("Within If " + businessEmailAddress);
                registrationBean = registrationDelegator.fetchFromStoxxUser(registrationBean, businessEmailAddress);
                if (Validator.isNotNull(registrationBean.getActivationLink())
                        && registrationBean.getActivationLink().indexOf(PropsUtil.get("INTRANET")) != -1
                        && registrationBean.getActivationLink().contains(StringPool.DOLLAR)) {
                    if (registrationDelegator.checkIfUserExistsInLiferay(registrationBean, businessEmailAddress)) {
                        registrationBean.setBusinessEmailAddress(businessEmailAddress);
                        registrationDelegator.fetchFromStoxxUser(registrationBean);
                    } else {
                        registrationBean.setCompanyName(RegistrationConstants.STAFF_COMPANY);
                        registrationBean.setUserType(STOXXConstants.STOXX_STAFF_USER);
                    }
                } else if (Validator.isNotNull(registrationBean.getActivationLink())
                        && registrationBean.getActivationLink().indexOf(PropsUtil.get(INTERNET)) != -1) {
                    if (registrationDelegator.checkIfUserExistsInLiferay(registrationBean, businessEmailAddress)) {
                        registrationBean.setBusinessEmailAddress(businessEmailAddress);
                        registrationDelegator.fetchFromStoxxUser(registrationBean);
                    } else {
                        registrationBean.setUserType(STOXXConstants.STOXX_GENERAL_USER);
                    }
                }
                actionResponseParameter = "setupProfileTRANSLATOR";
            }
            log.info("the action parameter is " + actionResponse.getRenderParameterMap());
            actionResponse.setRenderParameter(ACTION, actionResponseParameter);
        } catch (STOXXException e) {
            log.error("STOXXException in addUserRegistrationAction", e);
        } catch (Exception e) {
            log.error("Exception in addUserRegistrationAction ", e);
        }
    }

    public boolean fetchActivationLinkCreateDateDiff(String emailAddress) {
        int activationLinkDatediffDays = 0;
        boolean isActive = false;
        try {
            Date currentDate = new Date();
            RegistrationBean registrationBean1 = new RegistrationBean();
            registrationBean1.setBusinessEmailAddress(emailAddress);
            log.info("Inside fetch Activation Link");
            registrationBean1 = registrationDelegator.fetchFromStoxxUser(registrationBean1);
            if (Validator.isNull(registrationBean1.getActivationLinkCreatedDate())) {
                return isActive;
            }
            activationLinkDatediffDays = ((int) (currentDate.getTime() / (24 * 60 * 60 * 1000))
                    - (int) ((registrationBean1.getActivationLinkCreatedDate().getTime() / (24 * 60 * 60 * 1000))));
            log.info("the activationLinkDatediffDays is " + activationLinkDatediffDays);
            if (activationLinkDatediffDays >= 15) {
                isActive = Boolean.TRUE;
            }
        } catch (STOXXException e) {
            log.error("STOXXException in fetchActivationLinkCreatedateDiff", e);
        } catch (Exception e) {
            log.error("Exception in fetchActivationLinkCreatedateDiff", e);
        }
        return isActive;

    }

    private void createActivationLink(ActionRequest actionRequest, RegistrationBean registrationBean,
            ThemeDisplay themeDisplay) {
        try {
            String encryptedEmailId = STOXXEncrypter.encrypt(registrationBean.getBusinessEmailAddress(),
                    ENCRYPTION_KEY);
            String encrypteduserType = STOXXEncrypter.encrypt(registrationBean.getUserType(), ENCRYPTION_KEY);
            String activationLink = null;
            if (registrationBean.getUserType().equalsIgnoreCase(STOXXConstants.STOXX_STAFF_USER)) {
                activationLink = themeDisplay.getPortalURL() + "/web/stoxxnet/registration?" + encryptedEmailId
                        + StringPool.DOLLAR + encrypteduserType;
            } else {
                activationLink = themeDisplay.getPortalURL() + "/web/stoxxcom/registration?" + encryptedEmailId
                        + StringPool.DOLLAR + encrypteduserType;
            }
            log.info("the activation link is" + activationLink);
            registrationBean.setActivationLink(activationLink);
            registrationBean.setActivationLinkCreatedDate(new Date());
            log.info("registrationBean.getLinkedinReg() is" + registrationBean.getLinkedinReg());
            if (Validator.isNull(registrationBean.getLinkedinReg())) {
                registrationBean.setLinkedinReg(Boolean.FALSE);
            }
            if (Validator.isNotNull(registrationBean) && !registrationBean.getLinkedinReg()) {
                sendEmailByTemplate(registrationBean, themeDisplay);
                SessionMessages.add(actionRequest, "mail-success-message");
            }
            registrationDelegator.addUserRegistration(registrationBean);
        } catch (STOXXException e) {
            log.error("STOXXException createActivationLink", e);
        } catch (Exception e) {
            log.info("registrationBean ", e);
        }
    }

    @ActionMapping(params = "action=setupProfileAction")
    public void setupProfileAction(ActionRequest actionRequest, ActionResponse actionResponse, Model model,
            @ModelAttribute(REGISTRATION_BEAN) RegistrationBean registrationBean, BindingResult bindingResult) {
        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
        String emailVelocityTemplate = StringPool.BLANK;
        try {
            ServiceContext serviceContext = ServiceContextFactory.getInstance(User.class.getName(), actionRequest);
            log.info("Within setupProfileAction...");
            log.info("registrationBean.toString() : " + registrationBean.toString());
            HttpServletRequest httpreq = PortalUtil.getHttpServletRequest(actionRequest);
            HttpServletRequest httpOriginalreq = PortalUtil.getOriginalServletRequest(httpreq);
            //String remoteAddr = httpOriginalreq.getHeader("remote_addr"); // Should be Client-IP to retrieve IP address of the client machine  //
            String remoteAddr = httpOriginalreq.getHeader("Client-IP");
            if (Validator.isNull(remoteAddr)) {
                remoteAddr = httpOriginalreq.getRemoteAddr();
            }
            try {
                CaptchaUtil.check(actionRequest);
            } catch (Exception e) {
                if (e instanceof CaptchaTextException) {
                    SessionErrors.add(actionRequest, e.getClass(), e);
                    actionResponse.setRenderParameter(ACTION, "CaptchaException");
                    return;
                }
            }
            log.info("the remote address is " + remoteAddr);
            String location = RegistrationVOUtil.getLocationForUser(remoteAddr);
            if (null != location) {
                registrationBean.setLocation(location);
            }
            RegistrationBean registrationBean1 = registrationDelegator.fetchFromStoxxUser(registrationBean);
            registrationBean.setUserType(registrationBean1.getUserType());
            registrationBean.setCompanyId(themeDisplay.getCompanyId());
            registrationBean.setActivationLink(registrationBean.getActivationLink());
            log.info("registrationBean.getActivationLink() : " + registrationBean.getActivationLink());
            log.info("registrationBean.getIsExistingUser() : " + registrationBean.getIsExistingUser());
            log.info("registrationBean.getIsDeletedUser() : " + registrationBean.getIsDeletedUser());
            registrationBean.setActivationLinkCreatedDate(new Date());
            registrationDelegator.addUserRegistration(registrationBean, serviceContext, themeDisplay,
                    registrationBean.getIsExistingUser(), registrationBean.getIsDeletedUser());

            Layout layout = themeDisplay.getLayout();
            PortletURL loginActionUrl = PortletURLFactoryUtil.create(actionRequest, "58", layout.getPlid(),
                    "ACTION_PHASE");
            loginActionUrl.setWindowState(WindowState.MAXIMIZED);
            loginActionUrl.setPortletMode(PortletMode.VIEW);
            loginActionUrl.setParameter("saveLastPath", "0");
            loginActionUrl.setParameter("struts_action", "/login/login");
            loginActionUrl.setParameter("login", registrationBean.getBusinessEmailAddress());
            loginActionUrl.setParameter("password", registrationBean.getUserPassword());

            Map<String, Object> emailBodyMap = new HashMap<String, Object>();
            String emailTemplateFolder = "templates";
            emailBodyMap.put("toName", registrationBean.getBusinessEmailAddress());
            emailBodyMap.put("fromName", PrefsPropsUtil.getString("stoxx-reg-mail-from-name"));
            emailBodyMap.put("fromAddress", PrefsPropsUtil.getString("stoxx-reg-mail-fromAddress"));
            emailVelocityTemplate = emailTemplateFolder + StringPool.SLASH + "willingtocontact.vm";
            String subject = LanguageUtil.get(themeDisplay.getLocale(),
                    "stoxx-reg-mail-subject-for-wiiling-to-contact");
            if (registrationBean.isContactMe())
                stoxxMailUtil.shareByEmail(registrationBean.getBusinessEmailAddress(),
                        PrefsPropsUtil.getString("stoxx-reg-mail-from-name"), emailVelocityTemplate, subject,
                        emailBodyMap, themeDisplay);
            if (null != registrationBean.getUserType()) {
                if (registrationBean.getUserType().equalsIgnoreCase(STOXXConstants.STOXX_VENDOR_USER)) {
                    log.info("if redirected page is " + PortalUtil.getPortalURL(httpOriginalreq)
                            + RegistrationConstants.REGISTRATION_GROUP + PropsUtil.get(INTRANET)
                            + RegistrationConstants.REGISTRATION_DASHBOARD);
                    log.info("registrationBean.getDashboardRedirectCondition()  1>>>>>>>>>"
                            + registrationBean.getDashboardRedirectCondition());
                    loginActionUrl.setParameter(_58_REDIRECT, RegistrationConstants.REGISTRATION_GROUP
                            + PropsUtil.get("INTRANET") + RegistrationConstants.REGISTRATION_DASHBOARD);

                    actionResponse.sendRedirect(loginActionUrl.toString());
                } else if (registrationBean.getUserType().equalsIgnoreCase(STOXXConstants.STOXX_STAFF_USER)) {
                    log.info("registrationBean.getDashboardRedirectCondition()  2>>>>>>>>>"
                            + registrationBean.getDashboardRedirectCondition());
                    if (StringUtils.isNotBlank(registrationBean.getDashboardRedirectCondition())) {
                        log.info("registrationBean.getDashboardRedirectCondition()  3>>>>>>>>>"
                                + registrationBean.getDashboardRedirectCondition());
                        if ("yes".equalsIgnoreCase(registrationBean.getDashboardRedirectCondition())) {
                            log.info("registrationBean.getDashboardRedirectCondition()  4>>>>>>>>>"
                                    + registrationBean.getDashboardRedirectCondition());

                            log.info("if redirected page is " + PortalUtil.getPortalURL(httpOriginalreq)
                                    + RegistrationConstants.REGISTRATION_GROUP + PropsUtil.get(INTERNET)
                                    + RegistrationConstants.REGISTRATION_MYPROFILE);

                            loginActionUrl.setParameter(_58_REDIRECT, RegistrationConstants.REGISTRATION_GROUP
                                    + PropsUtil.get(INTERNET) + RegistrationConstants.REGISTRATION_MYPROFILE);
                            actionResponse.sendRedirect(loginActionUrl.toString());
                        } else {
                            if ("no".equalsIgnoreCase(registrationBean.getDashboardRedirectCondition())) {
                                log.info("registrationBean.getDashboardRedirectCondition()  4>>>>>>>>>"
                                        + registrationBean.getDashboardRedirectCondition());

                                log.info("if redirected page is " + PortalUtil.getPortalURL(httpOriginalreq)
                                        + RegistrationConstants.REGISTRATION_GROUP + PropsUtil.get(INTRANET)
                                        + RegistrationConstants.REGISTRATION_DASHBOARD);

                                loginActionUrl.setParameter(_58_REDIRECT, RegistrationConstants.REGISTRATION_GROUP
                                        + PropsUtil.get(INTRANET) + RegistrationConstants.REGISTRATION_DASHBOARD);
                                actionResponse.sendRedirect(loginActionUrl.toString());

                            }
                        }
                    }
                } else {
                    log.info("else redirected page is " + PortalUtil.getPortalURL(httpOriginalreq)
                            + RegistrationConstants.REGISTRATION_GROUP + PropsUtil.get("INTERNET")
                            + RegistrationConstants.REGISTRATION_DASHBOARD);
                    loginActionUrl.setParameter(_58_REDIRECT, RegistrationConstants.REGISTRATION_GROUP
                            + PropsUtil.get("INTERNET") + RegistrationConstants.REGISTRATION_DASHBOARD);
                    actionResponse.sendRedirect(loginActionUrl.toString());
                }
            }

        } catch (STOXXException e) {
            log.error("STOXXException createActivationLink", e);
        } catch (Exception e) {
            log.error("Exception createActivationLink", e);
            try {
                registrationDelegator.deleteUser(registrationBean.getBusinessEmailAddress(),
                        themeDisplay.getCompanyId());
            } catch (STOXXException e1) {
                log.info("the delete user STOXXException ", e1);
            }
            log.error("Exception in setupProfileAction" + e.getMessage(), e);
        }
    }

    public RegistrationDelegator getRegistrationDelegator() {
        return registrationDelegator;
    }

    public STOXXMailUtil getStoxxMailUtil() {
        return stoxxMailUtil;
    }

    @ResourceMapping("checkCaptcha")
    public void validateCaptcha(ResourceRequest resourceRequest, ResourceResponse resourceResponse,
            RegistrationBean registrationBean, BindingResult bindingResult, Model model) throws IOException {
        log.info("entering checkCaptcha method>>>>>>>>>>>>");
        JSONObject jsonMessageObject = JSONFactoryUtil.createJSONObject();
        boolean responseString = Boolean.FALSE;
        try {
            String enteredCaptcha = ParamUtil.getString(resourceRequest, "captchaText");
            String captchaDefault = getCaptchaValueFromSession(resourceRequest.getPortletSession());
            log.info("EnteredCaptcha is " + enteredCaptcha + " and the Captcha from session is " + captchaDefault);
            if (enteredCaptcha.equalsIgnoreCase(captchaDefault) && Validator.isNotNull(captchaDefault)
                    && Validator.isNotNull(enteredCaptcha)) {
                responseString = Boolean.TRUE;
            }
        } catch (Exception e) {
            responseString = Boolean.FALSE;
            log.error("IOException", e);
        } finally {
            try {
                jsonMessageObject.put("isValidCaptcha", responseString);
                PrintWriter writer = resourceResponse.getWriter();
                writer.print(jsonMessageObject);
            } catch (IOException e) {
                log.error("IOException", e);
            }
        }
    }

    public String getCaptchaValueFromSession(PortletSession session) {
        Enumeration<String> atNames = session.getAttributeNames();
        while (atNames.hasMoreElements()) {
            String name = atNames.nextElement();
            log.info("The session name is >>>>>>> " + name);
            if (name.contains("CAPTCHA_TEXT")) {
                return (String) session.getAttribute(name);
            }
        }
        return "";
    }

    public void setStoxxMailUtil(STOXXMailUtil stoxxMailUtil) {
        this.stoxxMailUtil = stoxxMailUtil;
    }

    public void setRegistrationDelegator(RegistrationDelegator registrationDelegator) {
        this.registrationDelegator = registrationDelegator;
    }

    private static final Logger log = Logger.getLogger(RegistrationController.class);

    private String businessEmailAddress;
    private String userType;

    public String getBusinessEmailAddress() {
        return businessEmailAddress;
    }

    public void setBusinessEmailAddress(String businessEmailAddress) {
        this.businessEmailAddress = businessEmailAddress;
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

}