Search in sources :

Example 6 with ZoneDescription

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

use of com.salesmanager.core.model.reference.zone.ZoneDescription 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)

Example 8 with ZoneDescription

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

the class ZoneServiceImpl method getZones.

@SuppressWarnings("unchecked")
@Override
public List<Zone> getZones(Country country, Language language) throws ServiceException {
    // Validate.notNull(country,"Country cannot be null");
    Validate.notNull(language, "Language cannot be null");
    List<Zone> zones = null;
    try {
        String countryCode = Constants.DEFAULT_COUNTRY;
        if (country != null) {
            countryCode = country.getIsoCode();
        }
        String cacheKey = ZONE_CACHE_PREFIX + countryCode + Constants.UNDERSCORE + language.getCode();
        zones = (List<Zone>) cache.getFromCache(cacheKey);
        if (zones == null) {
            zones = zoneRepository.listByLanguageAndCountry(countryCode, language.getId());
            // set names
            for (Zone zone : zones) {
                ZoneDescription description = zone.getDescriptions().get(0);
                zone.setName(description.getName());
            }
            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)

Example 9 with ZoneDescription

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

the class ReadableCountryPopulator method populate.

@Override
public ReadableCountry populate(Country source, ReadableCountry target, MerchantStore store, Language language) throws ConversionException {
    if (target == null) {
        target = new ReadableCountry();
    }
    target.setId(new Long(source.getId()));
    target.setCode(source.getIsoCode());
    target.setSupported(source.getSupported());
    if (!CollectionUtils.isEmpty(source.getDescriptions())) {
        target.setName(source.getDescriptions().iterator().next().getName());
    }
    if (!CollectionUtils.isEmpty(source.getZones())) {
        for (Zone z : source.getZones()) {
            ReadableZone readableZone = new ReadableZone();
            readableZone.setCountryCode(target.getCode());
            readableZone.setId(z.getId());
            if (!CollectionUtils.isEmpty(z.getDescriptions())) {
                for (ZoneDescription d : z.getDescriptions()) {
                    if (d.getLanguage().getId() == language.getId()) {
                        readableZone.setName(d.getName());
                        continue;
                    }
                }
            }
            target.getZones().add(readableZone);
        }
    }
    return target;
}
Also used : Zone(com.salesmanager.core.model.reference.zone.Zone) ReadableZone(com.salesmanager.shop.model.references.ReadableZone) ReadableZone(com.salesmanager.shop.model.references.ReadableZone) ReadableCountry(com.salesmanager.shop.model.references.ReadableCountry) ZoneDescription(com.salesmanager.core.model.reference.zone.ZoneDescription)

Aggregations

ZoneDescription (com.salesmanager.core.model.reference.zone.ZoneDescription)9 Zone (com.salesmanager.core.model.reference.zone.Zone)8 ServiceException (com.salesmanager.core.business.exception.ServiceException)6 Country (com.salesmanager.core.model.reference.country.Country)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Language (com.salesmanager.core.model.reference.language.Language)2 ReadableZone (com.salesmanager.shop.model.references.ReadableZone)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 ReadableCountry (com.salesmanager.shop.model.references.ReadableCountry)1 Resource (org.springframework.core.io.Resource)1