Search in sources :

Example 86 with Language

use of com.salesmanager.core.model.reference.language.Language in project shopizer by shopizer-ecommerce.

the class InitializationDatabaseImpl method createCountries.

private void createCountries() throws ServiceException {
    LOGGER.info(String.format("%s : Populating Countries ", name));
    List<Language> languages = languageService.list();
    for (String code : SchemaConstant.COUNTRY_ISO_CODE) {
        Locale locale = SchemaConstant.LOCALES.get(code);
        if (locale != null) {
            Country country = new Country(code);
            countryService.create(country);
            for (Language language : languages) {
                String name = locale.getDisplayCountry(new Locale(language.getCode()));
                // byte[] ptext = value.getBytes(Constants.ISO_8859_1);
                // String name = new String(ptext, Constants.UTF_8);
                CountryDescription description = new CountryDescription(language, name);
                countryService.addCountryDescription(country, description);
            }
        }
    }
}
Also used : Locale(java.util.Locale) CountryDescription(com.salesmanager.core.model.reference.country.CountryDescription) Language(com.salesmanager.core.model.reference.language.Language) Country(com.salesmanager.core.model.reference.country.Country)

Example 87 with Language

use of com.salesmanager.core.model.reference.language.Language in project shopizer by shopizer-ecommerce.

the class ZonesLoader method loadZones.

