Search in sources :

Example 1 with Address

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

the class StoreFilter method preHandle.

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    request.setCharacterEncoding("UTF-8");
    /**
     * if url contains /services exit from here !
     */
    if (request.getRequestURL().toString().toLowerCase().contains(SERVICES_URL_PATTERN) || request.getRequestURL().toString().toLowerCase().contains(REFERENCE_URL_PATTERN)) {
        return true;
    }
    try {
        /**
         * merchant store *
         */
        MerchantStore store = (MerchantStore) request.getSession().getAttribute(Constants.MERCHANT_STORE);
        String storeCode = request.getParameter(STORE_REQUEST_PARAMETER);
        // remove link set from controllers for declaring active - inactive
        // links
        request.removeAttribute(Constants.LINK_CODE);
        if (!StringUtils.isBlank(storeCode)) {
            if (store != null) {
                if (!store.getCode().equals(storeCode)) {
                    store = setMerchantStoreInSession(request, storeCode);
                }
            } else {
                // when url sm-shop/shop is being loaded for first time
                // store is null
                store = setMerchantStoreInSession(request, storeCode);
            }
        }
        if (store == null) {
            store = setMerchantStoreInSession(request, MerchantStore.DEFAULT_STORE);
        }
        if (StringUtils.isBlank(store.getStoreTemplate())) {
            store.setStoreTemplate(Constants.DEFAULT_TEMPLATE);
        }
        request.setAttribute(Constants.MERCHANT_STORE, store);
        /*
			//remote ip address
			String remoteAddress = "";
			try {
				
				if (request != null) {
					remoteAddress = request.getHeader("X-Forwarded-For");
					if (remoteAddress == null || "".equals(remoteAddress)) {
						remoteAddress = request.getRemoteAddr();
					}
				}
				remoteAddress = remoteAddress != null && remoteAddress.contains(",") ? remoteAddress.split(",")[0] : remoteAddress;
				LOGGER.info("remote ip addres {}", remoteAddress);
			} catch (Exception e) {
				LOGGER.error("Error while getting user remote address");
			}
			*/
        String ipAddress = GeoLocationUtils.getClientIpAddress(request);
        UserContext userContext = UserContext.create();
        userContext.setIpAddress(ipAddress);
        /**
         * customer *
         */
        Customer customer = (Customer) request.getSession().getAttribute(Constants.CUSTOMER);
        if (customer != null) {
            if (customer.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                request.getSession().removeAttribute(Constants.CUSTOMER);
            }
            if (!customer.isAnonymous()) {
                if (!request.isUserInRole("AUTH_CUSTOMER")) {
                    request.removeAttribute(Constants.CUSTOMER);
                }
            }
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            request.setAttribute(Constants.CUSTOMER, customer);
        }
        if (customer == null) {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            if (auth != null && request.isUserInRole("AUTH_CUSTOMER")) {
                customer = customerService.getByNick(auth.getName());
                if (customer != null) {
                    request.setAttribute(Constants.CUSTOMER, customer);
                }
            }
        }
        AnonymousCustomer anonymousCustomer = (AnonymousCustomer) request.getSession().getAttribute(Constants.ANONYMOUS_CUSTOMER);
        if (anonymousCustomer == null) {
            Address address = null;
            try {
                if (!StringUtils.isBlank(ipAddress)) {
                    com.salesmanager.core.model.common.Address geoAddress = customerService.getCustomerAddress(store, ipAddress);
                    if (geoAddress != null) {
                        address = new Address();
                        address.setCountry(geoAddress.getCountry());
                        address.setCity(geoAddress.getCity());
                        address.setZone(geoAddress.getZone());
                    /**
                     * no postal code *
                     */
                    // address.setPostalCode(geoAddress.getPostalCode());
                    }
                }
            } catch (Exception ce) {
                LOGGER.error("Cannot get geo ip component ", ce);
            }
            if (address == null) {
                address = new Address();
                address.setCountry(store.getCountry().getIsoCode());
                if (store.getZone() != null) {
                    address.setZone(store.getZone().getCode());
                } else {
                    address.setStateProvince(store.getStorestateprovince());
                }
            /**
             * no postal code *
             */
            // address.setPostalCode(store.getStorepostalcode());
            }
            anonymousCustomer = new AnonymousCustomer();
            anonymousCustomer.setBilling(address);
            request.getSession().setAttribute(Constants.ANONYMOUS_CUSTOMER, anonymousCustomer);
        } else {
            request.setAttribute(Constants.ANONYMOUS_CUSTOMER, anonymousCustomer);
        }
        /**
         * language & locale *
         */
        Language language = languageUtils.getRequestLanguage(request, response);
        request.setAttribute(Constants.LANGUAGE, language);
        Locale locale = languageService.toLocale(language, store);
        request.setAttribute(Constants.LOCALE, locale);
        // Locale locale = LocaleContextHolder.getLocale();
        LocaleContextHolder.setLocale(locale);
        /**
         * Breadcrumbs *
         */
        setBreadcrumb(request, locale);
        /**
         * Get global objects Themes are built on a similar way displaying
         * Header, Body and Footer Header and Footer are displayed on each
         * page Some themes also contain side bars which may include similar
         * emements
         *
         * Elements from Header : - CMS links - Customer - Mini shopping
         * cart - Store name / logo - Top categories - Search
         *
         * Elements from Footer : - CMS links - Store address - Global
         * payment information - Global shipping information
         */
        // get from the cache first
        /**
         * The cache for each object contains 2 objects, a Cache and a
         * Missed-Cache Get objects from the cache If not null use those
         * objects If null, get entry from missed-cache If missed-cache not
         * null then nothing exist If missed-cache null, add missed-cache
         * entry and load from the database If objects from database not
         * null store in cache
         */
        /**
         ***** CMS Objects *******
         */
        this.getContentObjects(store, language, request);
        /**
         ***** CMS Page names *********
         */
        this.getContentPageNames(store, language, request);
        /**
         ***** Top Categories *******
         */
        // this.getTopCategories(store, language, request);
        this.setTopCategories(store, language, request);
        /**
         ***** Default metatags ******
         */
        /**
         * Title Description Keywords
         */
        PageInformation pageInformation = new PageInformation();
        pageInformation.setPageTitle(store.getStorename());
        pageInformation.setPageDescription(store.getStorename());
        pageInformation.setPageKeywords(store.getStorename());
        @SuppressWarnings("unchecked") Map<String, ContentDescription> contents = (Map<String, ContentDescription>) request.getAttribute(Constants.REQUEST_CONTENT_OBJECTS);
        if (contents != null) {
            // for(String key : contents.keySet()) {
            // List<ContentDescription> contentsList = contents.get(key);
            // for(Content content : contentsList) {
            // if(key.equals(Constants.CONTENT_LANDING_PAGE)) {
            // List<ContentDescription> descriptions =
            // content.getDescriptions();
            ContentDescription contentDescription = contents.get(Constants.CONTENT_LANDING_PAGE);
            if (contentDescription != null) {
                // for(ContentDescription contentDescription : descriptions)
                // {
                // if(contentDescription.getLanguage().getCode().equals(language.getCode()))
                // {
                pageInformation.setPageTitle(contentDescription.getName());
                pageInformation.setPageDescription(contentDescription.getMetatagDescription());
                pageInformation.setPageKeywords(contentDescription.getMetatagKeywords());
            // }
            }
        // }
        // }
        // }
        }
        request.setAttribute(Constants.REQUEST_PAGE_INFORMATION, pageInformation);
        /**
         ***** Configuration objects ******
         */
        /**
         * SHOP configuration type Should contain - Different configuration
         * flags - Google analytics - Facebook page - Twitter handle - Show
         * customer login - ...
         */
        this.getMerchantConfigurations(store, request);
        /**
         ***** Shopping Cart ********
         */
        String shoppingCarCode = (String) request.getSession().getAttribute(Constants.SHOPPING_CART);
        if (shoppingCarCode != null) {
            request.setAttribute(Constants.REQUEST_SHOPPING_CART, shoppingCarCode);
        }
    } catch (Exception e) {
        LOGGER.error("Error in StoreFilter", e);
    }
    return true;
}
Also used : Address(com.salesmanager.shop.model.customer.address.Address) AnonymousCustomer(com.salesmanager.shop.model.customer.AnonymousCustomer) Customer(com.salesmanager.core.model.customer.Customer) UserContext(com.salesmanager.core.model.common.UserContext) AnonymousCustomer(com.salesmanager.shop.model.customer.AnonymousCustomer) Language(com.salesmanager.core.model.reference.language.Language) PageInformation(com.salesmanager.shop.model.shop.PageInformation) Authentication(org.springframework.security.core.Authentication) ContentDescription(com.salesmanager.core.model.content.ContentDescription) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with Address

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

