@@ -133,95 +133,108 @@ public void processBefore(CdsReadEventContext context) throws IOException {
133133 return ;
134134 }
135135 setErrorMessagesInCache (context );
136- if (context .getTarget ().getAnnotationValue (SDMConstants .ANNOTATION_IS_MEDIA_DATA , false )) {
137- try {
138- // update the uploadStatus of all blank attachments with success this is for existing
139- // attachments
140- logger .debug ("Target is a media entity, processing attachment logic" );
141- RepoValue repoValue = checkRepositoryTypeWithFallback (repositoryId , context );
142-
143- // Only process virus scan logic if repository info is available
144- if (repoValue != null ) {
145- logger .debug (
146- "Repository value found. Async virus scan enabled: {}" ,
147- repoValue .getIsAsyncVirusScanEnabled ());
148- Optional <CdsEntity > attachmentDraftEntity =
149- context .getModel ().findEntity (context .getTarget ().getQualifiedName () + "_drafts" );
150- String upIdKey = "" , upID = "" ;
151- if (attachmentDraftEntity .isPresent ()) {
152- upIdKey = SDMUtils .getUpIdKey (attachmentDraftEntity .get ());
153- CqnSelect select = (CqnSelect ) context .get ("cqn" );
154- upID = SDMUtils .fetchUPIDFromCQN (select , attachmentDraftEntity .get ());
155- logger .debug ("Processing attachments for upID: {}" , upID );
156-
157- if (!repoValue .getIsAsyncVirusScanEnabled ()) {
158- logger .debug ("Sync virus scan mode: updating in-progress upload status to success" );
159- dbQuery .updateInProgressUploadStatusToSuccess (
160- attachmentDraftEntity .get (), persistenceService , upID , upIdKey );
161- }
162- if (repoValue .getIsAsyncVirusScanEnabled ()) {
163- logger .debug ("Async virus scan mode: processing virus scan in-progress attachments" );
164- processVirusScanInProgressAttachments (context , upID , upIdKey );
165- }
166- }
167-
168- // Get attachment associations to handle deep reads with expand
169- CdsModel cdsModel = context .getModel ();
170- List <String > fieldNames =
171- getAttachmentAssociations (cdsModel , context .getTarget (), "" , new ArrayList <>());
172- logger .debug ("Found {} attachment associations" , fieldNames .size ());
173-
174- // Create a combined modifier that handles both expand scenarios and repositoryId filter
175- final SDMBeforeReadItemsModifier itemsModifier =
176- new SDMBeforeReadItemsModifier (fieldNames );
177- final Predicate repositoryFilter =
178- CQL .or (CQL .get ("repositoryId" ).eq (repositoryId ), CQL .get ("repositoryId" ).isNull ());
179- logger .debug (
180- "Creating CQN modifier with {} field names and repository filter" , fieldNames .size ());
181-
182- CqnSelect modifiedCqn =
183- CQL .copy (
184- context .getCqn (),
185- new Modifier () {
186- @ SuppressWarnings ({"rawtypes" , "unchecked" })
187- @ Override
188- public List items (List items ) {
189- // Always handle items for expand scenarios
190- return itemsModifier .items (items );
191- }
192-
193- @ Override
194- public Predicate where (Predicate where ) {
195- // Always apply repositoryId filter for all reads
196- if (where == null ) {
197- return repositoryFilter ;
198- }
199- return CQL .and (where , repositoryFilter );
200- }
201- });
202- context .setCqn (modifiedCqn );
203- logger .debug ("CQN query modified with repository filter and required fields" );
204- } else {
205- logger .warn (
206- "Repository value is null for repository ID: {}. Proceeding with limited functionality" ,
207- repositoryId );
208- context .setCqn (context .getCqn ());
209- }
210- } catch (Exception e ) {
211- logger .error ("Error in SDMReadAttachmentsHandler.processBefore: {}" , e .getMessage (), e );
212- // Re-throw to maintain error handling behavior
213- throw e ;
214- }
215-
216- } else {
136+ if (!SDMApplicationHandlerHelper .isMediaEntity (context .getTarget ())) {
217137 logger .debug (
218138 "Target entity {} is not a media entity, skipping attachment processing" ,
219139 context .getTarget ().getQualifiedName ());
220- context .setCqn (context .getCqn ());
140+ return ;
141+ }
142+ try {
143+ logger .debug ("Target is a media entity, processing attachment logic" );
144+ RepoValue repoValue = checkRepositoryTypeWithFallback (repositoryId , context );
145+ if (repoValue != null ) {
146+ processMediaEntityRead (context , repositoryId , repoValue );
147+ } else {
148+ logger .warn (
149+ "Repository value is null for repository ID: {}. Proceeding with limited functionality" ,
150+ repositoryId );
151+ }
152+ } catch (Exception e ) {
153+ logger .error ("Error in SDMReadAttachmentsHandler.processBefore: {}" , e .getMessage (), e );
154+ throw e ;
221155 }
222156 logger .debug ("END: Read attachments processing completed" );
223157 }
224158
159+ private void processMediaEntityRead (
160+ CdsReadEventContext context , String repositoryId , RepoValue repoValue ) {
161+ logger .debug (
162+ "Repository value found. Async virus scan enabled: {}" ,
163+ repoValue .getIsAsyncVirusScanEnabled ());
164+ Optional <CdsEntity > attachmentActiveEntity =
165+ context .getModel ().findEntity (context .getTarget ().getQualifiedName ());
166+ logger .debug (
167+ "Active entity: {}" ,
168+ attachmentActiveEntity .isPresent ()
169+ ? attachmentActiveEntity .get ().getQualifiedName ()
170+ : "No active entity" );
171+ Optional <CdsEntity > attachmentDraftEntity =
172+ context
173+ .getModel ()
174+ .findEntity (context .getTarget ().getQualifiedName () + "_drafts" )
175+ .or (() -> attachmentActiveEntity );
176+ logger .debug (
177+ "Draft entity: {}" ,
178+ attachmentDraftEntity .isPresent ()
179+ ? attachmentDraftEntity .get ().getQualifiedName ()
180+ : "No draft entity" );
181+
182+ if (attachmentDraftEntity .isPresent ()) {
183+ processUploadStatus (context , repoValue , attachmentDraftEntity .get ());
184+ }
185+
186+ List <String > fieldNames =
187+ getAttachmentAssociations (context .getModel (), context .getTarget (), "" , new ArrayList <>());
188+ logger .debug ("Found {} attachment associations" , fieldNames .size ());
189+
190+ CqnSelect modifiedCqn = buildModifiedCqn (context , repositoryId , fieldNames );
191+ context .setCqn (modifiedCqn );
192+ logger .debug ("CQN query modified with repository filter and required fields" );
193+ }
194+
195+ private void processUploadStatus (
196+ CdsReadEventContext context , RepoValue repoValue , CdsEntity attachmentEntity ) {
197+ String upIdKey = SDMUtils .getUpIdKey (attachmentEntity );
198+ CqnSelect select = (CqnSelect ) context .get ("cqn" );
199+ String upID = SDMUtils .fetchUPIDFromCQN (select , attachmentEntity );
200+ logger .debug ("Processing attachments for upID: {}" , upID );
201+
202+ if (!repoValue .getIsAsyncVirusScanEnabled ()) {
203+ logger .debug ("Sync virus scan mode: updating in-progress upload status to success" );
204+ dbQuery .updateInProgressUploadStatusToSuccess (
205+ attachmentEntity , persistenceService , upID , upIdKey );
206+ } else {
207+ logger .debug ("Async virus scan mode: processing virus scan in-progress attachments" );
208+ processVirusScanInProgressAttachments (context , upID , upIdKey );
209+ }
210+ }
211+
212+ private CqnSelect buildModifiedCqn (
213+ CdsReadEventContext context , String repositoryId , List <String > fieldNames ) {
214+ final SDMBeforeReadItemsModifier itemsModifier = new SDMBeforeReadItemsModifier (fieldNames );
215+ final Predicate repositoryFilter =
216+ CQL .or (CQL .get ("repositoryId" ).eq (repositoryId ), CQL .get ("repositoryId" ).isNull ());
217+ logger .debug (
218+ "Creating CQN modifier with {} field names and repository filter" , fieldNames .size ());
219+ return CQL .copy (
220+ context .getCqn (),
221+ new Modifier () {
222+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
223+ @ Override
224+ public List items (List items ) {
225+ return itemsModifier .items (items );
226+ }
227+
228+ @ Override
229+ public Predicate where (Predicate where ) {
230+ if (where == null ) {
231+ return repositoryFilter ;
232+ }
233+ return CQL .and (where , repositoryFilter );
234+ }
235+ });
236+ }
237+
225238 /**
226239 * Recursively get all attachment associations in the entity tree. This is needed to properly
227240 * handle deep navigation like Books/covers with $expand=statusNav
0 commit comments