-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |