Package com.lichtfragmente.servlets

Source Code of com.lichtfragmente.servlets.GalleryManagementServlet

package com.lichtfragmente.servlets;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.Vector;

import com.lichtfragmente.beans.ImageBean;
import com.lichtfragmente.beans.GalleryBean;
import com.lichtfragmente.helpers.*;

/**
* Servlet managing all tasks associated to the deletion and creation of galleries.
* In this web application each user can choose a bunch of his images and add them
* to a gallery in order to present this very selection to the public.
* This servlet is concered with the creation and deletion of such galleries.
**/
public class GalleryManagementServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
        //check login
      if (GlobalHelpers.isLoggedIn(request)) {
            String action=request.getParameter("action");

            //user wants to create a new gallery. dispatch the associated view
            if (action.compareTo("new")==0) {
                Vector<ImageBean> list=new Vector<ImageBean>();

                //fetch list of images for this very user
                synchronized(this) {
                    try {
                        int userid=(Integer)request.getSession().getAttribute("userid");
                        PreparedStatement stmt=DBInterface.doQuery("SELECT id FROM image WHERE owner=?");
                        stmt.setInt(1,userid);
                        ResultSet result=stmt.executeQuery();

                        while (result.next()) {
                            ImageBean temp=new ImageBean();
                            temp.setId(result.getInt(1));

                            list.add(temp);
                        }

                        result.close();
                        stmt.close();
                        DBInterface.close();
                    } catch (SQLException sql) {
                        throw new ServletException("Could not retrieve images: "+sql.getMessage());
                    }
                }

                //add list of images to the request and dispatch to view for creating galleries
                request.setAttribute("imagelist",list);
                RequestDispatcher dispatcher=request.getRequestDispatcher(GlobalHelpers.getIncludedUrl("gallerymgmt/new"));
                dispatcher.forward(request,response);
            } else if (action.compareTo("edit")==0) {
                //user wants to delete an existing gallery
                Vector<GalleryBean> list=new Vector<GalleryBean>();

                //fetch list of galleries for the given user
                synchronized(this) {
                    try {
                        int userid=(Integer)request.getSession().getAttribute("userid");
                        PreparedStatement stmt=DBInterface.doQuery("SELECT id,name FROM gallery WHERE owner=?");
                        stmt.setInt(1,userid);
                        ResultSet result=stmt.executeQuery();

                        while (result.next()) {
                            GalleryBean temp=new GalleryBean();
                            temp.setId(result.getInt(1));
                            temp.setName(result.getString(2));

                            list.add(temp);
                        }

                        result.close();
                        stmt.close();
                        DBInterface.close();
                    } catch (SQLException sql) {
                        throw new ServletException("Could not retrieve images: "+sql.getMessage());
                    }
                }

                //add list of galleries to request and dispatch to view for deleting galleries
                request.setAttribute("galleries",list);
                RequestDispatcher dispatcher=request.getRequestDispatcher(GlobalHelpers.getIncludedUrl("gallerymgmt/edit"));
                dispatcher.forward(request,response);
            }
        } else {
            response.sendRedirect("/lichtfragmente");
        }
    }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
        //check login
        if (GlobalHelpers.isLoggedIn(request)) {
            String action=request.getParameter("action");

            //handles POST requests for deleting galleries
            if (action.compareTo("new")==0) {
                //create gallery and get new gallery id
                int galleryid=this.createGallery(request);

                //redirect user to newly created gallery
                if (galleryid!=0) {
                    response.sendRedirect("/lichtfragmente/gallery/"+galleryid);
                }
            } else if (action.compareTo("edit")==0) {
                //handles POST requestfor deleting galleries
                //get IDs of galleries to delete from request
                String[] galleryids=request.getParameterValues("galleryid");

                //use the IDs to delete the associated galleries
                for (String id : galleryids) {
                    int int_id=Integer.parseInt(id);
                    this.deleteGallery(int_id);
                }

                //redirect back to gallery edit page
                response.sendRedirect("/lichtfragmente/gallery/manage/edit");
            }
        } else {
            response.sendRedirect("/lichtfragmente");
        }
  }

    /**
     * Abstracts the steps required to delete a gallery.
     * This method carries the steps out, which a required to delete a gallery
     * for a certain user. To do so, it uses the gallery's ID and first deletes
     * the list of images which are contained in the gallery (be aware that the
     * images themsevles are not deleted) and them goes on to delete the gallery
     * entry itself in the database.
     *
     * @param id The ID of the gallery to delete
     **/
    private synchronized void deleteGallery(int id)
    throws ServletException {
        try {
            //delete images associated to the gallery
            PreparedStatement stmt=DBInterface.doQuery("DELETE FROM gallery_has_image WHERE galleryid=?");
            stmt.setInt(1,id);
            stmt.executeUpdate();

            stmt.close();
            stmt=null;

            //delete gallery data itself
            stmt=DBInterface.doQuery("DELETE FROM gallery WHERE id=?");
            stmt.setInt(1,id);
            stmt.executeUpdate();

            stmt.close();
            DBInterface.close();
        } catch (SQLException sql) {
            throw new ServletException("Could not delete gallery: "+sql.getMessage());
        }
    }

    /**
     * Abstracts the steps to create a new gallery.
     * This method is used to create a new gallery in the database. To do so, it
     * takes a HTTP request. The request is used to fetch the HTML form data
     * to be inserted into the database.
     * The method then returns the ID of the newly created gallery.
     *
     * @param request The HTTP request containing the HTML form data
     * @return The ID of the newly created gallery
     **/
    private synchronized int createGallery(HttpServletRequest request)
    throws ServletException {
        int userid=(Integer)request.getSession().getAttribute("userid");
        int galleryid=0;

        try {
            //insert data from HTML form
            PreparedStatement stmt=DBInterface.doQuery("INSERT INTO gallery(name,owner,bgcolor,fontcolor) VALUES (?,?,?,?)");
            stmt.setString(1,request.getParameter("name"));
            stmt.setInt(2,userid);
            stmt.setString(3,request.getParameter("bgcolor"));
            stmt.setString(4,request.getParameter("fontcolor"));
            if (stmt.executeUpdate()==0) {
                throw new ServletException("Could not insert gallery data");
            }

            stmt.close();
            stmt=null;

            //select ID of newly created gallery
            stmt=DBInterface.doQuery("SELECT MAX(id) FROM gallery");
            ResultSet result=stmt.executeQuery();
            if (result.next()) {
                galleryid=result.getInt(1);
            }

            result.close();
            stmt.close();
            stmt=null;

            if (galleryid!=0) {
                //get IDs of image to be inserted into gallery
                String[] images=request.getParameterValues("images");

                //associate each image in the array to the gallery
                for (String img : images) {
                    stmt=DBInterface.doQuery("INSERT INTO gallery_has_image(galleryid,imageid) VALUES (?,?)");
                    stmt.setInt(1,galleryid);
                    stmt.setInt(2,Integer.parseInt(img));

                    stmt.executeUpdate();
                    stmt.close();
                    stmt=null;
                }
            } else {
                throw new ServletException("Could not assign images to gallery");
            }

            DBInterface.close();
        } catch (SQLException sql) {
            throw new ServletException("Could not create gallery: "+sql.getMessage());
        }

        //return ID of new gallery
        return galleryid;
    }
}
TOP

Related Classes of com.lichtfragmente.servlets.GalleryManagementServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.