Search in sources :

Example 1 with ZoneDescription

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

the class ZonesLoader method mapZone.

// internal complex mapping stuff, don't try this at home ...
private void mapZone(Language l, Map<String, List<ZoneDescription>> zonesDescriptionsMap, Map<String, Country> countriesMap, Map<String, Zone> zonesMap, Map<String, String> zonesMark, Map<String, String> list) {
    String zoneCode = list.get("zoneCode");
    ZoneDescription zoneDescription = new ZoneDescription();
    zoneDescription.setLanguage(l);
    zoneDescription.setName(list.get("zoneName"));
    Zone zone = null;
    List<ZoneDescription> descriptions = null;
    if (!zonesMap.containsKey(zoneCode)) {
        zone = new Zone();
        Country country = countriesMap.get(list.get("countryCode"));
        if (country == null) {
            LOGGER.warn("Country is null for " + zoneCode + " and country code " + list.get("countryCode"));
            return;
        }
        zone.setCountry(country);
        zone.setCode(zoneCode);
        zonesMap.put(zoneCode, zone);
    }
    if (zonesMark.containsKey(l.getCode() + "_" + zoneCode)) {
        LOGGER.warn("This zone seems to be a duplicate !  " + zoneCode + " and language code " + l.getCode());
        return;
    }
    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);
}
Also used : Zone(com.salesmanager.core.model.reference.zone.Zone) Country(com.salesmanager.core.model.reference.country.Country) ZoneDescription(com.salesmanager.core.model.reference.zone.ZoneDescription)

Example 2 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(String countryCode, Language language) throws ServiceException {
    Validate.notNull(countryCode, "countryCode cannot be null");
    Validate.notNull(language, "Language cannot be null");
    List<Zone> zones = null;
    try {
        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 3 with ZoneDescription

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

the class ReadableZonePopulator method populate.

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

Example 4 with ZoneDescription

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

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

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