Skip to content

Commit 6fb197e

Browse files
committed
fix(webUI): fixed the ability to specify function's return value before performing a command
Closes: #65
1 parent 16422f4 commit 6fb197e

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

src/main/java/ru/ewc/checklogic/FileStateFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public State initialState() {
7171
return new State(this.locators);
7272
}
7373

74+
@Override
75+
public boolean functionSpecified(final String arg) {
76+
return ((FunctionsLocator) new State(this.locators)
77+
.locatorFor(this.config.functionsLocatorName())).functionSpecified(arg);
78+
}
79+
7480
private void loadFunctionsLocator() {
7581
this.locators.add(
7682
new FunctionsLocator(

src/main/java/ru/ewc/checklogic/MockStateFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ public State initialState() {
4444
);
4545
}
4646

47+
@Override
48+
public boolean functionSpecified(final String arg) {
49+
return false;
50+
}
51+
4752
}

src/main/java/ru/ewc/checklogic/ServerInstance.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.nio.file.Paths;
2929
import java.util.List;
3030
import java.util.Map;
31+
import lombok.Getter;
3132
import ru.ewc.decisions.api.ComputationContext;
3233
import ru.ewc.decisions.api.DecisionTables;
3334
import ru.ewc.decisions.api.DecitaException;
@@ -45,6 +46,7 @@ public final class ServerInstance {
4546
/**
4647
* The root path for the external business logic resources.
4748
*/
49+
@Getter
4850
private final String root;
4951

5052
/**
@@ -130,28 +132,20 @@ public boolean isAvailable(final String command, final String field) {
130132
}
131133

132134
public void update(final List<String> values) {
133-
final InMemoryLocator request = InMemoryLocator.empty(this.requestLocatorName());
135+
final InMemoryLocator request = InMemoryLocator.empty(this.server.requestLocatorName());
134136
values.forEach(
135137
value -> {
136138
final String[] split = value.split(":");
137139
request.setFragmentValue(split[0].trim(), split[1].trim());
138140
});
139-
this.state.locators().put(this.requestLocatorName(), request);
141+
this.state.locators().put(this.server.requestLocatorName(), request);
140142
this.context = new ComputationContext(this.state, this.getAllTables());
141143
}
142144

143-
public String requestLocatorName() {
144-
return this.server.requestLocatorName();
145-
}
146-
147145
public boolean isEmpty() {
148146
return this.state.locators().isEmpty();
149147
}
150148

151-
public String getRoot() {
152-
return this.root;
153-
}
154-
155149
public void initialize() {
156150
this.state = this.states.initialState();
157151
this.context = new ComputationContext(this.state, this.getAllTables());
@@ -166,6 +160,13 @@ public void createTestFolder() {
166160
this.context = new ComputationContext(this.state, this.getAllTables());
167161
}
168162

163+
public boolean isNotSpecified(final String arg) {
164+
final String[] args = arg.split("::");
165+
final boolean function = this.server.functionsLocatorName().equals(args[0]);
166+
final boolean request = this.server.requestLocatorName().equals(args[0]);
167+
return function && !this.states.functionSpecified(args[1]) || request;
168+
}
169+
169170
private DecisionTables getAllTables() {
170171
return DecisionTables.using(new CombinedCsvFileReader(this.tables, ".csv", ";"));
171172
}

src/main/java/ru/ewc/checklogic/StateFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@
3434
*/
3535
interface StateFactory {
3636
State initialState();
37+
38+
boolean functionSpecified(String arg);
3739
}

src/main/java/ru/ewc/checklogic/server/CommandMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public String namesAsHtmlList(final ServerInstance computation, final String out
8282
public String commandArgsAsHtmlForm(final String command, final ServerInstance context) {
8383
return this.metadata.get(command).stream()
8484
.distinct()
85-
.filter(arg -> context.requestLocatorName().equals(arg.split("::")[0]))
85+
.filter(context::isNotSpecified)
8686
.map(
8787
arg -> new StringBuilder()
8888
.append("<div class='mb-3'>")

src/main/java/ru/ewc/checklogic/testing/FunctionsLocator.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public final class FunctionsLocator implements Locator {
4646
*/
4747
private final String name;
4848

49+
/**
50+
* The path to the Groovy scripts.
51+
*/
52+
private final Path path;
53+
4954
/**
5055
* The in-memory locator used to store overridden values.
5156
*/
@@ -59,6 +64,7 @@ public final class FunctionsLocator implements Locator {
5964
@SneakyThrows
6065
public FunctionsLocator(final String name, final Path path) {
6166
this.name = name;
67+
this.path = path;
6268
this.locator = new InMemoryLocator("locator", new HashMap<>());
6369
this.engine = new GroovyScriptEngine(new URL[]{path.toUri().toURL()});
6470
}
@@ -72,10 +78,12 @@ public String fragmentBy(
7278
final String result;
7379
if (this.locator.state().containsKey(fragment)) {
7480
result = this.locator.fragmentBy(fragment, context);
75-
} else {
81+
} else if (this.hasScript(fragment)) {
7682
final Binding binding = new Binding();
7783
binding.setVariable("context", context);
7884
result = this.engine.run("%s.groovy".formatted(fragment), binding).toString();
85+
} else {
86+
result = "undefined";
7987
}
8088
return result;
8189
}
@@ -89,4 +97,16 @@ public void setFragmentValue(final String fragment, final String value) {
8997
public String locatorName() {
9098
return this.name;
9199
}
100+
101+
public boolean functionSpecified(final String arg) {
102+
return this.hasValue(arg) || this.hasScript(arg);
103+
}
104+
105+
private boolean hasScript(final String arg) {
106+
return this.path.resolve("%s.groovy".formatted(arg)).toFile().exists();
107+
}
108+
109+
private boolean hasValue(final String arg) {
110+
return !"undefined".equals(this.locator.state().getOrDefault(arg, "undefined"));
111+
}
92112
}

0 commit comments

Comments
 (0)