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
114 if (! errors.hasConfigurationErrors()) {
\r
115 if (commandsAreValid(commands)) {
\r
116 if (hasQuery() || (commands.equals("record") && pzreq.getCommand("record").hasParameterValue("recordquery"))) {
\r
117 handleQueryStateChanges(commands);
\r
118 logger.debug("Processing request for " + commands);
\r
119 List<CommandThread> threadList = new ArrayList<CommandThread>();
\r
120 StringTokenizer tokens = new StringTokenizer(commands,",");
\r
121 while (tokens.hasMoreElements()) {
\r
122 threadList.add(new CommandThread(pzreq.getCommand(tokens.nextToken()),searchClient));
\r
124 for (CommandThread thread : threadList) {
\r
127 for (CommandThread thread : threadList) {
\r
130 } catch (InterruptedException e) {
\r
131 e.printStackTrace();
\r
134 for (CommandThread thread : threadList) {
\r
135 String commandName = thread.getCommand().getName();
\r
136 String response = thread.getResponse();
\r
137 responseLogger.debug("Response was: " + response);
\r
138 Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response);
\r
139 if (Pazpar2ResponseParser.docTypes.contains(responseObject.getType())) {
\r
140 pzresp.put(commandName, responseObject);
\r
142 if (commandName.equals("record") &&
\r
143 (pzreq.getRecord().hasParameterValue("offset") ||
\r
144 pzreq.getRecord().hasParameterValue("checksum"))) {
\r
145 RecordResponse recordResponse = new RecordResponse();
\r
146 recordResponse.setType("record");
\r
147 recordResponse.setXml(responseObject.getXml());
\r
148 recordResponse.setAttribute("activeclients", "0");
\r
149 pzresp.put(commandName, recordResponse);
\r
153 if (commands.equals("record")) {
\r
154 return pzresp.getRecord().getActiveClients();
\r
156 return pzresp.getActiveClients();
\r
159 logger.debug("Skipped requests for " + commands + " as there's not yet a query.");
\r
164 logger.error("Did not attemt to run command(s) due to a validation error.");
\r
168 logger.error("Did not attempt to execute query since there are configuration errors.");
\r
171 } catch (ClassCastException cce) {
\r
172 cce.printStackTrace();
\r
174 } catch (NullPointerException npe) {
\r
175 npe.printStackTrace();
\r
181 public boolean commandsAreValid(String commands) {
\r
182 if (commands.equals("record")) {
\r
183 if (!pzreq.getCommand("record").hasParameterValue("id")) {
\r
184 logger.error("Attempt to send record command without the id parameter");
\r
191 public String toggleRecord (String recId) {
\r
192 if (hasRecord(recId)) {
\r
193 pzreq.getRecord().removeParameters();
\r
194 pzresp.put("record", new RecordResponse());
\r
197 pzreq.getRecord().setId(recId);
\r
198 return doCommand("record");
\r
203 public boolean hasRecord (String recId) {
\r
204 return pzreq.getCommand("record").hasParameters() && pzresp.getRecord().getRecId().equals(recId);
\r
207 public String getCurrentStateKey () {
\r
208 return stateMgr.getCurrentState().getKey();
\r
211 public void setCurrentStateKey(String key) {
\r
212 stateMgr.setCurrentStateKey(key);
\r
215 protected boolean hasQuery() {
\r
216 return pzreq.getCommand("search").hasParameterValue("query");
\r
221 public ResultsPager getPager () {
\r
222 if (pager == null) {
\r
223 pager = new ResultsPager(pzresp);
\r
229 public ResultsPager setPager (int pageRange) {
\r
230 pager = new ResultsPager(pzresp,pageRange,pzreq);
\r
234 protected void handleQueryStateChanges (String commands) {
\r
235 if (stateMgr.hasPendingStateChange("search") && hasQuery()) {
\r
236 logger.debug("Found pending search change. Doing search before updating " + commands);
\r
239 if (stateMgr.hasPendingStateChange("record") && ! commands.equals("record")) {
\r
240 logger.debug("Found pending record ID change. Doing record before updating " + commands);
\r
241 stateMgr.hasPendingStateChange("record",false);
\r
242 if (pzreq.getCommand("record").hasParameterValue("id")) {
\r
245 pzresp.put("record", new RecordResponse());
\r
250 protected String doCommand(String commandName) {
\r
251 logger.debug(pzreq.getCommand(commandName).getEncodedQueryString() + ": Results for "+ pzreq.getCommand("search").getEncodedQueryString());
\r
252 return update(commandName);
\r
256 public void stateUpdated(String commandName) {
\r
257 logger.debug("State change reported for [" + commandName + "]");
\r
258 if (commandName.equals("show")) {
\r
259 logger.debug("Updating show");
\r
260 update(commandName);
\r
265 public boolean getAuthenticationRequired () {
\r
266 return searchClient.isAuthenticatingClient();
\r
270 public String getCheckHistory () {
\r
271 return ":pz2watch:stateForm:windowlocationhash";
\r
275 public String getWatchActiveclients () {
\r
276 return ":pz2watch:activeclientsForm:activeclientsField";
\r
280 public String getWatchActiveclientsRecord () {
\r
281 return ":pz2watch:activeclientsForm:activeclientsFieldRecord";
\r