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