diff --git a/src/main/java/ru/ewc/checklogic/Computation.java b/src/main/java/ru/ewc/checklogic/Computation.java index ae48ea7..17ad395 100644 --- a/src/main/java/ru/ewc/checklogic/Computation.java +++ b/src/main/java/ru/ewc/checklogic/Computation.java @@ -53,7 +53,6 @@ public final class Computation { */ private final CommandsFacade commands; - // @todo #12 Expose the state of the Locators /** * The current state of the system. */ @@ -117,7 +116,6 @@ public boolean hasStateFor(final String table) { return this.state.hasLocator(table); } - // @todo #10 Get the whole stored State (no table computations, just stored values) public Map stateFor(final String table, final Map entities) { final Locator locator = this.state.locatorFor(table); final ComputationContext context = new ComputationContext(this.state); @@ -132,6 +130,10 @@ public Computation withState(final Map> incoming) { return new Computation(this.decisions, this.commands, stateFrom(incoming)); } + public Map> storedState() { + return this.state.state(); + } + /** * Loads the state from the specified {@code InputStream}. * @@ -155,5 +157,4 @@ private static Locators stateFrom(final Map> stream) private static Function>, Locator> entryToLocator() { return e -> new InMemoryStorage(e.getValue()); } - } diff --git a/src/main/java/ru/ewc/checklogic/server/StatePage.java b/src/main/java/ru/ewc/checklogic/server/StatePage.java index 84202bb..8f636bf 100644 --- a/src/main/java/ru/ewc/checklogic/server/StatePage.java +++ b/src/main/java/ru/ewc/checklogic/server/StatePage.java @@ -60,10 +60,10 @@ public StatePage(final Computation computation) { ); } - // @todo #10 Design the html for the state page public Response statePage(final Request request) { this.computation.perform(new Transition("initialize", new Locators(Map.of()))); - return Response.htmlOk(this.template.renderTemplate(Map.of("state", "No state yet"))); + final StoredState stored = new StoredState(this.computation.storedState()); + return Response.htmlOk(this.template.renderTemplate(Map.of("state", stored.asHtmlList()))); } private static String readFileFromResources(final String file) { diff --git a/src/main/java/ru/ewc/checklogic/server/StoredState.java b/src/main/java/ru/ewc/checklogic/server/StoredState.java new file mode 100644 index 0000000..9f644a2 --- /dev/null +++ b/src/main/java/ru/ewc/checklogic/server/StoredState.java @@ -0,0 +1,68 @@ +/* + * MIT License + * + * Copyright (c) 2024 Decision-Driven Development + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package ru.ewc.checklogic.server; + +import java.util.Map; +import java.util.stream.Collectors; + +/** + * I am the class encapsulating the stored state of the application. My main responsibility is to + * present that state in a human-readable form. + * + * @since 0.3.0 + */ +public final class StoredState { + /** + * The state to be encapsulated. + */ + private final Map> state; + + /** + * Ctor. + * + * @param state The state to be encapsulated. + */ + public StoredState(final Map> state) { + this.state = state; + } + + public String asHtmlList() { + return "
    %s
".formatted(this.state.entrySet().stream() + .map( + entry -> "
  • %s

    %s
  • ".formatted( + entry.getKey(), + StoredState.toInnerList(entry.getValue()) + ) + ) + .collect(Collectors.joining()) + ); + } + + private static String toInnerList(final Map value) { + return "
      %s
    ".formatted(value.entrySet().stream() + .map(entry -> "
  • %s: %s
  • ".formatted(entry.getKey(), entry.getValue())) + .collect(Collectors.joining()) + ); + } +}