[New] Extended ServiceResult to have an i18n part
This commit is contained in:
parent
44f7ed163e
commit
0db98efd9f
|
@ -147,6 +147,24 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
return this.container.getRealm(realm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Realm for the current certificate
|
||||
*
|
||||
* @return the realm
|
||||
*/
|
||||
protected StrolchRealm getRealm() {
|
||||
return this.container.getRealm(getCertificate());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the realm name for the current certificate
|
||||
*
|
||||
* @return the realm
|
||||
*/
|
||||
protected String getRealmName() {
|
||||
return this.container.getRealm(getCertificate()).getRealm();
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a {@link StrolchTransaction} for the given realm, the action for the TX is this implementation's class
|
||||
* name. This transaction should be used in a try-with-resource clause so it is properly closed
|
||||
|
@ -160,7 +178,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
* if the {@link StrolchRealm} does not exist with the given name
|
||||
*/
|
||||
protected StrolchTransaction openTx(String realm) throws StrolchException {
|
||||
return this.container.getRealm(realm).openTx(getCertificate(), getClass(), false);
|
||||
return getRealm(realm).openTx(getCertificate(), getClass(), false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -178,7 +196,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
* if the {@link StrolchRealm} does not exist with the given name
|
||||
*/
|
||||
protected StrolchTransaction openTx(String realm, boolean readOnly) throws StrolchException {
|
||||
return this.container.getRealm(realm).openTx(getCertificate(), getClass(), readOnly);
|
||||
return getRealm(realm).openTx(getCertificate(), getClass(), readOnly);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -283,7 +301,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
* if the {@link StrolchRealm} does not exist with the given name
|
||||
*/
|
||||
protected StrolchTransaction openTx(String realm, String action, boolean readOnly) throws StrolchException {
|
||||
return this.container.getRealm(realm).openTx(getCertificate(), action, readOnly);
|
||||
return getRealm(realm).openTx(getCertificate(), action, readOnly);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -301,7 +319,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
* if the {@link StrolchRealm} does not exist with the given name
|
||||
*/
|
||||
protected StrolchTransaction openTx(String realm, String action) throws StrolchException {
|
||||
return this.container.getRealm(realm).openTx(getCertificate(), action, false);
|
||||
return getRealm(realm).openTx(getCertificate(), action, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -315,7 +333,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
* if the {@link StrolchRealm} does not exist with the given name
|
||||
*/
|
||||
protected StrolchTransaction openUserTx() throws StrolchException {
|
||||
return this.container.getRealm(getCertificate()).openTx(getCertificate(), getClass(), false);
|
||||
return getRealm().openTx(getCertificate(), getClass(), false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -332,7 +350,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
* if the {@link StrolchRealm} does not exist with the given name
|
||||
*/
|
||||
protected StrolchTransaction openUserTx(boolean readOnly) throws StrolchException {
|
||||
return this.container.getRealm(getCertificate()).openTx(getCertificate(), getClass(), false);
|
||||
return getRealm().openTx(getCertificate(), getClass(), readOnly);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -348,7 +366,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
* if the {@link StrolchRealm} does not exist with the given name
|
||||
*/
|
||||
protected StrolchTransaction openUserTx(String action) throws StrolchException {
|
||||
return this.container.getRealm(getCertificate()).openTx(getCertificate(), action, false);
|
||||
return getRealm().openTx(getCertificate(), action, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -365,7 +383,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
* if anything else goes wrong during execution
|
||||
*/
|
||||
protected void runAs(String username, SystemAction action) throws PrivilegeException, Exception {
|
||||
this.container.getPrivilegeHandler().runAs(username, action);
|
||||
getPrivilegeHandler().runAs(username, action);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -385,7 +403,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
*/
|
||||
protected <V> V runWithResult(String username, SystemActionWithResult<V> action)
|
||||
throws PrivilegeException, Exception {
|
||||
return this.container.getPrivilegeHandler().runWithResult(username, action);
|
||||
return getPrivilegeHandler().runWithResult(username, action);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -402,7 +420,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
* if anything else goes wrong during execution
|
||||
*/
|
||||
protected void runAs(String username, PrivilegedRunnable runnable) throws PrivilegeException, Exception {
|
||||
this.container.getPrivilegeHandler().runAs(username, runnable);
|
||||
getPrivilegeHandler().runAs(username, runnable);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -422,7 +440,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
*/
|
||||
protected <V> V runWithResult(String username, PrivilegedRunnableWithResult<V> runnable)
|
||||
throws PrivilegeException, Exception {
|
||||
return this.container.getPrivilegeHandler().runWithResult(username, runnable);
|
||||
return getPrivilegeHandler().runWithResult(username, runnable);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -437,7 +455,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
* if anything else goes wrong during execution
|
||||
*/
|
||||
protected void runAsAgent(SystemAction action) throws PrivilegeException, Exception {
|
||||
this.container.getPrivilegeHandler().runAsAgent(action);
|
||||
getPrivilegeHandler().runAsAgent(action);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -454,7 +472,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
* if anything else goes wrong during execution
|
||||
*/
|
||||
protected <V> V runAsAgentWithResult(SystemActionWithResult<V> action) throws PrivilegeException, Exception {
|
||||
return this.container.getPrivilegeHandler().runAsAgentWithResult(action);
|
||||
return getPrivilegeHandler().runAsAgentWithResult(action);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -470,7 +488,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
* if anything else goes wrong during execution
|
||||
*/
|
||||
protected void runAsAgent(PrivilegedRunnable runnable) throws PrivilegeException, Exception {
|
||||
this.container.getPrivilegeHandler().runAsAgent(runnable);
|
||||
getPrivilegeHandler().runAsAgent(runnable);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -489,7 +507,7 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
|||
*/
|
||||
protected <V> V runAsAgentWithResult(PrivilegedRunnableWithResult<V> runnable)
|
||||
throws PrivilegeException, Exception {
|
||||
return this.container.getPrivilegeHandler().runAsAgentWithResult(runnable);
|
||||
return getPrivilegeHandler().runAsAgentWithResult(runnable);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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 extends ServiceResult> T i18n(ResourceBundle bundle, String key) {
|
||||
this.i18nMessage = new I18nMessage(bundle, key);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends ServiceResult> 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 extends ServiceResult> 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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue