|
11 | 11 | import com.sap.cds.sdm.caching.CacheConfig; |
12 | 12 | import com.sap.cds.sdm.caching.ErrorMessageKey; |
13 | 13 | import com.sap.cds.sdm.constants.SDMConstants; |
| 14 | +import com.sap.cds.sdm.constants.SDMErrorKeys; |
| 15 | +import com.sap.cds.sdm.constants.SDMErrorMessages; |
14 | 16 | import com.sap.cds.sdm.handler.TokenHandler; |
15 | 17 | import com.sap.cds.sdm.handler.applicationservice.helper.SDMBeforeReadItemsModifier; |
16 | 18 | import com.sap.cds.sdm.handler.common.SDMApplicationHandlerHelper; |
@@ -67,6 +69,40 @@ private void setErrorMessagesInCache(CdsReadEventContext context) { |
67 | 69 | if (errorMessageCache == null) { |
68 | 70 | return; // Cache not initialized, skip |
69 | 71 | } |
| 72 | + |
| 73 | + // Check if localized error messages are already cached |
| 74 | + ErrorMessageKey cacheCheckKey = new ErrorMessageKey(); |
| 75 | + cacheCheckKey.setKey("localizedErrorMessagesSetInCache"); |
| 76 | + String cacheValue = errorMessageCache.get(cacheCheckKey); |
| 77 | + |
| 78 | + if ("true".equals(cacheValue)) { |
| 79 | + return; // Skip processing if already cached |
| 80 | + } |
| 81 | + |
| 82 | + Map<String, Object> errorMessages = SDMErrorMessages.getAllErrorMessages(); |
| 83 | + Map<String, Object> errorKeys = SDMErrorKeys.getAllErrorKeys(); |
| 84 | + String localizedMessage; |
| 85 | + String localizedErrorMessageKey; |
| 86 | + for (Map.Entry<String, Object> entry : errorMessages.entrySet()) { |
| 87 | + String errorMessage = entry.getKey(); |
| 88 | + Object errorValue = entry.getValue(); |
| 89 | + localizedErrorMessageKey = String.valueOf(errorKeys.get(errorMessage + "_KEY")); |
| 90 | + localizedMessage = |
| 91 | + context |
| 92 | + .getCdsRuntime() |
| 93 | + .getLocalizedMessage( |
| 94 | + localizedErrorMessageKey, null, context.getParameterInfo().getLocale()); |
| 95 | + ErrorMessageKey errorMessageKey = new ErrorMessageKey(); |
| 96 | + errorMessageKey.setKey(errorMessage); |
| 97 | + errorMessageCache.put( |
| 98 | + errorMessageKey, |
| 99 | + java.util.Objects.equals(localizedMessage, localizedErrorMessageKey) |
| 100 | + ? String.valueOf(errorValue) |
| 101 | + : localizedMessage); |
| 102 | + } |
| 103 | + |
| 104 | + // Mark that localized error messages have been cached |
| 105 | + errorMessageCache.put(cacheCheckKey, "true"); |
70 | 106 | } |
71 | 107 |
|
72 | 108 | @Before |
@@ -121,14 +157,15 @@ public Predicate where(Predicate where) { |
121 | 157 | } |
122 | 158 | }); |
123 | 159 | } |
124 | | - |
| 160 | + setErrorMessagesInCache(context); |
125 | 161 | context.setCqn(modifiedCqn); |
126 | 162 | } catch (Exception e) { |
127 | 163 | logger.error("Error in SDMReadAttachmentsHandler.processBefore: {}", e.getMessage(), e); |
128 | 164 | // Re-throw to maintain error handling behavior |
129 | 165 | throw e; |
130 | 166 | } |
131 | 167 | } |
| 168 | + // No action needed for non-media entities |
132 | 169 | } |
133 | 170 |
|
134 | 171 | /** |
|
0 commit comments