the class CustomerRegistrationIntegrationTest method registerCustomer.

@Test
public void registerCustomer() {
    final PersistableCustomer testCustomer = new PersistableCustomer();
    testCustomer.setEmailAddress("customer1@test.com");
    testCustomer.setPassword("clear123");
    testCustomer.setGender(CustomerGender.M.name());
    testCustomer.setLanguage("en");
    final Address billing = new Address();
    billing.setFirstName("customer1");
    billing.setLastName("ccstomer1");
    billing.setCountry("BE");
    testCustomer.setBilling(billing);
    testCustomer.setStoreCode(Constants.DEFAULT_STORE);
    final HttpEntity<PersistableCustomer> entity = new HttpEntity<>(testCustomer, getHeader());
    final ResponseEntity<PersistableCustomer> response = testRestTemplate.postForEntity("/api/v1/customer/register", entity, PersistableCustomer.class);
    assertThat(response.getStatusCode(), is(OK));
    // created customer can login
    final ResponseEntity<AuthenticationResponse> loginResponse = testRestTemplate.postForEntity("/api/v1/customer/login", new HttpEntity<>(new AuthenticationRequest("customer1@test.com", "clear123")), AuthenticationResponse.class);
    assertThat(loginResponse.getStatusCode(), is(OK));
    assertNotNull(loginResponse.getBody().getToken());
}
Also used : Address(com.salesmanager.shop.model.customer.address.Address) HttpEntity(org.springframework.http.HttpEntity) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) AuthenticationRequest(com.salesmanager.shop.store.security.AuthenticationRequest) AuthenticationResponse(com.salesmanager.shop.store.security.AuthenticationResponse) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with Address

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

