package uppg.IP2.booking; import java.io.*; import mixer.*; import java.sql.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class booking extends HttpServlet{ String html = null; String[] hours = {"08-09", "09-10", "10-11", "11-12", "12-13","13-14", "14-15", "15-16", "16-17"}; String[] rooms = {"607", "609", "610", "612", "613", "614", "616", "620"}; int[] helper = null; Connection con = null; PreparedStatement pstmt = null; PreparedStatement pqstmt = null; public void init() throws ServletException{ try{ String url = "jdbc:mysql://localhost/booking"; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, "josef", "cotaidis"); pstmt = con.prepareStatement("INSERT INTO roombooking (sdate, edate, room) values (?, ?, ?);"); pqstmt = con.prepareStatement("SELECT * FROM message WHERE sdate >= ? AND edate <= ? ORDER BY sdate, room;"); }catch(ClassNotFoundException cnfe){ log("Couldn't load database driver: " + cnfe.getMessage()); }catch(SQLException sqle){ log("SQLException caught: " + sqle.getMessage()); } if (html == null) { html = Mixer.getContent(new File(getServletContext().getRealPath("/WEB-INF/classes/uppg/IP2/booking/booking.html"))); } } public void destroy(){ try{ con.close(); }catch(SQLException sqle){ log("SQLException caught: " + sqle.getMessage()); } } /*String strDate = String.format("Time: %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", cal);*/ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{ Mixer mix = new Mixer(html); Calendar cal = Calendar.getInstance(); helper = new int[rooms.length]; java.sql.Timestamp from = java.sql.Timestamp.valueOf("2007-01-04 08:00:00"); java.sql.Timestamp to = java.sql.Timestamp.valueOf("2007-01-04 17:00:00"); try{ pqstmt.setTimestamp(1, from); pqstmt.setTimestamp(1, to); ResultSet rs = pqstmt.executeQuery(); }catch(SQLException sqle){ log("SQLException caught: " + sqle.getMessage()); } res.setContentType("text/html"); PrintWriter out = res.getWriter(); mix.add("===width1===", "width=\""+100/(rooms.length+1)+"%\""); for (int i = 0; i < rooms.length; i++){ mix.add("===width2===", "width=\""+100/(rooms.length+1)+"%\""); mix.add("", "===room===", rooms[i]); } for (int i = 0; i < hours.length; i++){ mix.add("", "===hour===", hours[i]); String tmp = ""; for (int j = 0; j < rooms.length; j++){ tmp = tmp + " "; } mix.add("", "===bookings===", tmp); } for (int i = 0; i < rooms.length; i++){ mix.add("", "===number===", ""+rooms[i]); mix.add("", "===roomnumber===", rooms[i]); } for (int i = cal.get(Calendar.YEAR); i < cal.get(Calendar.YEAR) + 4; i++){ mix.add("", "===year===", "" + i); mix.add("", "===yearnumber===", "" + i); } for (int i = 1; i <= 12; i++){ mix.add("", "===month===", "" + i); mix.add("", "===monthnumber===", "" + i); } for (int i = 1; i <= 31; i++){ mix.add("", "===day===", "" + i); mix.add("", "===daynumber===", "" + i); } for (int i = 8; i <= 17; i++){ mix.add("", "===fromhour===", "" + i); mix.add("", "===fromhournumber===", "kl: " + i); } for (int i = 8; i <= 17; i++){ mix.add("", "===tohour===", "" + i); mix.add("", "===tohournumber===", "kl: " + i); } out.println(mix.getMix()); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{ String dag = req.getParameter("ar") + "-" +req.getParameter("manad") + "-" +req.getParameter("dag") + " "; java.sql.Timestamp from = java.sql.Timestamp.valueOf(dag + req.getParameter("fran") + ":00:00"); java.sql.Timestamp to = java.sql.Timestamp.valueOf(dag + req.getParameter("till") + ":00:00"); try{ pstmt.setTimestamp(1, from); pstmt.setTimestamp(2, to); pstmt.setInt(3, Integer.parseInt(req.getParameter("grupprum"))); pstmt.executeUpdate(); }catch(SQLException sqle){ log("SQLException caught: " + sqle.getMessage()); } doGet(req, res); } }