import java.io.*; import mixer.*; import java.sql.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class adminMain extends HttpServlet{ String bookings = null; String users = null; Connection con = null; PreparedStatement pqstmt = null; PreparedStatement pqstmt2 = null; PreparedStatement pstmt = null; PreparedStatement pstmt2 = null; PreparedStatement pstmt3 = null; public void init() throws ServletException{ bookings = Mixer.getContent(new File(getServletContext().getRealPath("/WEB-INF/classes/bookings.html"))); users = Mixer.getContent(new File(getServletContext().getRealPath("/WEB-INF/classes/users.html"))); try{ String url = "jdbc:mysql://localhost/booking"; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, "josef", "cotaidis"); pqstmt = con.prepareStatement("SELECT * FROM roombooking INNER JOIN user ON roombooking.uid = user.uid;"); pqstmt2 = con.prepareStatement("SELECT * FROM user ORDER BY username;"); pstmt = con.prepareStatement("DELETE FROM roombooking WHERE bid = ?;"); pstmt2 = con.prepareStatement("INSERT INTO user (username, password) VALUES (?, ?);"); pstmt3 = con.prepareStatement("DELETE FROM user WHERE uid = ?;"); }catch(ClassNotFoundException cnfe){ log("Couldn't load database driver: " + cnfe.getMessage()); }catch(SQLException sqle){ log("SQLException caught: " + sqle.getMessage()); } } public void destroy(){ try{ con.close(); }catch(SQLException sqle){ log("SQLException caught: " + sqle.getMessage()); } } public int parseNumber(String str){ int tmp = 0; if (str.startsWith("0")) tmp = Integer.parseInt(str.substring(1)); else tmp = Integer.parseInt(str); return tmp; } public String parseNumber(int num){ String tmp = null; if (num < 10) tmp = "0" + Integer.toString(num); else tmp = Integer.toString(num); return tmp; } public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{ HttpSession session = req.getSession(); Object done = session.getAttribute("adminLogon.isDone"); if (done == null) { //Samma funktionalitet som för servlet/booking session.setAttribute("adminLogin.target", HttpUtils.getRequestURL(req).toString() + (req.getQueryString() == null?"":"?" + req.getQueryString())); res.sendRedirect("/booking/servlet/login"); return; } //Inte så smidig design men parametern action kan bara ha två värden. Det skulle vara bättre med separata servlets. if ("bookings".equals(req.getParameter("action"))){ bookings(req, res); }else users(req, res); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{ if ("bookings".equals(req.getParameter("nav"))){ String tmp = null; Enumeration names = req.getParameterNames(); while(names.hasMoreElements()){ tmp = (String)names.nextElement(); if ((tmp.length() > 7) && "checkbox".equals(tmp.substring(0, 8))){ log(tmp); if (req.getParameter(tmp) != null){ try{ pstmt.setInt(1, Integer.parseInt(req.getParameter(tmp))); pstmt.executeUpdate(); }catch(SQLException sqle){ log("SQLException caught: " + sqle.getMessage()); } } } } res.sendRedirect("/booking/servlet/adminMain?action=bookings"); }else if ("users".equals(req.getParameter("nav"))){ if (req.getParameter("ta bort") != null){ String tmp = null; Enumeration names = req.getParameterNames(); while(names.hasMoreElements()){ tmp = (String)names.nextElement(); if ((tmp.length() > 7) && "checkbox".equals(tmp.substring(0, 8))){ log(tmp); if (req.getParameter(tmp) != null){ try{ pstmt3.setInt(1, Integer.parseInt(req.getParameter(tmp))); pstmt3.executeUpdate(); }catch(SQLException sqle){ log("SQLException caught: " + sqle.getMessage()); } } } } }else{ try{ pstmt2.setString(1, req.getParameter("username")); pstmt2.setString(2, req.getParameter("password")); pstmt2.executeUpdate(); }catch(SQLException sqle){ log("SQLException caught: " + sqle.getMessage()); } } res.sendRedirect("/booking/servlet/adminMain?action=users"); } } public void bookings(HttpServletRequest req, HttpServletResponse res) throws IOException{ Mixer mix = new Mixer(bookings); res.setContentType("text/html"); PrintWriter out = res.getWriter(); ResultSet rs = null; try{ rs = pqstmt.executeQuery(); if (rs.next()){ rs.previous(); int i = 0; while (rs.next()){ mix.add("", "===r===", rs.getString("room")); mix.add("", "===d===", (rs.getTimestamp("sdate").toString().substring(0, 10))); mix.add("", "===st===", parseNumber(rs.getTimestamp("sdate").getHours())+":00"); mix.add("", "===sl===", parseNumber(rs.getTimestamp("edate").getHours())+":00"); mix.add("", "===a===", rs.getString("username")); mix.add("", "===n===", "" + i++); mix.add("", "===u===", rs.getInt("bid")+""); } }else{ mix.removeHTML(""); mix.removeHTML(""); mix.removeHTML(""); } }catch(SQLException sqle){ log("SQLException caught: " + sqle.getMessage()); } out.println(mix.getMix()); } public void users(HttpServletRequest req, HttpServletResponse res) throws IOException{ Mixer mix = new Mixer(users); res.setContentType("text/html"); PrintWriter out = res.getWriter(); ResultSet rs = null; try{ rs = pqstmt2.executeQuery(); if (rs.next()){ rs.previous(); int i = 0; while (rs.next()){ mix.add("", "===u===", ""+rs.getInt("uid")); mix.add("", "===na===", rs.getString("username")); mix.add("", "===p===", rs.getString("password")); mix.add("", "===nu===", "" + i++); mix.add("", "===n===", ""+rs.getInt("uid")); } }else{ mix.removeHTML(""); mix.removeHTML(""); mix.removeHTML(""); } }catch(SQLException sqle){ log("SQLException caught: " + sqle.getMessage()); } out.println(mix.getMix()); } }