Adds support for target category command - 'categories'
authorNiels Erik G. Nielsen <nielserik@indexdata.com>
Tue, 14 May 2013 01:32:01 +0000 (21:32 -0400)
committerNiels Erik G. Nielsen <nielserik@indexdata.com>
Tue, 14 May 2013 01:32:01 +0000 (21:32 -0400)
.. includes some further work on the disentanglement of the
logic/methods for different types of commands

src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java
src/main/java/com/indexdata/mkjsf/pazpar2/ServiceProxyExtensions.java
src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Commands.java
src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/CategoriesCommand.java [new file with mode: 0644]
src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/ServiceProxyCommands.java
src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java
src/main/java/com/indexdata/mkjsf/pazpar2/data/Responses.java
src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/CategoriesResponse.java [new file with mode: 0644]
src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/TargetCategory.java [new file with mode: 0644]
src/main/java/com/indexdata/mkjsf/pazpar2/state/Pazpar2State.java
src/main/java/com/indexdata/mkjsf/pazpar2/state/StateManager.java

index d7a9235..1c0fc07 100644 (file)
@@ -104,35 +104,49 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
     pzreq.getRecord().removeParametersInState();\r
     pzreq.getSearch().removeParametersInState();   \r
   }\r
-\r
     \r
   public void doSearch(String query) {\r
     pzreq.getSearch().setParameter(new CommandParameter("query","=",query));     \r
     doSearch();\r
   }\r
 \r
