Work on error reporting. Adds troubleshooter.
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / Pz2Session.java
index 822b416..74980bc 100644 (file)
@@ -15,6 +15,7 @@ import org.apache.log4j.Logger;
 import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException;\r
 import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
 import com.indexdata.pz2utils4jsf.controls.ResultsPager;\r
+import com.indexdata.pz2utils4jsf.pazpar2.data.ApplicationError;\r
 import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget;\r
 import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData;\r
 import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseParser;\r
@@ -40,7 +41,8 @@ public class Pz2Session implements Pz2Interface {
   private com.indexdata.masterkey.pazpar2.client.Pazpar2Client client = null;   \r
   private TargetFilter targetFilter = null;  \r
   private ResultsPager pager = null; \r
-      \r
+  private ApplicationTroubleshooter errorHelper = null;\r
+  \r
   public Pz2Session () {\r
     logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]");      \r
   }\r
@@ -48,15 +50,16 @@ public class Pz2Session implements Pz2Interface {
   public void init(Pz2Configurator pz2conf) {\r
     if (client==null) {\r
       logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(pz2conf));\r
-    try {\r
-      cfg = new com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration(pz2conf.getConfig());\r
-      client = new com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric(cfg);\r
-      resetDataObjects();\r
-    } catch (ProxyErrorException e) {\r
-      e.printStackTrace();\r
-    } catch (IOException ioe) {\r
-      ioe.printStackTrace();\r
-    }\r
+      try {\r
+        cfg = new com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration(pz2conf.getConfig());\r
+        client = new com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric(cfg);\r
+        errorHelper = new ApplicationTroubleshooter(pz2conf);        \r
+        resetDataObjects();\r
+      } catch (ProxyErrorException e) {\r
+        e.printStackTrace();\r
+      } catch (IOException ioe) {\r
+        ioe.printStackTrace();\r
+      }\r
     } else {\r
       logger.warn("Attempt to configure session but it already has a configured client");\r
     }\r
@@ -111,7 +114,9 @@ public class Pz2Session implements Pz2Interface {
         }\r
       }\r
       for (CommandThread thread : threadList) {\r
-         dataObjects.put(thread.getCommand().getName(), new Pazpar2ResponseParser().getObject(thread.getResponse()));        \r
+         String commandName = thread.getCommand().getName();\r
+         Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(thread.getResponse());\r
+         dataObjects.put(commandName, responseObject);        \r
       }\r
       return getActiveClients();\r
     } else {\r
@@ -268,41 +273,43 @@ public class Pz2Session implements Pz2Interface {
     queryStates.setCurrentStateKey(key);\r
   }\r
   \r
+  /**\r
+   * Returns true if application error found in any response data objects \r
+   */\r
   public boolean hasErrors () {\r
-    if (dataObjects.get("search").isError()) {\r
+    if (dataObjects.get("search").hasApplicationError()) {\r
       logger.info("Error detected in search");\r
       return true;\r
     }\r
     for (String name : dataObjects.keySet()) {\r
-      if (dataObjects.get(name).isError()) {\r
+      if (dataObjects.get(name).hasApplicationError()) {\r
         logger.info("Error detected in " + name);\r
         return true;\r
       }\r
     }    \r
     return false;\r
   }\r
-    \r
-  public String getErrorMessages() {\r
-    StringBuilder msgs = new StringBuilder("");\r
-    for (String name : dataObjects.keySet()) {     \r
-      if (dataObjects.get(name).isError()) {     \r
-        msgs.append(name + ": " + dataObjects.get(name).getErrorMessage());\r
-      } \r
-    }\r
-    return msgs.toString();\r
-  }\r
+\r
   \r
-  public String getFirstErrorMessage() {\r
-    if (dataObjects.get("search").isError()) {\r
-      return "Error doing search: " + dataObjects.get("search").getErrorMessage();\r
-    }\r
-    for (String name : dataObjects.keySet()) {     \r
-      if (dataObjects.get(name).isError()) {     \r
-        return name + ": " + dataObjects.get(name).getErrorMessage();        \r
-      } \r
+  /**\r
+   * Returns a search command error, if any, otherwise the first\r
+   * error found for an arbitrary command, if any, otherwise\r
+   * an empty dummy error. \r
+   */    \r
+  public ApplicationError getOneError() {\r
+    ApplicationError error = new ApplicationError();    \r
+    if (dataObjects.get("search").hasApplicationError()) {\r
+      error = dataObjects.get("search").getApplicationError();                        \r
+    } else {\r
+      for (String name : dataObjects.keySet()) {     \r
+        if (dataObjects.get(name).hasApplicationError()) {     \r
+          error = dataObjects.get(name).getApplicationError(); \r
+          break;\r
+        } \r
+      }\r
     }\r
-    return "";\r
-    \r
+    error.setTroubleshooter(errorHelper);\r
+    return error;         \r
   }\r
 \r
     \r
@@ -332,6 +339,10 @@ public class Pz2Session implements Pz2Interface {
     return pager;\r
   }\r
   \r
+  protected ApplicationTroubleshooter getTroubleshooter() {\r
+    return errorHelper;\r
+  }\r
+  \r
   private void handleQueryStateChanges (String commands) {\r
     if (queryStates.hasPendingStateChange("search")) { \r
       logger.debug("Found pending search change. Doing search before updating " + commands);\r