Moves request logic from pz2bean to individual commands
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / commands / RecordCommand.java
1 package com.indexdata.mkjsf.pazpar2.commands;\r
2 \r
3 import org.apache.log4j.Logger;\r
4 \r
5 import com.indexdata.mkjsf.pazpar2.ClientCommandResponse;\r
6 import com.indexdata.mkjsf.pazpar2.HttpResponseWrapper;\r
7 import com.indexdata.mkjsf.pazpar2.Pz2Bean;\r
8 import com.indexdata.mkjsf.pazpar2.commands.sp.RecordCommandSp;\r
9 import com.indexdata.mkjsf.pazpar2.commands.sp.ServiceProxyCommand;\r
10 import com.indexdata.mkjsf.pazpar2.data.RecordResponse;\r
11 import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
12 import com.indexdata.mkjsf.pazpar2.data.ResponseParser;\r
13 \r
14 public class RecordCommand extends Pazpar2Command implements ServiceProxyCommand {\r
15 \r
16   private static final long serialVersionUID = 2817539422114569506L;\r
17   private static Logger logger = Logger.getLogger(RecordCommand.class);\r
18 \r
19   public RecordCommand() {\r
20     super("record");\r
21   }\r
22   \r
23   @Override\r
24   public ResponseDataObject run() {\r
25     ResponseDataObject responseObject = null;\r
26     if (hasParameterValue("id")) {\r
27       HttpResponseWrapper commandResponse = Pz2Bean.get().getSearchClient().executeCommand(this);\r
28       \r
29       if (commandResponse.getContentType().contains("xml")) {\r
30         responseObject = ResponseParser.getParser().getDataObject((ClientCommandResponse)commandResponse);\r
31         if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
32           logger.debug("Storing " + responseObject.getType() + " in pzresp. ");\r
33         } else {        \r
34           logger.debug("Command was 'record' but response not '<record>' - assuming raw record response.");\r
35           ResponseDataObject recordResponse = new RecordResponse(); \r
36           recordResponse.setType("record");\r
37           recordResponse.setXml(responseObject.getXml());          \r
38           recordResponse.setAttribute("activeclients", "0");             \r
39         }\r
40       } else if (commandResponse.isBinary()) {\r
41         responseObject = new RecordResponse(); \r
42         responseObject.setType(getCommandName());\r
43         logger.info("Binary response");\r
44         responseObject.setAttribute("activeclients", "0");\r
45         responseObject.setXml("<record>binary response</record>");\r
46         responseObject.setBinary(commandResponse.getBytes());\r
47         \r
48       } else {\r
49         logger.error("Response was not found to be XML or binary. The response was not handled.");\r
50       }\r
51       Pz2Bean.get().getPzresp().put(getCommandName(), responseObject);\r
52     } else {\r
53       logger.debug("No record id parameter on this command. Ignoring request but clearing any previous record result.");\r
54       Pz2Bean.get().getPzresp().put(getCommandName(), new RecordResponse());\r
55     }\r
56     return responseObject;\r
57   }\r
58   \r
59   public void setId(String recId) {\r
60     setParameter(new CommandParameter("id","=",recId));\r
61   }\r
62   \r
63   public String getId () {\r
64     return getParameterValue("id");\r
65   }\r
66   \r
67   public void setOffset (String offset) {\r
68     setParameter(new CommandParameter("offset","=",offset));\r
69   }\r
70   \r
71   public String getOffset () {\r
72     return getParameterValue("offset");\r
73   }\r
74   \r
75   public void setChecksum (String checksum) {\r
76     setParameter(new CommandParameter("checksum","=",checksum));\r
77   }\r
78   \r
79   public String getChecksum () {\r
80     return getParameterValue("checksum");\r
81   }\r
82   \r
83   public void setNativesyntax (String nativesyntax) {\r
84     setParameterInState(new CommandParameter("nativesyntax","=",nativesyntax));\r
85   }\r
86   \r
87   public String getNativesyntax () {\r
88     return getParameterValue("nativesyntax");\r
89   }\r
90   \r
91   public void setSyntax (String syntax) {\r
92     setParameterInState(new CommandParameter("syntax","=",syntax));    \r
93   }\r
94   \r
95   public String getSyntax () {\r
96     return getParameterValue("syntax");\r
97   }\r
98   \r
99   public void setEsn (String esn) {\r
100     setParameter(new CommandParameter("esn","=",esn));\r
101   }\r
102   \r
103   public String getEsn () {\r
104     return getParameterValue("esn");\r
105   }\r
106   \r
107   public void setBinary (String binary) {\r
108     setParameter(new CommandParameter("binary","=",binary));\r
109   }\r
110   \r
111   public String getBinary () {\r
112     return getParameterValue("binary");\r
113   }\r
114 \r
115   @Override\r
116   public RecordCommand copy () {\r
117     RecordCommand newCommand = new RecordCommand();\r
118     for (String parameterName : parameters.keySet()) {\r
119       newCommand.setParameterInState(parameters.get(parameterName).copy());      \r
120     }    \r
121     return newCommand;\r
122   }\r
123   \r
124   \r
125   /**\r
126    * Returns a record command object with Service Proxy extension parameters \r
127    * \r
128    */\r
129   public RecordCommandSp getSp () {\r
130     return new RecordCommandSp(this);\r
131   }\r
132 \r
133   @Override\r
134   public boolean spOnly() {    \r
135     return false;\r
136   }\r
137 }\r