]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/msg/ExecutionAnswer.java
Stabilize attachments and events
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / msg / ExecutionAnswer.java
1 package org.argeo.slc.msg;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5
6 import org.apache.commons.io.IOUtils;
7 import org.argeo.slc.SlcException;
8
9 /** Answer to an execution of a remote service which performed changes. */
10 public class ExecutionAnswer {
11 public final static String OK = "OK";
12 public final static String ERROR = "ERROR";
13
14 private String status = OK;
15 private String message = "";
16
17 /** Canonical constructor */
18 public ExecutionAnswer(String status, String message) {
19 setStatus(status);
20 if (message == null)
21 throw new SlcException("Message cannot be null");
22 this.message = message;
23 }
24
25 /** Empty constructor */
26 public ExecutionAnswer() {
27 }
28
29 public String getStatus() {
30 return status;
31 }
32
33 public void setStatus(String status) {
34 if (status == null || (!status.equals(OK) && !status.equals(ERROR)))
35 throw new SlcException("Bad status format: " + status);
36 this.status = status;
37 }
38
39 public String getMessage() {
40 return message;
41 }
42
43 public void setMessage(String message) {
44 this.message = message;
45 }
46
47 public static ExecutionAnswer error(String message) {
48 return new ExecutionAnswer(ERROR, message);
49 }
50
51 public static ExecutionAnswer error(Throwable e) {
52 StringWriter writer = new StringWriter();
53 try {
54 e.printStackTrace(new PrintWriter(writer));
55 return new ExecutionAnswer(ERROR, writer.toString());
56 } finally {
57 IOUtils.closeQuietly(writer);
58 }
59 }
60
61 public static ExecutionAnswer ok(String message) {
62 return new ExecutionAnswer(OK, message);
63 }
64
65 }