Search in sources :

Example 1 with SecuredShopPersistableCustomer

use of com.salesmanager.shop.model.customer.SecuredShopPersistableCustomer in project shopizer by shopizer-ecommerce.

the class CustomerRegistrationController method displayRegistration.

@RequestMapping(value = "/registration.html", method = RequestMethod.GET)
public String displayRegistration(final Model model, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
    model.addAttribute("recapatcha_public_key", siteKeyKey);
    SecuredShopPersistableCustomer customer = new SecuredShopPersistableCustomer();
    AnonymousCustomer anonymousCustomer = (AnonymousCustomer) request.getAttribute(Constants.ANONYMOUS_CUSTOMER);
    if (anonymousCustomer != null) {
        customer.setBilling(anonymousCustomer.getBilling());
    }
    model.addAttribute("customer", customer);
    /**
     * template *
     */
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.register).append(".").append(store.getStoreTemplate());
    return template.toString();
}
Also used : SecuredShopPersistableCustomer(com.salesmanager.shop.model.customer.SecuredShopPersistableCustomer) AnonymousCustomer(com.salesmanager.shop.model.customer.AnonymousCustomer) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with SecuredShopPersistableCustomer

use of com.salesmanager.shop.model.customer.SecuredShopPersistableCustomer in project shopizer by shopizer-ecommerce.

the class CustomerRegistrationController method registerCustomer.

@RequestMapping(value = "/register.html", method = RequestMethod.POST)
public String registerCustomer(@Valid @ModelAttribute("customer") SecuredShopPersistableCustomer customer, BindingResult bindingResult, Model model, HttpServletRequest request, HttpServletResponse response, final Locale locale) throws Exception {
    MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
    Language language = super.getLanguage(request);
    String userName = null;
    String password = null;
    model.addAttribute("recapatcha_public_key", siteKeyKey);
    if (!StringUtils.isBlank(request.getParameter("g-recaptcha-response"))) {
        boolean validateCaptcha = captchaRequestUtils.checkCaptcha(request.getParameter("g-recaptcha-response"));
        if (!validateCaptcha) {
            LOGGER.debug("Captcha response does not matched");
            FieldError error = new FieldError("captchaChallengeField", "captchaChallengeField", messages.getMessage("validaion.recaptcha.not.matched", locale));
            bindingResult.addError(error);
        }
    }
    if (StringUtils.isNotBlank(customer.getUserName())) {
        if (customerFacade.checkIfUserExists(customer.getUserName(), merchantStore)) {
            LOGGER.debug("Customer with username {} already exists for this store ", customer.getUserName());
            FieldError error = new FieldError("userName", "userName", messages.getMessage("registration.username.already.exists", locale));
            bindingResult.addError(error);
        }
        userName = customer.getUserName();
    }
    if (StringUtils.isNotBlank(customer.getPassword()) && StringUtils.isNotBlank(customer.getCheckPassword())) {
        if (!customer.getPassword().equals(customer.getCheckPassword())) {
            FieldError error = new FieldError("password", "password", messages.getMessage("message.password.checkpassword.identical", locale));
            bindingResult.addError(error);
        }
        password = customer.getPassword();
    }
    if (bindingResult.hasErrors()) {
        LOGGER.debug("found {} validation error while validating in customer registration ", bindingResult.getErrorCount());
        StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.register).append(".").append(merchantStore.getStoreTemplate());
        return template.toString();
    }
    @SuppressWarnings("unused") CustomerEntity customerData = null;
    try {
        // set user clear password
        customer.setPassword(password);
        customerData = customerFacade.registerCustomer(customer, merchantStore, language);
    } catch (Exception e) {
        LOGGER.error("Error while registering customer.. ", e);
        ObjectError error = new ObjectError("registration", messages.getMessage("registration.failed", locale));
        bindingResult.addError(error);
        StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.register).append(".").append(merchantStore.getStoreTemplate());
        return template.toString();
    }
    try {
        /**
         * Send registration email
         */
        emailTemplatesUtils.sendRegistrationEmail(customer, merchantStore, locale, request.getContextPath());
    } catch (Exception e) {
        LOGGER.error("Cannot send email to customer ", e);
    }
    try {
        // refresh customer
        Customer c = customerFacade.getCustomerByUserName(customer.getUserName(), merchantStore);
        // authenticate
        customerFacade.authenticate(c, userName, password);
        super.setSessionAttribute(Constants.CUSTOMER, c, request);
        StringBuilder cookieValue = new StringBuilder();
        cookieValue.append(merchantStore.getCode()).append("_").append(c.getNick());
        // set username in the cookie
        Cookie cookie = new Cookie(Constants.COOKIE_NAME_USER, cookieValue.toString());
        cookie.setMaxAge(60 * 24 * 3600);
        cookie.setPath(Constants.SLASH);
        response.addCookie(cookie);
        String sessionShoppingCartCode = (String) request.getSession().getAttribute(Constants.SHOPPING_CART);
        if (!StringUtils.isBlank(sessionShoppingCartCode)) {
            ShoppingCart shoppingCart = customerFacade.mergeCart(c, sessionShoppingCartCode, merchantStore, language);
            ShoppingCartData shoppingCartData = this.populateShoppingCartData(shoppingCart, merchantStore, language);
            if (shoppingCartData != null) {
                request.getSession().setAttribute(Constants.SHOPPING_CART, shoppingCartData.getCode());
            }
            // set username in the cookie
            Cookie c1 = new Cookie(Constants.COOKIE_NAME_CART, shoppingCartData.getCode());
            c1.setMaxAge(60 * 24 * 3600);
            c1.setPath(Constants.SLASH);
            response.addCookie(c1);
        }
        return "redirect:/shop/customer/dashboard.html";
    } catch (Exception e) {
        LOGGER.error("Cannot authenticate user ", e);
        ObjectError error = new ObjectError("registration", messages.getMessage("registration.failed", locale));
        bindingResult.addError(error);
    }
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.register).append(".").append(merchantStore.getStoreTemplate());
    return template.toString();
}
Also used : Cookie(javax.servlet.http.Cookie) AnonymousCustomer(com.salesmanager.shop.model.customer.AnonymousCustomer) SecuredShopPersistableCustomer(com.salesmanager.shop.model.customer.SecuredShopPersistableCustomer) Customer(com.salesmanager.core.model.customer.Customer) FieldError(org.springframework.validation.FieldError) ShoppingCartData(com.salesmanager.shop.model.shoppingcart.ShoppingCartData) ServiceException(com.salesmanager.core.business.exception.ServiceException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ObjectError(org.springframework.validation.ObjectError) Language(com.salesmanager.core.model.reference.language.Language) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) CustomerEntity(com.salesmanager.shop.model.customer.CustomerEntity) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)2 AnonymousCustomer (com.salesmanager.shop.model.customer.AnonymousCustomer)2 SecuredShopPersistableCustomer (com.salesmanager.shop.model.customer.SecuredShopPersistableCustomer)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 ServiceException (com.salesmanager.core.business.exception.ServiceException)1 Customer (com.salesmanager.core.model.customer.Customer)1 Language (com.salesmanager.core.model.reference.language.Language)1 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)1 CustomerEntity (com.salesmanager.shop.model.customer.CustomerEntity)1 ShoppingCartData (com.salesmanager.shop.model.shoppingcart.ShoppingCartData)1 Cookie (javax.servlet.http.Cookie)1 FieldError (org.springframework.validation.FieldError)1 ObjectError (org.springframework.validation.ObjectError)1