Search in sources :

Example 21 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class PersistableOrderPopulator method populate.

@Override
public Order populate(PersistableOrder source, Order target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(productService, "productService must be set");
    Validate.notNull(digitalProductService, "digitalProductService must be set");
    Validate.notNull(productAttributeService, "productAttributeService must be set");
    Validate.notNull(customerService, "customerService must be set");
    Validate.notNull(countryService, "countryService must be set");
    Validate.notNull(zoneService, "zoneService must be set");
    Validate.notNull(currencyService, "currencyService must be set");
    try {
        Map<String, Country> countriesMap = countryService.getCountriesMap(language);
        Map<String, Zone> zonesMap = zoneService.getZones(language);
        /**
         * customer *
         */
        PersistableCustomer customer = source.getCustomer();
        if (customer != null) {
            if (customer.getId() != null && customer.getId() > 0) {
                Customer modelCustomer = customerService.getById(customer.getId());
                if (modelCustomer == null) {
                    throw new ConversionException("Customer id " + customer.getId() + " does not exists");
                }
                if (modelCustomer.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                    throw new ConversionException("Customer id " + customer.getId() + " does not exists for store " + store.getCode());
                }
                target.setCustomerId(modelCustomer.getId());
                target.setBilling(modelCustomer.getBilling());
                target.setDelivery(modelCustomer.getDelivery());
                target.setCustomerEmailAddress(source.getCustomer().getEmailAddress());
            }
        }
        target.setLocale(LocaleUtils.getLocale(store));
        CreditCard creditCard = source.getCreditCard();
        if (creditCard != null) {
            String maskedNumber = CreditCardUtils.maskCardNumber(creditCard.getCcNumber());
            creditCard.setCcNumber(maskedNumber);
            target.setCreditCard(creditCard);
        }
        Currency currency = null;
        try {
            currency = currencyService.getByCode(source.getCurrency());
        } catch (Exception e) {
            throw new ConversionException("Currency not found for code " + source.getCurrency());
        }
        if (currency == null) {
            throw new ConversionException("Currency not found for code " + source.getCurrency());
        }
        target.setCurrency(currency);
        target.setDatePurchased(source.getDatePurchased());
        // target.setCurrency(store.getCurrency());
        target.setCurrencyValue(new BigDecimal(0));
        target.setMerchant(store);
        target.setStatus(source.getOrderStatus());
        target.setPaymentModuleCode(source.getPaymentModule());
        target.setPaymentType(source.getPaymentType());
        target.setShippingModuleCode(source.getShippingModule());
        target.setCustomerAgreement(source.isCustomerAgreed());
        target.setConfirmedAddress(source.isConfirmedAddress());
        if (source.getPreviousOrderStatus() != null) {
            List<OrderStatus> orderStatusList = source.getPreviousOrderStatus();
            for (OrderStatus status : orderStatusList) {
                OrderStatusHistory statusHistory = new OrderStatusHistory();
                statusHistory.setStatus(status);
                statusHistory.setOrder(target);
                target.getOrderHistory().add(statusHistory);
            }
        }
        if (!StringUtils.isBlank(source.getComments())) {
            OrderStatusHistory statusHistory = new OrderStatusHistory();
            statusHistory.setStatus(null);
            statusHistory.setOrder(target);
            statusHistory.setComments(source.getComments());
            target.getOrderHistory().add(statusHistory);
        }
        List<PersistableOrderProduct> products = source.getOrderProductItems();
        if (CollectionUtils.isEmpty(products)) {
            throw new ConversionException("Requires at least 1 PersistableOrderProduct");
        }
        com.salesmanager.shop.populator.order.PersistableOrderProductPopulator orderProductPopulator = new PersistableOrderProductPopulator();
        orderProductPopulator.setProductAttributeService(productAttributeService);
        orderProductPopulator.setProductService(productService);
        orderProductPopulator.setDigitalProductService(digitalProductService);
        for (PersistableOrderProduct orderProduct : products) {
            OrderProduct modelOrderProduct = new OrderProduct();
            orderProductPopulator.populate(orderProduct, modelOrderProduct, store, language);
            target.getOrderProducts().add(modelOrderProduct);
        }
        List<OrderTotal> orderTotals = source.getTotals();
        if (CollectionUtils.isNotEmpty(orderTotals)) {
            for (OrderTotal total : orderTotals) {
                com.salesmanager.core.model.order.OrderTotal totalModel = new com.salesmanager.core.model.order.OrderTotal();
                totalModel.setModule(total.getModule());
                totalModel.setOrder(target);
                totalModel.setOrderTotalCode(total.getCode());
                totalModel.setTitle(total.getTitle());
                totalModel.setValue(total.getValue());
                target.getOrderTotal().add(totalModel);
            }
        }
    } catch (Exception e) {
        throw new ConversionException(e);
    }
    return target;
}
Also used : OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) OrderStatus(com.salesmanager.core.model.order.orderstatus.OrderStatus) Currency(com.salesmanager.core.model.reference.currency.Currency) ConversionException(com.salesmanager.core.business.exception.ConversionException) Zone(com.salesmanager.core.model.reference.zone.Zone) CreditCard(com.salesmanager.core.model.order.payment.CreditCard) ConversionException(com.salesmanager.core.business.exception.ConversionException) BigDecimal(java.math.BigDecimal) Country(com.salesmanager.core.model.reference.country.Country) OrderStatusHistory(com.salesmanager.core.model.order.orderstatus.OrderStatusHistory) OrderTotal(com.salesmanager.shop.model.order.total.OrderTotal)

