1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 | // XML classes
|
---|
6 | import org.w3c.dom.Node;
|
---|
7 | import org.w3c.dom.Text;
|
---|
8 | import org.w3c.dom.Document;
|
---|
9 | import org.w3c.dom.Element;
|
---|
10 |
|
---|
11 | import java.util.HashMap;
|
---|
12 |
|
---|
13 | public class QueryAction extends Action {
|
---|
14 |
|
---|
15 |
|
---|
16 | public String process (String xml_in) {
|
---|
17 |
|
---|
18 | Element message = converter_.getDOM(xml_in).getDocumentElement();
|
---|
19 | Element request = (Element)message.getElementsByTagName("request").item(0);
|
---|
20 |
|
---|
21 | String info = request.getAttribute("info");
|
---|
22 | // remove the q/ bit
|
---|
23 | info = GSPath.removeFirstLink(info);
|
---|
24 | String query_type = GSPath.getFirstLink(info);
|
---|
25 |
|
---|
26 | if (query_type.equals("")) {
|
---|
27 | query_type = "text";
|
---|
28 | }
|
---|
29 |
|
---|
30 | if (query_type.equals("text")) {
|
---|
31 | return textQuery(request);
|
---|
32 | }
|
---|
33 | if (query_type.equals("field")) {
|
---|
34 | return fieldQuery(request);
|
---|
35 | }
|
---|
36 | return unknownQuery(request);
|
---|
37 |
|
---|
38 |
|
---|
39 | }
|
---|
40 |
|
---|
41 | protected String textQuery(Element request) {
|
---|
42 |
|
---|
43 | Element query_request = doc_.createElement("message");
|
---|
44 | query_request.appendChild(doc_.importNode(request, true));
|
---|
45 |
|
---|
46 | String stylesheet = GSFile.stylesheetPath(config_, "textquery.xsl");
|
---|
47 |
|
---|
48 | //String stylesheet = GSFile.stylesheetPath(gsdl_home_, site_home_,
|
---|
49 | // display_home_, "textquery.xsl");
|
---|
50 | if (stylesheet==null) {
|
---|
51 | return GSHTML.errorPage("textquery stylesheet not found!");
|
---|
52 | }
|
---|
53 |
|
---|
54 | HashMap params = GSXML.extractParams((Element)GSXML.getChildByTagName(request, "paramList"));
|
---|
55 | String query = (String)params.get("q");
|
---|
56 | if (query==null|| query.equals("")) {
|
---|
57 | // if there is no q set, just output the search box
|
---|
58 | String transformed_result = transformer_.transform(stylesheet,
|
---|
59 | query_request);
|
---|
60 | return transformed_result;
|
---|
61 | }
|
---|
62 |
|
---|
63 | // else do the request, output search box and results
|
---|
64 | Element mr_message = doc_.createElement("message");
|
---|
65 | Element mr_request = doc_.createElement("request");
|
---|
66 | mr_message.appendChild(mr_request);
|
---|
67 |
|
---|
68 | mr_request.setAttribute("type", "query");
|
---|
69 | String to = (String)params.get("c"); // collection name
|
---|
70 | to = GSPath.appendLink(to, "TextQuery");
|
---|
71 | mr_request.setAttribute("to", to);
|
---|
72 |
|
---|
73 | // ignore paramList for now
|
---|
74 | Element paramList = doc_.createElement("paramList");
|
---|
75 | mr_request.appendChild(paramList);
|
---|
76 |
|
---|
77 | // content - contains the query string
|
---|
78 | Element content = doc_.createElement("content");
|
---|
79 | Text text = doc_.createTextNode(query);
|
---|
80 | content.appendChild(text);
|
---|
81 | mr_request.appendChild(content);
|
---|
82 | //return GSHTML.errorPage(GSHTML.htmlSafe(converter_.getString(mr_message)));
|
---|
83 |
|
---|
84 | Element result1 = (Element)mr_.process(mr_message);
|
---|
85 | // this result is the list of docs.
|
---|
86 | // now take that list, and get the Titles
|
---|
87 | // for now, just create a whole new request
|
---|
88 |
|
---|
89 | Element resource_list = (Element)result1.getElementsByTagName("resourceList").item(0); // should always be a resource list - but it may be empty
|
---|
90 |
|
---|
91 | Element first_resource = (Element)GSXML.getChildByTagName(resource_list, "resource");
|
---|
92 | if (first_resource == null) { // no docs found - dont bother getting metadata
|
---|
93 | Element result_response = (Element)GSXML.getChildByTagName(result1, "response");
|
---|
94 |
|
---|
95 | query_request.appendChild(doc_.importNode(result_response, true));
|
---|
96 |
|
---|
97 | String transformed_result = transformer_.transform(stylesheet,
|
---|
98 | query_request);
|
---|
99 |
|
---|
100 | return transformed_result;
|
---|
101 | }
|
---|
102 | // get the Title metadata
|
---|
103 | Element mr_message2 = doc_.createElement("message");
|
---|
104 | Element mr_request2 = doc_.createElement("request");
|
---|
105 | mr_message2.appendChild(mr_request2);
|
---|
106 |
|
---|
107 | mr_request2.setAttribute("type", "query");
|
---|
108 | to = (String)params.get("c"); // collection name
|
---|
109 | to = GSPath.appendLink(to, "MetadataRetrieve");
|
---|
110 | mr_request2.setAttribute("to", to);
|
---|
111 | Element content2 = doc_.createElement("content");
|
---|
112 | mr_request2.appendChild(content2);
|
---|
113 |
|
---|
114 | // the first part of the content is the doc list
|
---|
115 | content2.appendChild(doc_.importNode(resource_list, true));
|
---|
116 |
|
---|
117 | // the second part of the content is the metadata list
|
---|
118 | Element metadata_list = doc_.createElement("metadataList");
|
---|
119 | Element title = doc_.createElement("element");
|
---|
120 | title.setAttribute("name", "Title");
|
---|
121 | metadata_list.appendChild(title);
|
---|
122 | content2.appendChild(metadata_list);
|
---|
123 |
|
---|
124 | //return GSHTML.errorPage(GSHTML.htmlSafe(converter_.getString(mr_request2)));
|
---|
125 | Element result2 = (Element)mr_.process(mr_message2);
|
---|
126 |
|
---|
127 | Element result2_response = (Element)GSXML.getChildByTagName(result2, "response");
|
---|
128 |
|
---|
129 | query_request.appendChild(doc_.importNode(result2_response, true));
|
---|
130 | //return GSHTML.errorPage(GSHTML.htmlSafe(converter_.getString(query_request)));
|
---|
131 |
|
---|
132 | String transformed_result = transformer_.transform(stylesheet,
|
---|
133 | query_request);
|
---|
134 |
|
---|
135 | return transformed_result;
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected String fieldQuery(Element request) {
|
---|
140 | return GSHTML.errorPage("field query not implemented");
|
---|
141 |
|
---|
142 |
|
---|
143 | }
|
---|
144 | protected String unknownQuery(Element request) {
|
---|
145 |
|
---|
146 | return GSHTML.errorPage("unknown query subtype - unknown query not implemented");
|
---|
147 | }
|
---|
148 | }
|
---|