Sets up error central. Lets page author decide to show.
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / Pz2Bean.java
index d948152..b12082b 100644 (file)
@@ -18,7 +18,7 @@ import com.indexdata.pz2utils4jsf.controls.ResultsPager;
 import com.indexdata.pz2utils4jsf.errors.ConfigurationError;\r
 import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
 import com.indexdata.pz2utils4jsf.errors.ErrorHelper;\r
-import com.indexdata.pz2utils4jsf.errors.ErrorInterface;\r
+import com.indexdata.pz2utils4jsf.errors.ErrorCentral;\r
 import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandParameter;\r
 import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Commands;\r
 import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData;\r
@@ -42,10 +42,11 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable {
   @Inject StateManager stateMgr;\r
   @Inject Pazpar2Commands pzreq;\r
   @Inject Pazpar2Responses pzresp;\r
+  @Inject ErrorCentral errors;\r
   \r
   protected ResultsPager pager = null; \r
 \r
-  protected List<ErrorInterface> configurationErrors = null;\r
+  \r
   protected ErrorHelper errorHelper = null;\r
               \r
   public Pz2Bean () {\r
@@ -59,28 +60,17 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable {
     searchClient = new Pz2Client();\r
     logger.info("Using [" + Utils.objectId(searchClient) + "] configured by [" \r
                           + Utils.objectId(configurator) + "]" );    \r
-    configureClient(searchClient,configurator);\r
-    stateMgr.addStateListener(this);\r
+    configureClient(searchClient,configurator);    \r
+    stateMgr.addStateListener(this);    \r
   }  \r
   \r
   public void configureClient(SearchClient searchClient, ConfigurationReader configReader) {\r
-    configurationErrors = new ArrayList<ErrorInterface>();\r
-    errorHelper = new ErrorHelper(configReader);    \r
     logger.debug(Utils.objectId(this) + " will configure search client for the session");\r
     try {\r
       searchClient.configure(configReader);            \r
-      // At the time of writing this search client is injected using Weld. \r
-      // However, the client is used for asynchronously sending off requests\r
-      // to the server AND propagation of context to threads is currently \r
-      // not supported. Trying to do so throws a WELD-001303 error. \r
-      // To avoid that, a context free client is cloned from the context \r
-      // dependent one. \r
-      // If propagation to threads gets supported, the cloning can go.\r
-      //\r
-      // Commented as I'm trying with regular instantiation instead\r
-      // this.searchClient = searchClient.cloneMe();         \r
     } catch (ConfigurationException e) {\r
-      configurationErrors.add(new ConfigurationError("Search Client","Configuration",e.getMessage(),new ErrorHelper(configReader)));          \r
+      logger.debug("Pz2Bean adding configuration error");\r
+      errors.addConfigurationError(new ConfigurationError("Search Client","Configuration",e.getMessage()));                \r
     } \r
     logger.info(configReader.document());\r
     pzresp.reset();    \r
@@ -97,9 +87,9 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable {
     pzresp.reset();\r
     // resets some record and show command parameters without \r
     // changing state or creating state change feedback\r
-    pzreq.getRecordInState().removeParametersSilently();        \r
-    pzreq.getShowInState().setParameterSilently(new CommandParameter("start","=",0));    \r
-    logger.debug(Utils.objectId(this) + " is searching using "+pzreq.getCommandReadOnly("search").getUrlEncodedParameterValue("query"));\r
+    pzreq.getRecord().removeParametersInState();        \r
+    pzreq.getShow().setParameterInState(new CommandParameter("start","=",0));    \r
+    logger.debug(Utils.objectId(this) + " is searching using "+pzreq.getCommand("search").getUrlEncodedParameterValue("query"));\r
     doCommand("search");    \r
   }\r
       \r
@@ -120,49 +110,64 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable {
    * @return Number of activeclients at the time of the 'show' command\r
    */\r
   public String update (String commands) {\r
-    if (! hasConfigurationErrors()) {\r
-      if (hasQuery()) {\r
-        handleQueryStateChanges(commands);\r
-        logger.debug("Processing request for " + commands); \r
-        List<CommandThread> threadList = new ArrayList<CommandThread>();\r
-        StringTokenizer tokens = new StringTokenizer(commands,",");\r
-        while (tokens.hasMoreElements()) {          \r
-          threadList.add(new CommandThread(pzreq.getCommandReadOnly(tokens.nextToken()),searchClient));            \r
-        }\r
-        for (CommandThread thread : threadList) {\r
-          thread.start();\r
-        }\r
-        for (CommandThread thread : threadList) {\r
-          try {\r
-            thread.join();\r
-          } catch (InterruptedException e) {\r
-            e.printStackTrace();\r
+    if (! errors.hasConfigurationErrors()) {\r
+      if (commandsAreValid(commands)) {\r
+        if (hasQuery()) {\r
+          handleQueryStateChanges(commands);\r
+          logger.debug("Processing request for " + commands); \r
+          List<CommandThread> threadList = new ArrayList<CommandThread>();\r
+          StringTokenizer tokens = new StringTokenizer(commands,",");\r
+          while (tokens.hasMoreElements()) {          \r
+            threadList.add(new CommandThread(pzreq.getCommand(tokens.nextToken()),searchClient));            \r
           }\r
-        }\r
-        for (CommandThread thread : threadList) {\r
-           String commandName = thread.getCommand().getName();\r
-           String response = thread.getResponse();\r
-           responseLogger.debug("Response was: " + response);\r
-           Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response);\r
-           pzresp.put(commandName, responseObject);        \r
-        }\r
-        if (commands.equals("record")) {\r
-          logger.debug("Record: Active clients: "+pzresp.getRecord().getActiveClients());\r
-          return pzresp.getRecord().getActiveClients();\r
+          for (CommandThread thread : threadList) {\r
+            thread.start();\r
+          }\r
+          for (CommandThread thread : threadList) {\r
+            try {\r
+              thread.join();\r
+            } catch (InterruptedException e) {\r
+              e.printStackTrace();\r
+            }\r
+          }\r
+          for (CommandThread thread : threadList) {\r
+             String commandName = thread.getCommand().getName();\r
+             String response = thread.getResponse();\r
+             responseLogger.debug("Response was: " + response);\r
+             Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response);\r
+             pzresp.put(commandName, responseObject);        \r
+          }\r
+          if (commands.equals("record")) {\r
+            logger.debug("Record: Active clients: "+pzresp.getRecord().getActiveClients());\r
+            return pzresp.getRecord().getActiveClients();\r
+          } else {\r
+            return pzresp.getActiveClients();\r
+          }  \r
         } else {\r
-          return pzresp.getActiveClients();\r
-        }  \r
+          logger.debug("Skipped requests for " + commands + " as there's not yet a query."); \r
+          pzresp.reset();\r
+          return "0";\r
+        }\r
       } else {\r
-        logger.debug("Skipped requests for " + commands + " as there's not yet a query."); \r
-        pzresp.reset();\r
+        logger.error("Did not attemt to run command(s) due to a validation error.");\r
         return "0";\r
       }\r
-    } else {\r
+    } else {      \r
       logger.error("Did not attempt to execute query since there are configuration errors.");\r
       return "0";\r
     }\r
     \r
   }\r
+  \r
+  public boolean commandsAreValid(String commands) {\r
+    if (commands.equals("record")) {\r
+      if (!pzreq.getCommand("record").hasParameterSet("id")) {\r
+        logger.error("Attempt to send record command without the id parameter");\r
+        return false;\r
+      }\r
+    }\r
+    return true;\r
+  }\r
                                 \r
   public String toggleRecord (String recId) {\r
     if (hasRecord(recId)) {\r
@@ -177,7 +182,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable {
   \r
   @Override\r
   public boolean hasRecord (String recId) {\r
-    return pzreq.getCommandReadOnly("record").hasParameters() && pzresp.getRecord().getRecId().equals(recId);\r
+    return pzreq.getCommand("record").hasParameters() && pzresp.getRecord().getRecId().equals(recId);\r
   }\r
         \r
   public String getCurrentStateKey () {    \r
@@ -188,28 +193,10 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable {
     stateMgr.setCurrentStateKey(key);\r
   }\r
   \r
-  public boolean hasConfigurationErrors () {\r
-      return (configurationErrors.size()>0);      \r
-  }\r
-  \r
-  public boolean hasCommandErrors () {\r
-    return pzresp.hasApplicationError();\r
-  }\r
-  \r
-  /**\r
-   * Returns true if application error found in any response data objects \r
-   */\r
-  public boolean hasErrors () {\r
-    return hasConfigurationErrors() || hasCommandErrors();\r
-  }\r
-\r
-  public List<ErrorInterface> getConfigurationErrors() {    \r
-    return configurationErrors;\r
-  }\r
   \r
   \r
   protected boolean hasQuery() {        \r
-    return pzreq.getCommandReadOnly("search").hasParameterSet("query"); \r
+    return pzreq.getCommand("search").hasParameterSet("query"); \r
   }\r
     \r
     \r
@@ -224,11 +211,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable {
     pager =  new ResultsPager(pzresp,pageRange,pzreq);\r
     return pager;\r
   }\r
-  \r
-  protected ErrorHelper getTroubleshooter() {\r
-    return errorHelper;\r
-  }\r
-  \r
+    \r
   protected void handleQueryStateChanges (String commands) {\r
     if (stateMgr.hasPendingStateChange("search") && hasQuery()) { \r
       logger.debug("Found pending search change. Doing search before updating " + commands);      \r
@@ -237,7 +220,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable {
     if (stateMgr.hasPendingStateChange("record") && ! commands.equals("record")) {        \r
       logger.debug("Found pending record ID change. Doing record before updating " + commands);\r
       stateMgr.hasPendingStateChange("record",false);\r
-      if (pzreq.getCommandReadOnly("record").hasParameters()) {\r
+      if (pzreq.getCommand("record").hasParameterSet("id")) {\r
         update("record");\r
       } else {         \r
         pzresp.put("record", new RecordResponse());\r
@@ -246,7 +229,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable {
   }\r
   \r
   protected String doCommand(String commandName) {             \r
-    logger.debug(pzreq.getCommandReadOnly(commandName).getEncodedQueryString() + ": Results for "+ pzreq.getCommandReadOnly("search").getEncodedQueryString());\r
+    logger.debug(pzreq.getCommand(commandName).getEncodedQueryString() + ": Results for "+ pzreq.getCommand("search").getEncodedQueryString());\r
     return update(commandName);\r
   }\r
   \r