comparison software/mpdl-services-new/mpiwg-mpdl-cms-web/src/de/mpg/mpiwg/berlin/mpdl/servlets/cms/QueryDocuments.java @ 25:e9fe3186670c default tip

letzter Stand eingecheckt
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 21 May 2013 10:19:32 +0200
parents
children
comparison
equal deleted inserted replaced
23:e845310098ba 25:e9fe3186670c
1 package de.mpg.mpiwg.berlin.mpdl.servlets.cms;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.PrintWriter;
6 import java.util.ArrayList;
7 import java.util.Date;
8
9 import javax.servlet.ServletConfig;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 import org.apache.lucene.document.Fieldable;
16 import org.apache.lucene.search.Query;
17
18 import de.mpg.mpiwg.berlin.mpdl.cms.document.Document;
19 import de.mpg.mpiwg.berlin.mpdl.cms.document.DocumentHandler;
20 import de.mpg.mpiwg.berlin.mpdl.cms.document.Hits;
21 import de.mpg.mpiwg.berlin.mpdl.cms.lucene.IndexHandler;
22
23 public class QueryDocuments extends HttpServlet {
24 private static final long serialVersionUID = 1L;
25 public QueryDocuments() {
26 super();
27 }
28
29 public void init(ServletConfig config) throws ServletException {
30 super.init(config);
31 }
32
33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34 request.setCharacterEncoding("utf-8");
35 response.setCharacterEncoding("utf-8");
36 String query = request.getParameter("query");
37 String sortBy = request.getParameter("sortBy");
38 String[] sortFields = null;
39 if (sortBy != null && ! sortBy.trim().isEmpty())
40 sortFields = sortBy.split(" ");
41 String language = request.getParameter("language"); // optional: is also detected by translator service
42 if (language != null && language.equals("none"))
43 language = null;
44 String translate = request.getParameter("translate");
45 String pageStr = request.getParameter("page");
46 if (pageStr == null)
47 pageStr = "1";
48 int page = Integer.parseInt(pageStr);
49 String pageSizeStr = request.getParameter("pageSize");
50 if (pageSizeStr == null)
51 pageSizeStr = "10";
52 int pageSize = Integer.parseInt(pageSizeStr);
53 int from = (page * pageSize) - pageSize; // e.g. 0
54 int to = page * pageSize - 1; // e.g. 9
55 String outputFormat = request.getParameter("outputFormat");
56 if (outputFormat == null)
57 outputFormat = "html";
58 if (outputFormat.equals("xml"))
59 response.setContentType("text/xml");
60 else if (outputFormat.equals("html"))
61 response.setContentType("text/html");
62 else
63 response.setContentType("text/xml");
64 PrintWriter out = response.getWriter();
65 if (query == null) {
66 out.print("no query specified: please set parameter \"query\"");
67 out.close();
68 return;
69 }
70 try {
71 Date begin = new Date();
72 IndexHandler indexHandler = IndexHandler.getInstance();
73 boolean translateBool = false;
74 if (translate != null && translate.equals("true"))
75 translateBool = true;
76 boolean withHitHighlights = false;
77 if (query.contains("tokenOrig:") || query.contains("tokenMorph:") || query.contains("tokenReg:") || query.contains("tokenNorm:"))
78 withHitHighlights = true;
79 Hits hits = indexHandler.queryDocuments(query, sortFields, language, from, to, withHitHighlights, translateBool);
80 ArrayList<Document> docs = null;
81 if (hits != null)
82 docs = hits.getHits();
83 int hitsSize = -1;
84 int docsSize = -1;
85 if (hits != null)
86 hitsSize = hits.getSize();
87 if (docs != null)
88 docsSize = docs.size();
89 Date end = new Date();
90 long elapsedTime = end.getTime() - begin.getTime();
91 if (outputFormat.equals("xml")) {
92 out.print("<result>");
93 out.print("<query>");
94 out.print("<queryText>" + query + "</queryText>");
95 out.print("<resultPage>" + page + "</resultPage>");
96 out.print("<resultPageSize>" + pageSize + "</resultPageSize>");
97 out.print("</query>");
98 out.print("<hitsSize>" + hitsSize + "</hitsSize>");
99 out.print("<hits>");
100 for (int i=0; i<docsSize; i++) {
101 Document doc = docs.get(i);
102 out.print("<doc>");
103 String docId = doc.getFieldable("docId").stringValue();
104 out.print("<docId>" + docId + "</docId>");
105 Fieldable docCollectionNamesField = doc.getFieldable("collectionNames");
106 if (docCollectionNamesField != null) {
107 String docCollectionNames = docCollectionNamesField.stringValue();
108 out.print("<collectionName>" + docCollectionNames + "</collectionName>");
109 }
110 ArrayList<String> hitFragments = doc.getHitFragments();
111 if (hitFragments != null) {
112 out.print("<hitFragments>");
113 for (int j=0; j<hitFragments.size(); j++) {
114 String hitFragment = hitFragments.get(j);
115 out.print("<hitFragment>" + hitFragment + "</hitFragment>");
116 }
117 out.print("</hitFragments>");
118 }
119 out.print("</doc>");
120 }
121 out.print("</hits>");
122 out.print("<executionTime>" + elapsedTime + "</executionTime>");
123 out.print("</result>");
124 } else if (outputFormat.equals("html")) {
125 StringBuilder htmlStrBuilder = new StringBuilder();
126 String baseUrl = getBaseUrl(request);
127 String cssUrl = baseUrl + "/css/page.css";
128 htmlStrBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
129 htmlStrBuilder.append("<html>");
130 htmlStrBuilder.append("<head>");
131 htmlStrBuilder.append("<title>Query: " + query + "</title>");
132 htmlStrBuilder.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + cssUrl + "\"/>");
133 htmlStrBuilder.append("</head>");
134 htmlStrBuilder.append("<body>");
135 htmlStrBuilder.append("<table align=\"right\" valign=\"top\">");
136 htmlStrBuilder.append("<td>[<i>This is a MPIWG CMS technology service</i>] <a href=\"/mpiwg-mpdl-cms-web/index.html\"><img src=\"/mpiwg-mpdl-cms-web/images/info.png\" valign=\"bottom\" width=\"15\" height=\"15\" border=\"0\" alt=\"MPIWG CMS service\"/></a></td>");
137 htmlStrBuilder.append("</table>");
138 htmlStrBuilder.append("<p/>");
139 String luceneQueryStr = query;
140 Query luceneQuery = hits.getQuery();
141 if (query != null)
142 luceneQueryStr = luceneQuery.toString();
143 String sortByStr = sortBy;
144 if (sortBy == null)
145 sortByStr = "";
146 htmlStrBuilder.append("<h4>Query: " + luceneQueryStr + ", sorted by: " + sortByStr + "</h4>");
147 htmlStrBuilder.append("<form action=\"QueryDocuments\" method=\"get\">");
148 htmlStrBuilder.append("<input type=\"hidden\" name=\"query\" value=\"" + query + "\"/>");
149 if (translate != null)
150 htmlStrBuilder.append("<input type=\"hidden\" name=\"translate\" value=\"" + translate + "\"/>");
151 if (language != null)
152 htmlStrBuilder.append("<input type=\"hidden\" name=\"language\" value=\"" + language + "\"/>");
153 htmlStrBuilder.append("<input type=\"hidden\" name=\"page\" id=\"pageId\" value=\"" + page + "\"/>");
154 htmlStrBuilder.append("<input type=\"hidden\" name=\"sortBy\" id=\"sortById\" value=\"" + sortByStr + "\"/>");
155 htmlStrBuilder.append("<input type=\"submit\" id=\"submitId\" style=\"position: absolute; left: -9999px\"/>");
156 htmlStrBuilder.append("<table>");
157 htmlStrBuilder.append("<colgroup>");
158 htmlStrBuilder.append("<col width=\"3%\"/>");
159 htmlStrBuilder.append("<col width=\"7%\"/>");
160 htmlStrBuilder.append("<col width=\"3%\"/>");
161 htmlStrBuilder.append("<col width=\"12%\"/>");
162 htmlStrBuilder.append("<col width=\"70%\"/>");
163 htmlStrBuilder.append("</colgroup>");
164 htmlStrBuilder.append("<tr>");
165 int countPages = hitsSize / 10 + 1;
166 if (hitsSize % 10 == 0) // modulo operator: e.g. 280 % 10 is 0
167 countPages = hitsSize / 10;
168 int pageLeft = page - 1;
169 if (page == 1)
170 pageLeft = 1;
171 int pageRight = page + 1;
172 if (page == countPages)
173 pageRight = countPages;
174 htmlStrBuilder.append("<td align=\"left\" valign=\"top\"><button onclick=\"document.getElementById('pageId').value=" + pageLeft + "\" style=\"background:none;border:none;\"><img src=\"../images/left.gif\"/></button></td>");
175 htmlStrBuilder.append("<td align=\"middle\" valign=\"top\" nowrap=\"true\">" + page + " / " + countPages + "</td>");
176 htmlStrBuilder.append("<td align=\"left\" valign=\"top\"><button onclick=\"document.getElementById('pageId').value=" + pageRight + "\" style=\"background:none;border:none;\"><img src=\"../images/right.gif\"/></button></td>");
177 htmlStrBuilder.append("<td align=\"left\" valign=\"top\" nowrap=\"true\">Page: <input type=\"text\" size=\"3\" value=\"" + page + "\" id=\"pageTextId\" onkeydown=\"if (event.keyCode == 13) {document.getElementById('pageId').value=document.getElementById('pageTextId').value; document.getElementById('submitId').click();}\"/></td>");
178 int fromDisplay = from + 1;
179 int toDisplay = to + 1;
180 if (hitsSize < to)
181 toDisplay = hitsSize;
182 htmlStrBuilder.append("<td align=\"right\" valign=\"top\">" + fromDisplay + " - " + toDisplay + " of " + hitsSize + " documents" + "</td>");
183 htmlStrBuilder.append("</tr>");
184 htmlStrBuilder.append("</table>");
185 htmlStrBuilder.append("<p/>");
186 htmlStrBuilder.append("<table width=\"100%\" align=\"right\" border=\"2\" rules=\"groups\">");
187 htmlStrBuilder.append("<colgroup>");
188 htmlStrBuilder.append("<col width=\"3%\"/>");
189 htmlStrBuilder.append("<col width=\"15%\"/>");
190 htmlStrBuilder.append("<col width=\"45%\"/>");
191 htmlStrBuilder.append("<col width=\"10%\"/>");
192 htmlStrBuilder.append("<col width=\"5%\"/>");
193 htmlStrBuilder.append("<col width=\"10%\"/>");
194 htmlStrBuilder.append("<col width=\"6%\"/>");
195 htmlStrBuilder.append("<col width=\"5%\"/>");
196 htmlStrBuilder.append("<col width=\"4%\"/>");
197 htmlStrBuilder.append("</colgroup>");
198 htmlStrBuilder.append("<thead>");
199 htmlStrBuilder.append("<tr>");
200 htmlStrBuilder.append("<td align=\"left\" valign=\"top\" style=\"font-weight:bold;\">" + "No" + "</td>");
201 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + "<button onclick=\"document.getElementById('sortById').value='author'\" style=\"padding:0px;font-weight:bold;font-size:14px;background:none;border:none;\">" + "Author" + "</button></td>");
202 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + "<button onclick=\"document.getElementById('sortById').value='title'\" style=\"padding:0px;font-weight:bold;font-size:14px;background:none;border:none;\">" + "Title" + "</button></td>");
203 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + "<button onclick=\"document.getElementById('sortById').value='publisher'\" style=\"padding:0px;font-weight:bold;font-size:14px;background:none;border:none;\">" + "Place" + "</button></td>");
204 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + "<button onclick=\"document.getElementById('sortById').value='date'\" style=\"padding:0px;font-weight:bold;font-size:14px;background:none;border:none;\">" + "Year" + "</button></td>");
205 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + "<button onclick=\"document.getElementById('sortById').value='docId'\" style=\"padding:0px;font-weight:bold;font-size:14px;background:none;border:none;\">" + "Id" + "</button></td>");
206 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + "<button onclick=\"document.getElementById('sortById').value='lastModified'\" style=\"padding:0px;font-weight:bold;font-size:14px;background:none;border:none;\">" + "Last modified" + "</button></td>");
207 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + "<button onclick=\"document.getElementById('sortById').value='language'\" style=\"padding:0px;font-weight:bold;font-size:14px;background:none;border:none;\">" + "Language" + "</button></td>");
208 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + "<button onclick=\"document.getElementById('sortById').value='schemaName'\" style=\"padding:0px;font-weight:bold;font-size:14px;background:none;border:none;\">" + "Schema" + "</button></td>");
209 htmlStrBuilder.append("</tr>");
210 htmlStrBuilder.append("</thead>");
211 htmlStrBuilder.append("<tbody>");
212 for (int i=0; i<docsSize; i++) {
213 Document doc = docs.get(i);
214 htmlStrBuilder.append("<tr valign=\"top\">");
215 int num = (page - 1) * pageSize + i + 1;
216 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + num + ". " + "</td>");
217 Fieldable authorField = doc.getFieldable("author");
218 String author = "";
219 if (authorField != null)
220 author = authorField.stringValue();
221 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + author + "</td>");
222 Fieldable titleField = doc.getFieldable("title");
223 String title = "";
224 if (titleField != null)
225 title = titleField.stringValue();
226 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + title + "</td>");
227 Fieldable publisherField = doc.getFieldable("publisher");
228 String publisher = "";
229 if (publisherField != null)
230 publisher = publisherField.stringValue();
231 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + publisher + "</td>");
232 Fieldable yearField = doc.getFieldable("date");
233 String year = "";
234 if (yearField != null)
235 year = yearField.stringValue();
236 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + year + "</td>");
237 String docId = doc.getFieldable("docId").stringValue();
238 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + docId + "</td>");
239 Fieldable lastModifiedField = doc.getFieldable("lastModified");
240 String lastModified = "";
241 if (lastModifiedField != null)
242 lastModified = lastModifiedField.stringValue();
243 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + lastModified + "</td>");
244 Fieldable languageField = doc.getFieldable("language");
245 String lang = "";
246 if (languageField != null)
247 lang = languageField.stringValue();
248 htmlStrBuilder.append("<td align=\"left\" valign=\"top\" style=\"padding-left:5px\">" + lang + "</td>");
249 Fieldable schemaNameField = doc.getFieldable("schemaName");
250 String schemaName = "";
251 if (schemaNameField != null)
252 schemaName = schemaNameField.stringValue();
253 htmlStrBuilder.append("<td align=\"left\" valign=\"top\">" + schemaName + "</td>");
254 htmlStrBuilder.append("</tr>");
255 // Link row
256 htmlStrBuilder.append("<tr valign=\"top\">");
257 htmlStrBuilder.append("<td align=\"left\" valign=\"top\"></td>");
258 htmlStrBuilder.append("<td align=\"left\" valign=\"top\" colspan=\"8\">");
259 Fieldable echoIdField = doc.getFieldable("echoId");
260 String echoId = null;
261 if (echoIdField != null)
262 echoId = echoIdField.stringValue();
263 String urlDocuView = "http://echo.mpiwg-berlin.mpg.de/ECHOdocuView?url=" + echoId;
264 if (echoId == null)
265 urlDocuView = "http://echo.mpiwg-berlin.mpg.de/";
266 htmlStrBuilder.append("<img src=\"/mpiwg-mpdl-cms-web/images/book.png\" width=\"15\" height=\"15\" border=\"0\"/>" + " <a href=\"" + urlDocuView + "\">Full view</a>");
267 htmlStrBuilder.append(", " + "<a href=\"/mpiwg-mpdl-cms-web/query/GetPage?docId=" + docId + "\">Lite view</a>");
268 htmlStrBuilder.append("</td>");
269 htmlStrBuilder.append("</tr>");
270 htmlStrBuilder.append("<tr valign=\"top\">");
271 htmlStrBuilder.append("<td align=\"left\" valign=\"top\"></td>");
272 htmlStrBuilder.append("<td align=\"left\" valign=\"top\" colspan=\"8\">");
273 htmlStrBuilder.append("<img src=\"/mpiwg-mpdl-cms-web/images/download.png\" width=\"15\" height=\"15\" border=\"0\"/>" + " Download: <a href=\"/mpiwg-mpdl-cms-web/doc/GetDocument?id=" + docId + "\">XML</a>");
274 DocumentHandler docHandler = new DocumentHandler();
275 String destFileNamePdf = docHandler.getFullFileName(docId, "pdf");
276 String destFileNameHtml = docHandler.getFullFileName(docId, "html");
277 int lastDot = docId.lastIndexOf(".");
278 String docIdWithoutExtension = docId.substring(0, lastDot);
279 File destFilePdf = new File(destFileNamePdf);
280 File destFileHtml = new File(destFileNameHtml);
281 if (destFilePdf.exists())
282 htmlStrBuilder.append(", " + "<a href=\"/mpiwg-mpdl-cms-web/doc/GetDocument?id=" + docIdWithoutExtension + ".pdf" + "\">PDF</a>");
283 if (destFileHtml.exists())
284 htmlStrBuilder.append(", " + "<a href=\"/mpiwg-mpdl-cms-web/doc/GetDocument?id=" + docIdWithoutExtension + ".html" + "\">HTML</a>");
285 htmlStrBuilder.append("</td>");
286 htmlStrBuilder.append("</tr>");
287 // hit fragments row
288 ArrayList<String> hitFragments = doc.getHitFragments();
289 if (hitFragments != null) {
290 StringBuilder hitFragmentsStrBuilder = new StringBuilder();
291 hitFragmentsStrBuilder.append("<b>Hit summary: </b>");
292 hitFragmentsStrBuilder.append("(...) ");
293 for (int j=0; j<hitFragments.size(); j++) {
294 String hitFragment = hitFragments.get(j);
295 hitFragmentsStrBuilder.append(hitFragment + " (...) ");
296 }
297 htmlStrBuilder.append("<tr valign=\"top\">");
298 htmlStrBuilder.append("<td align=\"left\" valign=\"top\"></td>");
299 htmlStrBuilder.append("<td align=\"left\" valign=\"top\" colspan=\"8\">" + hitFragmentsStrBuilder.toString() + "</td>");
300 htmlStrBuilder.append("</tr>");
301 }
302 }
303 htmlStrBuilder.append("</tbody>");
304 htmlStrBuilder.append("</table>");
305 htmlStrBuilder.append("</form>");
306 htmlStrBuilder.append("<p/>");
307 htmlStrBuilder.append("Elapsed time: " + elapsedTime + " ms");
308 htmlStrBuilder.append("</body>");
309 htmlStrBuilder.append("</html>");
310 out.print(htmlStrBuilder.toString());
311 }
312 out.close();
313 } catch (Exception e) {
314 throw new ServletException(e);
315 }
316 }
317
318 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
319 doGet(request, response);
320 }
321
322 private String getBaseUrl(HttpServletRequest request) {
323 return getServerUrl(request) + request.getContextPath();
324 }
325
326 private String getServerUrl(HttpServletRequest request) {
327 if ( ( request.getServerPort() == 80 ) || ( request.getServerPort() == 443 ) )
328 return request.getScheme() + "://" + request.getServerName();
329 else
330 return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
331 }
332
333 }