the class ReadableCustomerMapper method merge.

@Override
public ReadableCustomer merge(Customer source, ReadableCustomer target, MerchantStore store, Language language) {
    if (source.getId() != null && source.getId() > 0) {
        target.setId(source.getId());
    }
    target.setEmailAddress(source.getEmailAddress());
    if (StringUtils.isNotEmpty(source.getNick())) {
        target.setUserName(source.getNick());
    }
    if (source.getDefaultLanguage() != null) {
        target.setLanguage(source.getDefaultLanguage().getCode());
    }
    if (source.getGender() != null) {
        target.setGender(source.getGender().name());
    }
    if (StringUtils.isNotEmpty(source.getProvider())) {
        target.setProvider(source.getProvider());
    }
    if (source.getBilling() != null) {
        Address address = new Address();
        address.setAddress(source.getBilling().getAddress());
        address.setCity(source.getBilling().getCity());
        address.setCompany(source.getBilling().getCompany());
        address.setFirstName(source.getBilling().getFirstName());
        address.setLastName(source.getBilling().getLastName());
        address.setPostalCode(source.getBilling().getPostalCode());
        address.setPhone(source.getBilling().getTelephone());
        if (source.getBilling().getCountry() != null) {
            address.setCountry(source.getBilling().getCountry().getIsoCode());
        }
        if (source.getBilling().getZone() != null) {
            address.setZone(source.getBilling().getZone().getCode());
        }
        if (source.getBilling().getState() != null) {
            address.setStateProvince(source.getBilling().getState());
        }
        target.setFirstName(address.getFirstName());
        target.setLastName(address.getLastName());
        target.setBilling(address);
    }
    if (source.getCustomerReviewAvg() != null) {
        target.setRating(source.getCustomerReviewAvg().doubleValue());
    }
    if (source.getCustomerReviewCount() != null) {
        target.setRatingCount(source.getCustomerReviewCount().intValue());
    }
    if (source.getDelivery() != null) {
        Address address = new Address();
        address.setCity(source.getDelivery().getCity());
        address.setAddress(source.getDelivery().getAddress());
        address.setCompany(source.getDelivery().getCompany());
        address.setFirstName(source.getDelivery().getFirstName());
        address.setLastName(source.getDelivery().getLastName());
        address.setPostalCode(source.getDelivery().getPostalCode());
        address.setPhone(source.getDelivery().getTelephone());
        if (source.getDelivery().getCountry() != null) {
            address.setCountry(source.getDelivery().getCountry().getIsoCode());
        }
        if (source.getDelivery().getZone() != null) {
            address.setZone(source.getDelivery().getZone().getCode());
        }
        if (source.getDelivery().getState() != null) {
            address.setStateProvince(source.getDelivery().getState());
        }
        target.setDelivery(address);
    } else {
        target.setDelivery(target.getBilling());
    }
    if (source.getAttributes() != null) {
        for (CustomerAttribute attribute : source.getAttributes()) {
            ReadableCustomerAttribute readableAttribute = new ReadableCustomerAttribute();
            readableAttribute.setId(attribute.getId());
            readableAttribute.setTextValue(attribute.getTextValue());
            ReadableCustomerOption option = new ReadableCustomerOption();
            option.setId(attribute.getCustomerOption().getId());
            option.setCode(attribute.getCustomerOption().getCode());
            CustomerOptionDescription d = new CustomerOptionDescription();
            d.setDescription(attribute.getCustomerOption().getDescriptionsSettoList().get(0).getDescription());
            d.setName(attribute.getCustomerOption().getDescriptionsSettoList().get(0).getName());
            option.setDescription(d);
            readableAttribute.setCustomerOption(option);
            ReadableCustomerOptionValue optionValue = new ReadableCustomerOptionValue();
            optionValue.setId(attribute.getCustomerOptionValue().getId());
            CustomerOptionValueDescription vd = new CustomerOptionValueDescription();
            vd.setDescription(attribute.getCustomerOptionValue().getDescriptionsSettoList().get(0).getDescription());
            vd.setName(attribute.getCustomerOptionValue().getDescriptionsSettoList().get(0).getName());
            optionValue.setCode(attribute.getCustomerOptionValue().getCode());
            optionValue.setDescription(vd);
            readableAttribute.setCustomerOptionValue(optionValue);
            target.getAttributes().add(readableAttribute);
        }
        if (source.getGroups() != null) {
            for (Group group : source.getGroups()) {
                ReadableGroup readableGroup = new ReadableGroup();
                readableGroup.setId(group.getId().longValue());
                readableGroup.setName(group.getGroupName());
                readableGroup.setType(group.getGroupType().name());
                target.getGroups().add(readableGroup);
            }
        }
    }
    return target;
}
Also used : CustomerOptionDescription(com.salesmanager.shop.model.customer.attribute.CustomerOptionDescription) ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) Group(com.salesmanager.core.model.user.Group) ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) Address(com.salesmanager.shop.model.customer.address.Address) ReadableCustomerOptionValue(com.salesmanager.shop.model.customer.attribute.ReadableCustomerOptionValue) ReadableCustomerAttribute(com.salesmanager.shop.model.customer.attribute.ReadableCustomerAttribute) CustomerAttribute(com.salesmanager.core.model.customer.attribute.CustomerAttribute) ReadableCustomerOption(com.salesmanager.shop.model.customer.attribute.ReadableCustomerOption) CustomerOptionValueDescription(com.salesmanager.shop.model.customer.attribute.CustomerOptionValueDescription) ReadableCustomerAttribute(com.salesmanager.shop.model.customer.attribute.ReadableCustomerAttribute)

