Package ee.widespace.forum.servlet

Source Code of ee.widespace.forum.servlet.ForumService

package ee.widespace.forum.servlet;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.parsers.*;


import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.*;


import ee.widespace.forum.*;
import ee.widespace.forum.data.ForumData;
import ee.widespace.util.Date;
import ee.widespace.xmlhandler.AdminHandler;
public class ForumService  extends HttpServlet {
/**
* doGet method comment.
*/
public void doGet( HttpServletRequest req,
          HttpServletResponse res )
throws ServletException, IOException {
  doService( req, res );
}
/**
* doPost method comment.
*/
public void doPost( HttpServletRequest req,
          HttpServletResponse res )
throws ServletException, IOException {
  doService( req, res );
}
/**
* doGet method comment.
*/
public void doService(HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException {
  String act = req.getParameter("action");
 
  if (act == null)
  act = "";
  else
    if (act.equals("new message")) {
      try {
        Date date     = new Date();
        Message msg   = new Message();
        msg.name      = req.getParameter("author");
        msg.subject   = req.getParameter("subject");
        msg.message   = req.getParameter("message");
        msg.replayID  = Integer.parseInt( req.getParameter("messageID") );
        msg.date      = date.toString();
        String forumName    = req.getParameter("forumName");
          if ( (msg.subject == null) || ( msg.subject.length() == 0 ) ){
            req.setAttribute("e" , "Field 'Subject' is empty");         
            req.getRequestDispatcher( "/forum.jsp?forumName=" + forumName + "&messageID= " + 0 )
              .forward( req, res );
            return;
          }
        Connection conn = getConnection();
        try {
         
          ForumData data = new ForumData(conn);
          data.addMessage( msg, forumName);
          conn.commit();
         
        } finally {
          conn.close();
        }

        res.sendRedirect("f_fr.jsp?forumName=" + forumName);
      } catch (Exception e) {
        e.printStackTrace();
          //req.getRequestDispatcher("/admin/errorPage.jsp").forward(req, res);
      }
    }
    else
    if (act.equals("delete forum")) {
      try {
        String forumName    = req.getParameter("forumName");
          if ( (forumName == null) || ( forumName.length() == 0 ) ){
            req.setAttribute("e" , "Field 'Subject' is empty");         
            req.getRequestDispatcher( "/forum.jsp?forumName=" + forumName + "&messageID= " + 0 )
              .forward( req, res );
            return;
          }
        Connection conn = getConnection();
        try {
         
          ForumData data = new ForumData(conn);
          int code = data.deleteForum( forumName);
          if ( code > 0 ){
            req.setAttribute( "e", "Forum not empty, can't delete" );
            System.out.println( "Forum not empty, can't delete" )
          }
          conn.commit();
         
        } finally {
          conn.close();
        }

        res.sendRedirect("main.jsp");
      } catch (Exception e) {
        e.printStackTrace();
          //req.getRequestDispatcher("/admin/errorPage.jsp").forward(req, res);
      }
    }
    else
    if (act.equals("delete message")) {
      try {
        int messageID     = Integer.parseInt( req.getParameter("messageID") );
        String forumName = req.getParameter("forumName");
       
        Connection conn  = getConnection();
        try {
          ForumData data = new ForumData(conn);
  System.out.println("delete message ... prepered");
          data.deleteMessage( messageID );
  System.out.println("delete message ... ok");
          conn.commit();
  System.out.println("delete message ...commit....ok");
        } finally {
          conn.close();
        }

        res.sendRedirect("f_fr.jsp?forumName=" + forumName);
      } catch (Exception e) {
        e.printStackTrace();
          //req.getRequestDispatcher("/admin/errorPage.jsp").forward(req, res);
      }
    }
    else
    if (act.equals("password check")) {
      try {
        //int messageID     = Integer.parseInt( req.getParameter("messageID") );
        //String forumName = req.getParameter("forumName");
        String login   = req.getParameter("login");
        String password  = req.getParameter("password");
  System.out.println("password check initiated , login = " +login+" ,password = " + password);
        try {
          List list = AdminHandler.parse( getServletContext()
              .getResource( "/WEB-INF/security.xml" )
              .toExternalForm()  );
          Iterator iter = list.iterator();
          while (iter.hasNext()) {
            Admin ad = (Admin)iter.next();
            if (login.equals( ad.name ) && password.equals( ad.password )){
              res.sendRedirect("mainAdmin.jsp?p=" + "yes");
  System.out.println("checking ....... , login = " +ad.name+" ,password = " + ad.password);
            }
          }
          res.sendRedirect("main.jsp");

        } catch (IOException e ){
          e.printStackTrace();         
        }

      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    else
    if (act.equals("new forum")) {
      try {
        String forumName = req.getParameter("forumName");
        System.out.println("forum Name = " + forumName);
          if ( (forumName == null) || ( forumName.length() == 0 ) ){
            req.setAttribute("e" , "Field 'Name' is empty");         
            req.getRequestDispatcher( "/newForum.jsp" )
              .forward( req, res );
            return;
          }
        Connection conn = getConnection();
        try {
         
          ForumData data = new ForumData(conn);
          data.addForum( forumName );
          conn.commit();
         
        } finally {
          conn.close();
        }

        res.sendRedirect("main.jsp");
      } catch (Exception e) {
        e.printStackTrace();
          //req.getRequestDispatcher("/admin/errorPage.jsp").forward(req, res);
      }
    }
   
   
  }


/**
* getConnection
*/
public Connection getConnection() throws SQLException {
  String url = getServletContext()
    .getInitParameter( "jdbc.url" );
 
  Connection con = DriverManager.getConnection( url );
  try {
    con.setAutoCommit( false );
    return con;
  } catch ( SQLException e ) {
    con.close();
    throw e;
  }
}


}






TOP

Related Classes of ee.widespace.forum.servlet.ForumService

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.