/* * Code by George Kichukov for SAS 11/03/2002 * Updated 11/24/02 * - added check session if logged in - took care of the different cases as descibed below * This class is used to display all records from the table selected in PresentMenu * and/or to redirect to the appropriate input handler * It takes the table_name and the operation from PresentMenu * If a list is presented the user can choose which one they want to administer * if the operation is add it redirects to Input * There are several cases: 1. All Requirements (e.g. all tables ending in _REQ) are serviced by Req.java and InputReq.java a) if it is major or minor req presents major or minor list to choose b) else - if Add go to Req - else - display list to choose and operation on top 2. All Other Tables are Serviced by Input.java a) if Add - go to Input b) if Modify/View/Delete - display all records to choose one to perform the operation on * Updated 12/05/02 - add checkDelete() javascript * Updated 12/08/02 - added some formatting */ package admin; import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; import utilities.*; public class TableDisplay extends HttpServlet { private String errorTarget = "../admin/index.jsp"; public void init(ServletConfig config) throws ServletException { super.init(config); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // If it is a get request forward to doPost() doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ResultSet rs=null; //name of the user String name=""; PrintWriter out = response.getWriter(); response.setContentType("text/html"); //first thing that every servlet or jsp should do is check if user is logged in HttpSession session = request.getSession(false); //if not logged in redirect to login if(session == null) { HTMLUtils.printRedirectPage(out, "You must login to the system prior to viewing this page.", errorTarget); return; } //else if logged in take the name from the session else { name=(String)session.getAttribute("user"); //if the name is null if(name == null) { HTMLUtils.printRedirectPage(out, "You must login to the system prior to viewing this page.", errorTarget); return; } } HTMLUtils.printAdminHeader(out); //display welcome message to the user out.println("

Welcome " + name + "

"); //get the parameters String table=request.getParameter("table"); String oper=request.getParameter("operation"); String html=""; try { //if it is the MAJOR or MINOR REQs select for which major or minor first if((table.equals("MAJOR_REQ")) || (table.equals("MINOR_REQ"))) { html = html + "
Please select one:"; //pass the table name and the operation along html = html + ""; html = html + ""; html = html + "
"; rs.close(); out.println(html); } //if it is any other req simply forward to Req else if((table.substring(table.length()-3, table.length())).equals("REQ")) { String target = "/servlet/admin.Req?table=" + table + "&oper=" + oper; try { // Forward the request to the target named ServletContext context = getServletContext(); RequestDispatcher dispatcher = context.getRequestDispatcher(target); dispatcher.forward(request, response); } catch(ServletException e){System.err.println("ServeltException" + e.getMessage());} catch(IOException e){System.err.println("IOException" + e.getMessage());} } //display a list of records for the user to choose from if the Operation is Modify/Delete/View //or redirect to Input of the operation is Add else { //redirect to Input of the operation is Add and pass the parameters (table, operation) if(oper.equals("Add")) { String target = "/servlet/admin.Input?table=" + table + "&oper=" + oper; try { // Forward the request to the target named ServletContext context = getServletContext(); RequestDispatcher dispatcher = context.getRequestDispatcher(target); dispatcher.forward(request, response); } catch(ServletException e){System.err.println("ServeltException" + e.getMessage());} catch(IOException e){System.err.println("IOException" + e.getMessage());} } //display a list of records for the user to choose from if the Operation is Modify/Delete/View else { if(oper.equals("Delete")) { //set up the confirm script out.println(""); } //get exact field names from table names e.g. from SCHOOLS to SchoolID or SchoolName String fieldID=table.substring(0,1) + (table.substring(1,table.length()-1)).toLowerCase() + "ID"; String fieldName=table.substring(0,1) + (table.substring(1,table.length()-1)).toLowerCase() + "Name"; html=html + "Please select the exact record that you want to " + oper.toLowerCase() + ": "; html=html + "
"; html = html + ""; html = html + ""; //for courses we need the course code as well if(table.equals("COURSES")) { rs=Retriever.getCourseList(); // Get the number of rows from the result set ResultSet rs2 = Retriever.getRowCount(table); rs2.next(); int rowcount = rs2.getInt(1); //use this to divide in three columns rowcount=rowcount/3; int counter=0; //set up the table html= html + "
"; while(rs.next()) { if(counter++==rowcount) { html= html + " "; counter=0; } html= html + "" + rs.getString(2) + "
"; } //end table html = html + "
"; } else { rs=Retriever.getList(fieldID,fieldName, table); // Get the number of rows from the result set ResultSet rs2 = Retriever.getRowCount(table); rs2.next(); int rowcount = rs2.getInt(1); if(rowcount>9) { //use this to divide in three columns rowcount=rowcount/3; int counter=0; //set up the table html= html + "
"; while(rs.next()) { if(counter++==rowcount+1) { html= html + " "; counter=0; } html= html + "" + rs.getString(2)+ "
"; } //end table html = html + "
"; } //no three columns else { while(rs.next()) { html= html + "" + rs.getString(2)+ "
"; } } } html = html + "
"; rs.close(); out.println(html); } } } catch(SQLException e){out.println("SQLException " + e.getMessage());} HTMLUtils.printAdminFooter(out); } }