Generalizes and distinguishes - pz2 and sp. Adds auth.
[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.ErrorInterface;\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  * Holds an error encountered during the execution of a command.\r
15  * \r
16  * An error can be received by a command thread as an exception message \r
17  * or as an error XML. In both cases the error (string or xml) will be embedded\r
18  * in a new 'applicationerror' element which in turn will be embedded in a\r
19  * command XML (i.e. a 'search' or a 'show' response XML)  \r
20  * \r
21  * The command response XML is subsequently parsed by Pazpar2ResponseParser, \r
22  * which will then create the CommandError object.\r
23  * \r
24  * @author Niels Erik\r
25  *\r
26  */\r
27 public class CommandError extends Pazpar2ResponseData implements ErrorInterface {\r
28 \r
29   private static final long serialVersionUID = 8878776025779714122L;\r
30   private ErrorCode applicationErrorCode;\r
31   private ErrorHelper errorHelper = null;\r
32   \r
33   \r
34   public CommandError () {    \r
35   }\r
36   \r
37   public String getLabel() {\r
38     return getOneElementValue("commandname");\r
39   }\r
40       \r
41   public String getMessage() {\r
42     if (hasPazpar2Error()) {      \r
43       return getPazpar2Error().getMsg();\r
44     } else {      \r
45       return getOneElementValue("errormessage");\r
46     }\r
47   }\r
48     \r
49   public String getException () {\r
50     return getOneElementValue("exception");\r
51   }\r
52     \r
53   public List<String> getSuggestions() { \r
54     if (errorHelper!=null) {\r
55       return errorHelper.getSuggestions(this);\r
56     } else {\r
57       List<String> nohelper = new ArrayList<String>();\r
58       nohelper.add("Tips: could not generate tips due to a programming error, error helper was not set");\r
59       return nohelper;\r
60     }\r
61   }\r
62   \r
63   /**\r
64    * Creates an XML string error message, embedded in an XML string document named by the command\r
65    * This is the XML that Pazpar2ResponseParser will turn into a CommandError object. \r
66    * @param commandName\r
67    * @param exceptionName\r
68    * @param errorMessage\r
69    * @return\r
70    */\r
71   public static String createErrorXml (String commandName, String exceptionName, String errorMessage) {\r
72     StringBuilder errorXml = new StringBuilder("");\r
73     errorXml.append("<" + commandName + ">"+nl);\r
74     errorXml.append(" <applicationerror>"+nl);\r
75     errorXml.append("  <commandname>" + commandName + "</commandname>"+nl);\r
76     errorXml.append("  <exception>" + XmlUtils.escape(exceptionName) + "</exception>"+nl);    \r
77     errorXml.append("  <errormessage>" + XmlUtils.escape(errorMessage) + "</errormessage>"+nl);    \r
78     errorXml.append(" </applicationerror>"+nl);\r
79     errorXml.append("</" + commandName + ">"+nl);\r
80     return errorXml.toString(); \r
81   }\r
82   \r
83   /**\r
84    * Embeds a Pazpar2 (or Pazpar2 client) error response document as a child element of\r
85    * a command response document (like 'search' or 'show').\r
86    * This is the XML that Pazpar2ResponseParser will turn into a CommandError object.\r
87    * \r
88    * \r
89    * @param commandName The name of the command during which's execution the error was encountered\r
90    * @param exceptionName The (possibly loosely defined) name of the exception that was thrown\r
91    * @param pazpar2ErrorXml The error document as created by Pazpar2, or the Service Proxy or \r
92    *                        by the Pazpar2 client itself. \r
93    * @return\r
94    */\r
95   public static String insertPazpar2ErrorXml (String commandName, String exceptionName, String pazpar2ErrorXml) {\r
96     StringBuilder errorXml = new StringBuilder("");\r
97     errorXml.append("<" + commandName + ">"+nl);\r
98     errorXml.append(" <applicationerror>"+nl);\r
99     errorXml.append("  <commandname>" + commandName + "</commandname>"+nl);\r
100     errorXml.append("  <exception>" + XmlUtils.escape(exceptionName) + "</exception>"+nl);    \r
101     errorXml.append(pazpar2ErrorXml+nl);    \r
102     errorXml.append(" </applicationerror>"+nl);\r
103     errorXml.append("</" + commandName + ">"+nl);\r
104     return errorXml.toString(); \r
105     \r
106   }\r
107    \r
108   /**\r
109    * Sets the object that should be used to analyze the error\r
110    *  \r
111    */\r
112   public void setErrorHelper (ErrorHelper errorHelper) {\r
113     this.errorHelper = errorHelper; \r
114   }\r
115 \r
116   @Override\r
117   public void setApplicationErrorCode(ErrorCode code) {\r
118     this.applicationErrorCode = code;    \r
119   }\r
120 \r
121   @Override\r
122   public ErrorCode getApplicationErrorCode() {\r
123     return applicationErrorCode;    \r
124   }\r
125   \r
126   public boolean hasPazpar2Error () {\r
127     return ( getOneElement("error") != null);            \r
128   }\r
129   \r
130   public Pazpar2Error getPazpar2Error() {\r
131     return (Pazpar2Error) getOneElement("error");\r
132   }\r
133 \r
134 \r
135 }\r