diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java index d7789e67..6dac45d5 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java @@ -2,6 +2,7 @@ import com.sap.cds.Result; import com.sap.cds.ql.Insert; +import com.sap.cds.ql.Update; import com.sap.cds.reflect.CdsAssociationType; import com.sap.cds.reflect.CdsElement; import com.sap.cds.reflect.CdsEntity; @@ -2000,6 +2001,21 @@ private void createDraftEntries( throw new ServiceException( "Failed to insert attachment entry in DB after retries: " + e.getMessage(), e); } + // newDraft strips @readonly fields; DB DEFAULT 'Unscanned' applies for status. + // Bypass handler chain via persistenceService to persist status=Clean and scannedAt. + if (targetEntity != null) { + Map scanFields = new HashMap<>(); + scanFields.put("status", "Clean"); + scanFields.put("scannedAt", Instant.now()); + String draftEntityName = targetEntity.getQualifiedName() + "_drafts"; + var scanUpdate = + Update.entity(draftEntityName) + .data(scanFields) + .where(doc -> doc.get("objectId").eq(newObjectId)); + persistenceService.run(scanUpdate); + logger.debug( + "Set status=Clean, scannedAt=now for copied draft attachment: {}", newObjectId); + } } else { logger.error("No suitable service found for entity: {}", request.getParentEntity()); throw new ServiceException( diff --git a/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMReadAttachmentsHandlerTest.java b/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMReadAttachmentsHandlerTest.java index a4f1ed45..7c2a179c 100644 --- a/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMReadAttachmentsHandlerTest.java +++ b/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMReadAttachmentsHandlerTest.java @@ -25,6 +25,7 @@ import java.lang.reflect.Method; import java.util.*; import org.ehcache.Cache; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -48,6 +49,13 @@ public class SDMReadAttachmentsHandlerTest { private static final String REPOSITORY_ID_KEY = "testRepoId"; + @AfterEach + void tearDown() throws Exception { + java.lang.reflect.Field field = CacheConfig.class.getDeclaredField("errorMessageCache"); + field.setAccessible(true); + field.set(null, null); + } + @Test void testModifyCqnForAttachmentsEntity_Success() throws IOException { // Arrange