diff --git a/li.strolch.agent/src/main/java/li/strolch/service/api/ServiceResult.java b/li.strolch.agent/src/main/java/li/strolch/service/api/ServiceResult.java index fc58fcff5..817bb2435 100644 --- a/li.strolch.agent/src/main/java/li/strolch/service/api/ServiceResult.java +++ b/li.strolch.agent/src/main/java/li/strolch/service/api/ServiceResult.java @@ -17,11 +17,15 @@ package li.strolch.service.api; import java.io.Serializable; +import com.google.gson.JsonObject; +import li.strolch.utils.helper.ExceptionHelper; + /** * @author Robert von Burg */ public class ServiceResult implements Serializable { private static final long serialVersionUID = 1L; + private ServiceResultState state; private String message; private Throwable throwable; @@ -30,27 +34,15 @@ public class ServiceResult implements Serializable { // } - /** - * @param state - */ public ServiceResult(ServiceResultState state) { this.state = state; } - /** - * @param state - * @param message - */ public ServiceResult(ServiceResultState state, String message) { this.state = state; this.message = message; } - /** - * @param state - * @param message - * @param throwable - */ public ServiceResult(ServiceResultState state, String message, Throwable throwable) { this.state = state; this.message = message; @@ -71,32 +63,18 @@ public class ServiceResult implements Serializable { return this.state != ServiceResultState.SUCCESS; } - /** - * @return the state - */ public ServiceResultState getState() { return this.state; } - /** - * @param state - * the state to set - */ public void setState(ServiceResultState state) { this.state = state; } - /** - * @return the message - */ public String getMessage() { return this.message; } - /** - * @param message - * the message to set - */ public void setMessage(String message) { this.message = message; } @@ -122,17 +100,10 @@ public class ServiceResult implements Serializable { return t; } - /** - * @return the throwable - */ public Throwable getThrowable() { return this.throwable; } - /** - * @param throwable - * the throwable to set - */ public void setThrowable(Throwable throwable) { this.throwable = throwable; } @@ -160,4 +131,14 @@ public class ServiceResult implements Serializable { public static ServiceResult failed(String error, Throwable t) { return new ServiceResult(ServiceResultState.FAILED, error, t); } + + public JsonObject toJson() { + JsonObject json = new JsonObject(); + + json.addProperty("state", state.name()); + json.addProperty("message", message); + json.addProperty("throwable", ExceptionHelper.formatException(throwable)); + + return json; + } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/ModelStatistics.java b/li.strolch.model/src/main/java/li/strolch/model/ModelStatistics.java index a5564afd6..18a978e99 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/ModelStatistics.java +++ b/li.strolch.model/src/main/java/li/strolch/model/ModelStatistics.java @@ -19,6 +19,7 @@ import static li.strolch.utils.helper.StringHelper.NULL; import java.util.Date; +import com.google.gson.JsonObject; import li.strolch.utils.helper.StringHelper; import li.strolch.utils.iso8601.ISO8601FormatFactory; @@ -64,7 +65,8 @@ public class ModelStatistics { /** * Adds the statistics of the other statistics to this statistics instance * - * @param statistics further statistics to add to this {@link ModelStatistics} + * @param statistics + * further statistics to add to this {@link ModelStatistics} */ public void add(ModelStatistics statistics) { this.nrOfOrders += statistics.nrOfOrders; @@ -89,4 +91,17 @@ public class ModelStatistics { builder.append("]"); return builder.toString(); } + + public JsonObject toJson() { + JsonObject json = new JsonObject(); + + json.addProperty("startTime", + this.startTime == null ? NULL : ISO8601FormatFactory.getInstance().formatDate(this.startTime)); + json.addProperty("durationNanos", durationNanos); + json.addProperty("nrOfResources", nrOfResources); + json.addProperty("nrOfOrders", nrOfOrders); + json.addProperty("nrOfActivities", nrOfActivities); + + return json; + } } \ No newline at end of file diff --git a/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java b/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java index 4b4a1a636..8b5920ffb 100644 --- a/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java +++ b/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java @@ -7,6 +7,7 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import java.util.List; import java.util.function.Function; +import java.util.stream.Collectors; import com.google.gson.Gson; import com.google.gson.JsonArray; @@ -63,6 +64,10 @@ public class ResponseUtil { return Response.ok(json, MediaType.APPLICATION_JSON).build(); } + public static Response toResponse(String member, T t, Function toJson) { + return toResponse(member, toJson.apply(t)); + } + public static Response toResponse(String member, JsonElement jsonElement) { JsonObject response = new JsonObject(); response.addProperty(MSG, StringHelper.DASH); @@ -73,6 +78,10 @@ public class ResponseUtil { return Response.ok(json, MediaType.APPLICATION_JSON).build(); } + public static Response toResponse(String member, List list, Function toJson) { + return toResponse(member, list.stream().map(toJson).collect(Collectors.toList())); + } + public static Response toResponse(String member, List jsonObjects) { JsonObject response = new JsonObject(); response.addProperty(MSG, StringHelper.DASH); diff --git a/li.strolch.service/src/main/java/li/strolch/service/executor/ServiceExecutionStatus.java b/li.strolch.service/src/main/java/li/strolch/service/executor/ServiceExecutionStatus.java index 6b8822741..c39562ff6 100644 --- a/li.strolch.service/src/main/java/li/strolch/service/executor/ServiceExecutionStatus.java +++ b/li.strolch.service/src/main/java/li/strolch/service/executor/ServiceExecutionStatus.java @@ -15,6 +15,7 @@ */ package li.strolch.service.executor; +import com.google.gson.JsonObject; import li.strolch.service.api.ServiceResult; import li.strolch.utils.helper.StringHelper; @@ -27,10 +28,6 @@ public class ServiceExecutionStatus { private volatile boolean started; private volatile ServiceResult result; - public ServiceExecutionStatus() { - // no arg constructor for JAXB - } - public ServiceExecutionStatus(String serviceName) { this.serviceName = serviceName; } @@ -72,4 +69,14 @@ public class ServiceExecutionStatus { public synchronized void started() { this.started = true; } + + public JsonObject toJson() { + JsonObject json = new JsonObject(); + + json.addProperty("serviceName", serviceName); + json.addProperty("started", started); + json.add("result", result.toJson()); + + return json; + } } \ No newline at end of file