Skip to content

Commit 8d64153

Browse files
Update SDMReadAttachmentsHandler.java
1 parent 756a04c commit 8d64153

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMReadAttachmentsHandler.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import com.sap.cds.sdm.caching.CacheConfig;
1212
import com.sap.cds.sdm.caching.ErrorMessageKey;
1313
import com.sap.cds.sdm.constants.SDMConstants;
14+
import com.sap.cds.sdm.constants.SDMErrorKeys;
15+
import com.sap.cds.sdm.constants.SDMErrorMessages;
1416
import com.sap.cds.sdm.handler.TokenHandler;
1517
import com.sap.cds.sdm.handler.applicationservice.helper.SDMBeforeReadItemsModifier;
1618
import com.sap.cds.sdm.handler.common.SDMApplicationHandlerHelper;
@@ -67,6 +69,40 @@ private void setErrorMessagesInCache(CdsReadEventContext context) {
6769
if (errorMessageCache == null) {
6870
return; // Cache not initialized, skip
6971
}
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");
70106
}
71107

72108
@Before
@@ -121,14 +157,15 @@ public Predicate where(Predicate where) {
121157
}
122158
});
123159
}
124-
160+
setErrorMessagesInCache(context);
125161
context.setCqn(modifiedCqn);
126162
} catch (Exception e) {
127163
logger.error("Error in SDMReadAttachmentsHandler.processBefore: {}", e.getMessage(), e);
128164
// Re-throw to maintain error handling behavior
129165
throw e;
130166
}
131167
}
168+
// No action needed for non-media entities
132169
}
133170

134171
/**

0 commit comments

Comments
 (0)