|
20 | 20 |
|
21 | 21 | public final class ModifyApplicationHandlerHelper { |
22 | 22 |
|
23 | | - private ModifyApplicationHandlerHelper() { |
24 | | - // avoid instantiation |
25 | | - } |
26 | | - |
27 | 23 | /** |
28 | 24 | * Handles attachments for entities. |
29 | 25 | * |
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} |
35 | 31 | */ |
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, |
39 | 36 | eventContext, path, (InputStream) value); |
40 | 37 |
|
41 | 38 | CdsDataProcessor.create().addConverter(ApplicationHandlerHelper.MEDIA_CONTENT_FILTER, converter).process(data, |
42 | 39 | entity); |
43 | 40 | } |
44 | 41 |
|
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, |
46 | 53 | ModifyAttachmentEventFactory eventFactory, EventContext eventContext, Path path, InputStream content) { |
47 | 54 | 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); |
50 | 57 | String contentId = (String) path.target().values().get(Attachments.CONTENT_ID); |
51 | 58 |
|
52 | 59 | // 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); |
54 | 61 |
|
55 | 62 | // process the event |
56 | | - return eventToProcess.processEvent(path, content, existingData, eventContext); |
| 63 | + return eventToProcess.processEvent(path, content, attachment, eventContext); |
57 | 64 | } |
58 | 65 |
|
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() |
61 | 68 | .filter(existingData -> ApplicationHandlerHelper.areKeysInData(keys, existingData)).findAny() |
62 | 69 | .orElse(Attachments.create()); |
63 | 70 | } |
64 | 71 |
|
| 72 | + private ModifyApplicationHandlerHelper() { |
| 73 | + // avoid instantiation |
| 74 | + } |
65 | 75 | } |
0 commit comments