Search in sources :

Example 1 with StandardCacheItem

use of org.broadleafcommerce.common.extension.StandardCacheItem in project BroadleafCommerce by BroadleafCommerce.

the class SparseTranslationOverrideStrategy method getLocaleBasedOverride.

@Override
public LocalePair getLocaleBasedOverride(String property, TranslatedEntity entityType, String entityId, String localeCode, String localeCountryCode, String basicCacheKey) {
    LocalePair override = null;
    if (preCachedSparseOverrideService.isActiveForType(Translation.class.getName())) {
        boolean isSpecificOnly = localeCode.equals(localeCountryCode) && localeCode.contains("_");
        boolean isGeneralOnly = localeCode.equals(localeCountryCode) && !localeCode.contains("_");
        String specificCacheKey = getCacheKey(entityType, entityId, property, localeCountryCode);
        String generalCacheKey = getCacheKey(entityType, entityId, property, localeCode);
        List<StandardCacheItem> overrides;
        if (isSpecificOnly) {
            overrides = preCachedSparseOverrideService.findElements(specificCacheKey);
        } else if (isGeneralOnly) {
            overrides = preCachedSparseOverrideService.findElements(generalCacheKey);
        } else {
            overrides = preCachedSparseOverrideService.findElements(specificCacheKey, generalCacheKey);
        }
        override = new LocalePair();
        if (!overrides.isEmpty()) {
            if (isSpecificOnly && ItemStatus.NONE != overrides.get(0).getItemStatus()) {
                StandardCacheItem specificTranslation = overrides.get(0);
                override.setSpecificItem(specificTranslation);
            } else if (isGeneralOnly && ItemStatus.NONE != overrides.get(0).getItemStatus()) {
                StandardCacheItem generalTranslation = overrides.get(0);
                override.setGeneralItem(generalTranslation);
            }
            if (!isSpecificOnly && !isGeneralOnly) {
                if (ItemStatus.NONE != overrides.get(0).getItemStatus()) {
                    StandardCacheItem specificTranslation = overrides.get(0);
                    override.setSpecificItem(specificTranslation);
                }
                if (ItemStatus.NONE != overrides.get(1).getItemStatus()) {
                    StandardCacheItem generalTranslation = overrides.get(1);
                    override.setGeneralItem(generalTranslation);
                }
            }
        }
    }
    return override;
}
Also used : Translation(org.broadleafcommerce.common.i18n.domain.Translation) StandardCacheItem(org.broadleafcommerce.common.extension.StandardCacheItem)

Example 2 with StandardCacheItem

use of org.broadleafcommerce.common.extension.StandardCacheItem in project BroadleafCommerce by BroadleafCommerce.

the class ThresholdCacheTranslationOverrideStrategy method buildSingleItemResponse.

protected void buildSingleItemResponse(LocalePair response, Translation translation) {
    StandardCacheItem cacheItem = new StandardCacheItem();
    cacheItem.setItemStatus(ItemStatus.NORMAL);
    cacheItem.setCacheItem(translation == null ? "" : translation);
    response.setSpecificItem(cacheItem);
}
Also used : StandardCacheItem(org.broadleafcommerce.common.extension.StandardCacheItem)

Example 3 with StandardCacheItem

use of org.broadleafcommerce.common.extension.StandardCacheItem in project BroadleafCommerce by BroadleafCommerce.

the class ThresholdCacheTranslationOverrideStrategy method getLocaleBasedOverride.

@Override
public LocalePair getLocaleBasedOverride(String property, TranslatedEntity entityType, String entityId, String localeCode, String localeCountryCode, String basicCacheKey) {
    String specificPropertyKey = property + "_" + localeCountryCode;
    String generalPropertyKey = property + "_" + localeCode;
    Element cacheResult = translationSupport.getCache().get(basicCacheKey);
    Element result;
    LocalePair response = new LocalePair();
    if (cacheResult == null) {
        statisticsService.addCacheStat(CacheStatType.TRANSLATION_CACHE_HIT_RATE.toString(), false);
        if (dao.countTranslationEntries(entityType, ResultType.STANDARD_CACHE) < translationSupport.getThresholdForFullCache()) {
            Map<String, Map<String, StandardCacheItem>> propertyTranslationMap = new HashMap<String, Map<String, StandardCacheItem>>();
            List<StandardCacheItem> convertedList = dao.readConvertedTranslationEntries(entityType, ResultType.STANDARD_CACHE);
            if (!CollectionUtils.isEmpty(convertedList)) {
                for (StandardCacheItem standardCache : convertedList) {
                    Translation translation = (Translation) standardCache.getCacheItem();
                    String key = translation.getFieldName() + "_" + translation.getLocaleCode();
                    if (!propertyTranslationMap.containsKey(key)) {
                        propertyTranslationMap.put(key, new HashMap<String, StandardCacheItem>());
                    }
                    propertyTranslationMap.get(key).put(translation.getEntityId(), standardCache);
                }
            }
            Element newElement = new Element(basicCacheKey, propertyTranslationMap);
            translationSupport.getCache().put(newElement);
            result = newElement;
        } else {
            // Translation is dual discriminated by site and catalog, which can make it impossible to find results under normal
            // circumstances because the two discriminators can cancel eachother out. We use the CATALOG_ONLY ResultType
            // to force the system to only honor the catalog discrimination during this call.
            Translation translation = dao.readTranslation(entityType, entityId, property, localeCode, localeCountryCode, ResultType.CATALOG_ONLY);
            buildSingleItemResponse(response, translation);
            return response;
        }
    } else {
        result = cacheResult;
        statisticsService.addCacheStat(CacheStatType.TRANSLATION_CACHE_HIT_RATE.toString(), true);
    }
    Map<String, Map<String, StandardCacheItem>> propertyTranslationMap = (Map<String, Map<String, StandardCacheItem>>) result.getObjectValue();
    // Check For a Specific Standard Site Match (language and country)
    StandardCacheItem specificTranslation = translationSupport.lookupTranslationFromMap(specificPropertyKey, propertyTranslationMap, entityId);
    // Check For a General Match (language and country)
    StandardCacheItem generalTranslation = translationSupport.lookupTranslationFromMap(generalPropertyKey, propertyTranslationMap, entityId);
    response.setSpecificItem(specificTranslation);
    response.setGeneralItem(generalTranslation);
    return response;
}
Also used : Translation(org.broadleafcommerce.common.i18n.domain.Translation) HashMap(java.util.HashMap) Element(net.sf.ehcache.Element) StandardCacheItem(org.broadleafcommerce.common.extension.StandardCacheItem) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with StandardCacheItem

