Work in progress on error detect,report,troubleshoot
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / data / CommandError.java
1 package com.indexdata.pz2utils4jsf.pazpar2.data;\r
2 \r
3 import static com.indexdata.pz2utils4jsf.utils.Utils.nl;\r
4 \r
5 import java.util.ArrayList;\r
6 import java.util.List;\r
7 \r
8 import com.indexdata.pz2utils4jsf.errors.ApplicationError;\r
9 import com.indexdata.pz2utils4jsf.errors.ErrorHelper;\r
10 import com.indexdata.pz2utils4jsf.errors.ErrorHelper.ErrorCode;\r
11 import com.indexdata.utils.XmlUtils;\r
12 \r
13 /**\r
14  * Captures errors encountered during the execution of a command. \r
15  * Is parsed by Pazpar2ResponseParser, piggybacked in a (seemingly)\r
16  * regular command respond.\r
17  * \r
18  * @author Niels Erik\r
19  *\r
20  */\r
21 public class CommandError extends Pazpar2ResponseData implements ApplicationError {\r
22 \r
23   private static final long serialVersionUID = 8878776025779714122L;\r
24   private ErrorCode applicationErrorCode;\r
25   private ErrorHelper errorHelper = null;\r
26   \r
27   \r
28   public CommandError () {    \r
29   }\r
30   \r
31   public String getLabel() {\r
32     return getOneElementValue("commandname");\r
33   }\r
34       \r
35   public String getMessage() {\r
36     return getOneElementValue("errormessage");\r
37   }\r
38     \r
39   public String getException () {\r
40     return getOneElementValue("exception");\r
41   }\r
42     \r
43   public List<String> getSuggestions() { \r
44     if (errorHelper!=null) {\r
45       return errorHelper.getSuggestions(this);\r
46     } else {\r
47       List<String> nohelper = new ArrayList<String>();\r
48       nohelper.add("Tips: could not generate tips due to a programming error, error helper was not set");\r
49       return nohelper;\r
50     }\r
51   }\r
52   \r
53   /**\r
54    * Creates an XML string error message, embedded in an XML string document named by the command\r
55    * @param commandName\r
56    * @param exceptionName\r
57    * @param errorMessage\r
58    * @return\r
59    */\r
60   public static String createErrorXml (String commandName, String exceptionName, String errorMessage) {\r
61     StringBuilder errorXml = new StringBuilder("");\r
62     errorXml.append("<" + commandName + ">"+nl);\r
63     errorXml.append(" <applicationerror>"+nl);\r
64     errorXml.append("  <commandname>" + commandName + "</commandname>"+nl);\r
65     errorXml.append("  <exception>" + XmlUtils.escape(exceptionName) + "</exception>"+nl);    \r
66     errorXml.append("  <errormessage>" + XmlUtils.escape(errorMessage) + "</errormessage>"+nl);    \r
67     errorXml.append(" </applicationerror>"+nl);\r
68     errorXml.append("</" + commandName + ">"+nl);\r
69     return errorXml.toString(); \r
70   }\r
71   \r
72   public void setErrorHelper (ErrorHelper errorHelper) {\r
73     this.errorHelper = errorHelper; \r
74   }\r
75 \r
76   @Override\r
77   public void setApplicationErrorCode(ErrorCode code) {\r
78     this.applicationErrorCode = code;    \r
79   }\r
80 \r
81   @Override\r
82   public ErrorCode getApplicationErrorCode() {\r
83     return applicationErrorCode;    \r
84   }\r
85 \r
86 }\r