Mercurial > hg > mpdl-group
comparison software/mpdl-services-new/mpiwg-mpdl-cms-web/src/de/mpg/mpiwg/berlin/mpdl/servlets/cms/MoreLikeThis.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.IOException; | |
4 import java.io.PrintWriter; | |
5 import java.util.ArrayList; | |
6 import java.util.Date; | |
7 | |
8 import javax.servlet.ServletException; | |
9 import javax.servlet.http.HttpServlet; | |
10 import javax.servlet.http.HttpServletRequest; | |
11 import javax.servlet.http.HttpServletResponse; | |
12 | |
13 import org.apache.lucene.document.Fieldable; | |
14 | |
15 import de.mpg.mpiwg.berlin.mpdl.cms.document.Document; | |
16 import de.mpg.mpiwg.berlin.mpdl.cms.document.Hits; | |
17 import de.mpg.mpiwg.berlin.mpdl.cms.lucene.IndexHandler; | |
18 | |
19 public class MoreLikeThis extends HttpServlet { | |
20 private static final long serialVersionUID = 1L; | |
21 | |
22 public MoreLikeThis() { | |
23 super(); | |
24 } | |
25 | |
26 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
27 request.setCharacterEncoding("utf-8"); | |
28 response.setCharacterEncoding("utf-8"); | |
29 String outputFormat = request.getParameter("outputFormat"); | |
30 if (outputFormat == null) | |
31 outputFormat = "html"; | |
32 String docId = request.getParameter("docId"); | |
33 String pageStr = request.getParameter("page"); | |
34 if (pageStr == null) | |
35 pageStr = "1"; | |
36 int page = Integer.parseInt(pageStr); | |
37 String pageSizeStr = request.getParameter("pageSize"); | |
38 if (pageSizeStr == null) | |
39 pageSizeStr = "10"; | |
40 int pageSize = Integer.parseInt(pageSizeStr); | |
41 int from = (page * pageSize) - pageSize; // e.g. 0 | |
42 int to = page * pageSize - 1; // e.g. 9 | |
43 PrintWriter out = response.getWriter(); | |
44 IndexHandler indexHandler; | |
45 try { | |
46 Date begin = new Date(); | |
47 indexHandler = IndexHandler.getInstance(); | |
48 Hits hits = indexHandler.moreLikeThis(docId, from, to); | |
49 ArrayList<Document> docs = null; | |
50 if (hits != null) | |
51 docs = hits.getHits(); | |
52 int hitsSize = -1; | |
53 int docsSize = -1; | |
54 if (hits != null) | |
55 hitsSize = hits.getSize(); | |
56 if (docs != null) | |
57 docsSize = docs.size(); | |
58 Date end = new Date(); | |
59 long elapsedTime = end.getTime() - begin.getTime(); | |
60 if (outputFormat.equals("xml")) | |
61 response.setContentType("text/xml"); | |
62 else if (outputFormat.equals("html")) | |
63 response.setContentType("text/html"); | |
64 else | |
65 response.setContentType("text/xml"); | |
66 if (outputFormat.equals("xml")) { | |
67 out.print("<result>"); | |
68 out.print("<query>"); | |
69 out.print("<docId>" + docId + "</docId>"); | |
70 out.print("<resultPage>" + page + "</resultPage>"); | |
71 out.print("<resultPageSize>" + pageSize + "</resultPageSize>"); | |
72 out.print("</query>"); | |
73 out.print("<hitsSize>" + hitsSize + "</hitsSize>"); | |
74 out.print("<hits>"); | |
75 for (int i=0; i<docsSize; i++) { | |
76 Document doc = docs.get(i); | |
77 out.print("<doc>"); | |
78 String similarDocId = doc.getFieldable("docId").stringValue(); | |
79 out.print("<docId>" + similarDocId + "</docId>"); | |
80 Fieldable docCollectionNamesField = doc.getFieldable("collectionNames"); | |
81 if (docCollectionNamesField != null) { | |
82 String docCollectionNames = docCollectionNamesField.stringValue(); | |
83 out.print("<collectionName>" + docCollectionNames + "</collectionName>"); | |
84 } | |
85 ArrayList<String> hitFragments = doc.getHitFragments(); | |
86 if (hitFragments != null) { | |
87 out.print("<hitFragments>"); | |
88 for (int j=0; j<hitFragments.size(); j++) { | |
89 String hitFragment = hitFragments.get(j); | |
90 out.print("<hitFragment>" + hitFragment + "</hitFragment>"); | |
91 } | |
92 out.print("</hitFragments>"); | |
93 } | |
94 out.print("</doc>"); | |
95 } | |
96 out.print("</hits>"); | |
97 out.print("<executionTime>" + elapsedTime + "</executionTime>"); | |
98 out.print("</result>"); | |
99 } else if (outputFormat.equals("html")) { | |
100 StringBuilder htmlStrBuilder = new StringBuilder(); | |
101 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\">"); | |
102 htmlStrBuilder.append("<html>"); | |
103 htmlStrBuilder.append("<head>"); | |
104 htmlStrBuilder.append("<title>Similar documents of: " + docId + "</title>"); | |
105 htmlStrBuilder.append("</head>"); | |
106 htmlStrBuilder.append("<body>"); | |
107 htmlStrBuilder.append("<h4>Similar documents of: " + docId + "</h4>"); | |
108 int fromDisplay = from + 1; | |
109 int toDisplay = to + 1; | |
110 if (hitsSize < to) | |
111 toDisplay = hitsSize; | |
112 htmlStrBuilder.append("Result: " + fromDisplay + " - " + toDisplay + " of " + hitsSize + " documents" + "</td>"); | |
113 htmlStrBuilder.append("<ul>"); | |
114 for (int i=0; i<docsSize; i++) { | |
115 Document doc = docs.get(i); | |
116 String similarDocId = doc.getFieldable("docId").stringValue(); | |
117 int num = (page - 1) * pageSize + i + 1; | |
118 htmlStrBuilder.append("<li>" + num + ". " + similarDocId + "</li>"); | |
119 } | |
120 htmlStrBuilder.append("</ul>"); | |
121 htmlStrBuilder.append("<p/>"); | |
122 htmlStrBuilder.append("Elapsed time: " + elapsedTime + " ms"); | |
123 htmlStrBuilder.append("</body>"); | |
124 htmlStrBuilder.append("</html>"); | |
125 out.print(htmlStrBuilder.toString()); | |
126 } | |
127 out.close(); | |
128 } catch (Exception e) { | |
129 throw new ServletException(e); | |
130 } | |
131 } | |
132 | |
133 /** | |
134 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) | |
135 */ | |
136 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
137 // TODO Auto-generated method stub | |
138 } | |
139 | |
140 } |