001: package events;
002:
003: import util.HibernateUtil;
004:
005: import javax.servlet.http.*;
006: import javax.servlet.ServletException;
007: import java.io.*;
008: import java.util.*;
009: import java.text.SimpleDateFormat;
010:
011: public class EventManagerServlet extends HttpServlet {
012:
013: protected void doGet(HttpServletRequest request,
014: HttpServletResponse response) throws ServletException,
015: IOException {
016:
017: SimpleDateFormat dateFormatter = new SimpleDateFormat(
018: "dd.MM.yyyy");
019:
020: try {
021: // Begin unit of work
022: HibernateUtil.getSessionFactory().getCurrentSession()
023: .beginTransaction();
024:
025: // Write HTML header
026: PrintWriter out = response.getWriter();
027: out
028: .println("<html><head><title>Event Manager</title></head><body>");
029:
030: // Handle actions
031: if ("store".equals(request.getParameter("action"))) {
032:
033: String eventTitle = request.getParameter("eventTitle");
034: String eventDate = request.getParameter("eventDate");
035:
036: if ("".equals(eventTitle) || "".equals(eventDate)) {
037: out
038: .println("<b><i>Please enter event title and date.</i></b>");
039: } else {
040: createAndStoreEvent(eventTitle, dateFormatter
041: .parse(eventDate));
042: out.println("<b><i>Added event.</i></b>");
043: }
044: }
045:
046: // Print page
047: printEventForm(out);
048: listEvents(out, dateFormatter);
049:
050: // Write HTML footer
051: out.println("</body></html>");
052: out.flush();
053: out.close();
054:
055: // End unit of work
056: HibernateUtil.getSessionFactory().getCurrentSession()
057: .getTransaction().commit();
058:
059: } catch (Exception ex) {
060: HibernateUtil.getSessionFactory().getCurrentSession()
061: .getTransaction().rollback();
062: throw new ServletException(ex);
063: }
064: }
065:
066: private void printEventForm(PrintWriter out) {
067: out.println("<h2>Add new event:</h2>");
068: out.println("<form>");
069: out
070: .println("Title: <input name='eventTitle' length='50'/><br/>");
071: out
072: .println("Date (e.g. 24.12.2009): <input name='eventDate' length='10'/><br/>");
073: out
074: .println("<input type='submit' name='action' value='store'/>");
075: out.println("</form>");
076: }
077:
078: private void listEvents(PrintWriter out,
079: SimpleDateFormat dateFormatter) {
080: List result = HibernateUtil.getSessionFactory()
081: .getCurrentSession().createCriteria(Event.class).list();
082: if (result.size() > 0) {
083: out.println("<h2>Events in database:</h2>");
084: out.println("<table border='1'>");
085: out.println("<tr>");
086: out.println("<th>Event title</th>");
087: out.println("<th>Event date</th>");
088: out.println("</tr>");
089: for (Iterator it = result.iterator(); it.hasNext();) {
090: Event event = (Event) it.next();
091: out.println("<tr>");
092: out.println("<td>" + event.getTitle() + "</td>");
093: out.println("<td>"
094: + dateFormatter.format(event.getDate())
095: + "</td>");
096: out.println("</tr>");
097: }
098: out.println("</table>");
099: }
100: }
101:
102: protected void createAndStoreEvent(String title, Date theDate) {
103: Event theEvent = new Event();
104: theEvent.setTitle(title);
105: theEvent.setDate(theDate);
106:
107: HibernateUtil.getSessionFactory().getCurrentSession().save(
108: theEvent);
109: }
110:
111: }
|