Work on error reporting. Adds troubleshooter.
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / data / ApplicationError.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.pazpar2.ApplicationTroubleshooter;\r
9 import com.indexdata.utils.XmlUtils;\r
10 \r
11 public class ApplicationError extends Pazpar2ResponseData {\r
12 \r
13   private static final long serialVersionUID = 8878776025779714122L;\r
14   private ApplicationTroubleshooter errorHelper = null;\r
15   \r
16   \r
17   public ApplicationError () {    \r
18   }\r
19   \r
20   public String getCommandName() {\r
21     return getOneElementValue("commandname");\r
22   }\r
23       \r
24   public String getErrorMessage() {\r
25     return getOneElementValue("errormessage");\r
26   }\r
27     \r
28   public String getException () {\r
29     return getOneElementValue("exception");\r
30   }\r
31     \r
32   public List<String> getSuggestions() { \r
33     if (errorHelper!=null) {\r
34       return errorHelper.getSuggestions(getCommandName(), getErrorMessage());\r
35     } else {\r
36       List<String> nohelper = new ArrayList<String>();\r
37       nohelper.add("Tips: could not generate tips due to a programming error, error helper was not set");\r
38       return nohelper;\r
39     }\r
40   }\r
41   \r
42   /**\r
43    * Creates an XML string error message, embedded in an XML string document named by the command\r
44    * @param commandName\r
45    * @param exceptionName\r
46    * @param errorMessage\r
47    * @return\r
48    */\r
49   public static String createErrorXml (String commandName, String exceptionName, String errorMessage) {\r
50     StringBuilder errorXml = new StringBuilder("");\r
51     errorXml.append("<" + commandName + ">"+nl);\r
52     errorXml.append(" <applicationerror>"+nl);\r
53     errorXml.append("  <commandname>" + commandName + "</commandname>");\r
54     errorXml.append("  <exception>" + XmlUtils.escape(exceptionName) + "</exception>"+nl);    \r
55     errorXml.append("  <errormessage>" + XmlUtils.escape(errorMessage) + "</errormessage>"+nl);\r
56     errorXml.append(" </applicationerror>"+nl);\r
57     errorXml.append("</" + commandName + ">"+nl);\r
58     return errorXml.toString(); \r
59   }\r
60   \r
61   public void setTroubleshooter (ApplicationTroubleshooter errorHelper) {\r
62     this.errorHelper = errorHelper; \r
63   }\r
64 \r
65 }\r