This is an automated email from the ASF dual-hosted git repository.

ashishvijaywargiya pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-plugins.git


The following commit(s) were added to refs/heads/trunk by this push:
     new a2be2accb Adopt update() DSL orderBy()/first()/ifExists() in 
createExampleStatus() (#412)
a2be2accb is described below

commit a2be2accb06d66f9e9bd23286d61b6ad1a71b726
Author: Ashish Vijaywargiya <[email protected]>
AuthorDate: Fri Sep 18 14:24:49 2026 +0530

    Adopt update() DSL orderBy()/first()/ifExists() in createExampleStatus() 
(#412)
    
    Replaces the manual orderBy('-statusDate').queryFirst() + if/store()
    guard with the update() DSL, now that EntityUpdateBuilder supports
    orderBy(). Preserves the existing "expire only the most recent status,
    silently no-op if none exists" behavior exactly. No behavior change.
---
 .../ofbiz/example/ExampleServicesScript.groovy     | 11 ++-----
 .../ofbiz/example/test/ExampleJupiterTests.groovy  | 36 ++++++++++++++++++++++
 2 files changed, 38 insertions(+), 9 deletions(-)

diff --git 
a/example/src/main/groovy/org/apache/ofbiz/example/ExampleServicesScript.groovy 
b/example/src/main/groovy/org/apache/ofbiz/example/ExampleServicesScript.groovy
index 555fd346d..524d552db 100644
--- 
a/example/src/main/groovy/org/apache/ofbiz/example/ExampleServicesScript.groovy
+++ 
b/example/src/main/groovy/org/apache/ofbiz/example/ExampleServicesScript.groovy
@@ -71,15 +71,8 @@ Map createExampleStatus() {
     nowTimestamp = UtilDateTime.nowTimestamp()
 
     // find the most recent status record and set the statusEndDate
-    GenericValue oldExampleStatus = from('ExampleStatus')
-            .where('exampleId', parameters.exampleId)
-            .orderBy('-statusDate')
-            .queryFirst()
-
-    if (oldExampleStatus) {
-        oldExampleStatus.statusEndDate = nowTimestamp
-        oldExampleStatus.store()
-    }
+    update('ExampleStatus').where([exampleId: parameters.exampleId])
+            .orderBy('-statusDate').first().ifExists().set([statusEndDate: 
nowTimestamp])
 
     GenericValue newEntity = makeValue('ExampleStatus')
     newEntity.setPKFields(parameters)
diff --git 
a/example/src/test/groovy/org/apache/ofbiz/example/test/ExampleJupiterTests.groovy
 
b/example/src/test/groovy/org/apache/ofbiz/example/test/ExampleJupiterTests.groovy
index cf5d2f631..6bce353ec 100644
--- 
a/example/src/test/groovy/org/apache/ofbiz/example/test/ExampleJupiterTests.groovy
+++ 
b/example/src/test/groovy/org/apache/ofbiz/example/test/ExampleJupiterTests.groovy
@@ -109,6 +109,42 @@ class ExampleJupiterTests implements JupiterTestHelper {
         assert example.statusId == updatedStatusId
     }
 
+    @Test
+    @Order(5)
+    void shouldExpirePreviousStatusOnCreateExampleStatus() {
+        GenericValue userLogin = delegator.findOne('UserLogin', [userLoginId: 
'system'], false)
+
+        Map<String, Object> createResult = dispatcher.runSync('createExample', 
[
+                exampleTypeId: 'CONTRIVED',
+                exampleName: 'Test Example - Status Expiry',
+                statusId: 'EXST_IN_DESIGN',
+                userLogin: userLogin
+        ])
+        assert ServiceUtil.isSuccess(createResult)
+        String exampleId = createResult.exampleId
+
+        // createExample's own "return" ECA already calls createExampleStatus 
once, seeding the
+        // first ExampleStatus row -- so the explicit call below is the second 
one, and it's this
+        // conversion's ifExists()/first()/orderBy() that must find and expire 
that ECA-created row.
+        List<GenericValue> afterCreate = 
from('ExampleStatus').where('exampleId', exampleId).queryList()
+        assert afterCreate.size() == 1
+        assert afterCreate[0].statusEndDate == null
+
+        Map<String, Object> secondStatusResult = 
dispatcher.runSync('createExampleStatus', [
+                exampleId: exampleId,
+                statusId: 'EXST_DEFINED',
+                userLogin: userLogin
+        ])
+        assert ServiceUtil.isSuccess(secondStatusResult)
+
+        List<GenericValue> afterSecond = 
from('ExampleStatus').where('exampleId', exampleId)
+                .orderBy('statusDate').queryList()
+        assert afterSecond.size() == 2
+        assert afterSecond[0].statusEndDate != null
+        assert afterSecond[1].statusEndDate == null
+        assert afterSecond[1].statusId == 'EXST_DEFINED'
+    }
+
     @ParameterizedTest(name = '[{index}] exampleTypeId={0}')
     @Order(2)
     @CsvSource([

Reply via email to