1 package com.indexdata.pz2utils4jsf.pazpar2.data;
\r
3 import static com.indexdata.pz2utils4jsf.utils.Utils.nl;
\r
5 import java.util.ArrayList;
\r
6 import java.util.List;
\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
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
18 * @author Niels Erik
\r
21 public class CommandError extends Pazpar2ResponseData implements ApplicationError {
\r
23 private static final long serialVersionUID = 8878776025779714122L;
\r
24 private ErrorCode applicationErrorCode;
\r
25 private ErrorHelper errorHelper = null;
\r
28 public CommandError () {
\r
31 public String getLabel() {
\r
32 return getOneElementValue("commandname");
\r
35 public String getMessage() {
\r
36 if (hasPazpar2Error()) {
\r
37 return getPazpar2Error().getMsg();
\r
39 return getOneElementValue("errormessage");
\r
43 public String getException () {
\r
44 return getOneElementValue("exception");
\r
47 public List<String> getSuggestions() {
\r
48 if (errorHelper!=null) {
\r
49 return errorHelper.getSuggestions(this);
\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
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
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
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
89 public void setErrorHelper (ErrorHelper errorHelper) {
\r
90 this.errorHelper = errorHelper;
\r
94 public void setApplicationErrorCode(ErrorCode code) {
\r
95 this.applicationErrorCode = code;
\r
99 public ErrorCode getApplicationErrorCode() {
\r
100 return applicationErrorCode;
\r
103 public boolean hasPazpar2Error () {
\r
104 return ( getOneElement("error") != null);
\r
107 public Pazpar2Error getPazpar2Error() {
\r
108 return (Pazpar2Error) getOneElement("error");
\r