Example 4 with Address

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

the class ReadableShopOrderPopulator method populate.

@Override
public ReadableShopOrder populate(ShopOrder source, ReadableShopOrder target, MerchantStore store, Language language) throws ConversionException {
    try {
        ReadableCustomer customer = new ReadableCustomer();
        PersistableCustomer persistableCustomer = source.getCustomer();
        customer.setEmailAddress(persistableCustomer.getEmailAddress());
        if (persistableCustomer.getBilling() != null) {
            Address address = new Address();
            address.setCity(persistableCustomer.getBilling().getCity());
            address.setCompany(persistableCustomer.getBilling().getCompany());
            address.setFirstName(persistableCustomer.getBilling().getFirstName());
            address.setLastName(persistableCustomer.getBilling().getLastName());
            address.setPostalCode(persistableCustomer.getBilling().getPostalCode());
            address.setPhone(persistableCustomer.getBilling().getPhone());
            if (persistableCustomer.getBilling().getCountry() != null) {
                address.setCountry(persistableCustomer.getBilling().getCountry());
            }
            if (persistableCustomer.getBilling().getZone() != null) {
                address.setZone(persistableCustomer.getBilling().getZone());
            }
            customer.setBilling(address);
        }
        if (persistableCustomer.getDelivery() != null) {
            Address address = new Address();
            address.setCity(persistableCustomer.getDelivery().getCity());
            address.setCompany(persistableCustomer.getDelivery().getCompany());
            address.setFirstName(persistableCustomer.getDelivery().getFirstName());
            address.setLastName(persistableCustomer.getDelivery().getLastName());
            address.setPostalCode(persistableCustomer.getDelivery().getPostalCode());
            address.setPhone(persistableCustomer.getDelivery().getPhone());
            if (persistableCustomer.getDelivery().getCountry() != null) {
                address.setCountry(persistableCustomer.getDelivery().getCountry());
            }
            if (persistableCustomer.getDelivery().getZone() != null) {
                address.setZone(persistableCustomer.getDelivery().getZone());
            }
            customer.setDelivery(address);
        }
        // TODO if ship to billing enabled, set delivery = billing
        /*			if(persistableCustomer.getAttributes()!=null) {
				for(PersistableCustomerAttribute attribute : persistableCustomer.getAttributes()) {
					ReadableCustomerAttribute readableAttribute = new ReadableCustomerAttribute();
					readableAttribute.setId(attribute.getId());
					ReadableCustomerOption option = new ReadableCustomerOption();
					option.setId(attribute.getCustomerOption().getId());
					option.setCode(attribute.getCustomerOption());
					
					CustomerOptionDescription d = new CustomerOptionDescription();
					d.setDescription(attribute.getCustomerOption().getDescriptionsSettoList().get(0).getDescription());
					d.setName(attribute.getCustomerOption().getDescriptionsSettoList().get(0).getName());
					option.setDescription(d);
					
					readableAttribute.setCustomerOption(option);
					
					ReadableCustomerOptionValue optionValue = new ReadableCustomerOptionValue();
					optionValue.setId(attribute.getCustomerOptionValue().getId());
					CustomerOptionValueDescription vd = new CustomerOptionValueDescription();
					vd.setDescription(attribute.getCustomerOptionValue().getDescriptionsSettoList().get(0).getDescription());
					vd.setName(attribute.getCustomerOptionValue().getDescriptionsSettoList().get(0).getName());
					optionValue.setCode(attribute.getCustomerOptionValue().getCode());
					optionValue.setDescription(vd);
					
					
					readableAttribute.setCustomerOptionValue(optionValue);
					customer.getAttributes().add(readableAttribute);
				}
			}*/
        target.setCustomer(customer);
    } catch (Exception e) {
        throw new ConversionException(e);
    }
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) Address(com.salesmanager.shop.model.customer.address.Address) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) ConversionException(com.salesmanager.core.business.exception.ConversionException)

