Skip to content

Commit 97f7eda

Browse files
committed
BroadleafCommerce#935 - Translations deleted at a site level should only override the exact same translation at a template level.
1 parent 1414580 commit 97f7eda

File tree

1 file changed

+64
-31
lines changed

1 file changed

+64
-31
lines changed

common/src/main/java/org/broadleafcommerce/common/i18n/service/TranslationServiceImpl.java

+64-31
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ protected String getOverrideTranslatedValue(String property, TranslatedEntity en
196196
String generalPropertyKey = property + "_" + localeCode;
197197
String cacheKey = getCacheKey(ResultType.STANDARD, entityType);
198198
Element cacheResult = getCache().get(cacheKey);
199+
Element result = null;
199200
String response = null;
200201
if (cacheResult == null) {
201202
statisticsService.addCacheStat(CacheStatType.TRANSLATION_CACHE_HIT_RATE.toString(), false);
@@ -212,32 +213,73 @@ protected String getOverrideTranslatedValue(String property, TranslatedEntity en
212213
propertyTranslationMap.get(key).put(translation.getEntityId(), standardCache);
213214
}
214215
}
215-
getCache().put(new Element(cacheKey, propertyTranslationMap));
216-
Translation bestTranslation = findBestStandardTranslation(specificPropertyKey, generalPropertyKey, propertyTranslationMap, entityId);
217-
if (bestTranslation != null) {
218-
response = bestTranslation.getTranslatedValue();
219-
} else {
220-
response = getTemplateTranslatedValue(cacheKey, property, entityType, entityId, localeCode,
221-
localeCountryCode, specificPropertyKey, generalPropertyKey);
222-
}
216+
Element newElement = new Element(cacheKey, propertyTranslationMap);
217+
getCache().put(newElement);
218+
result = newElement;
223219
} else {
224220
Translation translation = dao.readTranslation(entityType, entityId, property, localeCode, localeCountryCode, ResultType.IGNORE);
225221
if (translation != null) {
226222
response = translation.getTranslatedValue();
227223
}
228224
}
229225
} else {
226+
result = cacheResult;
230227
statisticsService.addCacheStat(CacheStatType.TRANSLATION_CACHE_HIT_RATE.toString(), true);
231-
Map<String, Map<String, StandardCacheItem>> propertyTranslationMap = (Map<String, Map<String, StandardCacheItem>>) cacheResult.getObjectValue();
232-
Translation bestTranslation = findBestStandardTranslation(specificPropertyKey, generalPropertyKey,
233-
propertyTranslationMap, entityId);
234-
if (bestTranslation != null) {
235-
response = bestTranslation.getTranslatedValue();
236-
} else {
237-
response = getTemplateTranslatedValue(cacheKey, property, entityType, entityId, localeCode,
238-
localeCountryCode, specificPropertyKey, generalPropertyKey);
228+
}
229+
230+
if (result != null) {
231+
Map<String, Map<String, StandardCacheItem>> propertyTranslationMap =
232+
(Map<String, Map<String, StandardCacheItem>>) result.getObjectValue();
233+
234+
boolean specificTranslationDeleted = false;
235+
boolean generalTranslationDeleted = false;
236+
237+
// Check For a Specific Standard Site Match (language and country)
238+
StandardCacheItem specificTranslation = lookupTranslationFromMap(specificPropertyKey, propertyTranslationMap,
239+
entityId);
240+
if (specificTranslation != null) {
241+
if (ItemStatus.DELETED.equals(specificTranslation.getItemStatus())) {
242+
specificTranslationDeleted = true;
243+
} else {
244+
response = ((Translation) specificTranslation.getCacheItem()).getTranslatedValue();
245+
return replaceEmptyWithNullResponse(response);
246+
}
239247
}
248+
249+
// Check For a General Match (language and country)
250+
StandardCacheItem generalTranslation = lookupTranslationFromMap(generalPropertyKey, propertyTranslationMap,
251+
entityId);
252+
if (generalTranslation != null) {
253+
if (ItemStatus.DELETED.equals(generalTranslation.getItemStatus())) {
254+
generalTranslationDeleted = true;
255+
if (specificTranslationDeleted) {
256+
return null;
257+
}
258+
}
259+
260+
if (specificTranslationDeleted) {
261+
response = ((Translation) generalTranslation.getCacheItem()).getTranslatedValue();
262+
return replaceEmptyWithNullResponse(response);
263+
}
264+
}
265+
266+
// Check for a Template Match
267+
if (specificTranslationDeleted) {
268+
// only check general properties since we explicitly deleted specific properties at standard (site) level
269+
specificPropertyKey = generalPropertyKey;
270+
} else if (generalTranslationDeleted) {
271+
// only check specific properties since we explicitly deleted general properties at standard (site) level
272+
generalPropertyKey = specificPropertyKey;
273+
}
274+
275+
response = getTemplateTranslatedValue(cacheKey, property, entityType, entityId, localeCode,
276+
localeCountryCode, specificPropertyKey, generalPropertyKey);
240277
}
278+
279+
return replaceEmptyWithNullResponse(response);
280+
}
281+
282+
protected String replaceEmptyWithNullResponse(String response) {
241283
if (!StringUtils.isEmpty(response)) {
242284
return response;
243285
}
@@ -292,24 +334,15 @@ protected String getTemplateTranslatedValue(String standardCacheKey, String prop
292334
}
293335
}
294336

295-
protected Translation findBestStandardTranslation(String specificPropertyKey, String generalPropertyKey, Map<String, Map<String, StandardCacheItem>> propertyTranslationMap, String entityId) {
337+
protected StandardCacheItem lookupTranslationFromMap(String key,
338+
Map<String, Map<String, StandardCacheItem>> propertyTranslationMap, String entityId) {
339+
296340
StandardCacheItem cacheItem = null;
297-
if (propertyTranslationMap.containsKey(specificPropertyKey)) {
298-
Map<String, StandardCacheItem> byEntity = propertyTranslationMap.get(specificPropertyKey);
341+
if (propertyTranslationMap.containsKey(key)) {
342+
Map<String, StandardCacheItem> byEntity = propertyTranslationMap.get(key);
299343
cacheItem = byEntity.get(entityId);
300344
}
301-
if (cacheItem == null && propertyTranslationMap.containsKey(generalPropertyKey)) {
302-
Map<String, StandardCacheItem> byEntity = propertyTranslationMap.get(generalPropertyKey);
303-
cacheItem = byEntity.get(entityId);
304-
}
305-
if (cacheItem != null) {
306-
if (ItemStatus.DELETED == cacheItem.getItemStatus()) {
307-
return DELETED_TRANSLATION;
308-
} else {
309-
return (Translation) cacheItem.getCacheItem();
310-
}
311-
}
312-
return null;
345+
return cacheItem;
313346
}
314347

315348
protected Translation findBestTemplateTranslation(String specificPropertyKey, String generalPropertyKey, Map<String, Map<String, Translation>> propertyTranslationMap, String entityId) {

0 commit comments

Comments
 (0)