You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2026/04/09 08:14:00 UTC

(causeway) branch main updated: CAUSEWAY-3989: CommandDto support for Yaml marshalling (in bulk)

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

ahuber pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/causeway.git


The following commit(s) were added to refs/heads/main by this push:
     new 2159a0432d3 CAUSEWAY-3989: CommandDto support for Yaml marshalling (in bulk)
2159a0432d3 is described below

commit 2159a0432d35acd0e3dedcb6c8fc02925b7e78ab
Author: andi-huber <ah...@apache.org>
AuthorDate: Thu Apr 9 10:13:55 2026 +0200

    CAUSEWAY-3989: CommandDto support for Yaml marshalling (in bulk)
---
 .../applib/util/schema/CommandDtoUtils.java        |  65 +++++++++++--
 .../org/apache/causeway/commons/io/YamlUtils.java  |  48 ++++++++--
 .../mmtest/schema/CommandDtoYamlRoundtripTest.java | 103 +++++++++++++++++++++
 .../commandlog/applib/dom/ReplayState.java         |   6 +-
 4 files changed, 203 insertions(+), 19 deletions(-)

diff --git a/api/applib/src/main/java/org/apache/causeway/applib/util/schema/CommandDtoUtils.java b/api/applib/src/main/java/org/apache/causeway/applib/util/schema/CommandDtoUtils.java
index 935fb354918..f97b639aff0 100644
--- a/api/applib/src/main/java/org/apache/causeway/applib/util/schema/CommandDtoUtils.java
+++ b/api/applib/src/main/java/org/apache/causeway/applib/util/schema/CommandDtoUtils.java
@@ -18,20 +18,36 @@
  */
 package org.apache.causeway.applib.util.schema;
 
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+
 import org.apache.causeway.applib.services.bookmark.Bookmark;
 import org.apache.causeway.commons.internal.base._Lazy;
+import org.apache.causeway.commons.internal.base._NullSafe;
 import org.apache.causeway.commons.internal.base._Strings;
+import org.apache.causeway.commons.io.DataSource;
 import org.apache.causeway.commons.io.DtoMapper;
 import org.apache.causeway.commons.io.JaxbUtils;
+import org.apache.causeway.commons.io.JsonUtils;
+import org.apache.causeway.commons.io.JsonUtils.JacksonCustomizer;
+import org.apache.causeway.commons.io.YamlUtils;
 import org.apache.causeway.schema.cmd.v2.ActionDto;
 import org.apache.causeway.schema.cmd.v2.CommandDto;
 import org.apache.causeway.schema.cmd.v2.MapDto;
+import org.apache.causeway.schema.cmd.v2.MemberDto;
 import org.apache.causeway.schema.cmd.v2.ParamsDto;
+import org.apache.causeway.schema.cmd.v2.PropertyDto;
 import org.apache.causeway.schema.common.v2.OidsDto;
 import org.apache.causeway.schema.common.v2.PeriodDto;
 
 import lombok.experimental.UtilityClass;
 
+import tools.jackson.databind.cfg.MapperBuilder;
+import tools.jackson.databind.jsontype.NamedType;
+
 /**
  * @since 1.x {@index}
  */
@@ -77,34 +93,30 @@ public PeriodDto timingsFor(final CommandDto commandDto) {
     }
 
     public String getUserData(final CommandDto dto, final String key) {
-        if(dto == null || key == null) {
+        if(dto == null || key == null)
             return null;
-        }
         return CommonDtoUtils.getMapValue(dto.getUserData(), key);
     }
 
     public void setUserData(
             final CommandDto dto, final String key, final String value) {
-        if(dto == null || key == null || _Strings.isNullOrEmpty(value)) {
+        if(dto == null || key == null || _Strings.isNullOrEmpty(value))
             return;
-        }
         final MapDto userData = userDataFor(dto);
         CommonDtoUtils.putMapKeyValue(userData, key, value);
     }
 
     public void setUserData(
             final CommandDto dto, final String key, final Bookmark bookmark) {
-        if(dto == null || key == null || bookmark == null) {
+        if(dto == null || key == null || bookmark == null)
             return;
-        }
         setUserData(dto, key, bookmark.toString());
     }
 
     public void clearUserData(
             final CommandDto dto, final String key) {
-        if(dto == null || key == null) {
+        if(dto == null || key == null)
             return;
-        }
         userDataFor(dto).getEntry().removeIf(x -> x.getKey().equals(key));
     }
 
@@ -117,4 +129,41 @@ private MapDto userDataFor(final CommandDto commandDto) {
         return userData;
     }
 