-  public void doSearch() { \r
-    stateMgr.hasPendingStateChange("search",false);\r
-    pzresp.resetSearchResponses();\r
-    // resets some record and show command parameters without \r
-    // changing state or creating state change feedback\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
+  public void doSearch() {\r
+    if (errors.hasConfigurationErrors()) {\r
+      logger.error("Ignoring search request due to configuration errors.");\r
+    } else {\r
+      stateMgr.hasPendingStateChange("search",false);\r
+      pzresp.resetSearchResponses();\r
+      // resets some record and show command parameters without \r
+      // changing state or creating state change feedback\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
   \r
   public String doRecord() {\r
-    ResponseDataObject responseObject = doCommand("record");\r
-    if (pzreq.getRecord().hasParameterValue("offset") ||\r
-          pzreq.getRecord().hasParameterValue("checksum")) {\r
+    if (errors.hasConfigurationErrors()) {\r
+      logger.error("Ignoring record request due to configuration errors.");\r
+      return "";\r
+    } else if (!pzreq.getCommand("record").hasParameterValue("id")) {\r
+      logger.debug("Ignoring record request due to no id parameter.");\r
+      return "";\r
+    } else if (pzresp.getSearch().hasApplicationError()) {\r
+      logger.debug("Ignoring record request due search error.");\r
+      return "";\r
+    } else {\r
+      ResponseDataObject responseObject = doCommand("record");\r
+      if (pzreq.getRecord().hasParameterValue("offset") ||\r
+            pzreq.getRecord().hasParameterValue("checksum")) {\r
         RecordResponse recordResponse = new RecordResponse();\r
         recordResponse.setType("record");\r
         recordResponse.setXml(responseObject.getXml());\r
         recordResponse.setAttribute("activeclients", "0");\r
         pzresp.put("record", recordResponse);\r
-     }\r
-     return pzresp.getRecord().getActiveClients();    \r
+      }\r
+      return pzresp.getRecord().getActiveClients();\r
+    }\r
   }\r
       \r
   /**\r
@@ -142,29 +156,20 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
    */\r
   public String update () {\r
     logger.debug("Updating show,stat,termlist,bytarget from pazpar2");\r
-    return update("show,stat,termlist,bytarget");\r
-  }\r
-  \r
-  public boolean validateUpdateRequest(String commands) {\r
     if (errors.hasConfigurationErrors()) {\r
-      logger.error("The command(s) " + commands + " are cancelled due to configuration errors.");\r
-      return false;\r
-    } else if (!commands.equals("search") && pzresp.getSearch().hasApplicationError()) {\r
-      logger.error("The command(s) " + commands + " are cancelled because the latest search command had an error.");\r
-      return false;\r
-    } else if (!commandsAreValid(commands)) {\r
-      logger.debug("The command(s) " + commands + " are cancelled because the were not found to be ready/valid.");\r
-      return false;\r
-    } else if (!hasQuery() &&  !(commands.equals("record") && pzreq.getCommand("record").hasParameterValue("recordquery"))) {\r
-      logger.debug("The command(s) " + commands + " are held off because there's not yet a query.");\r
-      return false;\r
+      logger.error("Ignoring show,stat,termlist,bytarget commands due to configuration errors.");\r
+      return "";\r
+    } else if (pzresp.getSearch().hasApplicationError()) {\r
+      logger.error("Ignoring show,stat,termlist,bytarget commands due to problem with most recent search.");\r
+      return "";\r
+    } else if (!hasQuery()) {\r
+      logger.error("Ignoring show,stat,termlist,bytarget commands because there is not yet a query.");\r
+      return "";\r
     } else {\r
-      return true;\r
+      return update("show,stat,termlist,bytarget");\r
     }\r
-    \r
-    \r
   }\r
-   \r
+     \r
   /**\r
    * Refreshes the data objects listed in 'commands' from pazpar2\r
    * \r
@@ -174,9 +179,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
   public String update (String commands) {\r
     logger.info("Request to update: " + commands);\r
     try {\r
-      if (!validateUpdateRequest(commands)) {\r
-        return "0";\r
-      } else if (commands.equals("search")) {\r
+      if (commands.equals("search")) {\r
         doSearch();\r
         return "";\r
       } else if (commands.equals("record")) {\r
@@ -234,17 +237,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
     }\r
     \r
   }\r
-  \r
-  public boolean commandsAreValid(String commands) {\r
-    if (commands.equals("record")) {\r
-      if (!pzreq.getCommand("record").hasParameterValue("id")) {\r
-        logger.debug("Skips sending record command due to lacking id parameter");\r
-        return false;\r
-      }\r
-    }\r
-    return true;\r
-  }\r
-                                \r
+                                  \r
   public String toggleRecord (String recId) {\r
     if (hasRecord(recId)) {\r
       pzreq.getRecord().removeParameters();  \r
@@ -313,20 +306,18 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
    * @return An XML response parsed to form a response data object\r
    */\r
   protected ResponseDataObject doCommand(String commandName) {\r
-    ResponseDataObject responseObject = null; \r
-    if (validateUpdateRequest(commandName)) {\r
-      logger.debug(pzreq.getCommand(commandName).getEncodedQueryString() + ": Results for "+ pzreq.getCommand("search").getEncodedQueryString());\r
-      Pazpar2Command command = pzreq.getCommand(commandName);\r
-      long start = System.currentTimeMillis();\r
-      HttpResponseWrapper commandResponse = searchClient.executeCommand(command);\r
-      long end = System.currentTimeMillis();\r
-      logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." );\r
-      responseLogger.debug("Response was: " + commandResponse.getResponseString());\r
-      responseObject = ResponseParser.getParser().getDataObject((ClientCommandResponse)commandResponse);\r
-      if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
-        pzresp.put(commandName, responseObject);\r
-      }      \r
-    }\r
+    ResponseDataObject responseObject = null;     \r
+    // logger.debug(pzreq.getCommand(commandName).getEncodedQueryString() + ": Results for "+ pzreq.getCommand("search").getEncodedQueryString());\r
+    Pazpar2Command command = pzreq.getCommand(commandName);\r
+    long start = System.currentTimeMillis();\r
+    HttpResponseWrapper commandResponse = searchClient.executeCommand(command);\r
+    long end = System.currentTimeMillis();\r
+    logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." );\r
+    responseLogger.debug("Response was: " + commandResponse.getResponseString());\r
+    responseObject = ResponseParser.getParser().getDataObject((ClientCommandResponse)commandResponse);\r
+    if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
+      pzresp.put(commandName, responseObject);\r
+    }          \r
     return responseObject;\r
   }\r
     \r
index dae3a17..f09176d 100644 (file)
@@ -20,6 +20,7 @@ import com.indexdata.mkjsf.pazpar2.data.AuthResponse;
 import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
 import com.indexdata.mkjsf.pazpar2.data.ResponseParser;\r
 import com.indexdata.mkjsf.pazpar2.data.Responses;\r
+import com.indexdata.mkjsf.pazpar2.data.sp.CategoriesResponse;\r
 import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser;\r
 import com.indexdata.mkjsf.utils.Utils;\r
 \r
@@ -185,4 +186,22 @@ public class ServiceProxyExtensions implements ServiceProxyInterface, Serializab
   public InitDocUpload getInitDocUpload () {\r
     return initDocUpload;\r
   }\r
+  \r
+  public CategoriesResponse getCategories () {\r
+    ResponseDataObject response = pz2.doCommand("categories");\r
+    if (response.hasApplicationError()) {\r
+      logger.debug(response.getXml());\r
+      return new CategoriesResponse();\r
+    } else {\r
+      try {\r
+        return (CategoriesResponse) response;\r
+      } catch (Exception e) {\r
+        e.printStackTrace();\r
+        logger.debug(response.getXml());\r
+        return new CategoriesResponse();\r
+      }\r
+    }\r
+  }\r
+  \r
+  \r
 }\r
