Skip to content

Commit

Permalink
feat(webUI): rendering state page with stored state
Browse files Browse the repository at this point in the history
Closes: #12
  • Loading branch information
nergal-perm committed May 17, 2024
1 parent 76fe0ed commit e6125a4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/main/java/ru/ewc/checklogic/Computation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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<String, String> stateFor(final String table, final Map<String, String> entities) {
final Locator locator = this.state.locatorFor(table);
final ComputationContext context = new ComputationContext(this.state);
Expand All @@ -132,6 +130,10 @@ public Computation withState(final Map<String, Map<String, Object>> incoming) {
return new Computation(this.decisions, this.commands, stateFrom(incoming));
}

public Map<String, Map<String, Object>> storedState() {
return this.state.state();
}

/**
* Loads the state from the specified {@code InputStream}.
*
Expand All @@ -155,5 +157,4 @@ private static Locators stateFrom(final Map<String, Map<String, Object>> stream)
private static Function<Map.Entry<String, Map<String, Object>>, Locator> entryToLocator() {
return e -> new InMemoryStorage(e.getValue());
}

}
4 changes: 2 additions & 2 deletions src/main/java/ru/ewc/checklogic/server/StatePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/ru/ewc/checklogic/server/StoredState.java
Original file line number Diff line number Diff line change
@@ -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<String, Map<String, Object>> state;

/**
* Ctor.
*
* @param state The state to be encapsulated.
*/
public StoredState(final Map<String, Map<String, Object>> state) {
this.state = state;
}

public String asHtmlList() {
return "<ul>%s</ul>".formatted(this.state.entrySet().stream()
.map(
entry -> "<li><h4>%s</h4>%s</li>".formatted(
entry.getKey(),
StoredState.toInnerList(entry.getValue())
)
)
.collect(Collectors.joining())
);
}

private static String toInnerList(final Map<String, Object> value) {
return "<ul>%s</ul>".formatted(value.entrySet().stream()
.map(entry -> "<li>%s: %s</li>".formatted(entry.getKey(), entry.getValue()))
.collect(Collectors.joining())
);
}
}

3 comments on commit e6125a4

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on e6125a4 May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 10-62ae1138 disappeared from src/main/java/ru/ewc/checklogic/Computation.java), that's why I closed #12. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on e6125a4 May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 12-1524dce0 disappeared from src/main/java/ru/ewc/checklogic/Computation.java), that's why I closed #14. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on e6125a4 May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 10-37a86df3 disappeared from src/main/java/ru/ewc/checklogic/server/StatePage.java), that's why I closed #13. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.