1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.Serializable;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.HashSet;
|
---|
7 |
|
---|
8 | import javax.xml.parsers.DocumentBuilder;
|
---|
9 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
10 | import javax.xml.transform.Result;
|
---|
11 | import javax.xml.transform.Transformer;
|
---|
12 | import javax.xml.transform.TransformerFactory;
|
---|
13 | import javax.xml.transform.dom.DOMSource;
|
---|
14 | import javax.xml.transform.stream.StreamResult;
|
---|
15 |
|
---|
16 | import org.greenstone.gsdl3.util.GSFile;
|
---|
17 | import org.greenstone.gsdl3.util.GSParams;
|
---|
18 | import org.greenstone.gsdl3.util.GSPath;
|
---|
19 | import org.greenstone.gsdl3.util.GSXML;
|
---|
20 | import org.greenstone.gsdl3.util.UserContext;
|
---|
21 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
22 | import org.greenstone.util.GlobalProperties;
|
---|
23 | import org.w3c.dom.Document;
|
---|
24 | import org.w3c.dom.Element;
|
---|
25 | import org.w3c.dom.Node;
|
---|
26 | import org.w3c.dom.NodeList;
|
---|
27 |
|
---|
28 | public class GeneralDocumentListAction extends Action
|
---|
29 | {
|
---|
30 |
|
---|
31 | /** process a request */
|
---|
32 | public Node process(Node message_node)
|
---|
33 | {
|
---|
34 | Element message = GSXML.nodeToElement(message_node);
|
---|
35 | Document doc = message.getOwnerDocument();
|
---|
36 |
|
---|
37 | // the result
|
---|
38 | Element result = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
39 | Element page_response = doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
40 | result.appendChild(page_response);
|
---|
41 |
|
---|
42 | // assume only one request
|
---|
43 | Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
44 | logger.debug(" request=" + this.converter.getString(request));
|
---|
45 |
|
---|
46 | UserContext userContext = new UserContext(request);
|
---|
47 |
|
---|
48 | // get the param list
|
---|
49 | Element cgi_param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
50 | HashMap<String, Serializable> params = GSXML.extractParams(cgi_param_list, false);
|
---|
51 |
|
---|
52 |
|
---|
53 | String service_name = (String) params.get(GSParams.SERVICE);
|
---|
54 | String cluster_name = (String) params.get(GSParams.CLUSTER);
|
---|
55 | String response_only_p = (String) params.get(GSParams.RESPONSE_ONLY);
|
---|
56 | boolean response_only = false;
|
---|
57 | if (response_only_p != null)
|
---|
58 | {
|
---|
59 | response_only = (response_only_p.equals("1") ? true : false);
|
---|
60 | }
|
---|
61 | String request_type = (String) params.get(GSParams.REQUEST_TYPE);
|
---|
62 | // what is carried out depends on the request_type
|
---|
63 | // if rt=d, then a describe request is done,
|
---|
64 | // is rt=r, a request and then a describe request is done
|
---|
65 |
|
---|
66 | // if ro=1, then this calls for a process only page - we do the request
|
---|
67 | // (rt should be r) and just give the response straight back
|
---|
68 | // without any page processing
|
---|
69 |
|
---|
70 | // where to send requests
|
---|
71 | String to;
|
---|
72 | if (cluster_name != null)
|
---|
73 | {
|
---|
74 | to = GSPath.appendLink(cluster_name, service_name);
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | to = service_name;
|
---|
79 | }
|
---|
80 |
|
---|
81 | // request the service info for the selected service - should be cached
|
---|
82 | Element description_elem = getServiceDescription(to, userContext);
|
---|
83 |
|
---|
84 | if (request_type.equals("d"))
|
---|
85 | {
|
---|
86 |
|
---|
87 | // we only want to describe the service
|
---|
88 | if (description_elem != null)
|
---|
89 | {
|
---|
90 | page_response.appendChild((Element) doc.importNode(description_elem, true));
|
---|
91 | }
|
---|
92 | addSiteMetadata(page_response, userContext);
|
---|
93 | addInterfaceOptions(page_response);
|
---|
94 | return result;
|
---|
95 | // thats all. format info here??
|
---|
96 | }
|
---|
97 |
|
---|
98 | //ok, so now we do the actual request
|
---|
99 | Element mr_query_message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
100 | Element mr_query_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
|
---|
101 |
|
---|
102 | mr_query_message.appendChild(mr_query_request);
|
---|
103 |
|
---|
104 | Element param_list = null;
|
---|
105 | // add in the service params - except the ones only used by the action
|
---|
106 | HashMap service_params = (HashMap) params.get(GSParams.SERVICE_PREFIX);
|
---|
107 | if (service_params != null)
|
---|
108 | {
|
---|
109 | param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
110 | GSXML.addParametersToList(param_list, service_params);
|
---|
111 | mr_query_request.appendChild(param_list);
|
---|
112 | }
|
---|
113 |
|
---|
114 | Element mr_query_response = (Element) this.mr.process(mr_query_message);
|
---|
115 | Element result_response = (Element) GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
|
---|
116 |
|
---|
117 | if (result_response == null) {
|
---|
118 | // something went wrong
|
---|
119 | logger.error("no result from sending request to "+to);
|
---|
120 | return result;
|
---|
121 | }
|
---|
122 |
|
---|
123 | // we need to get metadata for the documents
|
---|
124 | HashSet<String> doc_meta_names = new HashSet<String>();
|
---|
125 | // always want this one
|
---|
126 | doc_meta_names.add("root_assocfilepath");
|
---|
127 | // we use extraMetadataList, plus anything that shows up in the service's format statement
|
---|
128 | //get the format info for the service
|
---|
129 | Element format_elem = getFormatInfo(to, userContext);
|
---|
130 | if (format_elem != null) {
|
---|
131 | // set the format type - what would it be for this??
|
---|
132 | //format_elem.setAttribute(GSXML.TYPE_ATT, "");
|
---|
133 | // add to the response
|
---|
134 | page_response.appendChild(doc.importNode(format_elem, true));
|
---|
135 | }
|
---|
136 | // ... and find any metadata names mentioned in the format, or in extraMetadata
|
---|
137 | getRequiredMetadataNames(doc_meta_names, format_elem, request);
|
---|
138 |
|
---|
139 | // TODO, do we want docType and nodeType attributes for each doc Node??
|
---|
140 | addMetadataToDocNodes(result_response, doc_meta_names);
|
---|
141 |
|
---|
142 | if (response_only)
|
---|
143 | {
|
---|
144 | // just send the reponse as is
|
---|
145 | addSiteMetadata(result_response, userContext);
|
---|
146 | addInterfaceOptions(result_response);
|
---|
147 | return result_response;
|
---|
148 | }
|
---|
149 |
|
---|
150 | // else append the contents of the response to the page
|
---|
151 | GSXML.copyAllChildren(page_response, result_response);
|
---|
152 |
|
---|
153 | // another part of the page is the service description
|
---|
154 | if (description_elem != null)
|
---|
155 | {
|
---|
156 | page_response.appendChild((Element) doc.importNode(description_elem, true));
|
---|
157 | }
|
---|
158 |
|
---|
159 | addSiteMetadata(page_response, userContext);
|
---|
160 | addInterfaceOptions(page_response);
|
---|
161 |
|
---|
162 | return result;
|
---|
163 | }
|
---|
164 |
|
---|
165 | protected void addMetadataToDocNodes(Element result_elem, HashSet<String> meta_names) {
|
---|
166 |
|
---|
167 | Document doc = XMLConverter.newDOM();
|
---|
168 | Document result_doc = result_elem.getOwnerDocument();
|
---|
169 | HashMap<String, Element>coll_requests_map = new HashMap<String, Element>();
|
---|
170 |
|
---|
171 | Element message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
172 |
|
---|
173 | // the param list - will be the same for all collection requests
|
---|
174 | Element dm_param_list = createMetadataParamList(doc,meta_names);
|
---|
175 |
|
---|
176 | NodeList doc_nodes = result_elem.getElementsByTagName(GSXML.DOC_NODE_ELEM);
|
---|
177 | for (int i = 0; i < doc_nodes.getLength(); i++) {
|
---|
178 | Element dn = (Element) doc_nodes.item(i);
|
---|
179 | String collection = dn.getAttribute(GSXML.COLLECTION_ATT);
|
---|
180 |
|
---|
181 | Element this_coll_dn_list = coll_requests_map.get(collection);
|
---|
182 | if (this_coll_dn_list == null) {
|
---|
183 | Element this_coll_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_PROCESS, GSPath.appendLink(collection, "DocumentMetadataRetrieve"), new UserContext());
|
---|
184 | this_coll_request.appendChild(dm_param_list.cloneNode(true));
|
---|
185 | this_coll_dn_list = doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
186 | this_coll_request.appendChild(this_coll_dn_list);
|
---|
187 | coll_requests_map.put(collection, this_coll_dn_list);
|
---|
188 | message.appendChild(this_coll_request);
|
---|
189 | }
|
---|
190 |
|
---|
191 | this_coll_dn_list.appendChild(doc.importNode(dn, false));
|
---|
192 | } // for each node
|
---|
193 |
|
---|
194 | // ok, send off the message
|
---|
195 | Element dm_result = (Element)this.mr.process(message);
|
---|
196 |
|
---|
197 | String [] att_names = {GSXML.COLLECTION_ATT, GSXML.NODE_ID_ATT};
|
---|
198 | // go throught he original list of doc nodes
|
---|
199 | for (int i = 0; i < doc_nodes.getLength(); i++) {
|
---|
200 | Element dn = (Element) doc_nodes.item(i);
|
---|
201 | String collection = dn.getAttribute(GSXML.COLLECTION_ATT);
|
---|
202 | String node_id = dn.getAttribute(GSXML.NODE_ID_ATT);
|
---|
203 | // find the corresponding one in the result
|
---|
204 | Element new_dn = GSXML.findMatchingElement(dm_result, GSXML.DOC_NODE_ELEM, att_names, new String[] {collection, node_id});
|
---|
205 | if (new_dn != null) {
|
---|
206 | Node new_meta = GSXML.getChildByTagName(new_dn, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
207 | if (new_meta != null) {
|
---|
208 | dn.appendChild(result_doc.importNode(new_meta, true));
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 | }
|
---|