Skip to content

Commit 31f3988

Browse files
authored
chore: Code documentation (#499)
1 parent e5fb451 commit 31f3988

File tree

4 files changed

+63
-34
lines changed

4 files changed

+63
-34
lines changed

cds-feature-attachments/src/main/java/com/sap/cds/feature/attachments/handler/applicationservice/CreateAttachmentsHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public CreateAttachmentsHandler(ModifyAttachmentEventFactory eventFactory, Threa
4545
@Before
4646
@HandlerOrder(OrderConstants.Before.CHECK_CAPABILITIES)
4747
void processBeforeForDraft(CdsCreateEventContext context, List<CdsData> data) {
48-
ReadonlyDataContextEnhancer.enhanceReadonlyDataInContext(context, data, storageReader.get());
48+
// before the attachment's readonly fields are removed by the runtime, preserve them in a custom field in data
49+
ReadonlyDataContextEnhancer.preserveReadonlyFields(context.getTarget(), data, storageReader.get());
4950
}
5051

5152
@Before

cds-feature-attachments/src/main/java/com/sap/cds/feature/attachments/handler/applicationservice/UpdateAttachmentsHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public UpdateAttachmentsHandler(ModifyAttachmentEventFactory eventFactory, Attac
6161
@Before
6262
@HandlerOrder(OrderConstants.Before.CHECK_CAPABILITIES)
6363
void processBeforeForDraft(CdsUpdateEventContext context, List<CdsData> data) {
64-
ReadonlyDataContextEnhancer.enhanceReadonlyDataInContext(context, data, storageReader.get());
64+
// before the attachment's readonly fields are removed by the runtime, preserve them in a custom field in data
65+
ReadonlyDataContextEnhancer.preserveReadonlyFields(context.getTarget(), data, storageReader.get());
6566
}
6667

6768
@Before

cds-feature-attachments/src/main/java/com/sap/cds/feature/attachments/handler/applicationservice/helper/ModifyApplicationHandlerHelper.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,56 @@
2020

2121
public final class ModifyApplicationHandlerHelper {
2222

23-
private ModifyApplicationHandlerHelper() {
24-
// avoid instantiation
25-
}
26-
2723
/**
2824
* Handles attachments for entities.
2925
*
30-
* @param entity the {@link CdsEntity entity} to handle attachments for
31-
* @param data the given list of {@link CdsData data}
32-
* @param existingDataList the given list of existing {@link CdsData data}
33-
* @param eventFactory the {@link ModifyAttachmentEventFactory} to create the corresponding event
34-
* @param eventContext the current {@link EventContext}
26+
* @param entity the {@link CdsEntity entity} to handle attachments for
27+
* @param data the given list of {@link CdsData data}
28+
* @param existingAttachments the given list of existing {@link CdsData data}
29+
* @param eventFactory the {@link ModifyAttachmentEventFactory} to create the corresponding event
30+
* @param eventContext the current {@link EventContext}
3531
*/
36-
public static void handleAttachmentForEntities(CdsEntity entity, List<? extends CdsData> data,
37-
List<Attachments> existingDataList, ModifyAttachmentEventFactory eventFactory, EventContext eventContext) {
38-
Converter converter = (path, element, value) -> handleAttachmentForEntity(existingDataList, eventFactory,
32+
public static void handleAttachmentForEntities(CdsEntity entity, List<CdsData> data,
33+
List<Attachments> existingAttachments, ModifyAttachmentEventFactory eventFactory,
34+
EventContext eventContext) {
35+
Converter converter = (path, element, value) -> handleAttachmentForEntity(existingAttachments, eventFactory,
3936
eventContext, path, (InputStream) value);
4037

4138
CdsDataProcessor.create().addConverter(ApplicationHandlerHelper.MEDIA_CONTENT_FILTER, converter).process(data,
4239
entity);
4340
}
4441

45-
public static InputStream handleAttachmentForEntity(List<Attachments> existingDataList,
42+
/**
43+
* Handles attachments for a single entity.
44+
*
45+
* @param existingAttachments the list of existing {@link Attachments} to check against
46+
* @param eventFactory the {@link ModifyAttachmentEventFactory} to create the corresponding event
47+
* @param eventContext the current {@link EventContext}
48+
* @param path the {@link Path} of the attachment
49+
* @param content the content of the attachment
50+
* @return the processed content as an {@link InputStream}
51+
*/
52+
public static InputStream handleAttachmentForEntity(List<Attachments> existingAttachments,
4653
ModifyAttachmentEventFactory eventFactory, EventContext eventContext, Path path, InputStream content) {
4754
Map<String, Object> keys = ApplicationHandlerHelper.removeDraftKey(path.target().keys());
48-
ReadonlyDataContextEnhancer.fillReadonlyInContext((CdsData) path.target().values());
49-
Attachments existingData = getExistingData(keys, existingDataList);
55+
ReadonlyDataContextEnhancer.restoreReadonlyFields((CdsData) path.target().values());
56+
Attachments attachment = getExistingAttachment(keys, existingAttachments);
5057
String contentId = (String) path.target().values().get(Attachments.CONTENT_ID);
5158

5259
// for the current request find the event to process
53-
ModifyAttachmentEvent eventToProcess = eventFactory.getEvent(content, contentId, existingData);
60+
ModifyAttachmentEvent eventToProcess = eventFactory.getEvent(content, contentId, attachment);
5461

5562
// process the event
56-
return eventToProcess.processEvent(path, content, existingData, eventContext);
63+
return eventToProcess.processEvent(path, content, attachment, eventContext);
5764
}
5865

59-
private static Attachments getExistingData(Map<String, Object> keys, List<Attachments> existingDataList) {
60-
return existingDataList.stream()
66+
private static Attachments getExistingAttachment(Map<String, Object> keys, List<Attachments> existingAttachments) {
67+
return existingAttachments.stream()
6168
.filter(existingData -> ApplicationHandlerHelper.areKeysInData(keys, existingData)).findAny()
6269
.orElse(Attachments.create());
6370
}
6471

72+
private ModifyApplicationHandlerHelper() {
73+
// avoid instantiation
74+
}
6575
}

cds-feature-attachments/src/main/java/com/sap/cds/feature/attachments/handler/applicationservice/helper/ReadonlyDataContextEnhancer.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,50 @@
1111
import com.sap.cds.CdsDataProcessor.Validator;
1212
import com.sap.cds.feature.attachments.generated.cds4j.sap.attachments.Attachments;
1313
import com.sap.cds.feature.attachments.handler.common.ApplicationHandlerHelper;
14-
import com.sap.cds.services.EventContext;
14+
import com.sap.cds.reflect.CdsEntity;
1515

16+
/**
17+
* The class {@link ReadonlyDataContextEnhancer} provides methods to backup and restore readonly fields of attachments
18+
* in the data.
19+
*/
1620
public final class ReadonlyDataContextEnhancer {
1721

1822
private static final String DRAFT_READONLY_CONTEXT = "DRAFT_READONLY_CONTEXT";
1923

20-
private ReadonlyDataContextEnhancer() {
21-
}
22-
23-
public static void enhanceReadonlyDataInContext(EventContext context, List<? extends CdsData> data,
24-
boolean isDraft) {
24+
/**
25+
* Preserves the readonly fields of an {@link Attachments attachment} in a custom field with the name
26+
* {@value #DRAFT_READONLY_CONTEXT}. These readonly data will be removed from the data by the CAP Java runtime, but
27+
* the preserved copy still exists.
28+
*
29+
* @param target the target {@link CdsEntity entity}
30+
* @param data the list of {@link CdsData data} to enhance
31+
* @param isDraft <code>true</code> if the data is from a draft entity, <code>false</code> otherwise
32+
*/
33+
public static void preserveReadonlyFields(CdsEntity target, List<CdsData> data, boolean isDraft) {
2534

2635
Validator validator = (path, element, value) -> {
2736
if (isDraft) {
2837
Attachments values = Attachments.of(path.target().values());
29-
Attachments cdsData = Attachments.create();
30-
cdsData.setContentId(values.getContentId());
31-
cdsData.setStatus(values.getStatus());
32-
cdsData.setScannedAt(values.getScannedAt());
33-
path.target().values().put(DRAFT_READONLY_CONTEXT, cdsData);
38+
Attachments attachment = Attachments.create();
39+
attachment.setContentId(values.getContentId());
40+
attachment.setStatus(values.getStatus());
41+
attachment.setScannedAt(values.getScannedAt());
42+
path.target().values().put(DRAFT_READONLY_CONTEXT, attachment);
3443
} else {
3544
path.target().values().remove(DRAFT_READONLY_CONTEXT);
3645
}
3746
};
3847

3948
CdsDataProcessor.create().addValidator(ApplicationHandlerHelper.MEDIA_CONTENT_FILTER, validator).process(data,
40-
context.getTarget());
49+
target);
4150
}
4251

43-
public static void fillReadonlyInContext(CdsData data) {
52+
/**
53+
* Restores the readonly fields with the backup from the data in the custom field {@value #DRAFT_READONLY_CONTEXT}.
54+
*
55+
* @param data the {@link CdsData data} to restore with readonly fields
56+
*/
57+
public static void restoreReadonlyFields(CdsData data) {
4458
CdsData readOnlyData = (CdsData) data.get(DRAFT_READONLY_CONTEXT);
4559
if (Objects.nonNull(readOnlyData)) {
4660
data.put(Attachments.CONTENT_ID, readOnlyData.get(Attachments.CONTENT_ID));
@@ -50,4 +64,7 @@ public static void fillReadonlyInContext(CdsData data) {
5064
}
5165
}
5266

67+
private ReadonlyDataContextEnhancer() {
68+
// avoid instantiation
69+
}
5370
}

0 commit comments

Comments
 (0)