[New] Added new constructors to Result for ServiceResults

This commit is contained in:
Robert von Burg 2015-03-22 00:34:31 +01:00
parent 01963c7c20
commit da329c6e46
2 changed files with 23 additions and 1 deletions

@ -1 +1 @@
Subproject commit 8e75a7651ae64b0cd47cfe923244d6828a9f4c93 Subproject commit 6338690ad2f849bdcb8c4c728b6bf96a87db42d8

View File

@ -15,11 +15,14 @@
*/ */
package li.strolch.rest.model; package li.strolch.rest.model;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import li.strolch.service.api.ServiceResult;
import ch.eitchnet.utils.helper.StringHelper; import ch.eitchnet.utils.helper.StringHelper;
/** /**
@ -44,6 +47,17 @@ public class Result {
this.exceptionMsg = StringHelper.formatExceptionMessage(e); this.exceptionMsg = StringHelper.formatExceptionMessage(e);
} }
public Result(ServiceResult svcResult) {
if (svcResult.isOk()) {
this.msg = StringHelper.DASH;
} else {
this.msg = svcResult.getMessage();
Throwable t = svcResult.getThrowable();
if (t != null)
this.exceptionMsg = StringHelper.formatExceptionMessage(t);
}
}
public Result() { public Result() {
this.msg = StringHelper.DASH; this.msg = StringHelper.DASH;
} }
@ -63,4 +77,12 @@ public class Result {
public void setExceptionMsg(String exceptionMsg) { public void setExceptionMsg(String exceptionMsg) {
this.exceptionMsg = exceptionMsg; this.exceptionMsg = exceptionMsg;
} }
public static Response toResponse(ServiceResult svcResult) {
Result result = new Result(svcResult);
if (svcResult.isOk())
return Response.ok(result, MediaType.APPLICATION_JSON).build();
else
return Response.serverError().type(MediaType.APPLICATION_JSON).entity(result).build();
}
} }