public Map<String, Zone> loadZones(String jsonFilePath) throws Exception {
    List<Language> languages = languageService.list();
    List<Country> countries = countryService.list();
    Map<String, Country> countriesMap = new HashMap<String, Country>();
    for (Country country : countries) {
        countriesMap.put(country.getIsoCode(), country);
    }
    ObjectMapper mapper = new ObjectMapper();
    try {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream(jsonFilePath);
        @SuppressWarnings("unchecked") Map<String, Object> data = mapper.readValue(in, Map.class);
        Map<String, Zone> zonesMap = new HashMap<String, Zone>();
        Map<String, List<ZoneDescription>> zonesDescriptionsMap = new HashMap<String, List<ZoneDescription>>();
        Map<String, String> zonesMark = new HashMap<String, String>();
        for (Language l : languages) {
            @SuppressWarnings("rawtypes") List langList = (List) data.get(l.getCode());
            if (langList != null) {
                /**
                 * submethod
                 */
                for (Object z : langList) {
                    @SuppressWarnings("unchecked") Map<String, String> e = (Map<String, String>) z;
                    this.mapZone(l, zonesDescriptionsMap, countriesMap, zonesMap, zonesMark, e);
                /**
                 * String zoneCode = e.get("zoneCode"); ZoneDescription
                 * zoneDescription = new ZoneDescription();
                 * zoneDescription.setLanguage(l);
                 * zoneDescription.setName(e.get("zoneName")); Zone zone
                 * = null; List<ZoneDescription> descriptions = null; if
                 * (!zonesMap.containsKey(zoneCode)) { zone = new
                 * Zone(); Country country =
                 * countriesMap.get(e.get("countryCode")); if (country
                 * == null) { LOGGER.warn("Country is null for " +
                 * zoneCode + " and country code " +
                 * e.get("countryCode")); continue; }
                 * zone.setCountry(country); zonesMap.put(zoneCode,
                 * zone); zone.setCode(zoneCode);
                 *
                 * }
                 *
                 * if (zonesMark.containsKey(l.getCode() + "_" +
                 * zoneCode)) { LOGGER.warn("This zone seems to be a
                 * duplicate ! " + zoneCode + " and language code " +
                 * l.getCode()); continue; }
                 *
                 * zonesMark.put(l.getCode() + "_" + zoneCode,
                 * l.getCode() + "_" + zoneCode);
                 *
                 * if (zonesDescriptionsMap.containsKey(zoneCode)) {
                 * descriptions = zonesDescriptionsMap.get(zoneCode); }
                 * else { descriptions = new
                 * ArrayList<ZoneDescription>();
                 * zonesDescriptionsMap.put(zoneCode, descriptions); }
                 *
                 * descriptions.add(zoneDescription);
                 */
                }
            }
        }
        for (Map.Entry<String, Zone> entry : zonesMap.entrySet()) {
            String key = entry.getKey();
            Zone value = entry.getValue();
            // get descriptions
            List<ZoneDescription> descriptions = zonesDescriptionsMap.get(key);
            if (descriptions != null) {
                value.setDescriptons(descriptions);
            }
        }
        return zonesMap;
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) InputStream(java.io.InputStream) Zone(com.salesmanager.core.model.reference.zone.Zone) IOException(java.io.IOException) ServiceException(com.salesmanager.core.business.exception.ServiceException) Language(com.salesmanager.core.model.reference.language.Language) ServiceException(com.salesmanager.core.business.exception.ServiceException) Country(com.salesmanager.core.model.reference.country.Country) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ZoneDescription(com.salesmanager.core.model.reference.zone.ZoneDescription)

Example 88 with Language

use of com.salesmanager.core.model.reference.language.Language in project shopizer by shopizer-ecommerce.

the class ZonesLoader method loadIndividualZones.

// 
@SuppressWarnings({ "rawtypes", "unchecked" })
public List<Map<String, Zone>> loadIndividualZones() throws Exception {
    List<Map<String, Zone>> loadedZones = new ArrayList<Map<String, Zone>>();
    try {
        List<Resource> files = geZoneFiles(PATH);
        List<Language> languages = languageService.list();
        ObjectMapper mapper = new ObjectMapper();
        List<Country> countries = countryService.list();
        Map<String, Country> countriesMap = new HashMap<String, Country>();
        for (Country country : countries) {
            countriesMap.put(country.getIsoCode(), country);
        }
        Map<String, Zone> zonesMap = new LinkedHashMap<String, Zone>();
        Map<String, List<ZoneDescription>> zonesDescriptionsMap = new LinkedHashMap<String, List<ZoneDescription>>();
        Map<String, String> zonesMark = new LinkedHashMap<String, String>();
        // load files individually
        for (Resource resource : files) {
            InputStream in = resource.getInputStream();
            if (in == null) {
                continue;
            }
            Map<String, Object> data = mapper.readValue(in, Map.class);
            if (resource.getFilename().contains("_")) {
                for (Language l : languages) {
                    if (resource.getFilename().contains("_" + l.getCode())) {
                        // lead for this
                        // language
                        List langList = (List) data.get(l.getCode());
                        if (langList != null) {
                            /**
                             * submethod
                             */
                            for (Object z : langList) {
                                Map<String, String> e = (Map<String, String>) z;
                                mapZone(l, zonesDescriptionsMap, countriesMap, zonesMap, zonesMark, e);
                            }
                        }
                    }
                }
            } else {
                List langList = (List) data.get(ALL_REGIONS);
                if (langList != null) {
                    /**
                     * submethod
                     */
                    for (Language l : languages) {
                        for (Object z : langList) {
                            Map<String, String> e = (Map<String, String>) z;
                            mapZone(l, zonesDescriptionsMap, countriesMap, zonesMap, zonesMark, e);
                        }
                    }
                }
            }
            for (Map.Entry<String, Zone> entry : zonesMap.entrySet()) {
                String key = entry.getKey();
                Zone value = entry.getValue();
                // get descriptions
                List<ZoneDescription> descriptions = zonesDescriptionsMap.get(key);
                if (descriptions != null) {
                    value.setDescriptons(descriptions);
                }
            }
            loadedZones.add(zonesMap);
        }
        return loadedZones;
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Language(com.salesmanager.core.model.reference.language.Language) ArrayList(java.util.ArrayList) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ZoneDescription(com.salesmanager.core.model.reference.zone.ZoneDescription) Zone(com.salesmanager.core.model.reference.zone.Zone) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceException(com.salesmanager.core.business.exception.ServiceException) Country(com.salesmanager.core.model.reference.country.Country) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 89 with Language

use of com.salesmanager.core.model.reference.language.Language in project shopizer by shopizer-ecommerce.

the class ProductFacadeV2Impl method getProductPrice.

@Override
public ReadableProductPrice getProductPrice(Long id, ProductPriceRequest priceRequest, MerchantStore store, Language language) {
    Validate.notNull(id, "Product id cannot be null");
    Validate.notNull(priceRequest, "Product price request cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(language, "Language cannot be null");
    try {
        Product model = productService.findOne(id, store);
        List<ProductAttribute> attributes = null;
        if (!CollectionUtils.isEmpty(priceRequest.getOptions())) {
            List<Long> attrinutesIds = priceRequest.getOptions().stream().map(p -> p.getId()).collect(Collectors.toList());
            attributes = productAttributeService.getByAttributeIds(store, model, attrinutesIds);
            for (ProductAttribute attribute : attributes) {
                if (attribute.getProduct().getId().longValue() != id.longValue()) {
                    // throw unauthorized
                    throw new OperationNotAllowedException("Attribute with id [" + attribute.getId() + "] is not attached to product id [" + id + "]");
                }
            }
        }
        if (!StringUtils.isBlank(priceRequest.getSku())) {
            // change default availability with sku (instance availability)
            List<ProductAvailability> availabilityList = productAvailabilityService.getBySku(priceRequest.getSku(), store);
            if (CollectionUtils.isNotEmpty(availabilityList)) {
                model.setAvailabilities(new HashSet(availabilityList));
            }
        }
        FinalPrice price;
        // attributes can be null;
        price = pricingService.calculateProductPrice(model, attributes);
        ReadableProductPrice readablePrice = new ReadableProductPrice();
        ReadableFinalPricePopulator populator = new ReadableFinalPricePopulator();
        populator.setPricingService(pricingService);
        return populator.populate(price, readablePrice, store, language);
    } catch (Exception e) {
        throw new ServiceRuntimeException("An error occured while getting product price", e);
    }
}
Also used : ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) ReadableProductInstance(com.salesmanager.shop.model.catalog.product.product.instance.ReadableProductInstance) Autowired(org.springframework.beans.factory.annotation.Autowired) ProductFacade(com.salesmanager.shop.store.controller.product.facade.ProductFacade) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Language(com.salesmanager.core.model.reference.language.Language) ReadableFinalPricePopulator(com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ProductInstanceService(com.salesmanager.core.business.services.catalog.product.instance.ProductInstanceService) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) CategoryService(com.salesmanager.core.business.services.catalog.category.CategoryService) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ProductAttributeService(com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Product(com.salesmanager.core.model.catalog.product.Product) ProductCriteria(com.salesmanager.core.model.catalog.product.ProductCriteria) ProductPriceRequest(com.salesmanager.shop.model.catalog.product.ProductPriceRequest) ProductRelationshipService(com.salesmanager.core.business.services.catalog.product.relationship.ProductRelationshipService) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) ProductAvailabilityService(com.salesmanager.core.business.services.catalog.product.availability.ProductAvailabilityService) Page(org.springframework.data.domain.Page) ProductInstance(com.salesmanager.core.model.catalog.product.instance.ProductInstance) Collectors(java.util.stream.Collectors) Profile(org.springframework.context.annotation.Profile) List(java.util.List) LocaleUtils(com.salesmanager.shop.utils.LocaleUtils) Validate(org.apache.commons.lang3.Validate) ProductRelationship(com.salesmanager.core.model.catalog.product.relationship.ProductRelationship) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ProductRelationshipType(com.salesmanager.core.model.catalog.product.relationship.ProductRelationshipType) ReadableProductMapper(com.salesmanager.shop.mapper.catalog.ReadableProductMapper) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) Comparator(java.util.Comparator) ReadableProductList(com.salesmanager.shop.model.catalog.product.ReadableProductList) ReadableProductInstanceMapper(com.salesmanager.shop.mapper.catalog.product.ReadableProductInstanceMapper) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ReadableFinalPricePopulator(com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) HashSet(java.util.HashSet) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Example 90 with Language

use of com.salesmanager.core.model.reference.language.Language in project shopizer by shopizer-ecommerce.

the class ProductInstanceFacadeImpl method create.

@Override
public Long create(PersistableProductInstance productInstance, Long productId, MerchantStore store, Language language) {
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(productInstance, "ProductInstance cannot be null");
    Validate.notNull(productId, "Product id cannot be null");
    // variation and variation value should not be of same product option code
    if (productInstance.getVariant() != null && productInstance.getVariant().longValue() > 0 && productInstance.getVariantValue() != null && productInstance.getVariantValue().longValue() > 0) {
        List<ProductVariation> variations = productVariationService.getByIds(Arrays.asList(productInstance.getVariant(), productInstance.getVariantValue()), store);
        boolean differentOption = variations.stream().map(i -> i.getProductOption().getCode()).distinct().count() > 1;
        if (!differentOption) {
            throw new ConstraintException("Product option of instance.variant and instance.variantValue must be different");
        }
    }
    productInstance.setProductId(productId);
    productInstance.setId(null);
    ProductInstance instance = persistableProductInstanceMapper.convert(productInstance, store, language);
    try {
        productInstanceService.save(instance);
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("Cannot save product instance for store [" + store.getCode() + "] and productId [" + productId + "]", e);
    }
    return instance.getId();
}
Also used : ReadableEntityUtil.createReadableList(com.salesmanager.shop.util.ReadableEntityUtil.createReadableList) Arrays(java.util.Arrays) ReadableProductInstance(com.salesmanager.shop.model.catalog.product.product.instance.ReadableProductInstance) Autowired(org.springframework.beans.factory.annotation.Autowired) ProductFacade(com.salesmanager.shop.store.controller.product.facade.ProductFacade) ProductInstanceFacade(com.salesmanager.shop.store.controller.product.facade.ProductInstanceFacade) ServiceException(com.salesmanager.core.business.exception.ServiceException) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ProductVariationService(com.salesmanager.core.business.services.catalog.product.variation.ProductVariationService) ProductInstanceService(com.salesmanager.core.business.services.catalog.product.instance.ProductInstanceService) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ProductVariation(com.salesmanager.core.model.catalog.product.variation.ProductVariation) ConstraintException(com.salesmanager.shop.store.api.exception.ConstraintException) PersistableProductInstanceMapper(com.salesmanager.shop.mapper.catalog.product.PersistableProductInstanceMapper) Page(org.springframework.data.domain.Page) ProductInstance(com.salesmanager.core.model.catalog.product.instance.ProductInstance) Collectors(java.util.stream.Collectors) ProductCommonFacade(com.salesmanager.shop.store.controller.product.facade.ProductCommonFacade) List(java.util.List) Component(org.springframework.stereotype.Component) Validate(org.apache.commons.lang3.Validate) PersistableProductInstance(com.salesmanager.shop.model.catalog.product.product.instance.PersistableProductInstance) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) Optional(java.util.Optional) ReadableProductInstanceMapper(com.salesmanager.shop.mapper.catalog.product.ReadableProductInstanceMapper) ReadableEntityList(com.salesmanager.shop.model.entity.ReadableEntityList) ServiceException(com.salesmanager.core.business.exception.ServiceException) ProductVariation(com.salesmanager.core.model.catalog.product.variation.ProductVariation) ReadableProductInstance(com.salesmanager.shop.model.catalog.product.product.instance.ReadableProductInstance) ProductInstance(com.salesmanager.core.model.catalog.product.instance.ProductInstance) PersistableProductInstance(com.salesmanager.shop.model.catalog.product.product.instance.PersistableProductInstance) ConstraintException(com.salesmanager.shop.store.api.exception.ConstraintException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Aggregations

Language (com.salesmanager.core.model.reference.language.Language)148 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)115 ArrayList (java.util.ArrayList)58 List (java.util.List)56 ServiceException (com.salesmanager.core.business.exception.ServiceException)55 Collectors (java.util.stream.Collectors)50 Product (com.salesmanager.core.model.catalog.product.Product)45 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)44 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)42 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)38 Autowired (org.springframework.beans.factory.annotation.Autowired)35 ConversionException (com.salesmanager.core.business.exception.ConversionException)30 Category (com.salesmanager.core.model.catalog.category.Category)30 Validate (org.apache.commons.lang3.Validate)29 Customer (com.salesmanager.core.model.customer.Customer)28 Optional (java.util.Optional)28 Inject (javax.inject.Inject)28 Service (org.springframework.stereotype.Service)28 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)27 ImageFilePath (com.salesmanager.shop.utils.ImageFilePath)25