@@ -196,6 +196,7 @@ protected String getOverrideTranslatedValue(String property, TranslatedEntity en
196
196
String generalPropertyKey = property + "_" + localeCode ;
197
197
String cacheKey = getCacheKey (ResultType .STANDARD , entityType );
198
198
Element cacheResult = getCache ().get (cacheKey );
199
+ Element result = null ;
199
200
String response = null ;
200
201
if (cacheResult == null ) {
201
202
statisticsService .addCacheStat (CacheStatType .TRANSLATION_CACHE_HIT_RATE .toString (), false );
@@ -212,32 +213,73 @@ protected String getOverrideTranslatedValue(String property, TranslatedEntity en
212
213
propertyTranslationMap .get (key ).put (translation .getEntityId (), standardCache );
213
214
}
214
215
}
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 ;
223
219
} else {
224
220
Translation translation = dao .readTranslation (entityType , entityId , property , localeCode , localeCountryCode , ResultType .IGNORE );
225
221
if (translation != null ) {
226
222
response = translation .getTranslatedValue ();
227
223
}
228
224
}
229
225
} else {
226
+ result = cacheResult ;
230
227
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
+ }
239
247
}
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 );
240
277
}
278
+
279
+ return replaceEmptyWithNullResponse (response );
280
+ }
281
+
282
+ protected String replaceEmptyWithNullResponse (String response ) {
241
283
if (!StringUtils .isEmpty (response )) {
242
284
return response ;
243
285
}
@@ -292,24 +334,15 @@ protected String getTemplateTranslatedValue(String standardCacheKey, String prop
292
334
}
293
335
}
294
336
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
+
296
340
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 );
299
343
cacheItem = byEntity .get (entityId );
300
344
}
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 ;
313
346
}
314
347
315
348
protected Translation findBestTemplateTranslation (String specificPropertyKey , String generalPropertyKey , Map <String , Map <String , Translation >> propertyTranslationMap , String entityId ) {
0 commit comments