From f6c9f4f208a737118ca5616364a7349145d825f8 Mon Sep 17 00:00:00 2001 From: lwm14001 Date: Wed, 16 Nov 2016 16:14:42 -0500 Subject: [PATCH] Quinn wants the new code --- DCSJSON.java | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 DCSJSON.java diff --git a/DCSJSON.java b/DCSJSON.java new file mode 100644 index 0000000..f667d7a --- /dev/null +++ b/DCSJSON.java @@ -0,0 +1,146 @@ + +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.*; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.sql.DataSource; + +public class DCSJSON extends HttpServlet { + private static final long serialVersionUID = 1L; + + + // This sets up the JSON array for The final JSON object + public String reportStrings(ResultSet rs, String columnName) throws SQLException{ + + // Should use string buffer instead + String resultantJSON = "["; + + //index for code that should run after first loop back + int x = 0; + while(rs.next()){ + if (x!=0) {resultantJSON += ",";} + resultantJSON += "\"" + rs.getString(columnName) + "\""; + x += 1; + } + resultantJSON += "]"; + return resultantJSON; + } + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Context ctx = null; + Connection con = null; + Statement stmt = null; + ResultSet rs = null; + try{ + ctx = new InitialContext(); + DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/LocalStudentAdminDB"); + con = ds.getConnection(); + stmt = con.createStatement(); + PrintWriter out = response.getWriter(); + rs = stmt.executeQuery("CALL getAllTermNames()");//SELECT * FROM SA_Development.TERM" +// ResultSetMetaData rsmd = rs.getMetaData(); +// int columnCount = rsmd.getColumnCount(); + String JSONEntity = "{\"TERM_NAME\":" + reportStrings(rs,"TERM_NAME") + ",\"CAMPUS_NAME\":"; + rs.close(); + rs = stmt.executeQuery("CALL getAllCampusNames()"); + JSONEntity += reportStrings(rs,"CAMPUS_NAME") + ",\"DEPARTMENT_NAME\":"; + rs.close(); + rs = stmt.executeQuery("CALL getAllDepartmentNames()"); + JSONEntity += reportStrings(rs,"DEPARTMENT_NAME") + "}"; + response.setContentType("application/json"); + out.print(JSONEntity); + }catch(NamingException e){ + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + }finally{ + try { + rs.close(); + stmt.close(); + con.close(); + ctx.close(); + } catch (SQLException e) { + System.out.println("Exception in closing DB resources"); + } catch (NamingException e) { + System.out.println("Exception in closing Context"); + } + + } + } + public String extractDCSArgs(HttpServletRequest request) { + String result = "CALL getCourseSearchResults('" + + request.getParameter("Term") + "','" + + request.getParameter("Campus") + "'," + + request.getParameter("Availability") + "," + + request.getParameter("Honors") + "," + + request.getParameter("Q") + "," + + request.getParameter("W") + "," + + "'00:00:00'," + + "'23:59:59','" + + request.getParameter("Department") + "'," + + request.getParameter("CourseNumber"); + return result; + } + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Context ctx = null; + Connection con = null; + Statement stmt = null; + ResultSet classSearchRes = null; + //out.println(extractDCSArgs(request)); + try{ + ctx = new InitialContext(); + DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/LocalStudentAdminDB"); + con = ds.getConnection(); + stmt = con.createStatement(); + //add extractDCSArgs() + PrintWriter out = response.getWriter(); + classSearchRes = stmt.executeQuery("CALL getCourseSearchResults('SPRING_2016','Storrs',0,0,0,0,'00:00:00','23:59:59','Computer Science and Engineering',1729)"); + ResultSetMetaData rsmd = classSearchRes.getMetaData(); + + // DynamicClassSearchResult in JSON + String DCSRJSON = "["; + int addComma = 0; + while(classSearchRes.next()) { + if(addComma >= 1) {DCSRJSON += ",";} + DCSRJSON += reportResultRow(classSearchRes,classSearchRes.getMetaData()); + addComma += 1; + } + DCSRJSON += "]"; + response.setContentType("application/json"); + out.print(DCSRJSON); + }catch(NamingException e){ + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + }finally{ + try { + classSearchRes.close(); + stmt.close(); + con.close(); + ctx.close(); + } catch (SQLException e) { + System.out.println("Exception in closing DB resources"); + } catch (NamingException e) { + System.out.println("Exception in closing Context"); + } + + } + } + public String reportResultRow(ResultSet classSearchRes, ResultSetMetaData resTableMetaData) throws SQLException{ + String rowResultJSON = "{"; + int columnCount = resTableMetaData.getColumnCount(); + for(int i = 1;i <= columnCount;i++){ + if(i >= 1){rowResultJSON += ",";} + rowResultJSON += "\"" + resTableMetaData.getColumnName(i) + "\":\"" + classSearchRes.getString(i) + "\""; + } + rowResultJSON += "}"; + return rowResultJSON; + } +}