Example 22 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class PersistableCustomerReviewPopulator method populate.

@Override
public CustomerReview populate(PersistableCustomerReview source, CustomerReview target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(customerService, "customerService cannot be null");
    Validate.notNull(languageService, "languageService cannot be null");
    Validate.notNull(source.getRating(), "Rating cannot bot be null");
    try {
        if (target == null) {
            target = new CustomerReview();
        }
        if (source.getDate() == null) {
            String date = DateUtil.formatDate(new Date());
            source.setDate(date);
        }
        target.setReviewDate(DateUtil.getDate(source.getDate()));
        if (source.getId() != null && source.getId().longValue() == 0) {
            source.setId(null);
        } else {
            target.setId(source.getId());
        }
        Customer reviewer = customerService.getById(source.getCustomerId());
        Customer reviewed = customerService.getById(source.getReviewedCustomer());
        target.setReviewRating(source.getRating());
        target.setCustomer(reviewer);
        target.setReviewedCustomer(reviewed);
        Language lang = languageService.getByCode(language.getCode());
        if (lang == null) {
            throw new ConversionException("Invalid language code, use iso codes (en, fr ...)");
        }
        CustomerReviewDescription description = new CustomerReviewDescription();
        description.setDescription(source.getDescription());
        description.setLanguage(lang);
        description.setName("-");
        description.setCustomerReview(target);
        Set<CustomerReviewDescription> descriptions = new HashSet<CustomerReviewDescription>();
        descriptions.add(description);
        target.setDescriptions(descriptions);
    } catch (Exception e) {
        throw new ConversionException("Cannot populate CustomerReview", e);
    }
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) Language(com.salesmanager.core.model.reference.language.Language) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomerReview(com.salesmanager.shop.model.customer.PersistableCustomerReview) CustomerReview(com.salesmanager.core.model.customer.review.CustomerReview) Date(java.util.Date) ConversionException(com.salesmanager.core.business.exception.ConversionException) CustomerReviewDescription(com.salesmanager.core.model.customer.review.CustomerReviewDescription) HashSet(java.util.HashSet)

Example 23 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class CustomerFacadeImpl method requestPasswordReset.

