Skip to content

Commit 80b2a82

Browse files
committed
Support boolean arguments in formio.
1 parent 36b7d8c commit 80b2a82

File tree

7 files changed

+544
-17
lines changed

7 files changed

+544
-17
lines changed

query-engine/docs/query-engine-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ services:
5959
- 14268:14268
6060

6161
query-engine:
62-
image: ghcr.io/yaytay/query-engine-design-mode:0.0.39-3-main
62+
image: ghcr.io/yaytay/query-engine-design-mode:0.0.40-main
6363
ports:
6464
- 2000:8080
6565
volumes:

query-engine/src/main/java/uk/co/spudsoft/query/main/Version.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public final class Version {
3434
/**
3535
* The project version, as set in the Maven pom.xml.
3636
*/
37-
public static final String MAVEN_PROJECT_VERSION = "0.0.39-3-main";
37+
public static final String MAVEN_PROJECT_VERSION = "0.0.40-main";
3838

3939
private Version() {
4040
}

query-engine/src/main/java/uk/co/spudsoft/query/web/LoginRouter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import io.vertx.core.http.HttpMethod;
2929
import io.vertx.core.http.HttpServerRequest;
3030
import io.vertx.core.json.JsonObject;
31-
import io.vertx.ext.auth.VertxContextPRNG;
31+
import io.vertx.ext.auth.prng.VertxContextPRNG;
3232
import io.vertx.ext.web.RoutingContext;
3333
import io.vertx.ext.web.client.WebClient;
3434
import java.net.URLEncoder;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2023 jtalbut
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package uk.co.spudsoft.query.web.formio;
18+
19+
import com.fasterxml.jackson.core.JsonGenerator;
20+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21+
import java.io.IOException;
22+
23+
/**
24+
* Output a formio TextField component.
25+
*
26+
* @author jtalbut
27+
*/
28+
public class CheckBox extends Component<CheckBox> {
29+
30+
/**
31+
* Constructor.
32+
*
33+
* @param generator The Jackson JsonGenerator for FormIO.
34+
* @throws IOException if something goes wrong.
35+
*/
36+
@SuppressFBWarnings({"EI_EXPOSE_REP2", "CT_CONSTRUCTOR_THROW"})
37+
public CheckBox(JsonGenerator generator) throws IOException {
38+
super(generator, "checkbox");
39+
}
40+
41+
}

query-engine/src/main/java/uk/co/spudsoft/query/web/formio/FormBuilder.java

+42-14
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import uk.co.spudsoft.query.defn.Argument;
3636
import uk.co.spudsoft.query.defn.DataType;
3737
import uk.co.spudsoft.query.defn.ArgumentValue;
38+
import static uk.co.spudsoft.query.defn.DataType.Null;
3839
import uk.co.spudsoft.query.defn.Format;
3940
import uk.co.spudsoft.query.exec.FilterFactory;
4041
import uk.co.spudsoft.query.exec.conditions.ConditionInstance;
@@ -169,25 +170,22 @@ void buildArguments(JsonGenerator generator, PipelineFile pipeline) throws IOExc
169170

170171
private void buildArgument(Argument arg, JsonGenerator generator) throws IOException, IllegalStateException {
171172
switch (arg.getType()) {
172-
case Date:
173-
case Time:
174-
case DateTime:
175-
buildDateTime(generator, arg);
176-
break;
177-
case Double:
178-
case Integer:
179-
case Long:
180-
buildNumber(generator, arg);
181-
break;
182-
case String:
173+
case Date, Time, DateTime -> buildDateTime(generator, arg);
174+
case Double, Integer, Long, Float -> buildNumber(generator, arg);
175+
case String -> {
183176
if (!isNullOrEmpty(arg.getPossibleValues()) || !Strings.isNullOrEmpty(arg.getPossibleValuesUrl())) {
184177
buildSelect(generator, arg);
185178
} else {
186179
buildTextField(generator, arg);
187180
}
188-
break;
189-
default:
190-
throw new IllegalStateException("New types added to DataType and not implemented here");
181+
}
182+
case Boolean -> buildCheckBox(generator, arg);
183+
case Null -> {
184+
logger.warn("Argument {} is of null type", arg.getName());
185+
}
186+
default -> {
187+
logger.warn("Argument {} is of unknown type ({})", arg.getName(), arg.getType());
188+
}
191189
}
192190
}
193191

@@ -355,6 +353,36 @@ static LocalDateTime parseToLocalDateTime(String value) {
355353
}
356354
}
357355

356+
void buildCheckBox(JsonGenerator generator, Argument arg) throws IOException {
357+
try (CheckBox checkbox = new CheckBox(generator)) {
358+
checkbox
359+
.withLabel(arg.getTitle())
360+
.withPlaceholder(arg.getPrompt())
361+
.withDescription(arg.getDescription())
362+
.withKey(arg.getName())
363+
.withMultiple(arg.isMultiValued())
364+
;
365+
if (!Strings.isNullOrEmpty(arg.getDefaultValueExpression())) {
366+
JexlEvaluator evaluator = new JexlEvaluator(arg.getDefaultValueExpression());
367+
Object result = evaluator.evaluateAsObject(requestContext, null);
368+
if (result != null) {
369+
try {
370+
Boolean defaultBoolean = (Boolean) DataType.Boolean.cast(result);
371+
if (defaultBoolean != null) {
372+
checkbox.withDefaultValue(defaultBoolean.toString());
373+
}
374+
} catch (Throwable ex) {
375+
checkbox.withDefaultValue(result.toString());
376+
}
377+
}
378+
}
379+
380+
try (Validation v = checkbox.addValidate()) {
381+
v.withRequired(!arg.isOptional());
382+
}
383+
}
384+
}
385+
358386
void buildDateTime(JsonGenerator generator, Argument arg) throws IOException {
359387
try (DateTime dateTime = new DateTime(generator)) {
360388
dateTime

0 commit comments

Comments
 (0)