1 package com.indexdata.mkjsf.pazpar2;
\r
3 import java.io.Serializable;
\r
4 import java.util.ArrayList;
\r
5 import java.util.List;
\r
6 import java.util.StringTokenizer;
\r
8 import javax.annotation.PostConstruct;
\r
9 import javax.enterprise.context.SessionScoped;
\r
10 import javax.enterprise.inject.Alternative;
\r
11 import javax.inject.Inject;
\r
12 import javax.inject.Named;
\r
14 import org.apache.log4j.Logger;
\r
16 import com.indexdata.mkjsf.config.ConfigurationReader;
\r
17 import com.indexdata.mkjsf.controls.ResultsPager;
\r
18 import com.indexdata.mkjsf.errors.ConfigurationError;
\r
19 import com.indexdata.mkjsf.errors.ConfigurationException;
\r
20 import com.indexdata.mkjsf.errors.ErrorCentral;
\r
21 import com.indexdata.mkjsf.errors.ErrorHelper;
\r
22 import com.indexdata.mkjsf.pazpar2.commands.CommandParameter;
\r
23 import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Commands;
\r
24 import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseData;
\r
25 import com.indexdata.mkjsf.pazpar2.data.Pazpar2ResponseParser;
\r
26 import com.indexdata.mkjsf.pazpar2.data.Pazpar2Responses;
\r
27 import com.indexdata.mkjsf.pazpar2.data.RecordResponse;
\r
28 import com.indexdata.mkjsf.pazpar2.state.StateListener;
\r
29 import com.indexdata.mkjsf.pazpar2.state.StateManager;
\r
30 import com.indexdata.mkjsf.utils.Utils;
\r
32 @Named("pz2") @SessionScoped @Alternative
\r
33 public class Pz2Bean implements Pz2Interface, StateListener, Serializable {
\r
35 private static final long serialVersionUID = 3440277287081557861L;
\r
36 private static Logger logger = Logger.getLogger(Pz2Bean.class);
\r
37 private static Logger responseLogger = Logger.getLogger("com.indexdata.mkjsf.pazpar2.responses");
\r
39 protected SearchClient searchClient = null;
\r
41 @Inject ConfigurationReader configurator;
\r
42 @Inject StateManager stateMgr;
\r
43 @Inject Pazpar2Commands pzreq;
\r
44 @Inject Pazpar2Responses pzresp;
\r
45 @Inject ErrorCentral errors;
\r
47 protected ResultsPager pager = null;
\r
50 protected ErrorHelper errorHelper = null;
\r
53 logger.info("Instantiating pz2 bean [" + Utils.objectId(this) + "]");
\r
57 public void postConstruct() {
\r
58 logger.debug("in start of Pz2Bean post-construct configurator is " + configurator);
\r
59 logger.debug(Utils.objectId(this) + " will instantiate a Pz2Client next.");
\r
60 searchClient = new Pz2Client();
\r
61 logger.info("Using [" + Utils.objectId(searchClient) + "] configured by ["
\r
62 + Utils.objectId(configurator) + "]" );
\r
63 configureClient(searchClient,configurator);
\r
64 stateMgr.addStateListener(this);
\r
67 public void configureClient(SearchClient searchClient, ConfigurationReader configReader) {
\r
68 logger.debug(Utils.objectId(this) + " will configure search client for the session");
\r
70 searchClient.configure(configReader);
\r
71 } catch (ConfigurationException e) {
\r
72 logger.debug("Pz2Bean adding configuration error");
\r
73 errors.addConfigurationError(new ConfigurationError("Search Client","Configuration",e.getMessage()));
\r
75 logger.info(configReader.document());
\r
80 public void doSearch(String query) {
\r
81 pzreq.getSearch().setParameter(new CommandParameter("query","=",query));
\r
85 public void doSearch() {
\r
86 stateMgr.hasPendingStateChange("search",false);
\r
88 // resets some record and show command parameters without
\r
89 // changing state or creating state change feedback
\r
90 pzreq.getRecord().removeParametersInState();
\r
91 pzreq.getShow().setParameterInState(new CommandParameter("start","=",0));
\r
92 logger.debug(Utils.objectId(this) + " is searching using "+pzreq.getCommand("search").getUrlEncodedParameterValue("query"));
\r
93 doCommand("search");
\r
97 * Refreshes 'show', 'stat', 'termlist', and 'bytarget' data object from pazpar2
\r
99 * @return Number of activeclients at the time of the 'show' command.
\r
101 public String update () {
\r
102 logger.debug("Updating show,stat,termlist,bytarget from pazpar2");
\r
103 return update("show,stat,termlist,bytarget");
\r
107 * Refreshes the data objects listed in 'commands' from pazpar2
\r
110 * @return Number of activeclients at the time of the 'show' command
\r
112 public String update (String commands) {
\r
113 if (! errors.hasConfigurationErrors()) {
\r
114 if (commandsAreValid(commands)) {
\r
116 handleQueryStateChanges(commands);
\r
117 logger.debug("Processing request for " + commands);
\r
118 List<CommandThread> threadList = new ArrayList<CommandThread>();
\r
119 StringTokenizer tokens = new StringTokenizer(commands,",");
\r
120 while (tokens.hasMoreElements()) {
\r
121 threadList.add(new CommandThread(pzreq.getCommand(tokens.nextToken()),searchClient));
\r
123 for (CommandThread thread : threadList) {
\r
126 for (CommandThread thread : threadList) {
\r
129 } catch (InterruptedException e) {
\r
130 e.printStackTrace();
\r
133 for (CommandThread thread : threadList) {
\r
134 String commandName = thread.getCommand().getName();
\r
135 String response = thread.getResponse();
\r
136 responseLogger.debug("Response was: " + response);
\r
137 Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response);
\r
138 pzresp.put(commandName, responseObject);
\r
140 if (commands.equals("record")) {
\r
141 logger.debug("Record: Active clients: "+pzresp.getRecord().getActiveClients());
\r
142 return pzresp.getRecord().getActiveClients();
\r
144 return pzresp.getActiveClients();
\r
147 logger.debug("Skipped requests for " + commands + " as there's not yet a query.");
\r
152 logger.error("Did not attemt to run command(s) due to a validation error.");
\r
156 logger.error("Did not attempt to execute query since there are configuration errors.");
\r
162 public boolean commandsAreValid(String commands) {
\r
163 if (commands.equals("record")) {
\r
164 if (!pzreq.getCommand("record").hasParameterSet("id")) {
\r
165 logger.error("Attempt to send record command without the id parameter");
\r
172 public String toggleRecord (String recId) {
\r
173 if (hasRecord(recId)) {
\r
174 pzreq.getRecord().removeParameters();
\r
175 pzresp.put("record", new RecordResponse());
\r
178 pzreq.getRecord().setId(recId);
\r
179 return doCommand("record");
\r
184 public boolean hasRecord (String recId) {
\r
185 return pzreq.getCommand("record").hasParameters() && pzresp.getRecord().getRecId().equals(recId);
\r
188 public String getCurrentStateKey () {
\r
189 return stateMgr.getCurrentState().getKey();
\r
192 public void setCurrentStateKey(String key) {
\r
193 stateMgr.setCurrentStateKey(key);
\r
196 protected boolean hasQuery() {
\r
197 return pzreq.getCommand("search").hasParameterSet("query");
\r
202 public ResultsPager getPager () {
\r
203 if (pager == null) {
\r
204 pager = new ResultsPager(pzresp);
\r
210 public ResultsPager setPager (int pageRange) {
\r
211 pager = new ResultsPager(pzresp,pageRange,pzreq);
\r
215 protected void handleQueryStateChanges (String commands) {
\r
216 if (stateMgr.hasPendingStateChange("search") && hasQuery()) {
\r
217 logger.debug("Found pending search change. Doing search before updating " + commands);
\r
220 if (stateMgr.hasPendingStateChange("record") && ! commands.equals("record")) {
\r
221 logger.debug("Found pending record ID change. Doing record before updating " + commands);
\r
222 stateMgr.hasPendingStateChange("record",false);
\r
223 if (pzreq.getCommand("record").hasParameterSet("id")) {
\r
226 pzresp.put("record", new RecordResponse());
\r
231 protected String doCommand(String commandName) {
\r
232 logger.debug(pzreq.getCommand(commandName).getEncodedQueryString() + ": Results for "+ pzreq.getCommand("search").getEncodedQueryString());
\r
233 return update(commandName);
\r
237 public void stateUpdated(String commandName) {
\r
238 logger.debug("State change reported for [" + commandName + "]");
\r
239 if (commandName.equals("show")) {
\r
240 logger.debug("Updating show");
\r
241 update(commandName);
\r
246 public boolean getAuthenticationRequired () {
\r
247 return searchClient.isAuthenticatingClient();
\r
251 public String getCheckHistory () {
\r
252 return ":pz2watch:stateForm:windowlocationhash";
\r
256 public String getWatchActiveclients () {
\r
257 return ":pz2watch:activeclientsForm:activeclientsField";
\r
261 public String getWatchActiveclientsRecord () {
\r
262 return ":pz2watch:activeclientsForm:activeclientsFieldRecord";
\r