package com.lichtfragmente.servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.Vector;
import com.lichtfragmente.helpers.*;
import com.lichtfragmente.beans.ImageBean;
/**
* Manages a list of all images and carries out search queries for certain images.
* This servlet primarly fetches the list of all images available in the system and
* presents them in a view.
* Furthermore, by issuing a POST request to this servlet, on can filter out certain
* images using a search term as parameter. This search term is matched again the
* tags of and image.
**/
public class BrowseServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//check if user is logged in
if (GlobalHelpers.isLoggedIn(request)) {
//fetch all images and dispatch list to view
Vector<ImageBean> list=this.getImages();
request.setAttribute("images",list);
RequestDispatcher dispatcher=request.getRequestDispatcher(GlobalHelpers.getIncludedUrl("archive"));
dispatcher.forward(request,response);
} else {
response.sendRedirect("/lichtfragmente");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//check login
if (GlobalHelpers.isLoggedIn(request)) {
//search term to filter the images
String tag=request.getParameter("tag");
if (tag!=null) {
//get images containing the search term in the tags
Vector<ImageBean> list=this.getImagesByTag(tag);
request.setAttribute("images",list);
//dipatch the filtered list
RequestDispatcher dispatcher=request.getRequestDispatcher(GlobalHelpers.getIncludedUrl("archive"));
dispatcher.forward(request,response);
} else {
throw new ServletException("Keyword cannot be empty");
}
} else {
response.sendRedirect("/lichtfragmente");
}
}
/**
* Retrieves list of all images.
* This method retrieves a list of all images available in the system,
* ordered descendingly by submission date.
* As the view only display the thumbnails, we only return the image id
* and title to the view in a list of beans.
*
* @return Vector containing image information wrapped into beans.
**/
private Vector<ImageBean> getImages()
throws ServletException {
Vector<ImageBean> list=new Vector<ImageBean>();
synchronized(this) {
try {
//fetch the image data ordered by date in descending order
PreparedStatement stmt=DBInterface.doQuery("SELECT id,title FROM image ORDER BY date DESC");
ResultSet result=stmt.executeQuery();
while (result.next()) {
ImageBean temp=new ImageBean();
temp.setId(result.getInt(1));
temp.setTitle(result.getString(2));
list.add(temp);
}
} catch (SQLException sql) {
throw new ServletException("Could not perform search: "+sql.getMessage());
}
}
return list;
}
/**
* Retrieves list of all images filtered by a search keyword.
* This method retrieves a list of all images available in the system,
* whose 'tags' field matches a given search term, ordered descendingly by
* submission date.
* As the view only display the thumbnails, we only return the image id
* and title to the view in a list of beans.
*
* @return Vector containing image information wrapped into beans.
**/
private Vector<ImageBean> getImagesByTag(String tag)
throws ServletException {
Vector<ImageBean> list=new Vector<ImageBean>();
synchronized(this) {
try {
//filter the images according to the search term using the DB's LIKE operator
PreparedStatement stmt=DBInterface.doQuery("SELECT id,title FROM image WHERE lower(tags) LIKE '%' || ? || '%' ORDER BY date DESC");
//make the keyword lowercase to make the search case insensitive
stmt.setString(1,tag.toLowerCase());
ResultSet result=stmt.executeQuery();
while (result.next()) {
ImageBean temp=new ImageBean();
temp.setId(result.getInt(1));
temp.setTitle(result.getString(2));
list.add(temp);
}
} catch (SQLException sql) {
throw new ServletException("Could not perform search: "+sql.getMessage());
}
}
return list;
}
}