+    // -- YAML SUPPORT
+
+    public String toYaml(final Iterable<CommandDto> commandDtos) {
+        return YamlUtils.toStringUtf8(
+            _NullSafe.stream(commandDtos)
+                .collect(Collectors.toList()),
+            ((JacksonCustomizer) JsonUtils::jaxbAnnotationSupport)
+                .andThen((JacksonCustomizer) CommandDtoUtils::memberDtoSupport)
+                .andThen((JacksonCustomizer) JsonUtils::onlyIncludeNonNull)
+                ::accept);
+    }
+
+    public List<CommandDto> fromYaml(final DataSource commandDtosYaml) {
+        return YamlUtils.tryReadAsList(CommandDto.class, commandDtosYaml,
+                ((JacksonCustomizer) JsonUtils::jaxbAnnotationSupport)
+                .andThen((JacksonCustomizer) CommandDtoUtils::memberDtoSupport)
+                ::accept)
+            .ifFailureFail()
+            .getValue()
+            .orElseGet(Collections::emptyList);
+    }
+
+    // Mix-in to add type metadata to MemberDto
+    @JsonTypeInfo(
+            use = JsonTypeInfo.Id.NAME,
+            include = JsonTypeInfo.As.PROPERTY,
+            property = "type")
+    private abstract class AbstractDtoMixIn {}
+
+    private void memberDtoSupport(final MapperBuilder<?, ?> mb) {
+        // add mix-in so MemberDto carries @JsonTypeInfo without modifying source
+        mb.addMixIn(MemberDto.class, AbstractDtoMixIn.class);
+        // register concrete sub-types with logical names
+        mb.registerSubtypes(new NamedType(ActionDto.class, "ACT"));
+        mb.registerSubtypes(new NamedType(PropertyDto.class, "PROP"));
+    }
+
 }
diff --git a/commons/src/main/java/org/apache/causeway/commons/io/YamlUtils.java b/commons/src/main/java/org/apache/causeway/commons/io/YamlUtils.java
index 46198a578d3..ce0c4ec5159 100644
--- a/commons/src/main/java/org/apache/causeway/commons/io/YamlUtils.java
+++ b/commons/src/main/java/org/apache/causeway/commons/io/YamlUtils.java
@@ -19,6 +19,7 @@
 package org.apache.causeway.commons.io;
 
 import java.io.InputStream;
+import java.util.List;
 import java.util.Optional;
 import java.util.function.Consumer;
 
@@ -74,10 +75,23 @@ public <T> Try<T> tryRead(
             final @NonNull Class<T> mappedType,
             final @NonNull DataSource source,
             final JsonUtils.JacksonCustomizer ... customizers) {
-        return source.tryReadAll((final InputStream is)->{
-            return Try.call(()->createJacksonReader(Optional.empty(), customizers)
-                    .readValue(is, mappedType));
-        });
+        return source.tryReadAll((final InputStream is) -> Try.call(()->createJacksonReader(Optional.empty(), customizers)
+                .readValue(is, mappedType)));
+    }
+
+    /**
+     * Tries to deserialize YAML content from given {@link DataSource} into a {@link List}
+     * with given {@code elementType}.
+     */
+    public <T> Try<List<T>> tryReadAsList(
+            final @NonNull Class<T> elementType,
+            final @NonNull DataSource source,
+            final JsonUtils.JacksonCustomizer ... customizers) {
+        return source.tryReadAll((final InputStream is) -> Try.call(()->{
+            var mapper = createJacksonReader(Optional.empty(), customizers);
+            var collectionType = mapper.getTypeFactory().constructCollectionType(List.class, elementType);
+            return mapper.readValue(is, collectionType);
+        }));
     }
 
     /**
@@ -101,10 +115,24 @@ public <T> Try<T> tryReadCustomized(
             final @NonNull DataSource source,
             final @NonNull YamlLoadCustomizer loadCustomizer,
             final JsonUtils.JacksonCustomizer ... customizers) {
-        return source.tryReadAll((final InputStream is)->{
-            return Try.call(()->createJacksonReader(Optional.of(loadCustomizer), customizers)
-                    .readValue(is, mappedType));
-        });
+        return source.tryReadAll((final InputStream is) -> Try.call(()->createJacksonReader(Optional.of(loadCustomizer), customizers)
+                .readValue(is, mappedType)));
+    }
+
+    /**
+     * Tries to deserialize YAML content from given {@link DataSource} into a {@link List}
+     * with given {@code elementType}.
+     */
+    public <T> Try<List<T>> tryReadAsListCustomized(
+            final @NonNull Class<T> elementType,
+            final @NonNull DataSource source,
+            final @NonNull YamlLoadCustomizer loadCustomizer,
+            final JsonUtils.JacksonCustomizer ... customizers) {
+        return source.tryReadAll((final InputStream is) -> Try.call(()->{
+            var mapper = createJacksonReader(Optional.of(loadCustomizer), customizers);
+            var collectionType = mapper.getTypeFactory().constructCollectionType(List.class, elementType);
+            return mapper.readValue(is, collectionType);
+        }));
     }
 
     // -- WRITING
@@ -188,7 +216,7 @@ private YAMLMapper createJacksonReader(
                         .map(YamlUtils::createLoadSettings)
                         .orElseGet(YamlUtils::createLoadSettings))
                 .build();
-        
+
         var builder = YAMLMapper.builder(yamlFactory);
         JsonUtils.readingJavaTimeSupport(builder);
         JsonUtils.readingCanSupport(builder);
