From 04f4fed0bae2759db4ac31302209af6b8ee2dc4d Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 29 Nov 2017 16:55:34 +0100 Subject: [PATCH] [New] Added toJson() to ServiceResult, ModelStatistics --- .../li/strolch/service/api/ServiceResult.java | 47 ++++++------------- .../li/strolch/model/ModelStatistics.java | 17 ++++++- .../executor/ServiceExecutionStatus.java | 15 ++++-- 3 files changed, 41 insertions(+), 38 deletions(-) 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.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