|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.causeway.extensions.commandlog.applib.dom.replay; |
| 20 | + |
| 21 | +import java.util.Optional; |
| 22 | +import java.util.UUID; |
| 23 | + |
| 24 | +import jakarta.inject.Inject; |
| 25 | +import jakarta.inject.Named; |
| 26 | + |
| 27 | +import org.apache.causeway.applib.ViewModel; |
| 28 | +import org.apache.causeway.applib.annotation.DomainObject; |
| 29 | +import org.apache.causeway.applib.annotation.DomainObjectLayout; |
| 30 | +import org.apache.causeway.applib.annotation.Introspection; |
| 31 | +import org.apache.causeway.applib.annotation.ObjectSupport; |
| 32 | +import org.apache.causeway.applib.annotation.Property; |
| 33 | +import org.apache.causeway.applib.services.bookmark.Bookmark; |
| 34 | +import org.apache.causeway.applib.services.bookmark.BookmarkService; |
| 35 | +import org.apache.causeway.commons.internal.base._StableValue; |
| 36 | +import org.apache.causeway.commons.internal.exceptions._Exceptions; |
| 37 | +import org.apache.causeway.extensions.commandlog.applib.CausewayModuleExtCommandLogApplib; |
| 38 | +import org.apache.causeway.extensions.commandlog.applib.dom.CommandLogEntry; |
| 39 | +import org.apache.causeway.extensions.commandlog.applib.dom.CommandLogEntryRepository; |
| 40 | +import org.apache.causeway.extensions.commandlog.applib.dom.ReplayState; |
| 41 | +import org.apache.causeway.schema.cmd.v2.CommandDto; |
| 42 | + |
| 43 | +/** |
| 44 | + * Viewmodel that wraps a {@link CommandLogEntry}. |
| 45 | + */ |
| 46 | +@DomainObject(introspection = Introspection.ANNOTATION_REQUIRED) |
| 47 | +@DomainObjectLayout(cssClassFa = "terminal") |
| 48 | +@Named(ReplayableCommand.LOGICAL_TYPE_NAME) |
| 49 | +public record ReplayableCommand( |
| 50 | + String commandLogEntryId, |
| 51 | + BookmarkService bookmarkService, |
| 52 | + CommandLogEntryRepository commandLogEntryRepository, |
| 53 | + _StableValue<CommandRecord> recordRef) implements ViewModel { |
| 54 | + |
| 55 | + public static final String LOGICAL_TYPE_NAME = CausewayModuleExtCommandLogApplib.NAMESPACE + ".ReplayableCommand"; |
| 56 | + |
| 57 | + // decouple from the underlying entity |
| 58 | + record CommandRecord( |
| 59 | + CommandDto commandDto, |
| 60 | + ReplayState replayState) { |
| 61 | + } |
| 62 | + |
| 63 | + @Inject |
| 64 | + public ReplayableCommand( |
| 65 | + final String memento, |
| 66 | + final BookmarkService bookmarkService, |
| 67 | + final CommandLogEntryRepository commandLogEntryRepository) { |
| 68 | + this(memento, bookmarkService, commandLogEntryRepository, new _StableValue<>()); |
| 69 | + } |
| 70 | + |
| 71 | + @ObjectSupport public String title() { |
| 72 | + return "Replayable Command"; |
| 73 | + } |
| 74 | + |
| 75 | + @Property |
| 76 | + public UUID getInteractionId() { |
| 77 | + return commandRecord() |
| 78 | + .map(CommandRecord::commandDto) |
| 79 | + .map(CommandDto::getInteractionId) |
| 80 | + .map(UUID::fromString) |
| 81 | + .orElse(null); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public String viewModelMemento() { |
| 86 | + return commandLogEntryId; |
| 87 | + } |
| 88 | + |
| 89 | + // -- HELPER |
| 90 | + |
| 91 | + private Optional<CommandRecord> commandRecord() { |
| 92 | + return commandLogEntry() |
| 93 | + .map(commandLogEntry->new CommandRecord(commandLogEntry.getCommandDto(), commandLogEntry.getReplayState())); |
| 94 | + } |
| 95 | + |
| 96 | + private Optional<CommandLogEntry> commandLogEntry() { |
| 97 | + return bookmarkService.lookup(commandLogEntryBookmark(), CommandLogEntry.class); |
| 98 | + } |
| 99 | + |
| 100 | + private Bookmark commandLogEntryBookmark() { |
| 101 | + return bookmarkService.bookmarkFor(CommandLogEntry.class, commandLogEntryId) |
| 102 | + .orElseThrow(()->_Exceptions.unrecoverable( |
| 103 | + "framework error: cannot create bookmark for CommandLogEntry using id '%s'", |
| 104 | + commandLogEntryId)); |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments