14 package cz.vutbr.fit.knot.annotations.comet;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.net.URISyntaxException;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import javax.servlet.ServletException;
32 import javax.servlet.http.HttpServlet;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
61 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
62 throws ServletException, IOException {
63 response.setContentType(
"text/html;charset=UTF-8");
64 PrintWriter out = response.getWriter();
66 String uriStr = request.getParameter(
"uri");
70 idStr = uriStr.replace(AppBean.getBaseUri() +
"/serv/",
"");
72 idStr = uriStr.replace(AppBean.getBaseAnnotUri(),
"");
76 String reqType = request.getParameter(
"out");
77 if (reqType != null && reqType.equals(
"v1")) {
79 }
else if (reqType != null && reqType.equals(
"v2")) {
81 }
else if (reqType != null && reqType.equals(
"sug")) {
85 String reqUserStr = request.getParameter(
"user");
86 Integer reqUserID = null;
87 if (reqUserStr != null && !reqUserStr.isEmpty()) {
89 reqUserID = Integer.parseInt(reqUserStr);
90 }
catch (NumberFormatException numberFormatException) {
92 Object[] params =
new Object[2];
94 params[1] = reqUserStr;
95 List uList = AppBean.getPersistenceManager().queryDB(
"User.findByLogin", params);
96 if (uList != null && !uList.isEmpty()) {
98 reqUserID = user.getId();
108 id = Integer.parseInt(idStr);
109 }
catch (NumberFormatException nfe) {
111 String msg =
"NumberFormatException";
112 Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
119 String docStr = request.getParameter(
"doc");
120 Integer docId = null;
121 if (docStr != null && !docStr.isEmpty()) {
123 docId = Integer.parseInt(docStr);
124 }
catch (NumberFormatException nfe) {
126 String msg =
"NumberFormatException";
127 Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
134 String withCreatedStr = request.getParameter(
"withCreated");
135 boolean withCreated =
false;
136 if (withCreatedStr != null && !withCreatedStr.isEmpty()) {
137 if (withCreatedStr.equalsIgnoreCase(
"true")) {
144 Object[] params =
new Object[2];
147 List dList = AppBean.getPersistenceManager().queryDB(
"AnnotDocument.findById", params);
148 if (dList != null && !dList.isEmpty()) {
153 String msg =
"Document was not found.";
154 Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
161 String docUriStr = request.getParameter(
"docUri");
162 if (docUriStr != null && !docUriStr.isEmpty()) {
164 docUriStr = CoreFuncModule.cleanUpURI(docUriStr);
165 }
catch (URISyntaxException ex) {
167 String msg =
"Wrong URI of document.";
168 Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
174 if (docUriStr != null && !docUriStr.isEmpty()) {
175 Object[] params =
new Object[2];
177 params[1] = docUriStr;
178 List dList = AppBean.getPersistenceManager().queryDB(
"AnnotDocument.findByUri", params);
179 if (dList != null && !dList.isEmpty()) {
184 String msg =
"Document was not found.";
185 Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
194 Object[] params =
new Object[2];
197 List dList = AppBean.getPersistenceManager().queryDB(
"Annotation.findById", params);
198 if (dList != null && !dList.isEmpty()) {
203 String msg =
"Annotation was not found.";
204 Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
211 List<Annotation> outList =
new ArrayList<Annotation>();
212 if (annot == null && doc != null && reqUserID != null) {
214 }
else if (annot == null && doc != null) {
216 }
else if (annot == null && reqUserID != null) {
218 }
else if (annot != null && doc == null) {
220 }
else if (annot != null && doc != null) {
226 StringBuilder output =
new StringBuilder();
229 output.append(
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<annotations ");
235 Iterator<Annotation> annotIt = outList.iterator();
236 while (annotIt.hasNext()) {
238 output.append(a.toXMLString(
true));
240 output.append(
"</annotations>");
242 output.append(
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<annotations ");
248 Iterator<Annotation> annotIt = outList.iterator();
249 while (annotIt.hasNext()) {
253 output.append(
"</annotations>");
255 output.append(
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
256 output.append(
"<suggestion xmlns:geo=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">");
257 Iterator<Annotation> annotIt = outList.iterator();
258 while (annotIt.hasNext()) {
260 output.append(a.toSXMLString(
true, withCreated));
262 output.append(
"</suggestion>");
279 @SuppressWarnings(
"unchecked")
282 Object[] params =
new Object[2];
283 params[0] =
"sourceDocumentId";
284 params[1] = doc.getId();
285 List aList = AppBean.getPersistenceManager().queryDB(
"Annotation.findBySourceDocumentID", params);
286 if (aList != null && !aList.isEmpty()) {
290 return new ArrayList<Annotation>();
300 @SuppressWarnings(
"unchecked")
303 Object[] params =
new Object[4];
304 params[0] =
"sourceDocumentId";
305 params[1] = doc.getId();
306 params[2] =
"authorId";
307 params[3] = author.toString();
308 List aList = AppBean.getPersistenceManager().queryDB(
"Annotation.findBySourceDocumentIDAndAuthor", params);
309 if (aList != null && !aList.isEmpty()) {
313 return new ArrayList<Annotation>();
322 @SuppressWarnings(
"unchecked")
325 Object[] params =
new Object[2];
326 params[0] =
"authorId";
327 params[1] = author.toString();
328 List aList = AppBean.getPersistenceManager().queryDB(
"Annotation.findByAuthor", params);
329 if (aList != null && !aList.isEmpty()) {
333 return new ArrayList<Annotation>();
344 +
"<title>Servlet GetAnnotServlet</title>"
347 +
"<h1>Error - requested informations was not found</h1>"
348 +
"<p>Requested informations was not found.</p>"
363 protected void doGet(HttpServletRequest request, HttpServletResponse response)
364 throws ServletException, IOException {
378 protected void doPost(HttpServletRequest request, HttpServletResponse response)
379 throws ServletException, IOException {
390 return "Servlet for getting of annotations.";
AnnotDocument getSourceDocument()
static String getBaseAnnotUri()
static String getBaseUri()
void doPost(HttpServletRequest request, HttpServletResponse response)
List< Annotation > getAllAnnotationsForAuthor(Integer author)
static final int OUTPUT_TYPE_V2
Class representing annotated copy of document.
Document, annotations and types manipulation functionality.
Singleton for storing global variables.
static final int OUTPUT_TYPE_SUG
Servlet which makes available annotations.
static final int NAMESPACES_LOD_V2
void processRequest(HttpServletRequest request, HttpServletResponse response)
static final int LOG_LEVEL
static final int LOG_LEVEL_ALL
static final int LANGUAGE_ENG
List< Annotation > getAllAnnotationsForDoc(AnnotDocument doc)
List< Annotation > getAllAnnotationsForDocAndAuthor(AnnotDocument doc, Integer author)
Class responsible for localised strings.
void doGet(HttpServletRequest request, HttpServletResponse response)
Class representing annotation.
static final int NAMESPACES_LOD_V1
static final int OUTPUT_TYPE_V1