From 0db98efd9f7cff6e71b082218b8e11668fe85b1a Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Tue, 27 Aug 2019 08:32:08 +0200 Subject: [PATCH] [New] Extended ServiceResult to have an i18n part --- .../strolch/service/api/AbstractService.java | 48 ++++++++++++------ .../li/strolch/service/api/ServiceResult.java | 50 ++++++++++++++++--- 2 files changed, 77 insertions(+), 21 deletions(-) diff --git a/li.strolch.agent/src/main/java/li/strolch/service/api/AbstractService.java b/li.strolch.agent/src/main/java/li/strolch/service/api/AbstractService.java index 9f05b8b2b..b52419518 100644 --- a/li.strolch.agent/src/main/java/li/strolch/service/api/AbstractService.java +++ b/li.strolch.agent/src/main/java/li/strolch/service/api/AbstractService.java @@ -147,6 +147,24 @@ public abstract class AbstractService V runWithResult(String username, SystemActionWithResult action) throws PrivilegeException, Exception { - return this.container.getPrivilegeHandler().runWithResult(username, action); + return getPrivilegeHandler().runWithResult(username, action); } /** @@ -402,7 +420,7 @@ public abstract class AbstractService V runWithResult(String username, PrivilegedRunnableWithResult runnable) throws PrivilegeException, Exception { - return this.container.getPrivilegeHandler().runWithResult(username, runnable); + return getPrivilegeHandler().runWithResult(username, runnable); } /** @@ -437,7 +455,7 @@ public abstract class AbstractService V runAsAgentWithResult(SystemActionWithResult action) throws PrivilegeException, Exception { - return this.container.getPrivilegeHandler().runAsAgentWithResult(action); + return getPrivilegeHandler().runAsAgentWithResult(action); } /** @@ -470,7 +488,7 @@ public abstract class AbstractService V runAsAgentWithResult(PrivilegedRunnableWithResult runnable) throws PrivilegeException, Exception { - return this.container.getPrivilegeHandler().runAsAgentWithResult(runnable); + return getPrivilegeHandler().runAsAgentWithResult(runnable); } /** 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 b4f318f94..e64f4e61b 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 @@ -1,12 +1,12 @@ /* * Copyright 2013 Robert von Burg - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,7 +15,13 @@ */ package li.strolch.service.api; +import static li.strolch.utils.helper.ExceptionHelper.formatException; + +import java.util.ResourceBundle; + import com.google.gson.JsonObject; +import li.strolch.model.i18n.I18nMessageToJsonVisitor; +import li.strolch.utils.I18nMessage; import li.strolch.utils.helper.ExceptionHelper; /** @@ -27,6 +33,8 @@ public class ServiceResult { private String message; private Throwable throwable; + private I18nMessage i18nMessage; + public ServiceResult() { // } @@ -129,12 +137,42 @@ public class ServiceResult { return new ServiceResult(ServiceResultState.FAILED, error, t); } + @SuppressWarnings("unchecked") + public T i18n(ResourceBundle bundle, String key) { + this.i18nMessage = new I18nMessage(bundle, key); + return (T) this; + } + + @SuppressWarnings("unchecked") + public T i18n(ResourceBundle bundle, String key, String prop, Object value) { + this.i18nMessage = new I18nMessage(bundle, key).value(prop, value); + return (T) this; + } + + @SuppressWarnings("unchecked") + public T i18n(ResourceBundle bundle, String key, String prop1, Object value1, + String prop2, Object value2) { + this.i18nMessage = new I18nMessage(bundle, key).value(prop1, value1).value(prop2, value2); + return (T) this; + } + + public I18nMessage getI18nMessage() { + return this.i18nMessage; + } + public JsonObject toJson() { JsonObject json = new JsonObject(); - json.addProperty("state", state.name()); - json.addProperty("message", message); - json.addProperty("throwable", ExceptionHelper.formatException(throwable)); + json.addProperty("state", this.state.name()); + json.addProperty("msg", this.message); + + if (this.throwable != null) { + json.addProperty("exceptionMsg", ExceptionHelper.getExceptionMessageWithCauses(this.throwable)); + json.addProperty("throwable", formatException(this.throwable)); + } + + if (this.i18nMessage != null) + json.add("i18n", this.i18nMessage.accept(new I18nMessageToJsonVisitor())); return json; }