@Override
public void requestPasswordReset(String customerName, String customerContextPath, MerchantStore store, Language language) {
    try {
        // get customer by user name
        Customer customer = customerService.getByNick(customerName, store.getId());
        if (customer == null) {
            throw new ResourceNotFoundException("Customer [" + customerName + "] not found for store [" + store.getCode() + "]");
        }
        // generates unique token
        String token = UUID.randomUUID().toString();
        Date expiry = DateUtil.addDaysToCurrentDate(2);
        CredentialsReset credsRequest = new CredentialsReset();
        credsRequest.setCredentialsRequest(token);
        credsRequest.setCredentialsRequestExpiry(expiry);
        customer.setCredentialsResetRequest(credsRequest);
        customerService.saveOrUpdate(customer);
        // reset password link
        // this will build http | https ://domain/contextPath
        String baseUrl = filePathUtils.buildBaseUrl(customerContextPath, store);
        // need to add link to controller receiving user reset password
        // request
        String customerResetLink = new StringBuilder().append(baseUrl).append(String.format(resetCustomerLink, store.getCode(), token)).toString();
        resetPasswordRequest(customer, customerResetLink, store, lamguageService.toLocale(language, store));
    } catch (Exception e) {
        throw new ServiceRuntimeException("Error while executing resetPassword request", e);
    }
/**
 * User sends username (unique in the system)
 *
 * UserNameEntity will be the following { userName: "test@test.com" }
 *
 * The system retrieves user using userName (username is unique) if user
 * exists, system sends an email with reset password link
 *
 * How to retrieve a User from userName
 *
 * userFacade.findByUserName
 *
 * How to send an email
 *
 * How to generate a token
 *
 * Generate random token
 *
 * Calculate token expiration date
 *
 * Now + 48 hours
 *
 * Update User in the database with token
 *
 * Send reset token email
 */
}
Also used : Customer(com.salesmanager.core.model.customer.Customer) CredentialsReset(com.salesmanager.core.model.common.CredentialsReset) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Date(java.util.Date) ServiceException(com.salesmanager.core.business.exception.ServiceException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 24 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class CustomerFacadeImpl method updateAddress.

@Override
public void updateAddress(Long userId, MerchantStore merchantStore, Address address, final Language language) throws Exception {
    Customer customerModel = customerService.getById(userId);
    Map<String, Country> countriesMap = countryService.getCountriesMap(language);
    Country country = countriesMap.get(address.getCountry());
    if (customerModel == null) {
        LOG.error("Customer with ID {} does not exists..", userId);
        // throw new CustomerNotFoundException( "customer with given id does not exists" );
        throw new Exception("customer with given id does not exists");
    }
    if (address.isBillingAddress()) {
        LOG.info("updating customer billing address..");
        PersistableCustomerBillingAddressPopulator billingAddressPopulator = new PersistableCustomerBillingAddressPopulator();
        customerModel = billingAddressPopulator.populate(address, customerModel, merchantStore, merchantStore.getDefaultLanguage());
        customerModel.getBilling().setCountry(country);
        if (StringUtils.isNotBlank(address.getZone())) {
            Zone zone = zoneService.getByCode(address.getZone());
            if (zone == null) {
                throw new ConversionException("Unsuported zone code " + address.getZone());
            }
            customerModel.getBilling().setZone(zone);
            customerModel.getBilling().setState(null);
        } else {
            customerModel.getBilling().setZone(null);
        }
    } else {
        LOG.info("updating customer shipping address..");
        PersistableCustomerShippingAddressPopulator shippingAddressPopulator = new PersistableCustomerShippingAddressPopulator();
        customerModel = shippingAddressPopulator.populate(address, customerModel, merchantStore, merchantStore.getDefaultLanguage());
        customerModel.getDelivery().setCountry(country);
        if (StringUtils.isNotBlank(address.getZone())) {
            Zone zone = zoneService.getByCode(address.getZone());
            if (zone == null) {
                throw new ConversionException("Unsuported zone code " + address.getZone());
            }
            customerModel.getDelivery().setZone(zone);
            customerModel.getDelivery().setState(null);
        } else {
            customerModel.getDelivery().setZone(null);
        }
    }
    // same update address with customer model
    this.customerService.saveOrUpdate(customerModel);
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) Zone(com.salesmanager.core.model.reference.zone.Zone) Country(com.salesmanager.core.model.reference.country.Country) PersistableCustomerBillingAddressPopulator(com.salesmanager.shop.populator.customer.PersistableCustomerBillingAddressPopulator) ServiceException(com.salesmanager.core.business.exception.ServiceException) UserAlreadyExistException(com.salesmanager.shop.model.customer.UserAlreadyExistException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ConversionException(com.salesmanager.core.business.exception.ConversionException) PersistableCustomerShippingAddressPopulator(com.salesmanager.shop.populator.customer.PersistableCustomerShippingAddressPopulator)

Example 25 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class CustomerFacadeImpl method registerCustomer.

@Override
public PersistableCustomer registerCustomer(final PersistableCustomer customer, final MerchantStore merchantStore, Language language) throws Exception {
    LOG.info("Starting customer registration process..");
    if (userExist(customer.getUserName())) {
        throw new UserAlreadyExistException("User already exist");
    }
    Customer customerModel = getCustomerModel(customer, merchantStore, language);
    if (customerModel == null) {
        LOG.equals("Unable to create customer in system");
        // throw new CustomerRegistrationException( "Unable to register customer" );
        throw new Exception("Unable to register customer");
    }
    LOG.info("About to persist customer to database.");
    customerService.saveOrUpdate(customerModel);
    LOG.info("Returning customer data to controller..");
    // return customerEntityPoulator(customerModel,merchantStore);
    customer.setId(customerModel.getId());
    return customer;
}
Also used : UserAlreadyExistException(com.salesmanager.shop.model.customer.UserAlreadyExistException) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) ServiceException(com.salesmanager.core.business.exception.ServiceException) UserAlreadyExistException(com.salesmanager.shop.model.customer.UserAlreadyExistException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ConversionException(com.salesmanager.core.business.exception.ConversionException)

Aggregations

Customer (com.salesmanager.core.model.customer.Customer)71 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)33 ReadableCustomer (com.salesmanager.shop.model.customer.ReadableCustomer)32 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)31 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)30 Language (com.salesmanager.core.model.reference.language.Language)26 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)17 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)17 ConversionException (com.salesmanager.core.business.exception.ConversionException)16 ServiceException (com.salesmanager.core.business.exception.ServiceException)16 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)16 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)12 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)12 Authentication (org.springframework.security.core.Authentication)12 Date (java.util.Date)11 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)10 ArrayList (java.util.ArrayList)10 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)10 Product (com.salesmanager.core.model.catalog.product.Product)9 Country (com.salesmanager.core.model.reference.country.Country)9