Example 5 with Address

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

the class CustomerPopulator method populate.

/**
 * Creates a Customer entity ready to be saved
 */
@Override
public Customer populate(PersistableCustomer source, Customer target, MerchantStore store, Language language) throws ConversionException {
    try {
        if (source.getId() != null && source.getId() > 0) {
            target.setId(source.getId());
        }
        if (!StringUtils.isBlank(source.getPassword())) {
            target.setPassword(passwordEncoder.encode(source.getPassword()));
            target.setNick(source.getUserName());
            target.setAnonymous(false);
        }
        if (source.getBilling() != null) {
            target.setBilling(new Billing());
            if (!StringUtils.isEmpty(source.getFirstName())) {
                target.getBilling().setFirstName(source.getFirstName());
            }
            if (!StringUtils.isEmpty(source.getLastName())) {
                target.getBilling().setLastName(source.getLastName());
            }
        }
        if (!StringUtils.isBlank(source.getProvider())) {
            target.setProvider(source.getProvider());
        }
        if (!StringUtils.isBlank(source.getEmailAddress())) {
            target.setEmailAddress(source.getEmailAddress());
        }
        if (source.getGender() != null && target.getGender() == null) {
            target.setGender(com.salesmanager.core.model.customer.CustomerGender.valueOf(source.getGender()));
        }
        if (target.getGender() == null) {
            target.setGender(com.salesmanager.core.model.customer.CustomerGender.M);
        }
        Map<String, Country> countries = countryService.getCountriesMap(language);
        Map<String, Zone> zones = zoneService.getZones(language);
        target.setMerchantStore(store);
        Address sourceBilling = source.getBilling();
        if (sourceBilling != null) {
            Billing billing = target.getBilling();
            billing.setAddress(sourceBilling.getAddress());
            billing.setCity(sourceBilling.getCity());
            billing.setCompany(sourceBilling.getCompany());
            // billing.setCountry(country);
            if (!StringUtils.isEmpty(sourceBilling.getFirstName()))
                billing.setFirstName(sourceBilling.getFirstName());
            if (!StringUtils.isEmpty(sourceBilling.getLastName()))
                billing.setLastName(sourceBilling.getLastName());
            billing.setTelephone(sourceBilling.getPhone());
            billing.setPostalCode(sourceBilling.getPostalCode());
            billing.setState(sourceBilling.getStateProvince());
            Country billingCountry = null;
            if (!StringUtils.isBlank(sourceBilling.getCountry())) {
                billingCountry = countries.get(sourceBilling.getCountry());
                if (billingCountry == null) {
                    throw new ConversionException("Unsuported country code " + sourceBilling.getCountry());
                }
                billing.setCountry(billingCountry);
            }
            if (billingCountry != null && !StringUtils.isBlank(sourceBilling.getZone())) {
                Zone zone = zoneService.getByCode(sourceBilling.getZone());
                if (zone == null) {
                    throw new ConversionException("Unsuported zone code " + sourceBilling.getZone());
                }
                Zone zoneDescription = zones.get(zone.getCode());
                billing.setZone(zoneDescription);
            }
        // target.setBilling(billing);
        }
        if (target.getBilling() == null && source.getBilling() != null) {
            LOG.info("Setting default values for billing");
            Billing billing = new Billing();
            Country billingCountry = null;
            if (StringUtils.isNotBlank(source.getBilling().getCountry())) {
                billingCountry = countries.get(source.getBilling().getCountry());
                if (billingCountry == null) {
                    throw new ConversionException("Unsuported country code " + sourceBilling.getCountry());
                }
                billing.setCountry(billingCountry);
                target.setBilling(billing);
            }
        }
        Address sourceShipping = source.getDelivery();
        if (sourceShipping != null) {
            Delivery delivery = new Delivery();
            delivery.setAddress(sourceShipping.getAddress());
            delivery.setCity(sourceShipping.getCity());
            delivery.setCompany(sourceShipping.getCompany());
            delivery.setFirstName(sourceShipping.getFirstName());
            delivery.setLastName(sourceShipping.getLastName());
            delivery.setTelephone(sourceShipping.getPhone());
            delivery.setPostalCode(sourceShipping.getPostalCode());
            delivery.setState(sourceShipping.getStateProvince());
            Country deliveryCountry = null;
            if (!StringUtils.isBlank(sourceShipping.getCountry())) {
                deliveryCountry = countries.get(sourceShipping.getCountry());
                if (deliveryCountry == null) {
                    throw new ConversionException("Unsuported country code " + sourceShipping.getCountry());
                }
                delivery.setCountry(deliveryCountry);
            }
            if (deliveryCountry != null && !StringUtils.isBlank(sourceShipping.getZone())) {
                Zone zone = zoneService.getByCode(sourceShipping.getZone());
                if (zone == null) {
                    throw new ConversionException("Unsuported zone code " + sourceShipping.getZone());
                }
                Zone zoneDescription = zones.get(zone.getCode());
                delivery.setZone(zoneDescription);
            }
            target.setDelivery(delivery);
        }
        if (source.getRating() != null && source.getRating().doubleValue() > 0) {
            target.setCustomerReviewAvg(new BigDecimal(source.getRating().doubleValue()));
        }
        if (source.getRatingCount() > 0) {
            target.setCustomerReviewCount(source.getRatingCount());
        }
        if (target.getDelivery() == null && source.getDelivery() != null) {
            LOG.info("Setting default value for delivery");
            Delivery delivery = new Delivery();
            Country deliveryCountry = null;
            if (StringUtils.isNotBlank(source.getDelivery().getCountry())) {
                deliveryCountry = countries.get(source.getDelivery().getCountry());
                if (deliveryCountry == null) {
                    throw new ConversionException("Unsuported country code " + sourceShipping.getCountry());
                }
                delivery.setCountry(deliveryCountry);
                target.setDelivery(delivery);
            }
        }
        if (source.getAttributes() != null) {
            for (PersistableCustomerAttribute attr : source.getAttributes()) {
                CustomerOption customerOption = customerOptionService.getById(attr.getCustomerOption().getId());
                if (customerOption == null) {
                    throw new ConversionException("Customer option id " + attr.getCustomerOption().getId() + " does not exist");
                }
                CustomerOptionValue customerOptionValue = customerOptionValueService.getById(attr.getCustomerOptionValue().getId());
                if (customerOptionValue == null) {
                    throw new ConversionException("Customer option value id " + attr.getCustomerOptionValue().getId() + " does not exist");
                }
                if (customerOption.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                    throw new ConversionException("Invalid customer option id ");
                }
                if (customerOptionValue.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                    throw new ConversionException("Invalid customer option value id ");
                }
                CustomerAttribute attribute = new CustomerAttribute();
                attribute.setCustomer(target);
                attribute.setCustomerOption(customerOption);
                attribute.setCustomerOptionValue(customerOptionValue);
                attribute.setTextValue(attr.getTextValue());
                target.getAttributes().add(attribute);
            }
        }
        if (target.getDefaultLanguage() == null) {
            Language lang = source.getLanguage() == null ? language : languageService.getByCode(source.getLanguage());
            target.setDefaultLanguage(lang);
        }
    } catch (Exception e) {
        throw new ConversionException(e);
    }
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) Address(com.salesmanager.shop.model.customer.address.Address) Zone(com.salesmanager.core.model.reference.zone.Zone) BigDecimal(java.math.BigDecimal) ConversionException(com.salesmanager.core.business.exception.ConversionException) CustomerOption(com.salesmanager.core.model.customer.attribute.CustomerOption) CustomerOptionValue(com.salesmanager.core.model.customer.attribute.CustomerOptionValue) Language(com.salesmanager.core.model.reference.language.Language) PersistableCustomerAttribute(com.salesmanager.shop.model.customer.attribute.PersistableCustomerAttribute) CustomerAttribute(com.salesmanager.core.model.customer.attribute.CustomerAttribute) Billing(com.salesmanager.core.model.common.Billing) Country(com.salesmanager.core.model.reference.country.Country) Delivery(com.salesmanager.core.model.common.Delivery) PersistableCustomerAttribute(com.salesmanager.shop.model.customer.attribute.PersistableCustomerAttribute)

Aggregations

Address (com.salesmanager.shop.model.customer.address.Address)11 ConversionException (com.salesmanager.core.business.exception.ConversionException)7 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)4 ReadableCustomer (com.salesmanager.shop.model.customer.ReadableCustomer)4 Customer (com.salesmanager.core.model.customer.Customer)3 CustomerAttribute (com.salesmanager.core.model.customer.attribute.CustomerAttribute)3 ServiceException (com.salesmanager.core.business.exception.ServiceException)2 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)2 Language (com.salesmanager.core.model.reference.language.Language)2 Group (com.salesmanager.core.model.user.Group)2 CustomerOptionDescription (com.salesmanager.shop.model.customer.attribute.CustomerOptionDescription)2 CustomerOptionValueDescription (com.salesmanager.shop.model.customer.attribute.CustomerOptionValueDescription)2 ReadableCustomerAttribute (com.salesmanager.shop.model.customer.attribute.ReadableCustomerAttribute)2 ReadableCustomerOption (com.salesmanager.shop.model.customer.attribute.ReadableCustomerOption)2 ReadableCustomerOptionValue (com.salesmanager.shop.model.customer.attribute.ReadableCustomerOptionValue)2 ReadableGroup (com.salesmanager.shop.model.security.ReadableGroup)2 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)2 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)2 Product (com.salesmanager.core.model.catalog.product.Product)1 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)1