From: Niels Erik G. Nielsen Date: Thu, 7 Mar 2013 17:07:49 +0000 (-0500) Subject: Packaging pazpar2/io error messages in response objects X-Git-Tag: v0.0.7~218 X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=ab7a23c769e77affa25a47112d2e5e93ae23d7bc;p=mkjsf-moved-to-github.git Packaging pazpar2/io error messages in response objects For return to and display in the UI --- diff --git a/src/META-INF/resources/pz2utils/fieldlisteners.js b/src/META-INF/resources/pz2utils/fieldlisteners.js index c5cf63a..5f521f0 100644 --- a/src/META-INF/resources/pz2utils/fieldlisteners.js +++ b/src/META-INF/resources/pz2utils/fieldlisteners.js @@ -4,7 +4,7 @@ { //console.log('rendering ' + renderWhileActiveclients); var sourcecomp = document.getElementById("pz2watch:activeclientsField"); - jsf.ajax.request(sourcecomp, null,{render: renderWhileActiveclients}); + jsf.ajax.request(sourcecomp, null,{render: renderWhileActiveclients + " pz2watch:errorMessages"}); if (doRefresh) { //console.log('Will do another ajax request after a timeout in order to render: pz2watch:activeclientsField'); renderTargetsReqVar=setTimeout( diff --git a/src/META-INF/resources/pz2utils/pz2watch.xhtml b/src/META-INF/resources/pz2utils/pz2watch.xhtml index 511aae1..bfa2b3b 100644 --- a/src/META-INF/resources/pz2utils/pz2watch.xhtml +++ b/src/META-INF/resources/pz2utils/pz2watch.xhtml @@ -32,8 +32,12 @@ //]]> - - Active clients: + + + + +

+
diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java index 902ccb4..5764777 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java @@ -7,6 +7,7 @@ import org.apache.log4j.Logger; import com.indexdata.pz2utils4jsf.pazpar2.CommandThread; import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; +import com.indexdata.utils.XmlUtils; import com.indexdata.masterkey.pazpar2.client.ClientCommand; import com.indexdata.masterkey.pazpar2.client.Pazpar2Client; import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException; @@ -36,11 +37,13 @@ public class CommandThread extends Thread { long end = System.currentTimeMillis(); logger.debug("Executed " + command.getName() + " in " + (end-start) + " ms." ); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + logger.error("Message: " + e.getMessage()); + response = new StringBuilder("<"+command.getName()+">"+XmlUtils.escape(e.getMessage())+""); + logger.error(response.toString()); } catch (Pazpar2ErrorException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + logger.error(e.getMessage()); + response = new StringBuilder("<"+command.getName()+">"+XmlUtils.escape(e.getMessage())+""); + logger.error(response.toString()); } } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java index 41ca130..6abdb34 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java @@ -272,5 +272,18 @@ public class Pz2Bean implements Pz2Interface, Serializable { public void setCurrentStateKey(String key) { pz2.setCurrentStateKey(key); } + + public boolean hasErrors() { + return pz2.hasErrors(); + } + + public String getErrorMessages() { + return pz2.getErrorMessages(); + } + + public String getFirstErrorMessage() { + return pz2.getFirstErrorMessage(); + } + } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java index 8f6153a..b69b4e2 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java @@ -298,4 +298,11 @@ public interface Pz2Interface extends Serializable { * @param key corresponding to browsers hash string */ public void setCurrentStateKey(String key); + + public boolean hasErrors(); + + public String getErrorMessages(); + + public String getFirstErrorMessage(); + } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java index 9675696..822b416 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -19,6 +19,7 @@ import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseParser; import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.SearchResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.StatResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.TermListsResponse; @@ -83,7 +84,7 @@ public class Pz2Session implements Pz2Interface { logger.debug("Updating show,stat,termlist,bytarget from pazpar2"); return update("show,stat,termlist,bytarget"); } - + /** * Refreshes the data objects listed in 'commands' from pazpar2 * @@ -110,9 +111,7 @@ public class Pz2Session implements Pz2Interface { } } for (CommandThread thread : threadList) { - if (!thread.getCommand().getName().equals("search")) { - dataObjects.put(thread.getCommand().getName(), new Pazpar2ResponseParser().getObject(thread.getResponse())); - } + dataObjects.put(thread.getCommand().getName(), new Pazpar2ResponseParser().getObject(thread.getResponse())); } return getActiveClients(); } else { @@ -268,6 +267,44 @@ public class Pz2Session implements Pz2Interface { logger.debug("************** request to set state key to: [" + key + "]"); queryStates.setCurrentStateKey(key); } + + public boolean hasErrors () { + if (dataObjects.get("search").isError()) { + logger.info("Error detected in search"); + return true; + } + for (String name : dataObjects.keySet()) { + if (dataObjects.get(name).isError()) { + logger.info("Error detected in " + name); + return true; + } + } + return false; + } + + public String getErrorMessages() { + StringBuilder msgs = new StringBuilder(""); + for (String name : dataObjects.keySet()) { + if (dataObjects.get(name).isError()) { + msgs.append(name + ": " + dataObjects.get(name).getErrorMessage()); + } + } + return msgs.toString(); + } + + public String getFirstErrorMessage() { + if (dataObjects.get("search").isError()) { + return "Error doing search: " + dataObjects.get("search").getErrorMessage(); + } + for (String name : dataObjects.keySet()) { + if (dataObjects.get(name).isError()) { + return name + ": " + dataObjects.get(name).getErrorMessage(); + } + } + return ""; + + } + private boolean hasTargetFilter(TargetFilter targetFilter) { return hasTargetFilter() && targetFilter.equals(this.targetFilter); @@ -312,9 +349,9 @@ public class Pz2Session implements Pz2Interface { } } - private String getActiveClients() { - logger.debug("Active clients: "+getShow().getActiveClients()); + private String getActiveClients() { if (getShow()!=null) { + logger.debug("Active clients: "+getShow().getActiveClients()); return getShow().getActiveClients(); } else { return ""; @@ -380,13 +417,15 @@ public class Pz2Session implements Pz2Interface { } private void resetDataObjects() { - logger.debug("Resetting show,stat,termlist,bytarget response objects."); + logger.debug("Resetting show,stat,termlist,bytarget,search response objects."); dataObjects = new ConcurrentHashMap(); dataObjects.put("show", new ShowResponse()); dataObjects.put("stat", new StatResponse()); dataObjects.put("termlist", new TermListsResponse()); dataObjects.put("bytarget", new ByTarget()); dataObjects.put("record", new RecordResponse()); + dataObjects.put("search", new SearchResponse()); } + } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ErrorResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ErrorResponse.java new file mode 100644 index 0000000..ff6a530 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ErrorResponse.java @@ -0,0 +1,7 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +public class Pazpar2ErrorResponse extends Pazpar2ResponseData { + + private static final long serialVersionUID = 5261091417784893149L; + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java index 9412f90..a01ca76 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java @@ -14,6 +14,7 @@ public class Pazpar2ResponseData implements Serializable { HashMap attributes = new HashMap(); HashMap> elements = new HashMap>(); String textContent = ""; + String errorText = null; public void setType (String type) { this.type = type; @@ -86,6 +87,14 @@ public class Pazpar2ResponseData implements Serializable { return Integer.parseInt(val); } } + + public boolean isError () { + return (getOneElement("error") != null); + } + + public String getErrorMessage() { + return getOneElementValue("error"); + } } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java index a08fdbc..c4a4d11 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java @@ -38,7 +38,7 @@ public class Pazpar2ResponseParser extends DefaultHandler { private Pazpar2ResponseData result = null; private static final List docTypes = - Arrays.asList("bytarget","termlist","show","stat","record"); + Arrays.asList("bytarget","termlist","show","stat","record","search"); public Pazpar2ResponseParser() { try { @@ -109,6 +109,8 @@ public class Pazpar2ResponseParser extends DefaultHandler { ((TermListResponse)dataElements.peek()).addTerm((TermResponse)currentElement); } else if (localName.equals("record")) { currentElement = new RecordResponse(); + } else if (localName.equals("search")) { + currentElement = new SearchResponse(); } else { currentElement = new Pazpar2ResponseData(); } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/SearchResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/SearchResponse.java new file mode 100644 index 0000000..528f871 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/SearchResponse.java @@ -0,0 +1,7 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +public class SearchResponse extends Pazpar2ResponseData { + + private static final long serialVersionUID = -3320013021497018972L; + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ShowResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ShowResponse.java index 6c6af92..baed763 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ShowResponse.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ShowResponse.java @@ -12,7 +12,7 @@ public class ShowResponse extends Pazpar2ResponseData { public String getStatus() { - return getOneElement("status").getValue(); + return getOneElementValue("status"); } public String getActiveClients () { diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pz2utils/ListenerFieldIds.java b/src/main/java/com/indexdata/pz2utils4jsf/pz2utils/ListenerFieldIds.java index 02776f8..4315d82 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pz2utils/ListenerFieldIds.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pz2utils/ListenerFieldIds.java @@ -18,5 +18,9 @@ public class ListenerFieldIds implements Serializable { public String getActiveclients () { return ":pz2watch:activeclientsForm:activeclientsField"; } + + public String getErrorMessages () { + return ":pz2watch:activeclientsForm:errorMessages"; + } }