Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Quinn wants the new code
  • Loading branch information
lwm14001 committed Nov 16, 2016
1 parent f0eff32 commit f6c9f4f
Showing 1 changed file with 146 additions and 0 deletions.
146 changes: 146 additions & 0 deletions 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;
}
}

0 comments on commit f6c9f4f

Please sign in to comment.