88import com .sap .cds .sdm .caching .SecondaryPropertiesKey ;
99import com .sap .cds .sdm .constants .SDMConstants ;
1010import com .sap .cds .sdm .handler .TokenHandler ;
11+ import com .sap .cds .sdm .handler .applicationservice .helper .AttachmentsHandlerUtils ;
1112import com .sap .cds .sdm .model .CmisDocument ;
1213import com .sap .cds .sdm .model .SDMCredentials ;
1314import com .sap .cds .sdm .persistence .DBQuery ;
2930import java .util .Map ;
3031import java .util .Optional ;
3132import java .util .Set ;
33+ import org .slf4j .Logger ;
34+ import org .slf4j .LoggerFactory ;
3235
3336@ ServiceName (value = "*" , type = ApplicationService .class )
3437public class SDMCreateAttachmentsHandler implements EventHandler {
35-
3638 private final PersistenceService persistenceService ;
3739 private final SDMService sdmService ;
3840 private final TokenHandler tokenHandler ;
3941 private final DBQuery dbQuery ;
42+ private static final Logger logger = LoggerFactory .getLogger (SDMCreateAttachmentsHandler .class );
4043
4144 public SDMCreateAttachmentsHandler (
4245 PersistenceService persistenceService ,
@@ -52,17 +55,29 @@ public SDMCreateAttachmentsHandler(
5255 @ Before
5356 @ HandlerOrder (HandlerOrder .EARLY )
5457 public void processBefore (CdsCreateEventContext context , List <CdsData > data ) throws IOException {
55- List <String > attachmentCompositions = getEntityCompositions (context );
56- for (String composition : attachmentCompositions ) {
57- updateName (context , data , composition );
58+ // Get the combined mapping of attachment composition paths and names
59+ Map <String , String > compositionPathMapping =
60+ AttachmentsHandlerUtils .getAttachmentPathMapping (
61+ context .getModel (), context .getTarget (), persistenceService );
62+ logger .info ("Attachment compositions present in CDS Model : " + compositionPathMapping );
63+ for (Map .Entry <String , String > entry : compositionPathMapping .entrySet ()) {
64+ String attachmentCompositionDefinition = entry .getKey ();
65+ String attachmentCompositionName = entry .getValue ();
66+ updateName (context , data , attachmentCompositionDefinition , attachmentCompositionName );
5867 }
5968 }
6069
61- public void updateName (CdsCreateEventContext context , List <CdsData > data , String composition )
70+ public void updateName (
71+ CdsCreateEventContext context ,
72+ List <CdsData > data ,
73+ String attachmentCompositionDefinition ,
74+ String attachmentCompositionName )
6275 throws IOException {
6376 Map <String , String > propertyTitles = new HashMap <>();
6477 Map <String , String > secondaryPropertiesWithInvalidDefinitions = new HashMap <>();
65- Set <String > duplicateFilenames = SDMUtils .isFileNameDuplicateInDrafts (data , composition );
78+ String targetEntity = context .getTarget ().getQualifiedName ();
79+ Set <String > duplicateFilenames =
80+ SDMUtils .isFileNameDuplicateInDrafts (data , attachmentCompositionName , targetEntity );
6681 if (!duplicateFilenames .isEmpty ()) {
6782 handleDuplicateFilenames (context , duplicateFilenames );
6883 } else {
@@ -73,11 +88,11 @@ public void updateName(CdsCreateEventContext context, List<CdsData> data, String
7388 Map <String , String > badRequest = new HashMap <>();
7489 List <String > noSDMRoles = new ArrayList <>();
7590 for (Map <String , Object > entity : data ) {
76- List <Map <String , Object >> attachments = (List <Map <String , Object >>) entity .get (composition );
91+ List <Map <String , Object >> attachments =
92+ AttachmentsHandlerUtils .fetchAttachments (
93+ targetEntity , entity , attachmentCompositionName );
7794 Optional <CdsEntity > attachmentEntity =
78- context
79- .getModel ()
80- .findEntity (context .getTarget ().getQualifiedName () + "." + composition );
95+ context .getModel ().findEntity (attachmentCompositionDefinition );
8196 if (attachments != null && !attachments .isEmpty ()) {
8297 propertyTitles = SDMUtils .getPropertyTitles (attachmentEntity , attachments .get (0 ));
8398 secondaryPropertiesWithInvalidDefinitions =
@@ -92,10 +107,11 @@ public void updateName(CdsCreateEventContext context, List<CdsData> data, String
92107 filesNotFound ,
93108 filesWithUnsupportedProperties ,
94109 badRequest ,
95- composition ,
110+ attachmentCompositionDefinition ,
96111 attachmentEntity ,
97112 secondaryPropertiesWithInvalidDefinitions ,
98- noSDMRoles );
113+ noSDMRoles ,
114+ attachmentCompositionName );
99115 handleWarnings (
100116 context ,
101117 fileNameWithRestrictedCharacters ,
@@ -130,9 +146,12 @@ private void processEntity(
130146 String composition ,
131147 Optional <CdsEntity > attachmentEntity ,
132148 Map <String , String > secondaryPropertiesWithInvalidDefinitions ,
133- List <String > noSDMRoles )
149+ List <String > noSDMRoles ,
150+ String attachmentCompositionName )
134151 throws IOException {
135- List <Map <String , Object >> attachments = (List <Map <String , Object >>) entity .get (composition );
152+ String targetEntity = context .getTarget ().getQualifiedName ();
153+ List <Map <String , Object >> attachments =
154+ AttachmentsHandlerUtils .fetchAttachments (targetEntity , entity , attachmentCompositionName );
136155 if (attachments != null ) {
137156 for (Map <String , Object > attachment : attachments ) {
138157 processAttachment (
0 commit comments