/* * Code by George Kichukov for SAS 11/03/2002 * Updated 11/24/02 - add check login - add diferent cases * This class is used to Input data for any of the tables that don't end with _REQ * It gets all paramters (table, operation, specific record ID) and displays * appropriate input fields * If operation is ADD the input fields are empty but there is a hidden field with the table name * if the operation is Modify input fields are prepopulated * if the operations is View it is dipslayed in the same way as Modify but there is no Submit Button * if operation is delete - forward to Query and pass the parameters * * Updated 12/08/02 - added formatting */ package admin; import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; import utilities.*; public class Input 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 { //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("oper"); String id=request.getParameter("id"); //if operation is delete //pass all parameters over to Query if(oper.equals("Delete")) { String target = "/servlet/admin.Query?table=" + table + "&oper=" + oper + "&id=" + id; 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());} } //if the operation is Modify/View prepopulate the input fields with //the data for the selected ID from the databse else if ( (oper.equals("Modify")) || (oper.equals("View"))) { try { ResultSet rs=null; String fieldID =""; //get the primary key name from the table name e.g. CourseID from COURSES fieldID = table.substring(0,1) + (table.substring(1,table.length()-1)).toLowerCase() + "ID"; //select * columns from the specific record String sql = "select * from " + table + " where " + fieldID + "=" + id; rs = Retriever.executeSQL(sql); rs.next(); //get all column names and information from the table ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); //send all paramters to query out.println(""); } rs.close(); }//end try catch(SQLException e){out.println("SQLException " + e.getMessage());} }//end else if Modify //it is an Add Operation //display empty fields else { try { ResultSet rs=null; String fieldID =""; //get the primary key name from the table name e.g. CourseID from COURSES fieldID = table.substring(0,1) + (table.substring(1,table.length()-1)).toLowerCase() + "ID"; //select * columns from the table String sql = "select * from " + table; rs = Retriever.executeSQL(sql); //out.println(sql); rs.next(); //get all column names and information from the table ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); //send all paramters to query out.println(""); rs.close(); }//end try catch(SQLException e){out.println("SQLException " + e.getMessage());} }//end else for Add } //this method is used to return an array of MajorIDs from the star delimited List public String [] parseString(String in) { java.util.StringTokenizer st = new java.util.StringTokenizer(in, "*"); String [] result = new String[st.countTokens()]; int i=0; while (st.hasMoreTokens()) { result[i++]=st.nextToken(); } return result; } }