use of org.broadleafcommerce.common.extension.StandardCacheItem in project BroadleafCommerce by BroadleafCommerce.

the class SparseTranslationOverrideStrategy method initializeOverride.

@Override
public StandardCacheItem initializeOverride(Object entity) {
    String key = getCacheKey((Translation) entity);
    String dto = ((Translation) entity).getTranslatedValue();
    StandardCacheItem cacheItem = new StandardCacheItem();
    ItemStatus status = ItemStatus.NORMAL;
    if (extensionManager != null) {
        ExtensionResultHolder<ItemStatus> response = new ExtensionResultHolder<ItemStatus>();
        ExtensionResultStatusType result = extensionManager.buildStatus(entity, response);
        if (ExtensionResultStatusType.NOT_HANDLED != result && response.getResult() != null) {
            status = response.getResult();
        }
    }
    cacheItem.setItemStatus(status);
    cacheItem.setKey(key);
    cacheItem.setCacheItem(dto);
    return cacheItem;
}
Also used : ItemStatus(org.broadleafcommerce.common.extension.ItemStatus) Translation(org.broadleafcommerce.common.i18n.domain.Translation) StandardCacheItem(org.broadleafcommerce.common.extension.StandardCacheItem) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 5 with StandardCacheItem

use of org.broadleafcommerce.common.extension.StandardCacheItem in project BroadleafCommerce by BroadleafCommerce.

the class SparseTranslationOverrideStrategy method getLocaleBasedTemplateValue.

@Override
public LocalePair getLocaleBasedTemplateValue(String templateCacheKey, String property, TranslatedEntity entityType, String entityId, String localeCode, String localeCountryCode, String specificPropertyKey, String generalPropertyKey) {
    LocalePair override = null;
    if (preCachedSparseOverrideService.isActiveForType(Translation.class.getName())) {
        Site currentSite = BroadleafRequestContext.getBroadleafRequestContext().getNonPersistentSite();
        boolean isIsolatedActive = false;
        if (currentSite != null) {
            isIsolatedActive = preCachedSparseOverrideService.isActiveIsolatedSiteForType(currentSite.getId(), TranslationImpl.class.getName());
        }
        if (isIsolatedActive || templateEnabled) {
            override = new LocalePair();
            List<Translation> templateVals;
            if (!isIsolatedActive) {
                templateVals = getTemplateTranslations(entityType, entityId, property, localeCode);
            } else {
                // We need to include the standard site catalog in the query, so don't try to use an optimized template query
                templateVals = new ArrayList<Translation>();
                Translation translation = dao.readTranslation(entityType, entityId, property, localeCode, localeCountryCode, ResultType.CATALOG_ONLY);
                if (translation != null) {
                    templateVals.add(translation);
                }
            }
            List<String> codesToMatch = new ArrayList<String>();
            if (specificPropertyKey.endsWith(localeCountryCode) && generalPropertyKey.endsWith(localeCountryCode)) {
                codesToMatch.add(localeCountryCode);
            } else if (specificPropertyKey.endsWith(localeCode) && generalPropertyKey.endsWith(localeCode)) {
                codesToMatch.add(localeCode);
            } else {
                codesToMatch.add(localeCountryCode);
                codesToMatch.add(localeCode);
            }
            for (String code : codesToMatch) {
                for (Translation templateVal : templateVals) {
                    if (templateVal.getLocaleCode().equals(code)) {
                        StandardCacheItem cacheItem = new StandardCacheItem();
                        cacheItem.setItemStatus(ItemStatus.NORMAL);
                        cacheItem.setCacheItem(templateVal);
                        override.setSpecificItem(cacheItem);
                        break;
                    }
                }
                if (override.getSpecificItem() != null) {
                    break;
                }
            }
        }
    }
    return override;
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) Translation(org.broadleafcommerce.common.i18n.domain.Translation) StandardCacheItem(org.broadleafcommerce.common.extension.StandardCacheItem) ArrayList(java.util.ArrayList)

Aggregations

StandardCacheItem (org.broadleafcommerce.common.extension.StandardCacheItem)8 Translation (org.broadleafcommerce.common.i18n.domain.Translation)6 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Element (net.sf.ehcache.Element)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)1 ItemStatus (org.broadleafcommerce.common.extension.ItemStatus)1 Site (org.broadleafcommerce.common.site.domain.Site)1