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.UserBean;
import com.lichtfragmente.beans.ImageBean;
/**
* Servlet handling all actions which are associated to user's profile.
* This servlet carries out all tasks which have to be done in order to
* compile a user's profile.
* These tasks involve getting user information, getting a user's latest
* images, featured images and followers.
* Furthermore, when sent a POST request, this servlet carries out the
* actions necessary to modify the list followers a user has. This is done
* by the views by submitting AJAX POST requests.
**/
public class ProfileServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//check login
if (GlobalHelpers.isLoggedIn(request)) {
//get user id from URL
int userid=Integer.parseInt(request.getParameter("user"));
//get user information
UserBean userinfo=this.getUserInformation(userid);
request.setAttribute("user",userinfo);
//get featured image randomly
ImageBean featured=this.getFeatured(userid);
request.setAttribute("featured",featured);
//get list of followers
Vector<UserBean> followers=this.getFollowers(userid);
request.setAttribute("followers",followers);
//get 7 latest images
Vector<ImageBean> latest=this.getLatestImages(userid);
request.setAttribute("latest",latest);
//dispatch view
RequestDispatcher dispatcher=request.getRequestDispatcher(GlobalHelpers.getIncludedUrl("profile"));
dispatcher.forward(request,response);
} else {
response.sendRedirect("/lichtfragmente");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (GlobalHelpers.isLoggedIn(request)) {
PrintWriter out=response.getWriter();
//is the 'actionparameter set?'
//which is used to determine whether the servlet should
//carry out actions for 'follow' or 'unfollow'
if (request.getParameter("useraction")!=null) {
//get user id and id of user which wubmitted the request
int userid=Integer.parseInt(request.getParameter("user"));
int followerid=(Integer)request.getSession().getAttribute("userid");
//if user wants to follow the owner of this profile
if (request.getParameter("useraction").compareTo("follow")==0) {
synchronized(this) {
try {
//insert follower data
PreparedStatement stmt=DBInterface.doQuery("INSERT INTO user_has_follower(userid,followerid) VALUES(?,?)");
stmt.setInt(1,userid);
stmt.setInt(2,followerid);
stmt.executeUpdate();
//respond with 'success', which is interpreted by the js
out.print("success");
stmt.close();
DBInterface.close();
} catch (SQLException sql) {
out.print("error");
}
}
} else if (request.getParameter("useraction").compareTo("unfollow")==0) {
//user wants to carry out unfollow action
synchronized(this) {
try {
//remove user from followers list
PreparedStatement stmt=DBInterface.doQuery("DELETE FROM user_has_follower WHERE userid=? AND followerid=?");
stmt.setInt(1,userid);
stmt.setInt(2,followerid);
stmt.executeUpdate();
out.print("success");
stmt.close();
DBInterface.close();
} catch (SQLException sql) {
out.print("error: "+sql.getMessage());
}
}
}
}
} else {
response.sendRedirect("/lichtfragmente");
}
}
/**
* Gets information about the user.
* This method gets all information associated to a user given by
* his ID and returns it wrapped into a bean.
*
* @param userid The user's ID we want to obtain information of
* @return The user information wrapped into a bean
**/
private UserBean getUserInformation(int userid)
throws ServletException {
UserBean temp=new UserBean();
synchronized(this) {
try {
//request information
PreparedStatement stmt=DBInterface.doQuery("SELECT name,info,firstname,lastname FROM users WHERE id=?");
stmt.setInt(1,userid);
ResultSet result=stmt.executeQuery();
//store result
if (result.next()) {
temp.setId(userid);
temp.setName(result.getString("name"));
temp.setInfo(GlobalHelpers.nl2br(result.getString("info")));
temp.setFirstname(result.getString("firstname"));
temp.setLastname(result.getString("lastname"));
} else {
throw new ServletException("User with ID "+userid+" not found!");
}
result.close();
stmt.close();
DBInterface.close();
} catch(SQLException se) {
throw new ServletException("Could not retrieve user information! "+se.getMessage());
}
}
return temp;
}
/**
* Get latest images of a user.
* This method retrieves information of all images the user in question
* has uploaded to the site and returns the information wrapped into
* a list of beans.
*
* @param userid The ID of the user we want to retrieve the images of
* @return Vector containing image information as beans
**/
private Vector<ImageBean> getLatestImages(int userid)
throws ServletException {
Vector<ImageBean> resultList=new Vector<ImageBean>();
synchronized(this) {
try {
//get images for user id
PreparedStatement stmt=DBInterface.doQuery("SELECT id,title FROM image WHERE owner=? ORDER BY id DESC");
stmt.setInt(1,userid);
ResultSet result=stmt.executeQuery();
//wrap information and add it to list
while (result.next()) {
ImageBean temp=new ImageBean();
temp.setId(result.getInt("id"));
temp.setTitle(result.getString("title"));
resultList.add(temp);
}
result.close();
stmt.close();
DBInterface.close();
} catch (SQLException se) {
throw new ServletException("Could not retrieve latest images! "+se.getMessage());
}
}
return resultList;
}
/**
* Get list of followers for a certain user.
* This method retrieves the list of followers a certain user
* has wrapped into a list of beans.
*
* @param userid ID of the user we want to get the follower list of
* @return List of followers as beans
**/
private Vector<UserBean> getFollowers(int userid)
throws ServletException {
Vector<UserBean> list=new Vector<UserBean>();
synchronized(this) {
try {
//get names and IDs of followers the given user has
PreparedStatement stmt=DBInterface.doQuery("SELECT u.id,u.name " +
"FROM user_has_follower uhf " +
"INNER JOIN users u ON u.id=uhf.followerid " +
"WHERE uhf.userid=? " +
"ORDER BY RANDOM()");
stmt.setInt(1,userid);
ResultSet result=stmt.executeQuery();
//store information and add it to list
while (result.next()) {
UserBean temp=new UserBean();
temp.setId(result.getInt(1));
temp.setName(result.getString(2));
list.add(temp);
}
} catch (SQLException se) {
throw new ServletException("Could not retrieve follower information! "+se.getMessage());
}
}
return list;
}
/**
* Get a featured image for a given user.
* This method selects randomly an image belonging to a given
* user, which can be presented in the 'featured' section of
* the profile view.
*
* @param userid ID of the user for which we want a featured image
* @return A random image from the database as bean
**/
private ImageBean getFeatured(int userid)
throws ServletException {
ImageBean temp=null;
synchronized(this) {
try {
//select random image from DB
PreparedStatement stmt=DBInterface.doQuery("SELECT id,title " +
"FROM image " +
"WHERE owner=?" +
"ORDER BY RANDOM() LIMIT 1;");
stmt.setInt(1,userid);
ResultSet result=stmt.executeQuery();
if (result.next()) {
temp=new ImageBean();
temp.setId(result.getInt(1));
temp.setTitle(result.getString(2));
}
result.close();
stmt.close();
DBInterface.close();
} catch (SQLException se) {
throw new ServletException("Could not retrieve featrued image! "+se.getMessage());
}
}
return temp;
}
}