index 2524d47..3ceaa8d 100644 (file)
@@ -79,7 +79,7 @@ public class Pazpar2Commands implements Serializable {
     return (BytargetCommand) (stateMgr.getCommand(BYTARGET));\r
   }\r
   \r
-  public Pazpar2Command getCommand(String name) {\r
+  public Pazpar2Command getCommand(String name) {    \r
     return stateMgr.getCommand(name);\r
   }\r
   \r
diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/CategoriesCommand.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/sp/CategoriesCommand.java
new file mode 100644 (file)
index 0000000..65e1f26
--- /dev/null
@@ -0,0 +1,25 @@
+package com.indexdata.mkjsf.pazpar2.commands.sp;\r
+\r
+import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command;\r
+import com.indexdata.mkjsf.pazpar2.state.StateManager;\r
+\r
+public class CategoriesCommand extends Pazpar2Command implements ServiceProxyCommand {\r
+\r
+  private static final long serialVersionUID = 5023993689780291641L;\r
+\r
+  public CategoriesCommand(StateManager stateMgr) {\r
+    super("categories", stateMgr);\r
+  }\r
+\r
+  @Override\r
+  public Pazpar2Command copy() {\r
+    CategoriesCommand newCommand = new CategoriesCommand(stateMgr);\r
+    return newCommand;\r
+  }\r
+\r
+  @Override\r
+  public ServiceProxyCommand getSp() {\r
+    return this;\r
+  }\r
+\r
+}\r
index cac8100..8c4fefa 100644 (file)
@@ -10,6 +10,7 @@ import com.indexdata.mkjsf.utils.Utils;
 public class ServiceProxyCommands implements Serializable {\r
 \r
   public static final String AUTH = "auth";\r
+  public static final String CATEGORIES = "categories";\r
   private static final long serialVersionUID = 6223527018096841188L;\r
   private static Logger logger = Logger.getLogger(ServiceProxyCommands.class);\r
   private StateManager stateMgr = null; \r
@@ -18,11 +19,14 @@ public class ServiceProxyCommands implements Serializable {
     logger.info("Initializing ServiceProxyCommands [" + Utils.objectId(this) + "]");\r
     this.stateMgr = stateMgr;\r
   }\r
-\r
   \r
   public AuthCommand getAuth() {\r
     return (AuthCommand) (stateMgr.getCommand(AUTH));\r
   }\r
+  \r
+  public CategoriesCommand getCategories() {\r
+    return (CategoriesCommand) (stateMgr.getCommand(CATEGORIES));\r
+  }\r
 \r
 \r
 }\r
index 852ebb1..a26b6ad 100644 (file)
@@ -19,6 +19,8 @@ import org.xml.sax.XMLReader;
 import org.xml.sax.helpers.DefaultHandler;\r
 \r
 import com.indexdata.mkjsf.pazpar2.ClientCommandResponse;\r
+import com.indexdata.mkjsf.pazpar2.data.sp.CategoriesResponse;\r
+import com.indexdata.mkjsf.pazpar2.data.sp.TargetCategory;\r
 \r
 public class ResponseParser extends DefaultHandler {\r
 \r
@@ -30,7 +32,7 @@ public class ResponseParser extends DefaultHandler {
   private static Logger logger = Logger.getLogger(ResponseParser.class);\r
 \r
   public static List<String> docTypes = Arrays.asList(  "bytarget","termlist","show","stat","record","search","init",\r
-                                        /* SP extras */ "auth" );                                        \r
+                                        /* SP extras */ "auth", "categories" );                                        \r
   \r
   public ResponseParser() {\r
     try {\r
@@ -118,8 +120,14 @@ public class ResponseParser extends DefaultHandler {
       currentElement = new CommandError();\r
     } else if (localName.equals("error") && dataElements.peek().getType().equals("applicationerror")) {\r
       currentElement = new Pazpar2Error(); \r
+    // Service Proxy extras  \r
     } else if (localName.equals("auth")) {  \r
       currentElement = new AuthResponse();\r
+    } else if (localName.equals("categories")) {\r
+      currentElement = new CategoriesResponse();\r
+    } else if (localName.equals("category") && dataElements.peek().getType().equals("categories")) {\r
+      currentElement = new TargetCategory();\r
+    // Catch all\r
     } else {\r
       currentElement = new ResponseDataObject();\r
     }\r
index 1999774..5276eef 100644 (file)
@@ -16,6 +16,7 @@ import org.apache.log4j.Logger;
 \r
 import com.indexdata.mkjsf.errors.ErrorHelper;\r
 import com.indexdata.mkjsf.errors.ErrorInterface;\r
+import com.indexdata.mkjsf.pazpar2.data.sp.CategoriesResponse;\r
 import com.indexdata.mkjsf.utils.Utils;\r
 \r
 @Named("pzresp") @SessionScoped\r
@@ -89,16 +90,13 @@ public class Responses implements Serializable {
     resetSearchResponses();\r
     dataObjects.put("init", new InitResponse());\r
     dataObjects.put("auth", new AuthResponse());\r
+    dataObjects.put("categories", new CategoriesResponse());\r
   }\r
   \r
   public InitResponse getInit () {    \r
     return ((InitResponse) dataObjects.get("init"));\r
   }\r
   \r
-  public AuthResponse getAuth () {\r
-    return ((AuthResponse) dataObjects.get("auth"));\r
-  }\r
-\r
   public ShowResponse getShow () {\r
     return ((ShowResponse) dataObjects.get("show"));\r
   }\r
@@ -118,7 +116,7 @@ public class Responses implements Serializable {
   public TermListsResponse getTermLists () {\r
     return ((TermListsResponse) dataObjects.get("termlist"));\r
   }\r
-  \r
+    \r
   public List<TermResponse> getFacetTerms (String facet, int count) {\r
     return (getTermLists().getTermList(facet).getTerms(count));\r
   }\r
@@ -131,6 +129,17 @@ public class Responses implements Serializable {
     return ((ByTarget) dataObjects.get("bytarget"));\r
   }\r
 \r
+  // Service Proxy extras   \r
+  public AuthResponse getAuth () {\r
+    return ((AuthResponse) dataObjects.get("auth"));\r
+  }\r
+\r
+  public CategoriesResponse getCategories() {\r
+    return ((CategoriesResponse) dataObjects.get("categories"));\r
+  }\r
+  // Service Proxy extras\r
+  \r
+\r
   public ResponseDataObject getResponseObject (String name) {\r
     return dataObjects.get(name);\r
   }\r
diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/CategoriesResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/CategoriesResponse.java
new file mode 100644 (file)
index 0000000..52cd3d3
--- /dev/null
@@ -0,0 +1,25 @@
+package com.indexdata.mkjsf.pazpar2.data.sp;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import javax.enterprise.context.SessionScoped;\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
+\r
+@SessionScoped\r
+public class CategoriesResponse extends ResponseDataObject {\r
+\r
+  private static final long serialVersionUID = 5502182636437956412L;\r
+    \r
+  public List<TargetCategory> getTargetCategories() {\r
+    List<TargetCategory> targetCategories = new ArrayList<TargetCategory>();    \r
+    if (getElements("category") != null) {\r
+      for (ResponseDataObject element : getElements("category")) {\r
+        targetCategories.add((TargetCategory)element);\r
+      }\r
+    }\r
+    return targetCategories;\r
+  }\r
+\r
+  \r
+}\r
diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/TargetCategory.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/TargetCategory.java
new file mode 100644 (file)
index 0000000..3885792
--- /dev/null
@@ -0,0 +1,17 @@
+package com.indexdata.mkjsf.pazpar2.data.sp;\r
+\r
+import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
+\r
+public class TargetCategory extends ResponseDataObject {\r
+\r
+  private static final long serialVersionUID = -3027515807117682584L;\r
+\r
+  public String getCategoryName () {\r
+    return getOneElementValue("categoryName");\r
+  }\r
+  \r
+  public String getCategoryId() {\r
+    return getOneElementValue("categoryId");\r
+  }\r
+   \r
+}\r
index 2551c3b..13e3989 100644 (file)
@@ -15,6 +15,7 @@ import com.indexdata.mkjsf.pazpar2.commands.ShowCommand;
 import com.indexdata.mkjsf.pazpar2.commands.StatCommand;\r
 import com.indexdata.mkjsf.pazpar2.commands.TermlistCommand;\r
 import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand;\r
+import com.indexdata.mkjsf.pazpar2.commands.sp.CategoriesCommand;\r
 import com.indexdata.mkjsf.pazpar2.commands.sp.ServiceProxyCommands;\r
 \r
 /**\r
@@ -41,6 +42,7 @@ public class Pazpar2State {
     commands.put(Pazpar2Commands.BYTARGET, new BytargetCommand(mgr));  \r
 \r
     commands.put(ServiceProxyCommands.AUTH, new AuthCommand(mgr));\r
+    commands.put(ServiceProxyCommands.CATEGORIES, new CategoriesCommand(mgr));\r
     key = "#1";\r
   }\r
     \r
index 7106f6d..ef6d2f7 100644 (file)
@@ -21,7 +21,8 @@ public class StateManager implements Serializable {
 \r
   Map<String, Pazpar2State> states = new HashMap<String, Pazpar2State>();\r
   String currentKey = "";\r
-  private static List<String> allCommands = new ArrayList<String>(Arrays.asList("init","ping","settings","search","stat","show","record","termlist","bytarget","auth"));\r
+  private static List<String> allCommands = new ArrayList<String>(Arrays.asList("init","ping","settings","search","stat","show","record","termlist","bytarget",\r
+                                                                /* SP extras */ "auth","categories"));\r
   Map<String,Boolean> pendingStateChanges = new HashMap<String,Boolean>();\r
   private static Logger logger = Logger.getLogger(StateManager.class);\r
   private List<StateListener> listeners = new ArrayList<StateListener>();\r