@@ -210,7 +238,7 @@ private YAMLMapper createJacksonWriter(
                         .map(YamlUtils::createDumpSettings)
                         .orElseGet(YamlUtils::createDumpSettings))
                 .build();
-        
+
         var builder = YAMLMapper.builder(yamlFactory);
         JsonUtils.writingJavaTimeSupport(builder);
         JsonUtils.writingCanSupport(builder);
diff --git a/core/mmtest/src/test/java/org/apache/causeway/mmtest/schema/CommandDtoYamlRoundtripTest.java b/core/mmtest/src/test/java/org/apache/causeway/mmtest/schema/CommandDtoYamlRoundtripTest.java
new file mode 100644
index 00000000000..06d95152251
--- /dev/null
+++ b/core/mmtest/src/test/java/org/apache/causeway/mmtest/schema/CommandDtoYamlRoundtripTest.java
@@ -0,0 +1,103 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.causeway.mmtest.schema;
+
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.util.List;
+import java.util.UUID;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.causeway.applib.jaxb.JavaSqlXMLGregorianCalendarMarshalling;
+import org.apache.causeway.applib.util.schema.CommandDtoUtils;
+import org.apache.causeway.commons.io.DataSource;
+import org.apache.causeway.schema.cmd.v2.ActionDto;
+import org.apache.causeway.schema.cmd.v2.CommandDto;
+import org.apache.causeway.schema.cmd.v2.ParamDto;
+import org.apache.causeway.schema.cmd.v2.ParamsDto;
+import org.apache.causeway.schema.common.v2.OidDto;
+import org.apache.causeway.schema.common.v2.OidsDto;
+import org.apache.causeway.schema.common.v2.ValueType;
+
+class CommandDtoYamlRoundtripTest {
+
+    @Test
+    void test() {
+        var yaml = CommandDtoUtils.toYaml(List.of(commandDtoSample(), commandDtoSample()));
+        var afterRoundtrip = CommandDtoUtils.fromYaml(DataSource.ofStringUtf8(yaml));
+        assertEquals(yaml, CommandDtoUtils.toYaml(afterRoundtrip));
+    }
+
+    private CommandDto commandDtoSample() {
+
+        var commandDto = new CommandDto();
+        commandDto.setInteractionId(UUID.randomUUID().toString());
+        commandDto.setMajorVersion("2");
+        commandDto.setMinorVersion("0");
+        commandDto.setUsername("sven");
+
+        long instant = Instant.now().toEpochMilli();
+
+        var timestamp1 = new Timestamp(instant);
+        var timestamp2 = new Timestamp(instant + 1234L);
+        var timings = CommandDtoUtils.timingsFor(commandDto);
+        timings.setStartedAt(JavaSqlXMLGregorianCalendarMarshalling.toXMLGregorianCalendar(timestamp1));
+        timings.setCompletedAt(JavaSqlXMLGregorianCalendarMarshalling.toXMLGregorianCalendar(timestamp2));
+        commandDto.setTimestamp(JavaSqlXMLGregorianCalendarMarshalling.toXMLGregorianCalendar(timestamp1));
+
+        var action = new ActionDto();
+        commandDto.setMember(action);
+        action.setInteractionType(null);
+        action.setLogicalMemberIdentifier(null);
+
+        var params = new ParamsDto();
+        action.setParameters(params);
+
+        var param1 = new ParamDto();
+        params.getParameter().add(param1);
+
+        var ref = new OidDto();
+        param1.setReference(ref);
+        param1.setName("entity");
+        param1.setType(ValueType.REFERENCE);
+        ref.setType("demo.ActionCommandPublishingEntity");
+        ref.setId("63");
+
+        var param2 = new ParamDto();
+        params.getParameter().add(param2);
+
+        param2.setString("Monica2");
+        param2.setName("newName");
+        param2.setType(ValueType.STRING);
+
+        var targets = new OidsDto();
+        commandDto.setTargets(targets);
+
+        var target = new OidDto();
+        targets.getOid().add(target);
+        target.setType("demo.ActionCommandPublishingPage");
+        target.setId("PAAAg94peru5Z0Cw2FOcp1qflAATk66cZj_bmXAeSadQ1nHY8P3htbCB2ZXJzaW9uPSIxLjAiIGVuY29kaW5nPSJVVEYtOCI_Pjxyb290Lz4K");
+
+        return commandDto;
+    }
+
+}
diff --git a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/dom/ReplayState.java b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/dom/ReplayState.java
index 30a137e1aa3..d9c933dbaed 100644
--- a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/dom/ReplayState.java
+++ b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/dom/ReplayState.java
@@ -29,9 +29,13 @@
  */
 public enum ReplayState {
     /**
-     * As used on primary system.
+     * As used on primary system, indicating an initial state.
      */
     UNDEFINED,
+    /**
+     * Marks a {@link CommandLogEntry} as exported, such that consecutive export actions will skip those.
+     */
+    EXPORTED,
     /**
      * For use on secondary system, indicates that the command has not yet been replayed.
      */