Embeds pz2 error XML, if any, in the app error XML
[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     if (hasPazpar2Error()) {      \r
37       return getPazpar2Error().getMsg();\r
38     } else {      \r
39       return getOneElementValue("errormessage");\r
40     }\r
41   }\r
42     \r
43   public String getException () {\r
44     return getOneElementValue("exception");\r
45   }\r
46     \r
47   public List<String> getSuggestions() { \r
48     if (errorHelper!=null) {\r
49       return errorHelper.getSuggestions(this);\r
50     } else {\r
51       List<String> nohelper = new ArrayList<String>();\r
52       nohelper.add("Tips: could not generate tips due to a programming error, error helper was not set");\r
53       return nohelper;\r
54     }\r
55   }\r
56   \r
57   /**\r
58    * Creates an XML string error message, embedded in an XML string document named by the command\r
59    * @param commandName\r
60    * @param exceptionName\r
61    * @param errorMessage\r
62    * @return\r
63    */\r
64   public static String createErrorXml (String commandName, String exceptionName, String errorMessage) {\r
65     StringBuilder errorXml = new StringBuilder("");\r
66     errorXml.append("<" + commandName + ">"+nl);\r
67     errorXml.append(" <applicationerror>"+nl);\r
68     errorXml.append("  <commandname>" + commandName + "</commandname>"+nl);\r
69     errorXml.append("  <exception>" + XmlUtils.escape(exceptionName) + "</exception>"+nl);    \r
70     errorXml.append("  <errormessage>" + XmlUtils.escape(errorMessage) + "</errormessage>"+nl);    \r
71     errorXml.append(" </applicationerror>"+nl);\r
72     errorXml.append("</" + commandName + ">"+nl);\r
73     return errorXml.toString(); \r
74   }\r
75   \r
76   public static String insertPazpar2ErrorXml (String commandName, String exceptionName, String pazpar2ErrorXml) {\r
77     StringBuilder errorXml = new StringBuilder("");\r
78     errorXml.append("<" + commandName + ">"+nl);\r
79     errorXml.append(" <applicationerror>"+nl);\r
80     errorXml.append("  <commandname>" + commandName + "</commandname>"+nl);\r
81     errorXml.append("  <exception>" + XmlUtils.escape(exceptionName) + "</exception>"+nl);    \r
82     errorXml.append(pazpar2ErrorXml+nl);    \r
83     errorXml.append(" </applicationerror>"+nl);\r
84     errorXml.append("</" + commandName + ">"+nl);\r
85     return errorXml.toString(); \r
86     \r
87   }\r
88     \r
89   public void setErrorHelper (ErrorHelper errorHelper) {\r
90     this.errorHelper = errorHelper; \r
91   }\r
92 \r
93   @Override\r
94   public void setApplicationErrorCode(ErrorCode code) {\r
95     this.applicationErrorCode = code;    \r
96   }\r
97 \r
98   @Override\r
99   public ErrorCode getApplicationErrorCode() {\r
100     return applicationErrorCode;    \r
101   }\r
102   \r
103   public boolean hasPazpar2Error () {\r
104     return ( getOneElement("error") != null);            \r
105   }\r
106   \r
107   public Pazpar2Error getPazpar2Error() {\r
108     return (Pazpar2Error) getOneElement("error");\r
109   }\r
110 \r
111 \r
112 }\r