Search in sources :

Example 16 with Zone

use of com.salesmanager.core.model.reference.zone.Zone in project shopizer by shopizer-ecommerce.

the class InitializationDatabaseImpl method createZones.

private void createZones() throws ServiceException {
    LOGGER.info(String.format("%s : Populating Zones ", name));
    try {
        Map<String, Zone> zonesMap = new HashMap<String, Zone>();
        zonesMap = zonesLoader.loadZones("reference/zoneconfig.json");
        this.addZonesToDb(zonesMap);
        /*              
              for (Map.Entry<String, Zone> entry : zonesMap.entrySet()) {
            	    String key = entry.getKey();
            	    Zone value = entry.getValue();
            	    if(value.getDescriptions()==null) {
            	    	LOGGER.warn("This zone " + key + " has no descriptions");
            	    	continue;
            	    }
            	    
            	    List<ZoneDescription> zoneDescriptions = value.getDescriptions();
            	    value.setDescriptons(null);

            	    zoneService.create(value);
            	    
            	    for(ZoneDescription description : zoneDescriptions) {
            	    	description.setZone(value);
            	    	zoneService.addDescription(value, description);
            	    }
              }*/
        // lookup additional zones
        // iterate configured languages
        LOGGER.info("Populating additional zones");
        // load reference/zones/* (zone config for additional country)
        // example in.json and in-fr.son
        // will load es zones and use a specific file for french es zones
        List<Map<String, Zone>> loadIndividualZones = zonesLoader.loadIndividualZones();
        loadIndividualZones.forEach(this::addZonesToDb);
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) HashMap(java.util.HashMap) Zone(com.salesmanager.core.model.reference.zone.Zone) HashMap(java.util.HashMap) Map(java.util.Map) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 17 with Zone

use of com.salesmanager.core.model.reference.zone.Zone in project shopizer by shopizer-ecommerce.

the class InitializationDatabaseImpl method addZonesToDb.

private void addZonesToDb(Map<String, Zone> zonesMap) throws RuntimeException {
    try {
        for (Map.Entry<String, Zone> entry : zonesMap.entrySet()) {
            String key = entry.getKey();
            Zone value = entry.getValue();
            if (value.getDescriptions() == null) {
                LOGGER.warn("This zone " + key + " has no descriptions");
                continue;
            }
            List<ZoneDescription> zoneDescriptions = value.getDescriptions();
            value.setDescriptons(null);
            zoneService.create(value);
            for (ZoneDescription description : zoneDescriptions) {
                description.setZone(value);
                zoneService.addDescription(value, description);
            }
        }
    } catch (Exception e) {
        LOGGER.error("An error occured while loading zones", e);
    }
}
Also used : Zone(com.salesmanager.core.model.reference.zone.Zone) HashMap(java.util.HashMap) Map(java.util.Map) ServiceException(com.salesmanager.core.business.exception.ServiceException) ZoneDescription(com.salesmanager.core.model.reference.zone.ZoneDescription)

Example 18 with Zone

use of com.salesmanager.core.model.reference.zone.Zone 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 19 with Zone

use of com.salesmanager.core.model.reference.zone.Zone 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 20 with Zone

use of com.salesmanager.core.model.reference.zone.Zone in project shopizer by shopizer-ecommerce.

the class ZoneServiceImpl method getZones.

@Override
@SuppressWarnings("unchecked")
public Map<String, Zone> getZones(Language language) throws ServiceException {
    Map<String, Zone> zones = null;
    try {
        String cacheKey = ZONE_CACHE_PREFIX + language.getCode();
        zones = (Map<String, Zone>) cache.getFromCache(cacheKey);
        if (zones == null) {
            zones = new HashMap<String, Zone>();
            List<Zone> zns = zoneRepository.listByLanguage(language.getId());
            // set names
            for (Zone zone : zns) {
                ZoneDescription description = zone.getDescriptions().get(0);
                zone.setName(description.getName());
                zones.put(zone.getCode(), zone);
            }
            cache.putInCache(zones, cacheKey);
        }
    } catch (Exception e) {
        LOGGER.error("getZones()", e);
    }
    return zones;
}
Also used : Zone(com.salesmanager.core.model.reference.zone.Zone) ServiceException(com.salesmanager.core.business.exception.ServiceException) ZoneDescription(com.salesmanager.core.model.reference.zone.ZoneDescription)

Aggregations

Zone (com.salesmanager.core.model.reference.zone.Zone)29 Country (com.salesmanager.core.model.reference.country.Country)20 ServiceException (com.salesmanager.core.business.exception.ServiceException)15 Language (com.salesmanager.core.model.reference.language.Language)13 ZoneDescription (com.salesmanager.core.model.reference.zone.ZoneDescription)8 ArrayList (java.util.ArrayList)8 Delivery (com.salesmanager.core.model.common.Delivery)7 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)7 Billing (com.salesmanager.core.model.common.Billing)6 ConversionException (com.salesmanager.core.business.exception.ConversionException)5 Customer (com.salesmanager.core.model.customer.Customer)5 BigDecimal (java.math.BigDecimal)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 Product (com.salesmanager.core.model.catalog.product.Product)3 OrderProduct (com.salesmanager.core.model.order.orderproduct.OrderProduct)3 Currency (com.salesmanager.core.model.reference.currency.Currency)3 ShippingQuote (com.salesmanager.core.model.shipping.ShippingQuote)3 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)3