Search in sources :

Example 16 with Translation

use of org.broadleafcommerce.common.i18n.domain.Translation 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 17 with Translation

use of org.broadleafcommerce.common.i18n.domain.Translation 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)

Example 18 with Translation

use of org.broadleafcommerce.common.i18n.domain.Translation in project BroadleafCommerce by BroadleafCommerce.

the class ThresholdCacheTranslationOverrideStrategy method getLocaleBasedTemplateValue.

@Override
public LocalePair getLocaleBasedTemplateValue(String templateCacheKey, String property, TranslatedEntity entityType, String entityId, String localeCode, String localeCountryCode, String specificPropertyKey, String generalPropertyKey) {
    Element cacheResult = translationSupport.getCache().get(templateCacheKey);
    LocalePair response = new LocalePair();
    if (cacheResult == null) {
        statisticsService.addCacheStat(CacheStatType.TRANSLATION_CACHE_HIT_RATE.toString(), false);
        if (dao.countTranslationEntries(entityType, ResultType.TEMPLATE_CACHE) < translationSupport.getTemplateThresholdForFullCache()) {
            Map<String, Map<String, Translation>> propertyTranslationMap = new HashMap<String, Map<String, Translation>>();
            List<Translation> translationList = dao.readAllTranslationEntries(entityType, ResultType.TEMPLATE_CACHE);
            if (!CollectionUtils.isEmpty(translationList)) {
                for (Translation translation : translationList) {
                    String key = translation.getFieldName() + "_" + translation.getLocaleCode();
                    if (!propertyTranslationMap.containsKey(key)) {
                        propertyTranslationMap.put(key, new HashMap<String, Translation>());
                    }
                    propertyTranslationMap.get(key).put(translation.getEntityId(), translation);
                }
            }
            translationSupport.getCache().put(new Element(templateCacheKey, propertyTranslationMap));
            Translation translation = translationSupport.findBestTemplateTranslation(specificPropertyKey, generalPropertyKey, propertyTranslationMap, entityId);
            if (translation != null) {
                buildSingleItemResponse(response, translation);
            }
        } else {
            Translation translation = dao.readTranslation(entityType, entityId, property, localeCode, localeCountryCode, ResultType.TEMPLATE);
            if (translation != null) {
                buildSingleItemResponse(response, translation);
            }
        }
    } else {
        statisticsService.addCacheStat(CacheStatType.TRANSLATION_CACHE_HIT_RATE.toString(), true);
        Map<String, Map<String, Translation>> propertyTranslationMap = (Map<String, Map<String, Translation>>) cacheResult.getObjectValue();
        Translation bestTranslation = translationSupport.findBestTemplateTranslation(specificPropertyKey, generalPropertyKey, propertyTranslationMap, entityId);
        if (bestTranslation != null) {
            buildSingleItemResponse(response, bestTranslation);
        }
    }
    return response;
}
Also used : Translation(org.broadleafcommerce.common.i18n.domain.Translation) HashMap(java.util.HashMap) Element(net.sf.ehcache.Element) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with Translation

use of org.broadleafcommerce.common.i18n.domain.Translation in project BroadleafCommerce by BroadleafCommerce.

the class TranslationBatchReadCache method getThreadlocalCache.

protected static Map<String, Translation> getThreadlocalCache() {
    long threadId = Thread.currentThread().getId();
    Element cacheElement = getCache().get(threadId);
    return cacheElement == null ? null : (Map<String, Translation>) cacheElement.getObjectValue();
}
Also used : Translation(org.broadleafcommerce.common.i18n.domain.Translation) Element(net.sf.ehcache.Element)

Example 20 with Translation

use of org.broadleafcommerce.common.i18n.domain.Translation in project BroadleafCommerce by BroadleafCommerce.

the class TranslationServiceImpl method getTemplateTranslatedValue.

protected String getTemplateTranslatedValue(String standardCacheKey, String property, TranslatedEntity entityType, String entityId, String localeCode, String localeCountryCode, String specificPropertyKey, String generalPropertyKey) {
    String cacheKey = getCacheKey(ResultType.TEMPLATE, entityType);
    StandardCacheItem translation = null;
    LocalePair override = null;
    for (TranslationOverrideStrategy strategy : strategies) {
        override = strategy.getLocaleBasedTemplateValue(cacheKey, property, entityType, entityId, localeCode, localeCountryCode, specificPropertyKey, generalPropertyKey);
        if (override != null) {
            translation = override.getSpecificItem();
            if (!strategy.validateTemplateProcessing(standardCacheKey, cacheKey)) {
                return null;
            }
            break;
        }
    }
    if (override == null) {
        throw new IllegalStateException("Expected at least one TranslationOverrideStrategy to return a valid value");
    }
    return translation == null ? null : replaceEmptyWithNullResponse(((Translation) translation.getCacheItem()).getTranslatedValue());
}
Also used : Translation(org.broadleafcommerce.common.i18n.domain.Translation) StandardCacheItem(org.broadleafcommerce.common.extension.StandardCacheItem)

Aggregations

Translation (org.broadleafcommerce.common.i18n.domain.Translation)26 TranslationImpl (org.broadleafcommerce.common.i18n.domain.TranslationImpl)8 ArrayList (java.util.ArrayList)6 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)5 StandardCacheItem (org.broadleafcommerce.common.extension.StandardCacheItem)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 Element (net.sf.ehcache.Element)4 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)4 Field (org.broadleafcommerce.openadmin.web.form.entity.Field)4 Predicate (javax.persistence.criteria.Predicate)3 TranslatedEntity (org.broadleafcommerce.common.i18n.domain.TranslatedEntity)3 Transactional (org.springframework.transaction.annotation.Transactional)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)2 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)2 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)2 Entity (org.broadleafcommerce.openadmin.dto.Entity)2 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)2 TranslationForm (org.broadleafcommerce.openadmin.web.form.TranslationForm)2