From 1724e77234e35c61de392b943730b76771ce5bb3 Mon Sep 17 00:00:00 2001 From: "Niels Erik G. Nielsen" Date: Thu, 28 Feb 2013 16:51:52 -0500 Subject: [PATCH] Adds beans and configs --- src/META-INF/beans.xml | 7 + src/META-INF/context.xml | 9 + src/META-INF/faces-config.xml | 10 + .../config/FacesModuleConfiguration.java | 50 +++ .../config/JsfdemoModuleConfiguration.java | 17 + .../config/JsfdemoPazpar2ClientConfiguration.java | 19 + .../indexdata/pz2utils4jsf/controls/PageLink.java | 38 ++ .../pz2utils4jsf/controls/ResultsPager.java | 145 ++++++++ .../exceptions/CustomExceptionHandler.java | 49 +++ .../exceptions/CustomExceptionHandlerFactory.java | 20 + .../pz2utils4jsf/pazpar2/CommandParameter.java | 124 +++++++ .../pz2utils4jsf/pazpar2/CommandThread.java | 55 +++ .../indexdata/pz2utils4jsf/pazpar2/Expression.java | 25 ++ .../pz2utils4jsf/pazpar2/Pazpar2Command.java | 93 +++++ .../indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java | 261 +++++++++++++ .../pz2utils4jsf/pazpar2/Pz2Interface.java | 300 +++++++++++++++ .../indexdata/pz2utils4jsf/pazpar2/Pz2Session.java | 387 ++++++++++++++++++++ .../pz2utils4jsf/pazpar2/TargetFilter.java | 47 +++ .../pz2utils4jsf/pazpar2/data/ByTarget.java | 22 ++ .../indexdata/pz2utils4jsf/pazpar2/data/Hit.java | 47 +++ .../pz2utils4jsf/pazpar2/data/Location.java | 54 +++ .../pazpar2/data/Pazpar2ResponseData.java | 91 +++++ .../pazpar2/data/Pazpar2ResponseParser.java | 141 +++++++ .../pz2utils4jsf/pazpar2/data/RecordResponse.java | 59 +++ .../pz2utils4jsf/pazpar2/data/ShowResponse.java | 49 +++ .../pz2utils4jsf/pazpar2/data/StatResponse.java | 53 +++ .../pz2utils4jsf/pazpar2/data/Target.java | 33 ++ .../pazpar2/data/TermListResponse.java | 52 +++ .../pazpar2/data/TermListsResponse.java | 30 ++ .../pz2utils4jsf/pazpar2/data/TermResponse.java | 22 ++ .../pazpar2/data/TermXTargetResponse.java | 28 ++ .../pz2utils4jsf/pazpar2/state/QueryState.java | 147 ++++++++ .../pz2utils4jsf/pazpar2/state/QueryStates.java | 76 ++++ .../pz2utils4jsf/pz2utils/ListenerFieldIds.java | 22 ++ 34 files changed, 2582 insertions(+) create mode 100644 src/META-INF/beans.xml create mode 100644 src/META-INF/context.xml create mode 100644 src/META-INF/faces-config.xml create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/config/FacesModuleConfiguration.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/config/JsfdemoModuleConfiguration.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/config/JsfdemoPazpar2ClientConfiguration.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/controls/PageLink.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/controls/ResultsPager.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/exceptions/CustomExceptionHandler.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/exceptions/CustomExceptionHandlerFactory.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandParameter.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Expression.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pazpar2Command.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/TargetFilter.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ByTarget.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Hit.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Location.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/RecordResponse.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ShowResponse.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/StatResponse.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Target.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermListResponse.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermListsResponse.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermResponse.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermXTargetResponse.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryState.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryStates.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pz2utils/ListenerFieldIds.java diff --git a/src/META-INF/beans.xml b/src/META-INF/beans.xml new file mode 100644 index 0000000..b87599c --- /dev/null +++ b/src/META-INF/beans.xml @@ -0,0 +1,7 @@ + + + \ No newline at end of file diff --git a/src/META-INF/context.xml b/src/META-INF/context.xml new file mode 100644 index 0000000..7f18535 --- /dev/null +++ b/src/META-INF/context.xml @@ -0,0 +1,9 @@ + + + + WEB-INF/web.xml + META-INF/context.xml + \ No newline at end of file diff --git a/src/META-INF/faces-config.xml b/src/META-INF/faces-config.xml new file mode 100644 index 0000000..8e033da --- /dev/null +++ b/src/META-INF/faces-config.xml @@ -0,0 +1,10 @@ + + + + + com.indexdata.pz2utils4jsf.exceptions.CustomExceptionHandlerFactory + + \ No newline at end of file diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/FacesModuleConfiguration.java b/src/main/java/com/indexdata/pz2utils4jsf/config/FacesModuleConfiguration.java new file mode 100644 index 0000000..055ca0e --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/FacesModuleConfiguration.java @@ -0,0 +1,50 @@ +package com.indexdata.pz2utils4jsf.config; + +import java.io.IOException; +import java.io.Serializable; + +import javax.faces.context.ExternalContext; +import javax.faces.context.FacesContext; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; + +import com.indexdata.masterkey.config.MasterkeyConfiguration; +import com.indexdata.masterkey.config.ModuleConfiguration; +import com.indexdata.masterkey.config.ModuleConfigurationGetter; + +public abstract class FacesModuleConfiguration implements ModuleConfigurationGetter, Serializable { + + private static final long serialVersionUID = -7225977088088592928L; + private ModuleConfiguration config; + + public FacesModuleConfiguration() { + if (FacesContext.getCurrentInstance() != null) { + ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); + HttpServletRequest request = (HttpServletRequest) externalContext.getRequest(); + ServletContext servletContext = (ServletContext) externalContext.getContext(); + String host = request.getServerName(); + try { + config = MasterkeyConfiguration.getModuleConfiguration(servletContext, host, getComponentName(), getModuleName()); + } catch (IOException ioe) { + ioe.printStackTrace(); + } + } + } + + public String get(String name) { + return config.get(name); + } + + public String get(String name, String defaultValue) { + return config.get(name, defaultValue); + } + + public ModuleConfiguration getModuleConfiguration () { + return config; + } + + public abstract String getModuleName(); + + public abstract String getComponentName(); + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/JsfdemoModuleConfiguration.java b/src/main/java/com/indexdata/pz2utils4jsf/config/JsfdemoModuleConfiguration.java new file mode 100644 index 0000000..1d4b8bd --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/JsfdemoModuleConfiguration.java @@ -0,0 +1,17 @@ +package com.indexdata.pz2utils4jsf.config; + +import com.indexdata.pz2utils4jsf.config.FacesModuleConfiguration; + +public abstract class JsfdemoModuleConfiguration extends FacesModuleConfiguration { + + private static final long serialVersionUID = -2936339728016978922L; + + @Override + public abstract String getModuleName(); + + @Override + public String getComponentName() { + return "jsfdemo"; + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/config/JsfdemoPazpar2ClientConfiguration.java b/src/main/java/com/indexdata/pz2utils4jsf/config/JsfdemoPazpar2ClientConfiguration.java new file mode 100644 index 0000000..a19a95f --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/config/JsfdemoPazpar2ClientConfiguration.java @@ -0,0 +1,19 @@ +package com.indexdata.pz2utils4jsf.config; + +import javax.enterprise.context.SessionScoped; +import javax.inject.Named; + +import com.indexdata.pz2utils4jsf.config.JsfdemoModuleConfiguration; + +@Named("pz2clientConfig") +@SessionScoped +public class JsfdemoPazpar2ClientConfiguration extends JsfdemoModuleConfiguration { + + private static final long serialVersionUID = 8865086878660568870L; + + @Override + public String getModuleName() { + return "pz2client"; + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/controls/PageLink.java b/src/main/java/com/indexdata/pz2utils4jsf/controls/PageLink.java new file mode 100644 index 0000000..e9c4ac0 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/controls/PageLink.java @@ -0,0 +1,38 @@ +package com.indexdata.pz2utils4jsf.controls; + +import java.io.Serializable; + +import com.indexdata.pz2utils4jsf.controls.ResultsPager; + +public class PageLink implements Serializable { + + private static final long serialVersionUID = -468888598965842949L; + String text = ""; + int page = 0; + ResultsPager pager; + public PageLink(String text, int page, ResultsPager pager) { + this.text = text; + this.page = page; + this.pager = pager; + } + + public boolean isLink() { + return page>0; + } + + public boolean isCurrent() { + return (pager.getCurrentPageNum()==page); + } + + public String getText() { + return text; + } + + public int getPage() { + return page; + } + + public int getStart() { + return pager.getPageSize()*(page-1); + } +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/controls/ResultsPager.java b/src/main/java/com/indexdata/pz2utils4jsf/controls/ResultsPager.java new file mode 100644 index 0000000..95a2048 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/controls/ResultsPager.java @@ -0,0 +1,145 @@ +package com.indexdata.pz2utils4jsf.controls; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import com.indexdata.pz2utils4jsf.controls.PageLink; +import com.indexdata.pz2utils4jsf.pazpar2.Pz2Session; +import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse; + +public class ResultsPager implements Serializable { + + private static final long serialVersionUID = 8854795222615583071L; + private Pz2Session pz2session = null; + private int pageRangeLength = 13; + + public ResultsPager(Pz2Session session) { + this.pz2session = session; + } + + public ResultsPager(Pz2Session session, int pageRange) { + this.pz2session = session; + this.pageRangeLength = pageRange; + } + + private boolean hasHits () { + return (getShow().getMerged()>0); + } + + public int getCurrentPageNum () { + if (hasHits() && getShow().getNum()>0) { + return (getShow().getStart()/getShow().getNum())+1; + } else { + return 0; + } + } + + public int getPageSize() { + return getShow().getNum(); + } + + public int getFirstDisplayedPageNum () { + if (hasHits()) { + if (getCurrentPageNum() - (pageRangeLength/2) < 1) { + return 1; + } else { + return (getCurrentPageNum()-(pageRangeLength/2)); + } + } else { + return 0; + } + } + + public int getLastDisplayedPageNum () { + if (hasHits()) { + if ((getFirstDisplayedPageNum() + pageRangeLength-1) > getLastPageNum()) { + return getLastPageNum(); + } else { + return getFirstDisplayedPageNum() + pageRangeLength - 1; + } + } else { + return 0; + } + } + + public int getLastPageNum () { + if (hasHits()) { + return (int) Math.ceil(new Double(getShow().getMerged())/new Double(getShow().getNum())); + } else { + return 0; + } + } + + public List setPageLinks (int rangeLength) { + this.pageRangeLength = rangeLength; + return getPageLinks(); + } + + public List getPageLinks () { + ArrayList range = new ArrayList(); + if (hasHits()) { + for (int i = getFirstDisplayedPageNum(); i>0 && i<=getLastDisplayedPageNum();i++) { + range.add(new PageLink(i+"",i,this)); + } + } + return range; + } + + + public PageLink getPreviousPageLink (String text) { + String linkText = (text!=null && text.length()>0 ? text : "Prev"); + if (hasHits() && getCurrentPageNum()>1) { + return new PageLink(linkText,getCurrentPageNum()-1,this); + } else { + return new PageLink(linkText,0,this); + } + } + + public PageLink getNextPageLink (String text) { + String linkText = (text!=null && text.length()>0 ? text : "Next"); + if (hasHits() && getCurrentPageNum()1; + } + + public boolean hasNextPage () { + return getCurrentPage() < getLastPageNum(); + } + + public boolean hasPageAfterLastDisplayed() { + return getLastDisplayedPageNum() < getLastPageNum(); + } + + + private ShowResponse getShow() { + return pz2session.getShow(); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/exceptions/CustomExceptionHandler.java b/src/main/java/com/indexdata/pz2utils4jsf/exceptions/CustomExceptionHandler.java new file mode 100644 index 0000000..048ef05 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/exceptions/CustomExceptionHandler.java @@ -0,0 +1,49 @@ +package com.indexdata.pz2utils4jsf.exceptions; + +import java.util.Iterator; + +import javax.faces.FacesException; +import javax.faces.application.NavigationHandler; +import javax.faces.application.ViewExpiredException; +import javax.faces.context.ExceptionHandler; +import javax.faces.context.ExceptionHandlerWrapper; +import javax.faces.context.FacesContext; +import javax.faces.event.ExceptionQueuedEvent; +import javax.faces.event.ExceptionQueuedEventContext; + +public class CustomExceptionHandler extends ExceptionHandlerWrapper { + + private ExceptionHandler wrapped; + + public CustomExceptionHandler (ExceptionHandler wrapped) { + this.wrapped = wrapped; + } + + @Override + public ExceptionHandler getWrapped() { + return this.wrapped; + } + + @Override + public void handle() throws FacesException { + for (Iterator i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) { + ExceptionQueuedEvent event = i.next(); + ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource(); + Throwable t = context.getException(); + if (t instanceof ViewExpiredException) { + // ViewExpiredException vee = (ViewExpiredException) t; + FacesContext fc = FacesContext.getCurrentInstance(); + // Map requestMap = fc.getExternalContext().getRequestMap(); + NavigationHandler nav = fc.getApplication().getNavigationHandler(); + context.getContext().getPartialViewContext().setRenderAll(true); + try { + nav.handleNavigation(fc, null, "search"); + fc.renderResponse(); + } finally { + i.remove(); + } + } + } + getWrapped().handle(); + } +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/exceptions/CustomExceptionHandlerFactory.java b/src/main/java/com/indexdata/pz2utils4jsf/exceptions/CustomExceptionHandlerFactory.java new file mode 100644 index 0000000..ee60210 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/exceptions/CustomExceptionHandlerFactory.java @@ -0,0 +1,20 @@ +package com.indexdata.pz2utils4jsf.exceptions; + +import javax.faces.context.ExceptionHandler; +import javax.faces.context.ExceptionHandlerFactory; + +public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory { + + private ExceptionHandlerFactory parent; + + public CustomExceptionHandlerFactory (ExceptionHandlerFactory parent) { + this.parent = parent; + } + @Override + public ExceptionHandler getExceptionHandler() { + ExceptionHandler result = parent.getExceptionHandler(); + result = new CustomExceptionHandler(result); + return result; + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandParameter.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandParameter.java new file mode 100644 index 0000000..d589160 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandParameter.java @@ -0,0 +1,124 @@ +package com.indexdata.pz2utils4jsf.pazpar2; + +import java.io.Serializable; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.HashMap; +import java.util.Map; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter; +import com.indexdata.pz2utils4jsf.pazpar2.Expression; + +public class CommandParameter implements Serializable { + + private static Logger logger = Logger.getLogger(CommandParameter.class); + + private static final long serialVersionUID = 625502285668766749L; + String name = null; + String operator = null; + String value = null; + Map expressions = new HashMap(); + + public CommandParameter (String name) { + logger.debug("Instantiating command parameter (1) " + name); + this.name = name; + } + + public CommandParameter (String name, String operator, String value, Expression... expressions) { + logger.debug("Instantiating command parameter (2) " + name + " with " + expressions); + this.name = name; + this.operator = operator; + this.value = value; + for (Expression expr : expressions) { + this.expressions.put(expr.toString(), expr); + } + } + + public CommandParameter (String name, String operator, String value) { + logger.debug("Instantiating command parameter (3) " + name + " ("+this+") with " + name + " and " + value); + this.name = name; + this.operator = operator; + this.value = value; + } + + public CommandParameter (String name, String operator, int value) { + logger.debug("Instantiating command parameter (4) " + name + " ("+this+") with " + name + " and " + value); + this.name = name; + this.operator = operator; + this.value = value+""; + } + + + public String getName () { + return name; + } + + public Map getExpressions () { + return expressions; + } + + public void addExpression(Expression expression) { + logger.debug("Adding expression [" + expression + "] to " + this.getName() + " ("+this+")"); + this.expressions.put(expression.toString(),expression); + } + + public void removeExpression(Expression expression) { + this.expressions.remove(expression.toString()); + } + + + public boolean hasOperator() { + return operator != null; + } + + public String getEncodedQueryString () { + try { + return name + operator + URLEncoder.encode(getValueWithExpressions(),"UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + return null; + } + } + + public String getSimpleValue() { + return value; + } + + public String getValueWithExpressions () { + StringBuilder completeValue = new StringBuilder((value==null ? "" : value)); + for (String key : expressions.keySet()) { + completeValue.append(" and " + expressions.get(key)); + } + return completeValue.toString(); + + } + + @Override + public boolean equals (Object otherParameter) { + return + ((otherParameter instanceof CommandParameter) + && this.getValueWithExpressions().equals(((CommandParameter) otherParameter).getValueWithExpressions())); + } + + @Override + public int hashCode () { + return getValueWithExpressions().hashCode(); + } + + public String toString() { + return getValueWithExpressions(); + } + + public CommandParameter copy() { + CommandParameter newParam = new CommandParameter(name); + newParam.value = this.value; + newParam.operator = this.operator; + for (String key : expressions.keySet()) { + newParam.addExpression(expressions.get(key).copy()); + } + return newParam; + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java new file mode 100644 index 0000000..902ccb4 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java @@ -0,0 +1,55 @@ +package com.indexdata.pz2utils4jsf.pazpar2; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.pazpar2.CommandThread; +import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; +import com.indexdata.masterkey.pazpar2.client.ClientCommand; +import com.indexdata.masterkey.pazpar2.client.Pazpar2Client; +import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException; + +public class CommandThread extends Thread { + + private static Logger logger = Logger.getLogger(CommandThread.class); + Pazpar2Command command; + Pazpar2Client client; + private ByteArrayOutputStream baos = new ByteArrayOutputStream(); + private StringBuilder response = new StringBuilder(""); + + public CommandThread (Pazpar2Command command, Pazpar2Client client) { + this.command = command; + this.client = client; + } + + public void run() { + ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString()); + if (command.getName().equals("search")) { + client.setSearchCommand(clientCommand); + } + try { + long start = System.currentTimeMillis(); + client.executeCommand(clientCommand, baos); + response.append(baos.toString("UTF-8")); + long end = System.currentTimeMillis(); + logger.debug("Executed " + command.getName() + " in " + (end-start) + " ms." ); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Pazpar2ErrorException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public String getResponse () { + return response.toString(); + } + + public Pazpar2Command getCommand() { + return command; + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Expression.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Expression.java new file mode 100644 index 0000000..3b5260a --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Expression.java @@ -0,0 +1,25 @@ +package com.indexdata.pz2utils4jsf.pazpar2; + +import com.indexdata.pz2utils4jsf.pazpar2.Expression; + +public class Expression { + + String leftEntity; + String operator; + String rightEntity; + public Expression (String leftEntity, String operator, String rightEntity) { + this.leftEntity = leftEntity; + this.operator = operator; + this.rightEntity = rightEntity; + } + + public Expression copy() { + return new Expression(leftEntity,operator,rightEntity); + } + + public String toString() { + return leftEntity + operator + rightEntity; + } + + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pazpar2Command.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pazpar2Command.java new file mode 100644 index 0000000..9d30f64 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pazpar2Command.java @@ -0,0 +1,93 @@ +package com.indexdata.pz2utils4jsf.pazpar2; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter; +import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; + +public class Pazpar2Command implements Serializable { + + private static Logger logger = Logger.getLogger(Pazpar2Command.class); + private static final long serialVersionUID = -6825491856480675917L; + public static List allCommands = new ArrayList(Arrays.asList("init","ping","settings","search","stat","show","record","termlist","bytarget")); + + private String name = ""; + private Map parameters = new HashMap(); + + public Pazpar2Command (String name) { + this.name = name; + } + + public Pazpar2Command copy () { + Pazpar2Command newCommand = new Pazpar2Command(name); + for (String parameterName : parameters.keySet()) { + newCommand.setParameter(parameters.get(parameterName).copy()); + } + return newCommand; + } + + public String getName() { + return name; + } + + public void setParameter (CommandParameter parameter) { + logger.debug("Setting parameter " + parameter.getName() + "=" + parameter.getValueWithExpressions() + " to " + this.getName()); + parameters.put(parameter.getName(),parameter); + } + + public CommandParameter getParameter (String name) { + return parameters.get(name); + } + + public void removeParameter (String name) { + parameters.remove(name); + } + + public void removeParameters() { + parameters = new HashMap(); + } + + public boolean hasParameters () { + return (parameters.keySet().size()>0); + } + + public String getEncodedQueryString () { + StringBuilder queryString = new StringBuilder("command="+name); + for (CommandParameter parameter : parameters.values()) { + queryString.append("&"+parameter.getEncodedQueryString()); + } + return queryString.toString(); + } + + public String getValueWithExpressions() { + StringBuilder value = new StringBuilder(""); + for (CommandParameter parameter : parameters.values()) { + value.append("&" + parameter.getName() + parameter.operator + parameter.getValueWithExpressions()); + } + return value.toString(); + } + + @Override + public boolean equals (Object otherCommand) { + return + ((otherCommand instanceof Pazpar2Command) + && this.getValueWithExpressions().equals(((Pazpar2Command) otherCommand).getValueWithExpressions())); + } + + @Override + public int hashCode () { + return getValueWithExpressions().hashCode(); + } + + public String toString () { + return parameters.toString(); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java new file mode 100644 index 0000000..73042fb --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java @@ -0,0 +1,261 @@ +package com.indexdata.pz2utils4jsf.pazpar2; + +import java.io.Serializable; +import java.util.List; + +import javax.enterprise.context.SessionScoped; +import javax.inject.Named; + +import com.indexdata.pz2utils4jsf.controls.ResultsPager; +import com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface; +import com.indexdata.pz2utils4jsf.pazpar2.Pz2Session; +import com.indexdata.pz2utils4jsf.pazpar2.TargetFilter; +import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; +import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.StatResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.TermListsResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.TermResponse; + +@Named("pz2") +@SessionScoped +public class Pz2Bean implements Pz2Interface, Serializable { + + private static final long serialVersionUID = 3440277287081557861L; + Pz2Session pz2; + + public Pz2Bean () { + pz2 = new Pz2Session(); + } + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#doSearch(java.lang.String) + */ + public void doSearch(String query) { + pz2.doSearch(query); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#doSearch() + */ + public void doSearch() { + pz2.doSearch(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#update() + */ + public String update() { + return pz2.update(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#update(java.lang.String) + */ + public String update(String commands) { + return pz2.update(commands); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setQuery(java.lang.String) + */ + public void setQuery(String query) { + pz2.setQuery(query); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getQuery() + */ + public String getQuery() { + return pz2.getQuery(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setFacet(java.lang.String, java.lang.String) + */ + public void setFacet(String facetKey, String term) { + pz2.setFacet(facetKey, term); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#removeFacet(java.lang.String, java.lang.String) + */ + public void removeFacet(String facetKey, String term) { + pz2.removeFacet(facetKey, term); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setFacetOnQuery(java.lang.String, java.lang.String) + */ + public void setFacetOnQuery(String facetKey, String term) { + pz2.setFacetOnQuery(facetKey, term); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setTargetFilter(java.lang.String, java.lang.String) + */ + public void setTargetFilter(String targetId, String targetName) { + pz2.setTargetFilter(targetId, targetName); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#removeTargetFilter() + */ + public void removeTargetFilter () { + pz2.removeTargetFilter(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getTargetFilter() + */ + public TargetFilter getTargetFilter() { + return pz2.getTargetFilter(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#hasTargetFilter() + */ + public boolean hasTargetFilter() { + return pz2.hasTargetFilter(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setSort(java.lang.String) + */ + public void setSort(String sortOption) { + pz2.setSort(sortOption); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getSort() + */ + public String getSort() { + return pz2.getSort(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setPageSize(int) + */ + public void setPageSize(int perPageOption) { + pz2.setPageSize(perPageOption); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getPageSize() + */ + public int getPageSize() { + return pz2.getPageSize(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setStart(int) + */ + public void setStart(int start) { + pz2.setStart(start); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getStart() + */ + public int getStart() { + return pz2.getStart(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#toggleRecord(java.lang.String) + */ + public String toggleRecord(String recid) { + return pz2.toggleRecord(recid); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getShow() + */ + public ShowResponse getShow() { + return pz2.getShow(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getStat() + */ + public StatResponse getStat() { + return pz2.getStat(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#hasRecord(java.lang.String) + */ + public boolean hasRecord(String recId) { + return pz2.hasRecord(recId); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getRecord() + */ + public RecordResponse getRecord() { + return pz2.getRecord(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getTermLists() + */ + public TermListsResponse getTermLists() { + return pz2.getTermLists(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getFacetTerms(java.lang.String, int) + */ + public List getFacetTerms(String facet, int count) { + return pz2.getFacetTerms(facet, count); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getFacetTerms(java.lang.String) + */ + public List getFacetTerms(String facet) { + return pz2.getFacetTerms(facet); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getByTarget() + */ + public ByTarget getByTarget() { + return pz2.getByTarget(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#hasRecords() + */ + public boolean hasRecords() { + return pz2.hasRecords(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setPager(int) + */ + public ResultsPager setPager(int pageRange) { + return pz2.setPager(pageRange); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getPager() + */ + public ResultsPager getPager() { + return pz2.getPager(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getCurrentStateKey() + */ + public String getCurrentStateKey() { + return pz2.getCurrentStateKey(); + } + + /* (non-Javadoc) + * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setCurrentStateKey(java.lang.String) + */ + public void setCurrentStateKey(String key) { + pz2.setCurrentStateKey(key); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java new file mode 100644 index 0000000..6aa1921 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Interface.java @@ -0,0 +1,300 @@ +package com.indexdata.pz2utils4jsf.pazpar2; + +import java.util.List; + +import com.indexdata.pz2utils4jsf.controls.ResultsPager; +import com.indexdata.pz2utils4jsf.pazpar2.TargetFilter; +import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; +import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.StatResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.TermListsResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.TermResponse; + +public interface Pz2Interface { + + /** + * Executes a Pazpar2 search using the given query string + * + * @param query + */ + public void doSearch(String query); + + /** + * Executes a Pazpar2 search using the current query + */ + public void doSearch(); + + /** + * Updates display data objects by issuing the following pazpar2 commands: + * 'show', 'stat', 'termlist' and 'bytarget'. + * + * Returns a count of the remaining active clients from the most recent search. + * + * After refreshing the data from pazpar2 the UI components displaying those + * data should be re-rendered. + * + * @return count of activeclients + */ + public String update(); + + /** + * Updates the data objects given by a comma separated list of one or more commands - + * i.e. "show,state,termlist,bytarget". + * + * May not be useful for the UI directly. + * + * @param commands Command separated list of pazpar2 commands. + * @return count of activeclients + * + */ + public String update (String commands); + + /** + * Sets a query to used by the next search command + * + * @param query a query on pazpar2 query syntax + * + */ + public void setQuery (String query); + + /** + * Gets the current query + * @return a pazpar2 query string + */ + public String getQuery (); + + /** + * Sets a facet to limit the current query by, + * then executes the search + * + * @param facetKey i.e. 'au' for author + * @param term i.e. 'Dickens, Charles' + */ + public void setFacet(String facetKey, String term); + + /** + * Removes a facet set by setFacet(...), then executes + * the search. + * + * Will not remove facets set by setFacetOnQuery(...) + * + * @param facetKey i.e. 'au' for author + * @param term i.e. 'Dickens, Charles' + */ + public void removeFacet (String facetKey, String term); + + /** + * Sets a facet to limit the current query by. The + * facet is appended to the query string itself (rather + * as a separately managed entity. It will thus appear + * in a query field as retrieved by getQuery(). It will + * not be removed by removeFacet(...) + * + * @param facetKey i.e. 'au' for author + * @param term i.e. 'Dickens, Charles' + */ + public void setFacetOnQuery(String facetKey, String term); + + /** + * Adds a target filter to limit the current query by, then + * executes the current search. + * + * @param targetId pazpar2's ID for the target to limit by + * @param targetName a descriptive name for the target + */ + public void setTargetFilter (String targetId, String targetName); + + /** + * Removes the current target filter from the search + * + */ + public void removeTargetFilter (); + + /** + * + * @return The target filter set on the current search command + */ + public TargetFilter getTargetFilter(); + + /** + * Resolves if the current search command has a target filter - to + * be used by the UI for conditional rendering of target filter info. + * + * @return true if the current search command is limited by a target + * filter + */ + public boolean hasTargetFilter(); + + /** + * Sets the ordering of records (hits) in the 'show' display object + */ + + /** + * Sets the sort order for results, the updates the 'show' data object + * from pazpar2. Set valid sort options in the documentation for pazpar2. + * + * The parts of the UI that display 'show' data should be rendered following + * this request. + * + * @param sortOption + */ + public void setSort(String sortOption); + + /** + * Retrieves the current sort order for results + * @return sort order - i.e. 'relevance' + */ + public String getSort(); + + /** + * Sets the number of records that pazpar2 should show at a time. Is + * followed by an update of the show data object from pazpar2. + * + * To be used by the UI for paging. After setting page size the parts + * of the UI that displays 'show' data should be rendered. + * + * @param perPageOption i.e. 10, default is 20. + */ + public void setPageSize (int perPageOption); + + /** + * Retrieves the currently defined number of items to show at a time + * + * @return number of result records that will be shown from pazpar2 + */ + public int getPageSize(); + + /** + * Sets the first record to show - starting at record '0'. After setting + * first record number, the 'show' data object will be updated from pazpar2, + * and the parts of the UI displaying show data should be re-rendered. + * + * To be used by the UI for paging. + * + * @param start first record to show + */ + public void setStart (int start); + + /** + * Retrieves the sequence number of the record that pazpaz2 will return as + * the first record in 'show' + * + * @return sequence number of the first record to be shown (numbering starting at '0') + * + */ + public int getStart(); + + /** + * Will retrieve or remove the record with the given recid from memory. + * + * A pazpar2 'record' command will then be issued. The part of the UI + * showing record data should thus be re-rendered. + * + * @param recid + * @return + */ + public String toggleRecord(String recid); + + /** + * Returns the 'show' data as retrieved from pazpar2 by the most + * recent update request + * + * @return pazpar2 'show' response object + */ + public ShowResponse getShow(); + + /** + * Returns the 'stat' data as retrieved from pazpar2 by the most + * recent update request + * + * @return pazpar2 'stat' response object + */ + public StatResponse getStat(); + + /** + * Resolves whether the backend has a record with the given recid in memory + * + * @return true if the bean currently holds the record with recid + */ + public boolean hasRecord (String recId); + + /** + * Resolves whether the back-end has any records in memory (in 'show') for + * display in UI + * + * @return true if there are records to display + */ + public boolean hasRecords (); + + /** + * Returns a pazpar2 record as retrieved by the most recent 'record' + * request + * @return record data object + */ + public RecordResponse getRecord(); + + /** + * Returns a set of term lists (targets and facets) as retrieved by the + * most recent 'termlist' command + * @return set of termlists + */ + public TermListsResponse getTermLists (); + + /** + * Returns up to 'count' terms from the facet given by the 'facet' parameter + * @param facet name of the facet + * @param count maximum number of facet terms to return + * @return facet term list limited to 'count' terms + */ + public List getFacetTerms (String facet, int count); + + /** + * Returns all the terms of a given facet - or as many as pazpar2 returns + * @param facet name of the facet + * @return facet term list + */ + public List getFacetTerms (String facet); + + /** + * Returns a ByTarget data object as retrieved by the most recent 'bytarget' + * request to pazpar2 + * + * @return ByTarget response data object + */ + public ByTarget getByTarget(); + + /** + * Initiates a pager object, a component holding the data to draw a sequence + * of page numbers to navigate by and mechanisms to navigate with + * + * @param pageRange number of pages to display in the pager + * @return ResultsPager the initiated pager component + */ + public ResultsPager setPager(int pageRange); + + /** + * Gives a component for drawing a pager to navigate by. + * @return ResultsPager pager component + */ + public ResultsPager getPager(); + + /** + * Returns the current hash key used, as used for internal session state tracking + * and potentially for browser history entries as well + * + * A UI author would not normally be concerned with retrieving this. It's used by the + * framework internally + * + * @return string that can be used for browsers window.location.hash + */ + public String getCurrentStateKey (); + + /** + * Sets the current state key, i.e. when user clicks back or forward in browser history. + * Would normally be automatically handled by the frameworks components. + * + * @param key corresponding to browsers hash string + */ + public void setCurrentStateKey(String key); +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java new file mode 100644 index 0000000..0d8cc3f --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -0,0 +1,387 @@ +package com.indexdata.pz2utils4jsf.pazpar2; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.controls.ResultsPager; +import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter; +import com.indexdata.pz2utils4jsf.pazpar2.CommandThread; +import com.indexdata.pz2utils4jsf.pazpar2.Expression; +import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; +import com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface; +import com.indexdata.pz2utils4jsf.pazpar2.Pz2Session; +import com.indexdata.pz2utils4jsf.pazpar2.TargetFilter; +import com.indexdata.masterkey.pazpar2.client.Pazpar2Client; +import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration; +import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric; +import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException; +import com.indexdata.pz2utils4jsf.config.JsfdemoPazpar2ClientConfiguration; +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.ShowResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.StatResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.TermListsResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.TermResponse; +import com.indexdata.pz2utils4jsf.pazpar2.state.QueryStates; + +public class Pz2Session implements Serializable, Pz2Interface { + + private static Logger logger = Logger.getLogger(Pz2Session.class); + + private Map dataObjects = new ConcurrentHashMap(); + private QueryStates queryStates = new QueryStates(); + + private static final long serialVersionUID = 3947514708343320514L; + private Pazpar2ClientConfiguration cfg = null; + private Pazpar2Client client = null; + private TargetFilter targetFilter = null; + private ResultsPager pager = null; + + public Pz2Session () { + logger.debug("Instantiating pz2"); + try { + cfg = new Pazpar2ClientConfiguration(new JsfdemoPazpar2ClientConfiguration().getModuleConfiguration()); + client = new Pazpar2ClientGeneric(cfg); + resetDataObjects(); + } catch (ProxyErrorException e) { + e.printStackTrace(); + } + } + + public void doSearch(String query) { + setCommandParameter("search",new CommandParameter("query","=",query)); + doSearch(); + } + + public void doSearch() { + queryStates.hasPendingStateChange("search",false); + resetDataObjects(); + setCommandParameter("show",new CommandParameter("start","=",0)); + logger.info("Searching using "+getCommand("search").getParameter("query").getEncodedQueryString()); + doCommand("search"); + } + + /** + * Refreshes 'show', 'stat', 'termlist', and 'bytarget' data object from pazpar2 + * + * @return Number of activeclients at the time of the 'show' command. + */ + public String update () { + logger.info("Updating show,stat,termlist, and bytarget data from pazpar2"); + return update("show,stat,termlist,bytarget"); + } + + /** + * Refreshes the data objects listed in 'commands' from pazpar2 + * + * @param commands + * @return Number of activeclients at the time of the 'show' command + */ + public String update (String commands) { + if (hasQuery()) { + handleQueryStateChanges(commands); + logger.debug("Processing request for " + commands); + List threadList = new ArrayList(); + StringTokenizer tokens = new StringTokenizer(commands,","); + while (tokens.hasMoreElements()) { + threadList.add(new CommandThread(getCommand(tokens.nextToken()),client)); + } + for (CommandThread thread : threadList) { + thread.start(); + } + for (CommandThread thread : threadList) { + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + for (CommandThread thread : threadList) { + if (!thread.getCommand().getName().equals("search")) { + dataObjects.put(thread.getCommand().getName(), new Pazpar2ResponseParser().getObject(thread.getResponse())); + } + } + return getActiveClients(); + } else { + logger.info("Skipped requests for " + commands + " as there's not yet a query."); + resetDataObjects(); + return "0"; + } + + } + + public void setQuery (String query) { + logger.debug("Creating new command parameter for " + query); + setCommandParameter("search",new CommandParameter("query","=",query)); + } + + public String getQuery () { + return getCommandParameterValueSimple("search","query",null); + } + + public void setFacet (String facetKey, String term) { + if (term != null && term.length()>0) { + queryStates.getCurrentState().setCommandParameterExpression("search","query",new Expression(facetKey,"=",term),queryStates); + doSearch(); + } + } + + public void setFacetOnQuery (String facetKey, String term) { + String facetExpression = facetKey + "=" + term; + if (term != null && term.length()>0) { + setCommandParameter("search",new CommandParameter("query","=", getQuery() + " and " + facetExpression)); + doSearch(); + } + } + + public void removeFacet(String facetKey, String term) { + queryStates.getCurrentState().removeCommandParameterExpression("search","query",new Expression(facetKey,"=",term),queryStates); + doSearch(); + } + + public void setTargetFilter (String targetId, String targetName) { + if (hasTargetFilter(new TargetFilter(targetId,targetName))) { + logger.debug("Already using target filter " + this.targetFilter.getFilterExpression()); + } else { + this.targetFilter = new TargetFilter(targetId,targetName); + setCommandParameter("search",new CommandParameter("filter","=",this.targetFilter.getFilterExpression())); + doSearch(); + } + } + + public TargetFilter getTargetFilter () { + return targetFilter; + } + + public void removeTargetFilter () { + logger.debug("Removing target filter " + targetFilter.getFilterExpression()); + this.targetFilter = null; + removeCommandParameter("search","filter"); + doSearch(); + } + + public boolean hasTargetFilter() { + return targetFilter != null; + } + + public void setSort (String sortOption) { + logger.debug("Setting sort option: " + sortOption); + setCommandParameter("show",new CommandParameter("sort","=",sortOption)); + update("show"); + } + + public String getSort () { + return getCommandParameterValue("show","sort","relevance"); + } + + public void setPageSize (int perPageOption) { + if (getPageSize()!=perPageOption) { + logger.debug("Setting perpage option to " + perPageOption + " and resetting start page."); + setCommandParameter("show",new CommandParameter("num","=",perPageOption)); + setCommandParameter("show",new CommandParameter("start","=",0)); + update("show"); + } else { + logger.debug("Not updating page size, already is " + perPageOption); + } + } + + public int getPageSize () { + return getCommandParameterValue("show","num",20); + } + + public void setStart (int start) { + logger.debug("Setting start num to " + start); + setCommandParameter("show", new CommandParameter("start","=",start)); + update("show"); + } + + public int getStart() { + return getCommandParameterValue("show","start",0); + } + + public String toggleRecord (String recId) { + if (hasRecord(recId)) { + removeCommand("record"); + dataObjects.put("record", new RecordResponse()); + return ""; + } else { + return updateRecord(recId); + } + } + + private String updateRecord(String recId) { + setCommandParameter("record",new CommandParameter("id","=",recId)); + return doCommand("record"); + } + + public boolean hasRecord (String recId) { + return getCommand("record").hasParameters() && getRecord().getRecId().equals(recId); + } + + public ShowResponse getShow () { + return ((ShowResponse) dataObjects.get("show")); + } + + public StatResponse getStat () { + return ((StatResponse) dataObjects.get("stat")); + } + + public RecordResponse getRecord() { + return ((RecordResponse) dataObjects.get("record")); + } + + public TermListsResponse getTermLists () { + return ((TermListsResponse) dataObjects.get("termlist")); + } + + public List getFacetTerms (String facet, int count) { + return (getTermLists().getTermList(facet).getTerms(count)); + } + + public List getFacetTerms (String facet) { + return (getTermLists().getTermList(facet).getTerms()); + } + + public ByTarget getByTarget() { + return ((ByTarget) dataObjects.get("bytarget")); + } + + + public String getCurrentStateKey () { + return queryStates.getCurrentStateKey(); + } + + public void setCurrentStateKey(String key) { + logger.debug("************** request to set state key to: [" + key + "]"); + queryStates.setCurrentStateKey(key); + } + + private boolean hasTargetFilter(TargetFilter targetFilter) { + return hasTargetFilter() && targetFilter.equals(this.targetFilter); + } + + private boolean hasQuery() { + return !(getCommand("search").getParameter("query") == null); + } + + public boolean hasRecords () { + return getStat().getRecords() > 0 + && getShow().getHits() != null + && getShow().getHits().size()>0; + } + + public ResultsPager getPager () { + if (pager == null) { + pager = new ResultsPager(this); + } + return pager; + } + + public ResultsPager setPager (int pageRange) { + pager = new ResultsPager(this,pageRange); + return pager; + } + + private void handleQueryStateChanges (String commands) { + if (queryStates.hasPendingStateChange("search")) { + logger.debug("Found pending search change. Doing search before updating " + commands); + doSearch(); + } + if (queryStates.hasPendingStateChange("record") && ! commands.equals("record")) { + logger.debug("Found pending record ID change. Doing record before updating " + commands); + queryStates.hasPendingStateChange("record",false); + if (getCommand("record").hasParameters()) { + updateRecord(getCommand("record").getParameter("id").getSimpleValue()); + } else { + removeCommand("record"); + dataObjects.put("record", new RecordResponse()); + } + } + } + + private String getActiveClients() { + logger.debug("Active clients: "+getShow().getActiveClients()); + if (getShow()!=null) { + return getShow().getActiveClients(); + } else { + return ""; + } + } + + private Pazpar2Command getCommand(String name) { + return queryStates.getCurrentState().getCommand(name); + } + + private void setCommandParameter(String commandName, CommandParameter parameter) { + logger.debug("Setting parameter for " + commandName + ": " + parameter); + queryStates.getCurrentState().setCommandParameter(commandName, parameter, queryStates); + } + + + private void removeCommandParameter(String commandName, String parameterName) { + queryStates.getCurrentState().removeCommandParameter(commandName,parameterName,queryStates); + } + + private void removeCommand (String commandName) { + queryStates.getCurrentState().removeCommand(commandName, queryStates); + } + + private String getCommandParameterValue (String commandName, String parameterName, String defaultValue) { + Pazpar2Command command = getCommand(commandName); + if (command != null) { + CommandParameter parameter = command.getParameter(parameterName); + if (parameter != null) { + return parameter.getValueWithExpressions(); + } + } + return defaultValue; + } + + private String getCommandParameterValueSimple (String commandName, String parameterName, String defaultValue) { + Pazpar2Command command = getCommand(commandName); + if (command != null) { + CommandParameter parameter = command.getParameter(parameterName); + if (parameter != null) { + return parameter.getSimpleValue(); + } + } + return defaultValue; + } + + + private int getCommandParameterValue (String commandName, String parameterName, int defaultValue) { + Pazpar2Command command = getCommand(commandName); + if (command != null) { + CommandParameter parameter = command.getParameter(parameterName); + if (parameter != null) { + return Integer.parseInt(parameter.getSimpleValue()); + } + } + return defaultValue; + } + + private String doCommand(String commandName) { + Pazpar2Command command = getCommand(commandName); + logger.debug(command.getEncodedQueryString() + ": Results for "+ getCommand("search").getEncodedQueryString()); + return update(commandName); + } + + private void resetDataObjects() { + logger.debug("Resetting show,stat,termlist,bytarget 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()); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/TargetFilter.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/TargetFilter.java new file mode 100644 index 0000000..f4d117c --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/TargetFilter.java @@ -0,0 +1,47 @@ +package com.indexdata.pz2utils4jsf.pazpar2; + +import java.io.Serializable; + +import com.indexdata.pz2utils4jsf.pazpar2.TargetFilter; + +public class TargetFilter implements Serializable { + + private static final long serialVersionUID = 2389085467202526537L; + + private String targetName; + private String targetId; + + public TargetFilter (String targetId, String targetName) { + this.targetId = targetId; + this.targetName = targetName; + } + + public String getTargetName () { + return targetName; + } + + public String getTargetId () { + return targetId; + } + + public String getFilterExpression () { + return "pz:id="+targetId; + } + + @Override + public boolean equals(Object o) { + if (o instanceof TargetFilter) { + return targetName.equals(((TargetFilter) o).getTargetName()) && + targetId.equals(((TargetFilter) o).getTargetId()); + } else { + return false; + } + } + + @Override + public int hashCode () { + return (targetId+targetName).hashCode(); + } + + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ByTarget.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ByTarget.java new file mode 100644 index 0000000..39ccac0 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ByTarget.java @@ -0,0 +1,22 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import java.util.ArrayList; +import java.util.List; + +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; +import com.indexdata.pz2utils4jsf.pazpar2.data.Target; + +public class ByTarget extends Pazpar2ResponseData { + + private static final long serialVersionUID = 3960644950805644518L; + + public List getTargets() { + List targets = new ArrayList(); + if (getElements("target") != null) { + for (Pazpar2ResponseData element : getElements("target")) { + targets.add((Target)element); + } + } + return targets; + } +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Hit.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Hit.java new file mode 100644 index 0000000..6c2504e --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Hit.java @@ -0,0 +1,47 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import java.util.ArrayList; +import java.util.List; + +import com.indexdata.pz2utils4jsf.pazpar2.data.Location; +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; + +public class Hit extends Pazpar2ResponseData { + + + private static final long serialVersionUID = 9039281987691623220L; + + public List getLocations() { + List locations = new ArrayList(); + for (Pazpar2ResponseData element : getElements("location")) { + locations.add((Location)element); + } + return locations; + } + + public String getTitle () { + return getOneElementValue("md-title"); + } + + public String getTitleRemainder() { + return getOneElementValue("md-title-remainder"); + } + + public String getAuthor (String prefix) { + return getOneElement("md-author") != null ? prefix + getOneElement("md-author").getValue() : ""; + } + + public String getAuthor () { + return getOneElementValue("md-author"); + } + + public String getTitleResponsibility() { + return getOneElementValue("md-title-responsibility"); + } + + public String getRecId() { + return getOneElementValue("recid"); + } + + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Location.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Location.java new file mode 100644 index 0000000..526dda0 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Location.java @@ -0,0 +1,54 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; + + +public class Location extends Pazpar2ResponseData { + + private static final long serialVersionUID = -1386527442857478225L; + + public String getId() { + return getAttribute("id"); + } + + public String getName () { + return getAttribute("name"); + } + + public String getSubject() { + return getOneElementValue("md-subject"); + } + + public String getSubjects() { + StringBuilder builder = new StringBuilder(""); + for (Pazpar2ResponseData data : getElements("md-subject")) { + if (builder.length()==0) { + builder.append(data.getValue()); + } else { + builder.append(", "); + builder.append(data.getValue()); + } + } + return builder.toString(); + } + + public String getAuthor() { + return getOneElementValue("md-author"); + } + + public String getAuthors() { + StringBuilder builder = new StringBuilder(""); + if (getElements("md-author") != null) { + for (Pazpar2ResponseData data : getElements("md-author")) { + if (builder.length()==0) { + builder.append(data.getValue()); + } else { + builder.append(", "); + builder.append(data.getValue()); + } + } + } + return builder.toString(); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java new file mode 100644 index 0000000..9412f90 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseData.java @@ -0,0 +1,91 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; + +public class Pazpar2ResponseData implements Serializable { + + private static final long serialVersionUID = -3909755656714679959L; + String type = null; + HashMap attributes = new HashMap(); + HashMap> elements = new HashMap>(); + String textContent = ""; + + public void setType (String type) { + this.type = type; + } + + public String getType () { + return type; + } + + public void setAttribute (String name, String value) { + attributes.put(name, value); + } + + public String getAttribute (String name) { + return attributes.get(name); + } + + public void addElement (String name, Pazpar2ResponseData value) { + if (elements.containsKey(name)) { + elements.get(name).add(value); + } else { + List list = new ArrayList(); + list.add(value); + elements.put(name,list); + } + } + + public List getElements (String name) { + return elements.get(name); + } + + public Pazpar2ResponseData getOneElement (String name) { + if (elements.get(name) != null) { + return elements.get(name).get(0); + } else { + return null; + } + } + + public String getOneElementValue (String name) { + if (getOneElement(name)!=null && getOneElement(name).getValue().length()>0) { + return getOneElement(name).getValue(); + } else { + return ""; + } + } + + public void appendContent (String content) { + textContent = textContent + content; + } + + public String getValue () { + return textContent; + } + + public String getProperty(String name) { + List els = elements.get(name); + if (els != null) { + return els.get(0).getValue(); + } else { + return null; + } + } + + public int getIntValue(String name) { + String val = getOneElementValue(name); + if (val.length()==0) { + return 0; + } else { + return Integer.parseInt(val); + } + } + + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java new file mode 100644 index 0000000..a08fdbc --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Pazpar2ResponseParser.java @@ -0,0 +1,141 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Arrays; +import java.util.List; +import java.util.Stack; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.Attributes; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.DefaultHandler; + +import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; +import com.indexdata.pz2utils4jsf.pazpar2.data.Hit; +import com.indexdata.pz2utils4jsf.pazpar2.data.Location; +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; +import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.StatResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.Target; +import com.indexdata.pz2utils4jsf.pazpar2.data.TermListResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.TermListsResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.TermResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.TermXTargetResponse; + +public class Pazpar2ResponseParser extends DefaultHandler { + + private XMLReader xmlReader = null; + private Pazpar2ResponseData currentElement = null; + private Stack dataElements = new Stack(); + private Pazpar2ResponseData result = null; + + private static final List docTypes = + Arrays.asList("bytarget","termlist","show","stat","record"); + + public Pazpar2ResponseParser() { + try { + initSax(); + } catch (ParserConfigurationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + private void initSax() throws ParserConfigurationException, SAXException { + SAXParserFactory spf = SAXParserFactory.newInstance(); + spf.setNamespaceAware(true); + SAXParser saxParser = spf.newSAXParser(); + xmlReader = saxParser.getXMLReader(); + xmlReader.setContentHandler(this); + } + + public Pazpar2ResponseData getObject (String response) { + try { + xmlReader.parse(new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8")))); + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return result; + } + + /** + * Receive notification at the start of element + * + */ + @Override + public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { + if (localName.equals("show")) { + currentElement = new ShowResponse(); + } else if (localName.equals("hit")) { + currentElement = new Hit(); + } else if (localName.equals("location")) { + currentElement = new Location(); + } else if (localName.equals("bytarget")) { + currentElement = new ByTarget(); + } else if (localName.equals("target")) { + currentElement = new Target(); + } else if (localName.equals("stat")) { + currentElement = new StatResponse(); + } else if (localName.equals("termlist")) { + currentElement = new TermListsResponse(); + } else if (localName.equals("list")) { + currentElement = new TermListResponse(); + ((TermListResponse)currentElement).setName(atts.getValue("name")); + ((TermListsResponse)dataElements.peek()).addTermList((TermListResponse)currentElement); + } else if (localName.equals("term")) { + if (dataElements.peek().getAttribute("name").equals("xtargets")) { + currentElement = new TermXTargetResponse(); + } else { + currentElement = new TermResponse(); + } + ((TermListResponse)dataElements.peek()).addTerm((TermResponse)currentElement); + } else if (localName.equals("record")) { + currentElement = new RecordResponse(); + } else { + currentElement = new Pazpar2ResponseData(); + } + currentElement.setType(localName); + for (int i=0; i< atts.getLength(); i++) { + currentElement.setAttribute(atts.getLocalName(i), atts.getValue(i)); + } + if (!docTypes.contains(localName)) { + dataElements.peek().addElement(localName, currentElement); + } + dataElements.push(currentElement); + } + + @Override + public void characters(char[] ch, int start, int length) throws SAXException { + String data = new String(ch, start, length); + dataElements.peek().appendContent(data); + } + + @Override + public void endElement(String namespaceURI, String localName, String qName) throws SAXException { + if (dataElements.size()==1) { + result = dataElements.pop(); + } else { + dataElements.pop(); + } + } + + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/RecordResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/RecordResponse.java new file mode 100644 index 0000000..d1af720 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/RecordResponse.java @@ -0,0 +1,59 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import java.util.ArrayList; +import java.util.List; + +import com.indexdata.pz2utils4jsf.pazpar2.data.Location; +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; + +public class RecordResponse extends Pazpar2ResponseData { + + private static final long serialVersionUID = 6682722004285796002L; + + public String getRecId () { + return getOneElementValue("recid"); + } + + public List getLocations() { + List locations = new ArrayList(); + for (Pazpar2ResponseData element : getElements("location")) { + locations.add((Location)element); + } + return locations; + } + + public String getTitle() { + return getOneElementValue("md-title"); + } + + public String getDate() { + return getOneElementValue("md-date"); + } + + public String getAuthor() { + return getOneElementValue("md-author"); + } + + public String getSubject() { + return getOneElementValue("md-subject"); + } + + public String getSubjects() { + StringBuilder builder = new StringBuilder(""); + for (Pazpar2ResponseData data : getElements("md-subject")) { + if (builder.length()==0) { + builder.append(data.getValue()); + } else { + builder.append(", "); + builder.append(data.getValue()); + } + } + return builder.toString(); + } + + public Location getFirstLocation () { + return getLocations().size()>0 ? getLocations().get(0) : null; + } + + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ShowResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ShowResponse.java new file mode 100644 index 0000000..6c6af92 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/ShowResponse.java @@ -0,0 +1,49 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import java.util.ArrayList; +import java.util.List; + +import com.indexdata.pz2utils4jsf.pazpar2.data.Hit; +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; + +public class ShowResponse extends Pazpar2ResponseData { + + private static final long serialVersionUID = 7103554232106330370L; + + + public String getStatus() { + return getOneElement("status").getValue(); + } + + public String getActiveClients () { + return getOneElementValue("activeclients"); + } + + public int getMerged () { + return getIntValue("merged"); + } + + public String getTotal () { + return getOneElementValue("total"); + } + + public int getStart () { + return getIntValue("start"); + } + + public int getNum () { + return getIntValue("num"); + } + + public List getHits() { + List hits = new ArrayList(); + if (getElements("hit") != null) { + for (Pazpar2ResponseData element : getElements("hit")) { + hits.add((Hit)element); + } + } + return hits; + } + + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/StatResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/StatResponse.java new file mode 100644 index 0000000..c0df51d --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/StatResponse.java @@ -0,0 +1,53 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; + +public class StatResponse extends Pazpar2ResponseData { + + private static final long serialVersionUID = -6578979787689458761L; + + public int getHits() { + return getProperty("hits")==null ? 0 : Integer.parseInt(getProperty("hits")); + } + + public int getClients () { + return getIntValue("clients"); + } + + public int getActiveClients () { + return getIntValue("activeclients"); + } + + public int getRecords () { + return getIntValue("records"); + } + + public String getUnconnected() { + return getOneElementValue("unconnected"); + } + + public String getConnecting() { + return getOneElementValue("connecting"); + } + + public String getWorking() { + return getOneElementValue("working"); + } + + public String getIdle() { + return getOneElementValue("idle"); + } + + public String getFailed() { + return getOneElementValue("failed"); + } + + public String getError() { + return getOneElementValue("error"); + } + + public String getProgress() { + return getOneElementValue("progress"); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Target.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Target.java new file mode 100644 index 0000000..11526f3 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/Target.java @@ -0,0 +1,33 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; + +public class Target extends Pazpar2ResponseData { + + private static final long serialVersionUID = 3343881183545520108L; + + public String getId () { + return getOneElementValue("id"); + } + + public String getName() { + return getOneElementValue("name"); + } + + public String getHits() { + return getOneElementValue("hits"); + } + + public String getDiagnostic() { + return getOneElementValue("diagnostic"); + } + + public String getRecords() { + return getOneElementValue("records"); + } + + public String getState () { + return getOneElementValue("state"); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermListResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermListResponse.java new file mode 100644 index 0000000..a4a33fb --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermListResponse.java @@ -0,0 +1,52 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; +import com.indexdata.pz2utils4jsf.pazpar2.data.TermListResponse; +import com.indexdata.pz2utils4jsf.pazpar2.data.TermResponse; + +public class TermListResponse extends Pazpar2ResponseData { + + private static Logger logger = Logger.getLogger(TermListResponse.class); + private static final long serialVersionUID = 3838585739723097393L; + String name = ""; + List terms = new ArrayList(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getTerms() { + return terms; + } + + public List getTerms(int count) { + List firstTerms = new ArrayList(); + for (int i=0; i terms) { + this.terms = terms; + } + + public void addTerm(TermResponse term) { + terms.add(term); + } + + public String toString () { + return terms.toString(); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermListsResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermListsResponse.java new file mode 100644 index 0000000..c060579 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermListsResponse.java @@ -0,0 +1,30 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import java.util.HashMap; +import java.util.Map; + +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; +import com.indexdata.pz2utils4jsf.pazpar2.data.TermListResponse; + +public class TermListsResponse extends Pazpar2ResponseData { + + private static final long serialVersionUID = -1370643625715834978L; + private int activeClients = -1; + private Map termLists = new HashMap(); + + public int getActiveClients() { + return activeClients; + } + public void setActiveClients(int activeClients) { + this.activeClients = activeClients; + } + + public void addTermList(TermListResponse termList) { + this.termLists.put(termList.getName(),termList); + } + public TermListResponse getTermList(String name) { + return termLists.get(name); + } + + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermResponse.java new file mode 100644 index 0000000..5b2e90e --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermResponse.java @@ -0,0 +1,22 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; + +public class TermResponse extends Pazpar2ResponseData { + + private static final long serialVersionUID = -8323959763575180678L; + + protected int frequency = -1; + + public String getName() { + return getProperty("name"); + } + public int getFrequency() { + return Integer.parseInt(getProperty("frequency")); + } + + public String toString() { + return getProperty("name"); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermXTargetResponse.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermXTargetResponse.java new file mode 100644 index 0000000..a435b34 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/data/TermXTargetResponse.java @@ -0,0 +1,28 @@ +package com.indexdata.pz2utils4jsf.pazpar2.data; + +import com.indexdata.pz2utils4jsf.pazpar2.data.TermResponse; + +public class TermXTargetResponse extends TermResponse { + + private static final long serialVersionUID = 5201902652960804977L; + + public String getId() { + return getOneElement("id").getValue(); + } + public int getApproximation () { + return Integer.parseInt(getOneElement("approximation").getValue()); + } + public int getRecords () { + return Integer.parseInt(getOneElement("records").getValue()); + } + public int getFiltered () { + return Integer.parseInt(getOneElement("filtered").getValue()); + } + public String getState () { + return getOneElement("state").getValue(); + } + public String getDiagnostic () { + return getOneElement("diagnostic").getValue(); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryState.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryState.java new file mode 100644 index 0000000..5f8ef23 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryState.java @@ -0,0 +1,147 @@ +package com.indexdata.pz2utils4jsf.pazpar2.state; + +import java.io.Serializable; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter; +import com.indexdata.pz2utils4jsf.pazpar2.Expression; +import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; +import com.indexdata.pz2utils4jsf.pazpar2.state.QueryState; +import com.indexdata.pz2utils4jsf.pazpar2.state.QueryStates; + +public class QueryState implements Serializable { + + private static final long serialVersionUID = -1465676392610954168L; + private static Logger logger = Logger.getLogger(QueryState.class); + private Map pz2commands = new HashMap(); + private String key = null; + + public QueryState () { + for (String command : Arrays.asList("init","ping","settings","search","stat","show","record","termlist","bytarget")) { + pz2commands.put(command, new Pazpar2Command(command)); + } + } + + private QueryState copy() { + QueryState newState = new QueryState(); + for (String commandName : pz2commands.keySet()) { + newState.setCommand(pz2commands.get(commandName).copy()); + } + return newState; + } + + public void setCommandParameter(String commandName, CommandParameter parameter, QueryStates queryStates) { + CommandParameter current = getCommand(commandName).getParameter(parameter.getName()); + if (current != null && current.equals(parameter)) { + logger.debug("Recieved parameter but already have " + parameter.getValueWithExpressions() + " in this state. No state change."); + } else { + logger.debug("New command parameter received: " + parameter.getValueWithExpressions() + ". Initiating new state."); + QueryState newState = this.copy(); + newState._setCommandParameter(commandName,parameter); + logger.debug("Old state: " + this); + logger.debug("New state: " + newState); + queryStates.setCurrentState(newState); + } + } + + public void removeCommandParameter(String commandName, String parameterName, QueryStates queryStates) { + QueryState newState = this.copy(); + newState._removeCommandParameter(commandName, parameterName); + queryStates.setCurrentState(newState); + } + + public void setCommandParameterExpression (String commandName, String parameterName, Expression expression, QueryStates queryStates) { + QueryState newState = this.copy(); + newState._setCommandParameterExpression(commandName, parameterName, expression); + queryStates.setCurrentState(newState); + } + + public void _setCommandParameterExpression (String commandName, String parameterName, Expression expression) { + getCommand(commandName).getParameter(parameterName).addExpression(expression); + } + + public void removeCommandParameterExpression (String commandName, String parameterName, Expression expression, QueryStates queryStates) { + QueryState newState = this.copy(); + newState._removeCommandParameterExpression(commandName, parameterName, expression); + queryStates.setCurrentState(newState); + } + + public void _removeCommandParameterExpression (String commandName, String parameterName, Expression expression) { + getCommand(commandName).getParameter(parameterName).removeExpression(expression); + + } + + public void _setCommandParameter(String commandName, CommandParameter parameter) { + getCommand(commandName).setParameter(parameter); + } + + public void _removeCommandParameter(String commandName, String parameterName) { + getCommand(commandName).removeParameter(parameterName); + } + + public void _removeCommand(String commandName) { + getCommand(commandName).removeParameters(); + } + + + public void removeCommand (String commandName, QueryStates queryStates) { + QueryState newState = this.copy(); + newState._removeCommand(commandName); + queryStates.setCurrentState(newState); + + } + + public Pazpar2Command getCommand(String name) { + return pz2commands.get(name); + } + + private void setCommand(Pazpar2Command command) { + pz2commands.put(command.getName(), command); + } + + public String getKey() { + if (key == null) { + StringBuilder querystatebuilder = new StringBuilder("#"); + for (Pazpar2Command command : pz2commands.values()) { + if (command.hasParameters()) { + querystatebuilder.append("||"+command.getName()+"::"); + querystatebuilder.append(command.getValueWithExpressions()); + } + } + key = querystatebuilder.toString(); + return key; + } else { + return key; + } + } + + public void setKey(String key) { + logger.debug("Setting key on demand to: " + key); + this.key = key; + } + + public String toString () { + return pz2commands.toString(); + } + + public boolean searchEquals(Object otherQueryState) { + if (otherQueryState instanceof QueryState) { + return getCommand("search").equals(((QueryState) otherQueryState).getCommand("search")); + } else { + return false; + } + } + + public boolean equals (Object otherQueryState) { + if (otherQueryState instanceof QueryState) { + return this.toString().equals(otherQueryState.toString()); + } else { + return false; + } + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryStates.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryStates.java new file mode 100644 index 0000000..ea1264d --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryStates.java @@ -0,0 +1,76 @@ +package com.indexdata.pz2utils4jsf.pazpar2.state; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; +import com.indexdata.pz2utils4jsf.pazpar2.state.QueryState; +import com.indexdata.pz2utils4jsf.pazpar2.state.QueryStates; + +public class QueryStates implements Serializable { + + private static final long serialVersionUID = 6131720167974584659L; + private static Logger logger = Logger.getLogger(QueryStates.class); + + Map queryStates = new HashMap(); + String currentStateKey = ""; + Map pendingStateChanges = new HashMap(); + + public QueryStates () { + queryStates.put("#initial", new QueryState()); + currentStateKey = "#initial"; + for (String command : Pazpar2Command.allCommands) { + pendingStateChanges.put(command, new Boolean(false)); + } + } + + public String getCurrentStateKey() { + return currentStateKey; + } + + public void setCurrentStateKey(String key) { + + if (currentStateKey.equals(key)) { + logger.debug("setCurrentStateKey: no key change detected"); + } else { + logger.debug("State key change. Was: [" + currentStateKey + "]. Will be ["+key+"]"); + if (queryStates.get(key).getCommand("search").equals(getCurrentState().getCommand("search"))) { + logger.debug("No search change detected"); + } else { + hasPendingStateChange("search",true); + } + if (queryStates.get(key).getCommand("record").equals(getCurrentState().getCommand("record"))) { + logger.debug("No record change detected"); + } else { + hasPendingStateChange("record",true); + } + currentStateKey = key; + } + } + + public QueryState getCurrentState() { + if (queryStates.get(currentStateKey) == null) { + return new QueryState(); + } else { + return queryStates.get(currentStateKey); + } + } + + public void setCurrentState(QueryState queryState) { + logger.debug("Setting current state: " + queryState.getKey()); + queryStates.put(queryState.getKey(), queryState); + setCurrentStateKey(queryState.getKey()); + } + + public void hasPendingStateChange(String command, boolean bool) { + pendingStateChanges.put(command, new Boolean(bool)); + } + + public boolean hasPendingStateChange (String command) { + return pendingStateChanges.get(command).booleanValue(); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pz2utils/ListenerFieldIds.java b/src/main/java/com/indexdata/pz2utils4jsf/pz2utils/ListenerFieldIds.java new file mode 100644 index 0000000..02776f8 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pz2utils/ListenerFieldIds.java @@ -0,0 +1,22 @@ +package com.indexdata.pz2utils4jsf.pz2utils; + +import java.io.Serializable; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Named; + +@Named("pz2watch") +@ApplicationScoped +public class ListenerFieldIds implements Serializable { + + private static final long serialVersionUID = -57079241763914538L; + + public String getHistory () { + return ":pz2watch:stateForm:windowlocationhash"; + } + + public String getActiveclients () { + return ":pz2watch:activeclientsForm:activeclientsField"; + } + +} -- 1.7.10.4