Skip to content

Commit 218bf1f

Browse files
Merge pull request #421 from cap-java/repoexception
When repository not onboarded Read attachment throws error. Hiding link url in attachment.cds
2 parents 21d9e6f + 9f17c8d commit 218bf1f

4 files changed

Lines changed: 87 additions & 55 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public SDMCreateAttachmentsHandler(
6060
@HandlerOrder(HandlerOrder.DEFAULT)
6161
public void processBefore(CdsCreateEventContext context, List<CdsData> data) throws IOException {
6262
logger.info("Target Entity : " + context.getTarget().getQualifiedName());
63+
logger.debug("CDS Data attachments : " + data);
6364

6465
for (CdsData entityData : data) {
6566
Map<String, Map<String, String>> attachmentCompositionDetails =

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

Lines changed: 79 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -114,67 +114,77 @@ private void setErrorMessagesInCache(CdsReadEventContext context) {
114114
@HandlerOrder(HandlerOrder.EARLY + 500)
115115
public void processBefore(CdsReadEventContext context) throws IOException {
116116
String repositoryId = SDMConstants.REPOSITORY_ID;
117+
if (repositoryId == null) {
118+
return;
119+
}
120+
setErrorMessagesInCache(context);
117121
if (context.getTarget().getAnnotationValue(SDMConstants.ANNOTATION_IS_MEDIA_DATA, false)) {
118122
try {
119123
// update the uploadStatus of all blank attachments with success this is for existing
120124
// attachments
121-
RepoValue repoValue =
122-
sdmService.checkRepositoryType(repositoryId, context.getUserInfo().getTenant());
123-
Optional<CdsEntity> attachmentDraftEntity =
124-
context.getModel().findEntity(context.getTarget().getQualifiedName() + "_drafts");
125-
String upIdKey = "", upID = "";
126-
if (attachmentDraftEntity.isPresent()) {
127-
upIdKey = SDMUtils.getUpIdKey(attachmentDraftEntity.get());
128-
CqnSelect select = (CqnSelect) context.get("cqn");
129-
upID = SDMUtils.fetchUPIDFromCQN(select, attachmentDraftEntity.get());
130-
131-
if (!repoValue.getIsAsyncVirusScanEnabled()) {
132-
133-
dbQuery.updateInProgressUploadStatusToSuccess(
134-
attachmentDraftEntity.get(), persistenceService, upID, upIdKey);
135-
}
136-
if (repoValue.getIsAsyncVirusScanEnabled()) {
137-
processVirusScanInProgressAttachments(context, upID, upIdKey);
125+
RepoValue repoValue = checkRepositoryTypeWithFallback(repositoryId, context);
126+
127+
// Only process virus scan logic if repository info is available
128+
if (repoValue != null) {
129+
Optional<CdsEntity> attachmentDraftEntity =
130+
context.getModel().findEntity(context.getTarget().getQualifiedName() + "_drafts");
131+
String upIdKey = "", upID = "";
132+
if (attachmentDraftEntity.isPresent()) {
133+
upIdKey = SDMUtils.getUpIdKey(attachmentDraftEntity.get());
134+
CqnSelect select = (CqnSelect) context.get("cqn");
135+
upID = SDMUtils.fetchUPIDFromCQN(select, attachmentDraftEntity.get());
136+
137+
if (!repoValue.getIsAsyncVirusScanEnabled()) {
138+
139+
dbQuery.updateInProgressUploadStatusToSuccess(
140+
attachmentDraftEntity.get(), persistenceService, upID, upIdKey);
141+
}
142+
if (repoValue.getIsAsyncVirusScanEnabled()) {
143+
processVirusScanInProgressAttachments(context, upID, upIdKey);
144+
}
138145
}
139-
}
140146

141-
// Get attachment associations to handle deep reads with expand
142-
CdsModel cdsModel = context.getModel();
143-
List<String> fieldNames =
144-
getAttachmentAssociations(cdsModel, context.getTarget(), "", new ArrayList<>());
145-
146-
// Create a combined modifier that handles both expand scenarios and repositoryId filter
147-
final SDMBeforeReadItemsModifier itemsModifier = new SDMBeforeReadItemsModifier(fieldNames);
148-
final Predicate repositoryFilter =
149-
CQL.or(CQL.get("repositoryId").eq(repositoryId), CQL.get("repositoryId").isNull());
150-
151-
CqnSelect modifiedCqn =
152-
CQL.copy(
153-
context.getCqn(),
154-
new Modifier() {
155-
@SuppressWarnings({"rawtypes", "unchecked"})
156-
@Override
157-
public List items(List items) {
158-
// Always handle items for expand scenarios
159-
return itemsModifier.items(items);
160-
}
161-
162-
@Override
163-
public Predicate where(Predicate where) {
164-
// Always apply repositoryId filter for all reads
165-
if (where == null) {
166-
return repositoryFilter;
147+
// Get attachment associations to handle deep reads with expand
148+
CdsModel cdsModel = context.getModel();
149+
List<String> fieldNames =
150+
getAttachmentAssociations(cdsModel, context.getTarget(), "", new ArrayList<>());
151+
152+
// Create a combined modifier that handles both expand scenarios and repositoryId filter
153+
final SDMBeforeReadItemsModifier itemsModifier =
154+
new SDMBeforeReadItemsModifier(fieldNames);
155+
final Predicate repositoryFilter =
156+
CQL.or(CQL.get("repositoryId").eq(repositoryId), CQL.get("repositoryId").isNull());
157+
158+
CqnSelect modifiedCqn =
159+
CQL.copy(
160+
context.getCqn(),
161+
new Modifier() {
162+
@SuppressWarnings({"rawtypes", "unchecked"})
163+
@Override
164+
public List items(List items) {
165+
// Always handle items for expand scenarios
166+
return itemsModifier.items(items);
167+
}
168+
169+
@Override
170+
public Predicate where(Predicate where) {
171+
// Always apply repositoryId filter for all reads
172+
if (where == null) {
173+
return repositoryFilter;
174+
}
175+
return CQL.and(where, repositoryFilter);
167176
}
168-
return CQL.and(where, repositoryFilter);
169-
}
170-
});
171-
setErrorMessagesInCache(context);
172-
context.setCqn(modifiedCqn);
177+
});
178+
context.setCqn(modifiedCqn);
179+
} else {
180+
context.setCqn(context.getCqn());
181+
}
173182
} catch (Exception e) {
174183
logger.error("Error in SDMReadAttachmentsHandler.processBefore: {}", e.getMessage(), e);
175184
// Re-throw to maintain error handling behavior
176185
throw e;
177186
}
187+
178188
} else {
179189
context.setCqn(context.getCqn());
180190
}
@@ -340,4 +350,24 @@ private void processAttachmentVirusScanStatus(
340350
e.getMessage());
341351
}
342352
}
353+
354+
/**
355+
* Checks the repository type with fallback handling. Returns null if the check fails, allowing
356+
* the caller to proceed with limited functionality.
357+
*
358+
* @param repositoryId the repository ID to check
359+
* @param context the CDS read event context containing user information
360+
* @return the RepoValue if successful, null otherwise
361+
*/
362+
private RepoValue checkRepositoryTypeWithFallback(
363+
String repositoryId, CdsReadEventContext context) {
364+
try {
365+
return sdmService.checkRepositoryType(repositoryId, context.getUserInfo().getTenant());
366+
} catch (Exception e) {
367+
logger.warn(
368+
"Failed to check repository type, proceeding without repository info: {}",
369+
e.getMessage());
370+
return null;
371+
}
372+
}
343373
}

sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/helper/AttachmentsHandlerUtils.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,13 @@ public static List<Map<String, Object>> fetchAttachments(
201201
String targetEntity, Map<String, Object> entity, String attachmentCompositionName) {
202202
String[] targetEntityPath = targetEntity.split("\\.");
203203
targetEntity = targetEntityPath[targetEntityPath.length - 1];
204-
entity = AttachmentsHandlerUtils.wrapEntityWithParent(entity, targetEntity.toLowerCase());
204+
entity = AttachmentsHandlerUtils.wrapEntityWithParent(entity, targetEntity);
205205
String[] compositionParts = attachmentCompositionName.split("\\.");
206206
String attachmentKeyFromComposition =
207207
compositionParts[compositionParts.length - 1]; // Last part (e.g., "attachments")
208208
String parentKeyFromComposition =
209209
compositionParts.length >= 2
210-
? compositionParts[compositionParts.length - 2].toLowerCase()
210+
? compositionParts[compositionParts.length - 2]
211211
: null; // Second last part (e.g., "chapters")
212212

213213
// Find all attachment arrays in the nested entity structure
@@ -445,12 +445,11 @@ public static Map<String, String> getAttachmentParentTitles(
445445

446446
String[] targetEntityPath = targetEntity.split("\\.");
447447
String entityName = targetEntityPath[targetEntityPath.length - 1];
448-
Map<String, Object> wrappedEntity = wrapEntityWithParent(entity, entityName.toLowerCase());
448+
Map<String, Object> wrappedEntity = wrapEntityWithParent(entity, entityName);
449449

450450
for (Map.Entry<String, String> compositionEntry : compositionPathMapping.entrySet()) {
451451
String compositionPath = compositionEntry.getValue();
452-
String parentTitle =
453-
findParentTitle(wrappedEntity, compositionPath, entityName.toLowerCase());
452+
String parentTitle = findParentTitle(wrappedEntity, compositionPath, entityName);
454453
if (parentTitle != null) {
455454
parentTitles.put(compositionPath, parentTitle);
456455
}

sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,9 @@ public JSONObject getRepositoryInfo(SDMCredentials sdmCredentials) {
581581
String responseString = EntityUtils.toString(response.getEntity());
582582
return new JSONObject(responseString);
583583
} catch (IOException e) {
584-
throw new ServiceException(SDMUtils.getErrorMessage("REPOSITORY_ERROR"));
584+
throw new ServiceException(SDMUtils.getErrorMessage("REPOSITORY_ERROR"), e);
585+
} catch (Exception e) {
586+
throw new ServiceException(SDMUtils.getErrorMessage("REPOSITORY_ERROR"), e);
585587
}
586588
}
587589

0 commit comments

Comments
 (0)