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;
}
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;
}
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;
}
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();
}
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());
}
Aggregations