Skip to content

Commit 353eda1

Browse files
Merge branch 'develop' into discardDraftIdBug
2 parents 039f02b + 729799c commit 353eda1

4 files changed

Lines changed: 268 additions & 66 deletions

File tree

sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,20 @@ private void createLink(EventContext context) throws IOException {
332332
String upIdKey =
333333
attachmentDraftEntity.isPresent() ? getUpIdKey(attachmentDraftEntity.get()) : "up__ID";
334334
CqnSelect select = (CqnSelect) context.get("cqn");
335-
String upID = fetchUPIDFromCQN(select);
335+
// Get parent entity to extract its key names dynamically
336+
String parentEntityName = null;
337+
ObjectMapper mapper = new ObjectMapper();
338+
JsonNode root = mapper.readTree(select.toString());
339+
JsonNode refArray = root.path("SELECT").path("from").path("ref");
340+
if (refArray.isArray() && refArray.size() >= 2) {
341+
JsonNode parentNode = refArray.get(refArray.size() - 2);
342+
parentEntityName = parentNode.path("id").asText();
343+
}
344+
345+
Optional<CdsEntity> parentEntity =
346+
parentEntityName != null ? cdsModel.findEntity(parentEntityName) : Optional.empty();
347+
348+
String upID = fetchUPIDFromCQN(select, parentEntity.orElse(null));
336349
String filenameInRequest = context.get("name").toString();
337350

338351
Result result =
@@ -343,7 +356,8 @@ private void createLink(EventContext context) throws IOException {
343356
validateLinkName(filenameInRequest, result);
344357

345358
Boolean isSystemUser = context.getUserInfo().isSystemUser();
346-
String entityName = context.getTarget().getQualifiedName().split("\\.")[2];
359+
String[] parts = context.getTarget().getQualifiedName().split("\\.");
360+
String entityName = parts[parts.length - 1];
347361
String folderName = upID + "__" + entityName;
348362

349363
String folderId = sdmService.getFolderId(result, persistenceService, folderName, isSystemUser);
@@ -563,22 +577,29 @@ private void handleCreateLinkResult(
563577
}
564578
}
565579

566-
private String fetchUPIDFromCQN(CqnSelect select) {
580+
private String fetchUPIDFromCQN(CqnSelect select, CdsEntity parentEntity) {
567581
try {
568582
String upID = null;
569583
ObjectMapper mapper = new ObjectMapper();
570584
JsonNode root = mapper.readTree(select.toString());
571585
JsonNode refArray = root.path("SELECT").path("from").path("ref");
572586
JsonNode secondLast = refArray.get(refArray.size() - 2);
573587
JsonNode whereArray = secondLast.path("where");
588+
589+
// Get the actual key field names from the parent entity
590+
List<String> keyElementNames = getKeyElementNames(parentEntity);
591+
574592
for (int i = 0; i < whereArray.size(); i++) {
575593
JsonNode node = whereArray.get(i);
576-
if (node.has("ref")
577-
&& node.get("ref").isArray()
578-
&& node.get("ref").get(0).asText().equals("ID")) {
579-
JsonNode valNode = whereArray.get(i + 2);
580-
upID = valNode.path("val").asText();
581-
break;
594+
595+
if (node.has("ref") && node.get("ref").isArray()) {
596+
String fieldName = node.get("ref").get(0).asText();
597+
598+
if (keyElementNames.contains(fieldName) && !fieldName.equals("IsActiveEntity")) {
599+
JsonNode valNode = whereArray.get(i + 2);
600+
upID = valNode.path("val").asText();
601+
break;
602+
}
582603
}
583604
}
584605
if (upID == null) {

sdm/src/test/java/integration/com/sap/cds/sdm/Api.java

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class Api implements ApiInterface {
1313
private static final ObjectMapper objectMapper = new ObjectMapper();
1414
private final String token;
1515
private final String serviceName;
16+
private static final int MAX_RETRIES = 3;
17+
private static final int RETRY_DELAY_MS = 1000;
1618

1719
public Api(Map<String, String> config) {
1820
this.config = new HashMap<>(config);
@@ -26,6 +28,50 @@ public Api(Map<String, String> config) {
2628
this.serviceName = this.config.get("serviceName");
2729
}
2830

31+
private Response executeWithRetry(Request request) throws IOException {
32+
IOException lastException = null;
33+
for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) {
34+
try {
35+
Response response = httpClient.newCall(request).execute();
36+
if (response.code() == 502 && attempt < MAX_RETRIES) {
37+
System.out.println(
38+
"Received 502 Bad Gateway, retrying... (attempt "
39+
+ attempt
40+
+ "/"
41+
+ MAX_RETRIES
42+
+ ")");
43+
response.close();
44+
Thread.sleep(RETRY_DELAY_MS);
45+
continue;
46+
}
47+
return response;
48+
} catch (java.net.SocketTimeoutException e) {
49+
lastException = e;
50+
if (attempt < MAX_RETRIES) {
51+
System.out.println(
52+
"Socket timeout occurred, retrying... (attempt "
53+
+ attempt
54+
+ "/"
55+
+ MAX_RETRIES
56+
+ "): "
57+
+ e.getMessage());
58+
try {
59+
Thread.sleep(RETRY_DELAY_MS);
60+
} catch (InterruptedException ie) {
61+
Thread.currentThread().interrupt();
62+
throw new IOException("Retry interrupted", ie);
63+
}
64+
} else {
65+
throw e;
66+
}
67+
} catch (InterruptedException e) {
68+
Thread.currentThread().interrupt();
69+
throw new IOException("Retry interrupted", e);
70+
}
71+
}
72+
throw lastException;
73+
}
74+
2975
public String createEntityDraft(
3076
String appUrl, String entityName, String entityName2, String srvpath) {
3177
MediaType mediaType = MediaType.parse("application/json");
@@ -46,7 +92,7 @@ public String createEntityDraft(
4692
.addHeader("Authorization", token)
4793
.build();
4894

49-
try (Response response = httpClient.newCall(request).execute()) {
95+
try (Response response = executeWithRetry(request)) {
5096
if (!response.isSuccessful()) {
5197
if (response.code() == 401) {
5298
System.out.println(
@@ -83,7 +129,7 @@ public String editEntityDraft(String appUrl, String entityName, String srvpath,
83129
.addHeader("Authorization", token)
84130
.build();
85131

86-
try (Response response = httpClient.newCall(request).execute()) {
132+
try (Response response = executeWithRetry(request)) {
87133
if (response.code() != 200) {
88134
System.out.println("Edit entity failed. Error : " + response.body().string());
89135
throw new IOException("Could not edit entity");
@@ -116,7 +162,7 @@ public String saveEntityDraft(String appUrl, String entityName, String srvpath,
116162
.addHeader("Authorization", token)
117163
.build();
118164

119-
try (Response response = httpClient.newCall(request).execute()) {
165+
try (Response response = executeWithRetry(request)) {
120166
if (response.code() != 200) {
121167
System.out.println("Save entity failed. Error : " + response.body().string());
122168
throw new IOException("Could not save entity");
@@ -139,7 +185,7 @@ public String saveEntityDraft(String appUrl, String entityName, String srvpath,
139185
.addHeader("Authorization", token)
140186
.build();
141187

142-
try (Response draftResponse = httpClient.newCall(request).execute()) {
188+
try (Response draftResponse = executeWithRetry(request)) {
143189
if (draftResponse.code() != 200) {
144190
String draftResponseBodyString = draftResponse.body().string();
145191
System.out.println("Save entity failed. Error : " + draftResponseBodyString);
@@ -178,7 +224,7 @@ public String deleteEntity(String appUrl, String entityName, String entityID) {
178224
.addHeader("Authorization", token)
179225
.build();
180226

181-
try (Response response = httpClient.newCall(request).execute()) {
227+
try (Response response = executeWithRetry(request)) {
182228
if (!response.isSuccessful()) {
183229
System.out.println("Delete entity failed. Error : " + response.body().string());
184230
throw new IOException("Could not delete entity");
@@ -207,7 +253,7 @@ public String deleteEntityDraft(String appUrl, String entityName, String entityI
207253
.addHeader("Authorization", token)
208254
.build();
209255

210-
try (Response response = httpClient.newCall(request).execute()) {
256+
try (Response response = executeWithRetry(request)) {
211257
if (!response.isSuccessful()) {
212258
System.out.println("Delete entity failed. Error : " + response.body().string());
213259
throw new IOException("Could not delete entity");
@@ -235,7 +281,7 @@ public String checkEntity(String appUrl, String entityName, String entityID) {
235281
.addHeader("Authorization", token)
236282
.build();
237283

238-
try (Response checkResponse = httpClient.newCall(request).execute()) {
284+
try (Response checkResponse = executeWithRetry(request)) {
239285
if (checkResponse.code() != 200) {
240286
System.out.println("Verify entity failed. Error : " + checkResponse.body().string());
241287
throw new IOException("Entity doesn't exist");
@@ -285,7 +331,7 @@ public List<String> createAttachment(
285331
.addHeader("Authorization", token)
286332
.build();
287333

288-
try (Response response = httpClient.newCall(postRequest).execute()) {
334+
try (Response response = executeWithRetry(postRequest)) {
289335
if (response.code() != 201) {
290336
System.out.println(
291337
"Create Attachment in the section: "
@@ -320,7 +366,7 @@ public List<String> createAttachment(
320366
.addHeader("Authorization", token)
321367
.build();
322368

323-
try (Response fileResponse = httpClient.newCall(fileRequest).execute()) {
369+
try (Response fileResponse = executeWithRetry(fileRequest)) {
324370
if (fileResponse.code() != 204) {
325371
String responseBodyString = fileResponse.body().string();
326372
System.out.println(
@@ -349,7 +395,7 @@ public List<String> createAttachment(
349395
.addHeader("Authorization", token)
350396
.build();
351397

352-
try (Response deleteResponse = httpClient.newCall(request).execute()) {
398+
try (Response deleteResponse = executeWithRetry(request)) {
353399
if (deleteResponse.code() != 204) {
354400
System.out.println(
355401
"Delete Attachment in section :"
@@ -416,7 +462,7 @@ public String readAttachment(
416462
.build();
417463

418464
try {
419-
Response response = httpClient.newCall(request).execute();
465+
Response response = executeWithRetry(request);
420466
if (!response.isSuccessful()) {
421467
System.out.println(
422468
"Read Attachment failed in the "
@@ -458,7 +504,7 @@ public String readAttachmentDraft(
458504
.build();
459505

460506
try {
461-
Response response = httpClient.newCall(request).execute();
507+
Response response = executeWithRetry(request);
462508
if (!response.isSuccessful()) {
463509
System.out.println("Read draft attachment failed. Error : " + response.body().string());
464510
throw new IOException("Could not read attachment");
@@ -492,7 +538,7 @@ public String deleteAttachment(
492538
.addHeader("Authorization", token)
493539
.build();
494540

495-
try (Response deleteResponse = httpClient.newCall(request).execute()) {
541+
try (Response deleteResponse = executeWithRetry(request)) {
496542
if (deleteResponse.code() != 204) {
497543
System.out.println(
498544
"Delete Attachment failed in the "
@@ -535,7 +581,7 @@ public String renameAttachment(
535581
.addHeader("Authorization", token)
536582
.build();
537583

538-
try (Response renameResponse = httpClient.newCall(request).execute()) {
584+
try (Response renameResponse = executeWithRetry(request)) {
539585
if (!renameResponse.isSuccessful()) {
540586
System.out.println(
541587
"Rename Attachment failed in the "
@@ -580,7 +626,7 @@ public String updateSecondaryProperty(
580626
.addHeader("Authorization", token)
581627
.build();
582628

583-
try (Response updateResponse = httpClient.newCall(request).execute()) {
629+
try (Response updateResponse = executeWithRetry(request)) {
584630
if (updateResponse.code() != 200) {
585631
System.out.println(
586632
"Updating secondary property failed. Error: " + updateResponse.body().string());
@@ -624,7 +670,7 @@ public String updateInvalidSecondaryProperty(
624670
.addHeader("Authorization", token)
625671
.build();
626672

627-
try (Response updateResponse = httpClient.newCall(request).execute()) {
673+
try (Response updateResponse = executeWithRetry(request)) {
628674
if (updateResponse.code() != 200) {
629675
System.out.println(
630676
"Updating secondary property failed. Error : " + updateResponse.body().string());
@@ -670,7 +716,7 @@ public String copyAttachment(
670716
Request request =
671717
new Request.Builder().url(url).post(body).addHeader("Authorization", token).build();
672718

673-
try (Response response = httpClient.newCall(request).execute()) {
719+
try (Response response = executeWithRetry(request)) {
674720
if (!response.isSuccessful()) {
675721
throw new IOException(
676722
"Could not copy attachments: " + response.code() + " - " + response.body().string());
@@ -715,7 +761,7 @@ public String createLink(
715761
Request request =
716762
new Request.Builder().url(url).post(body).addHeader("Authorization", token).build();
717763

718-
try (Response response = httpClient.newCall(request).execute()) {
764+
try (Response response = executeWithRetry(request)) {
719765
if (!response.isSuccessful()) {
720766
throw new IOException(
721767
"Could not create link: " + response.code() + " - " + response.body().string());
@@ -758,7 +804,7 @@ public String openAttachment(
758804
Request request =
759805
new Request.Builder().url(url).post(body).addHeader("Authorization", token).build();
760806

761-
try (Response response = httpClient.newCall(request).execute()) {
807+
try (Response response = executeWithRetry(request)) {
762808
if (!response.isSuccessful()) {
763809
throw new IOException(
764810
"Could not open attachment: " + response.code() + " - " + response.body().string());
@@ -807,7 +853,7 @@ public String editLink(
807853
Request request =
808854
new Request.Builder().url(url).post(body).addHeader("Authorization", token).build();
809855

810-
try (Response response = httpClient.newCall(request).execute()) {
856+
try (Response response = executeWithRetry(request)) {
811857
if (!response.isSuccessful()) {
812858
throw new IOException(
813859
"Could not edit link: " + response.code() + " - " + response.body().string());
@@ -842,7 +888,7 @@ public Map<String, Object> fetchMetadata(
842888
Request request =
843889
new Request.Builder().url(url).get().addHeader("Authorization", token).build();
844890

845-
try (Response response = httpClient.newCall(request).execute()) {
891+
try (Response response = executeWithRetry(request)) {
846892
if (response.code() != 200) {
847893
System.out.println("Response code: " + response.code());
848894
System.out.println(
@@ -883,7 +929,7 @@ public Map<String, Object> fetchMetadataDraft(
883929
Request request =
884930
new Request.Builder().url(url).get().addHeader("Authorization", token).build();
885931

886-
try (Response response = httpClient.newCall(request).execute()) {
932+
try (Response response = executeWithRetry(request)) {
887933
if (response.code() != 200) {
888934
System.out.println("Response code: " + response.code());
889935
System.out.println(
@@ -921,7 +967,7 @@ public List<Map<String, Object>> fetchEntityMetadata(
921967
Request request =
922968
new Request.Builder().url(url).get().addHeader("Authorization", token).build();
923969

924-
try (Response response = httpClient.newCall(request).execute()) {
970+
try (Response response = executeWithRetry(request)) {
925971
if (response.code() != 200) {
926972
System.out.println("Response code: " + response.code());
927973
System.out.println(
@@ -965,7 +1011,7 @@ public List<Map<String, Object>> fetchEntityMetadataDraft(
9651011
Request request =
9661012
new Request.Builder().url(url).get().addHeader("Authorization", token).build();
9671013

968-
try (Response response = httpClient.newCall(request).execute()) {
1014+
try (Response response = executeWithRetry(request)) {
9691015
if (response.code() != 200) {
9701016
System.out.println("Response code: " + response.code());
9711017
System.out.println(

0 commit comments

Comments
 (0)