List of usage examples for com.google.gson JsonPrimitive JsonPrimitive
public JsonPrimitive(Character c)
From source file:com.bios.controller.services.BootstrapService.java
/** * Handles the HTTP <code>POST</code> method to perform the bootstrap function. * Firstly, the method checks for a valid token. If there are any errors such * as an valid token or a missing bootstrap file, a json object with the status * "error" is created together with a json array of the errors that caused it. * Else if the token is valid and there are no errors, the method then * then unzips the zip file and reads the csv files to upload the relevant data * into the respective DAOs and database. While it does this, ArrayLists are used * to keep track of the errors such as invalid inputs or bids that cannot be bidded * for in round 1. If these errors exist, then the json object would contain the * status "error" and store the errors. Otherwise, the json object will have the * status "success" to signifiy that the bootstrap was successful with no errors. * @param request servlet request/*from www . j ava 2 s . c om*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String token = request.getParameter("token"); Part filePart = request.getPart("bootstrap-file"); // Retrieves <input type="file" name="file"> JsonObject responseObject = new JsonObject(); JsonArray allErrorsArr = new JsonArray(); if (token == null) { allErrorsArr.add(new JsonPrimitive("missing token")); } if (filePart == null) { allErrorsArr.add(new JsonPrimitive("missing bootstrap-file")); } if (allErrorsArr.size() > 0) { responseObject.addProperty("status", "error"); responseObject.add("message", allErrorsArr); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } if (token.length() == 0) { allErrorsArr.add(new JsonPrimitive("blank token")); } if (filePart.getSize() == 0) { allErrorsArr.add(new JsonPrimitive("blank bootstrap-file")); } if (allErrorsArr.size() > 0) { responseObject.addProperty("status", "error"); responseObject.add("message", allErrorsArr); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } try { String result = JWTUtility.verify(token, "abcdefgh12345678"); if (!result.equals("admin")) { JsonPrimitive value = new JsonPrimitive("invalid token"); allErrorsArr.add(value); responseObject.addProperty("status", "error"); responseObject.add("message", allErrorsArr); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } } catch (JWTException e) { // This means not an admin, or token expired JsonPrimitive value = new JsonPrimitive("invalid username/token"); allErrorsArr.add(value); responseObject.addProperty("status", "error"); responseObject.add("message", allErrorsArr); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } InputStream inputStream = filePart.getInputStream(); ServletContext context = getServletContext(); //Uploaded to a file called bootstrap.zip File f = new File(context.getRealPath("/WEB-INF") + "/bootstrap.zip"); OutputStream outputStream = new FileOutputStream(f); int read = 0; byte[] bytes = new byte[1024]; //the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached // while ((read = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } //TODO if the folder structure is invalid, let user know that nothing was uploaded //BootstrapValidator validator = new BootstrapValidator(); ConnectionManager manager = new ConnectionManager(); manager.deleteAllTables(); BootstrapUnzipper unzipper = new BootstrapUnzipper(); unzipper.unzip(context); BootstrapCSVReader csvReader = new BootstrapCSVReader(); ArrayList<DataError> allCourses = csvReader.retrieveCourses(context); ArrayList<DataError> allSections = csvReader.retrieveSections(context); ArrayList<DataError> allStudents = csvReader.retrieveStudents(context); ArrayList<DataError> allPrerequisites = csvReader.retrievePrerequisites(context); ArrayList<DataError> allCompletedCourses = csvReader.retrieveCompletedCourses(context); ArrayList<DataError> allBids = csvReader.retrieveBids(context); ArrayList<ArrayList<DataError>> listOfErrors = new ArrayList<>(); listOfErrors.add(allBids); listOfErrors.add(allCourses); listOfErrors.add(allCompletedCourses); listOfErrors.add(allPrerequisites); listOfErrors.add(allSections); listOfErrors.add(allStudents); JsonObject object = new JsonObject(); if (!allCourses.isEmpty() || !allSections.isEmpty() || !allStudents.isEmpty() || !allBids.isEmpty() || !allPrerequisites.isEmpty() || !allCompletedCourses.isEmpty()) { object.add("status", new JsonPrimitive("error")); } else { object.add("status", new JsonPrimitive("success")); } int[] totalLines = csvReader.getTotalLines(); int[] totalErrorLines = csvReader.getTotalErrorLines(); String[] allHeaders = new String[] { "bid.csv", "course.csv", "course_completed.csv", "prerequisite.csv", "section.csv", "student.csv" }; JsonObject item = new JsonObject(); JsonArray itemArr = new JsonArray(); item.add("bid.csv", new JsonPrimitive(totalLines[3] - totalErrorLines[3])); itemArr.add(item); item = new JsonObject(); item.add("course.csv", new JsonPrimitive(totalLines[0] - totalErrorLines[0])); itemArr.add(item); item = new JsonObject(); item.add("course_completed.csv", new JsonPrimitive(totalLines[5] - totalErrorLines[5])); itemArr.add(item); item = new JsonObject(); item.add("prerequisite.csv", new JsonPrimitive(totalLines[4] - totalErrorLines[4])); itemArr.add(item); item = new JsonObject(); item.add("section.csv", new JsonPrimitive(totalLines[1] - totalErrorLines[1])); itemArr.add(item); item = new JsonObject(); item.add("student.csv", new JsonPrimitive(totalLines[2] - totalErrorLines[2])); itemArr.add(item); object.add("num-record-loaded", itemArr); LinkedHashMap<String, ArrayList<DataError>> allErrorMsgs = new LinkedHashMap<>(); ArrayList<JsonObject> allErrorsArrList = new ArrayList<>(); JsonArray allErrors = new JsonArray(); //System.out.println(object.get("status").getAsString().equals("error")); if (object.get("status").getAsString().equals("error")) { for (int i = 0; i < allHeaders.length; i++) { ArrayList<DataError> list = listOfErrors.get(i); for (int j = 0; j < list.size(); j++) { DataError de = list.get(j); if (!allErrorMsgs.containsKey(de.getLineNum() + "")) { ArrayList<DataError> l = new ArrayList<>(); l.add(de); allErrorMsgs.put(de.getLineNum() + "", l); } else { allErrorMsgs.get(de.getLineNum() + "").add(de); } } Iterator<Entry<String, ArrayList<DataError>>> iter = allErrorMsgs.entrySet().iterator(); while (iter.hasNext()) { Entry<String, ArrayList<DataError>> m = iter.next(); ArrayList<DataError> errorList = m.getValue(); JsonObject error = new JsonObject(); error.add("file", new JsonPrimitive(allHeaders[i])); JsonArray errMsgs = new JsonArray(); for (DataError err : errorList) { error.add("line", new JsonPrimitive(err.getLineNum())); errMsgs.add(new JsonPrimitive(err.getDescription())); } error.add("message", errMsgs); allErrors.add(error); } allErrorMsgs = new LinkedHashMap<>(); } } if (allErrors.size() > 0) { object.add("error", allErrors); } MinBidDAO.getInstance().drop(); MinBidDAO.getInstance().refresh(); response.setContentType("application/json"); response.getWriter().write(object.toString()); }
From source file:com.bios.controller.services.DeleteBidService.java
/** * Handles the HTTP <code>GET</code> method and deletes the bid. It retrieves the userID, courseID, section ID, and amount bidded to verify the bid. * Upon verification, the bid will be deleted from bidDAO and database. Upon successful deletion, a json object is created with the * status "success". If the bid deletion is unsuccessful, a json object with the status "error" is created together with a * json array of the errors that caused the unsuccessful deletion. * * @param request servlet request/*from w w w. j av a 2s . c om*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ConnectionManager manager = new ConnectionManager(); String requestObject = request.getParameter("r"); String token = request.getParameter("token"); ArrayList<String> errorList = new ArrayList<String>(); JsonObject responseObject = new JsonObject(); JsonArray allErrors = new JsonArray(); if (requestObject == null) { errorList.add("missing r"); } else if (requestObject.length() == 0) { errorList.add("blank r"); } if (token == null) { errorList.add("missing token"); } else if (token.length() == 0) { errorList.add("blank token"); } else { try { String result = JWTUtility.verify(token, "abcdefgh12345678"); if (!result.equals("admin")) { errorList.add("invalid token"); } } catch (JWTException e) { // do we need this? // This means not an admin, or token expired errorList.add("invalid username/token"); } } JsonElement jelementRequest = new JsonParser().parse(requestObject); JsonObject json = jelementRequest.getAsJsonObject(); JsonElement crse = json.get("course"); JsonElement sec = json.get("section"); JsonElement user = json.get("userid"); if (crse == null) { errorList.add("missing course"); } else if (crse.getAsString().length() == 0) { errorList.add("blank course"); } if (sec == null) { errorList.add("missing section"); } else if (sec.getAsString().length() == 0) { errorList.add("blank section"); } if (user == null) { errorList.add("missing userid"); } else if (user.getAsString().length() == 0) { errorList.add("blank userid"); } if (errorList.size() > 0) { errorList.sort(new ErrorMsgComparator()); for (String s : errorList) { allErrors.add(new JsonPrimitive(s)); } responseObject.addProperty("status", "error"); responseObject.add("message", allErrors); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } String courseID = crse.getAsString(); String sectionID = sec.getAsString(); String userID = user.getAsString(); BidDAO bidDAO = BidDAO.getInstance(); CourseDAO courseDAO = CourseDAO.getInstance(); SectionDAO sectionDAO = SectionDAO.getInstance(); StudentDAO studentDAO = StudentDAO.getInstance(); String roundStatus = manager.getBiddingRoundStatus(); Bid bid = bidDAO.getBid(userID, courseID, sectionID); if (roundStatus.equals("stopped") || !bidDAO.deleteBid(userID, courseID, sectionID)) { responseObject.addProperty("status", "error"); if (courseDAO.findCourse(courseID) == null) { allErrors.add(new JsonPrimitive("invalid course")); } if (sectionDAO.findSection(courseID, sectionID) == null) { allErrors.add(new JsonPrimitive("invalid section")); } if (studentDAO.findStudent(userID) == null) { allErrors.add(new JsonPrimitive("invalid userid")); } if (roundStatus.equals("stopped")) { allErrors.add(new JsonPrimitive("round ended")); } if (roundStatus.equals("started") && allErrors.size() == 0) { allErrors.add(new JsonPrimitive("no such bid")); } responseObject.add("message", allErrors); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } else { // no errors detected in the deletion of bids MinBidDAO.getInstance().getMinBidList().remove(courseID + "-" + sectionID); MinBidDAO.getInstance().refresh(); responseObject.addProperty("status", "success"); Student stud = studentDAO.retrieve(userID); stud.seteDollar(stud.geteDollar() + bid.getAmount()); manager.refundEDollar(userID, bid.getAmount()); bidDAO.deleteBidFromDB(userID, courseID, sectionID); } response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); }
From source file:com.bios.controller.services.DropSectionService.java
/** * Handles the HTTP <code>GET</code> method by allowing the administrator to drop a user's * enrollment in a section. This web service requires a valid token userid, course and section, * and a bid of the student, that was successful. The bid can only be dropped if the round 2 is active. * If succesful, a json object with the status "success" will be created. Otherwise, this method creates a * json object with the status "error" and with a json array of the errors that caused the unsuccessful * dropping of the section./*from w ww. jav a2 s . co m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ConnectionManager manager = new ConnectionManager(); String requestObject = request.getParameter("r"); String token = request.getParameter("token"); ArrayList<String> errorList = new ArrayList<String>(); JsonObject responseObject = new JsonObject(); JsonArray allErrors = new JsonArray(); if (requestObject == null) { errorList.add("missing r"); } else if (requestObject.length() == 0) { errorList.add("blank r"); } if (token == null) { errorList.add("missing token"); } else if (token.length() == 0) { errorList.add("blank token"); } else { try { String result = JWTUtility.verify(token, "abcdefgh12345678"); if (!result.equals("admin")) { errorList.add("invalid token"); } } catch (JWTException e) { // do we need this? // This means not an admin, or token expired errorList.add("invalid username/token"); } } JsonElement jelementRequest = new JsonParser().parse(requestObject); JsonObject json = jelementRequest.getAsJsonObject(); JsonElement crse = json.get("course"); JsonElement sec = json.get("section"); JsonElement user = json.get("userid"); if (crse == null) { errorList.add("missing course"); } else if (crse.getAsString().length() == 0) { errorList.add("blank course"); } if (sec == null) { errorList.add("missing section"); } else if (sec.getAsString().length() == 0) { errorList.add("blank section"); } if (user == null) { errorList.add("missing userid"); } else if (user.getAsString().length() == 0) { errorList.add("blank userid"); } if (errorList.size() > 0) { errorList.sort(new ErrorMsgComparator()); for (String s : errorList) { allErrors.add(new JsonPrimitive(s)); } responseObject.addProperty("status", "error"); responseObject.add("message", allErrors); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } Student stud = StudentDAO.getInstance().retrieve(user.getAsString()); Course c = CourseDAO.getInstance().findCourse(crse.getAsString()); SectionStudent toBeDropped = SectionStudentDAO.getInstance().findSectionStudent(crse.getAsString(), sec.getAsString(), user.getAsString()); Section s = SectionDAO.getInstance().findSection(crse.getAsString(), sec.getAsString()); if (c == null) { allErrors.add(new JsonPrimitive("invalid course")); } else if (s == null) { allErrors.add(new JsonPrimitive("invalid section")); } if (stud == null) { allErrors.add(new JsonPrimitive("invalid userid")); } if (manager.getBiddingRound() == 2 && manager.getBiddingRoundStatus().equals("started")) { if (user != null) { if (toBeDropped != null) { double currAmount = stud.geteDollar(); double amountFromDropping = toBeDropped.getAmount(); double finalAmount = currAmount + amountFromDropping; manager.setEDollar(user.getAsString(), finalAmount); String userid = user.getAsString(); String courseID = crse.getAsString(); String sectionID = sec.getAsString(); SectionStudentDAO.getInstance().deleteStudentSection(userid, amountFromDropping, courseID, sectionID); SectionStudentDAO.getInstance().deleteStudentSectionFromDB(userid, amountFromDropping, courseID, sectionID); BidDAO.getInstance().deleteBid(userid, courseID, sectionID); BidDAO.getInstance().deleteBidFromDB(userid, courseID, sectionID); responseObject.addProperty("status", "success"); } else { JsonPrimitive value = new JsonPrimitive("no such enrollment record"); allErrors.add(value); responseObject.addProperty("status", "error"); responseObject.add("message", allErrors); } } } else { //round not active JsonPrimitive value = new JsonPrimitive("round not active"); allErrors.add(value); responseObject.addProperty("status", "error"); responseObject.add("message", allErrors); } response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); }
From source file:com.bios.controller.services.DumpBidService.java
/** * Handles the HTTP <code>GET</code> method which utilises a courseid and sectionid and a token of an administrator. After the courseid and * secitonid and token of the administrator is passed in, the method first verifies that the token is valid. If the token is valid, the * method checks the respective DAOs and retrieves the respective information of the bids placed and places it in the json object. If an * error occurs, such as a missing input, a json object is created with the status "error" with the error message respective to the error that * caused the unsuccessful dump./*from w w w . jav a 2 s. c o m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ConnectionManager manager = new ConnectionManager(); String requestObject = request.getParameter("r"); String token = request.getParameter("token"); ArrayList<String> errorList = new ArrayList<String>(); JsonObject responseObject = new JsonObject(); JsonArray allErrors = new JsonArray(); if (requestObject == null) { errorList.add("missing r"); } else if (requestObject.length() == 0) { errorList.add("blank r"); } if (token == null) { errorList.add("missing token"); } else if (token.length() == 0) { errorList.add("blank token"); } else { try { String result = JWTUtility.verify(token, "abcdefgh12345678"); if (!result.equals("admin")) { errorList.add("invalid token"); } } catch (JWTException e) { // do we need this? // This means not an admin, or token expired errorList.add("invalid username/token"); } } JsonArray allBids = new JsonArray(); JsonElement jelementRequest = new JsonParser().parse(requestObject); JsonObject json = jelementRequest.getAsJsonObject(); JsonElement course = json.get("course"); JsonElement section = json.get("section"); if (course == null) { errorList.add("missing course"); } else if (course.getAsString().length() == 0) { errorList.add("blank course"); } if (section == null) { errorList.add("missing section"); } else if (section.getAsString().length() == 0) { errorList.add("blank section"); } if (errorList.size() > 0) { errorList.sort(new ErrorMsgComparator()); for (String s : errorList) { allErrors.add(new JsonPrimitive(s)); } responseObject.addProperty("status", "error"); responseObject.add("message", allErrors); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } ArrayList<Section> sectionList = SectionDAO.getInstance().getSectionList(); Section s = SectionDAO.getInstance().findSection(course.getAsString(), section.getAsString()); Course c = CourseDAO.getInstance().findCourse(course.getAsString()); if (c == null) { JsonPrimitive value = new JsonPrimitive("invalid course"); allErrors.add(value); responseObject.addProperty("status", "error"); responseObject.add("message", allErrors); } else if (c != null && s == null) { JsonPrimitive value = new JsonPrimitive("invalid course"); allErrors.add(value); responseObject.addProperty("status", "error"); responseObject.add("message", allErrors); } int round = manager.getBiddingRound(); String status = manager.getBiddingRoundStatus(); // // //round active: status is '-' // if (c != null && s != null && status.equals("started")) { // responseObject.addProperty("status", "success"); // BidDAO bidDAO = BidDAO.getInstance(); // ArrayList<Bid> successBids = bidDAO.getSuccessfulBidsWithID(c.getCourseID(), s.getSectionID()); // Collections.sort(successBids, new BidComparator()); // //arrange the names in ascending order - undone // if (successBids.size() == 0) { // System.out.println("PENDING:" + successBids.size()); // } // for (int i = 0; i < successBids.size(); i++) { // Bid currBid = successBids.get(i); // System.out.println("Bid: " + currBid.getStatus()); // JsonObject obj = new JsonObject(); // obj.add("row", new JsonPrimitive((i + 1))); // obj.add("userid", new JsonPrimitive(currBid.getUserID())); // obj.add("amount", new JsonPrimitive(currBid.getAmount())); // obj.add("result", new JsonPrimitive("-")); // // allActiveBids.add(obj); // } // // responseObject.add("bids", allActiveBids); // // } else if (c != null && s != null && status.equals("stopped")) {//notactive: status is successful/unsuccessful; bids should be in an array // responseObject.addProperty("status", "success"); // ArrayList<Bid> allBids = BidDAO.getInstance().getBids(c.getCourseID(), s.getSectionID()); // allBids.sort(new BidComparator()); // // for (int i = 0; i < allBids.size(); i++) { // Bid bid = allBids.get(i); // // JsonObject obj = new JsonObject(); // // obj.add("row", new JsonPrimitive((i + 1))); // obj.add("userid", new JsonPrimitive(bid.getUserID())); // obj.add("amount", new JsonPrimitive(bid.getAmount())); // if (bid.getStatus().equals("success")) { // obj.add("result", new JsonPrimitive("in")); // } else { // obj.add("result", new JsonPrimitive("out")); // } // allInactiveBids.add(obj); // } // // responseObject.add("bids", allInactiveBids); // } ArrayList<Bid> bidList = null; if (round == 1 && status.equals("started")) { bidList = BidDAO.getInstance().getBidList(); } else if (status.equals("stopped") && round == 1) { bidList = RoundOneBidDAO.getInstance().getBidList(); } else if (round == 2 && status.equals("started")) { bidList = BidDAO.getInstance().getPendingBids(); } else { bidList = RoundTwoBidDAO.getInstance().getBidList(); } bidList.sort(new BidComparator()); System.out.println("BIDLIST: " + bidList.size()); int count = 1; for (int i = 0; i < bidList.size(); i++) { Bid bid = bidList.get(i); if (bid.getCourseID().equals(course.getAsString()) && bid.getSectionID().equals(section.getAsString())) { JsonObject obj = new JsonObject(); obj.add("row", new JsonPrimitive(count)); obj.add("userid", new JsonPrimitive(bid.getUserID())); obj.add("amount", new JsonPrimitive(bid.getAmount())); if (bid.getStatus().equals("pending")) { obj.add("result", new JsonPrimitive("-")); } else if (bid.getStatus().equals("success")) { obj.add("result", new JsonPrimitive("in")); } else { obj.add("result", new JsonPrimitive("out")); } allBids.add(obj); count++; } } responseObject.addProperty("status", "success"); responseObject.add("bids", allBids); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); }
From source file:com.bios.controller.services.DumpSectionService.java
/** * Handles the HTTP <code>GET</code> method which uses the courseid, a sectionid and token of an administrator. After the * courseid, sectionid and token of the administrator is obtained, the method first verifies that the token is valid and present in the session * object. Subsequently, if the token is valid, the method checks the respective DAOs and retrieves the respective information of the bids of * that particular courses section and places it in a json object. If an error occurs, such as a missing input, a json object will be created * with the status "error" and will reflect the errors in a json array within the object. * @param request servlet request/* w w w . jav a2s . c om*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ConnectionManager manager = new ConnectionManager(); String requestObject = request.getParameter("r"); String token = request.getParameter("token"); ArrayList<String> errorList = new ArrayList<String>(); JsonObject responseObject = new JsonObject(); JsonArray allErrors = new JsonArray(); if (requestObject == null) { errorList.add("missing r"); } else if (requestObject.length() == 0) { errorList.add("blank r"); } if (token == null) { errorList.add("missing token"); } else if (token.length() == 0) { errorList.add("blank token"); } else { try { String result = JWTUtility.verify(token, "abcdefgh12345678"); if (!result.equals("admin")) { errorList.add("invalid token"); } } catch (JWTException e) { // do we need this? // This means not an admin, or token expired errorList.add("invalid username/token"); } } JsonElement jelementRequest = new JsonParser().parse(requestObject); JsonObject json = jelementRequest.getAsJsonObject(); JsonElement crse = json.get("course"); JsonElement sec = json.get("section"); if (crse == null) { errorList.add("missing course"); } else if (crse.getAsString().length() == 0) { errorList.add("blank course"); } if (sec == null) { errorList.add("missing section"); } else if (sec.getAsString().length() == 0) { errorList.add("blank section"); } if (errorList.size() > 0) { errorList.sort(new ErrorMsgComparator()); for (String s : errorList) { allErrors.add(new JsonPrimitive(s)); } responseObject.addProperty("status", "error"); responseObject.add("message", allErrors); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } String courseID = crse.getAsString(); String sectionID = sec.getAsString(); int round = manager.getBiddingRound(); String status = manager.getBiddingRoundStatus(); // Will now check to see if there are any errors here Course course = CourseDAO.getInstance().findCourse(crse.getAsString()); Section section = null; if (course == null) { allErrors.add(new JsonPrimitive("invalid course")); } else { section = SectionDAO.getInstance().findSection(course.getCourseID(), sec.getAsString()); if (section == null) { allErrors.add(new JsonPrimitive("invalid section")); } } if (allErrors.size() != 0) { responseObject.add("status", new JsonPrimitive("error")); responseObject.add("message", allErrors); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } JsonArray allResults = new JsonArray(); ArrayList<SectionStudent> allSecStudList = SectionStudentDAO.getInstance() .getSectionStudentListWithID(crse.getAsString(), sec.getAsString()); ArrayList<SectionStudent> secStudList = new ArrayList<>(); for (SectionStudent secStud : allSecStudList) { if (secStud.getCourseID().equals(courseID) && secStud.getSectionID().equals(sectionID)) { secStudList.add(secStud); } // String cID = secStud.getCourseID(); // String sID = secStud.getSectionID(); // // ArrayList<Bid> halfBids = new ArrayList<>(); // if (round == 1 || round == 2 && status.equals("started")) { // halfBids = BidDAO.getInstance().getSuccessfulBidsWithID(cID, sID); // } else if (round == 2 && status.equals("stopped")) { // halfBids = BidDAO.getInstance().getBids(sID, sID); // } // allBids.addAll(halfBids); } secStudList.sort(new SectionStudentComparator()); for (SectionStudent secStud : secStudList) { JsonObject obj = new JsonObject(); obj.addProperty("userid", secStud.getUserID()); obj.addProperty("amount", secStud.getAmount()); allResults.add(obj); } // // After we sort all the bids, then we shall add them to be responded to // allBids.sort(new BidUserComparator()); // for (Bid bid : allBids) { // JsonObject obj = new JsonObject(); // obj.add("userid", new JsonPrimitive(bid.getUserID())); // obj.add("amount", new JsonPrimitive(bid.getAmount())); // allResults.add(obj); // } responseObject.add("status", new JsonPrimitive("success")); responseObject.add("students", allResults); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); }
From source file:com.bios.controller.services.DumpTableService.java
/** * Handles the HTTP <code>GET</code> method that makes use of a token. If a token is valid and present in the session object, the method goes * on to create a json object. In the json object, the respective course, section, student, prerequisite, completed-course, bid and section-student * objects will be placed in the tables. If any errors occur, such as an invalid or blank token, a json object with the status "error" will be created a * with any error messages within a json array in the json object. * * @param request servlet request// w w w . j av a 2 s .c o m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ConnectionManager manager = new ConnectionManager(); String token = request.getParameter("token"); ArrayList<String> errorList = new ArrayList<String>(); JsonObject responseObject = new JsonObject(); JsonArray allErrors = new JsonArray(); if (token == null) { errorList.add("missing token"); } else if (token.length() == 0) { errorList.add("blank token"); } else { try { String result = JWTUtility.verify(token, "abcdefgh12345678"); if (!result.equals("admin")) { errorList.add("invalid token"); } } catch (JWTException e) { // do we need this? // This means not an admin, or token expired errorList.add("invalid username/token"); } } JsonObject object = new JsonObject(); object.add("status", new JsonPrimitive("success")); ArrayList<Course> courseList = CourseDAO.getInstance().getCourseList(); ArrayList<Section> sectionList = SectionDAO.getInstance().getSectionList(); ArrayList<Student> studentList = StudentDAO.getInstance().getStudentList(); ArrayList<Prerequisite> prerequisiteList = PrerequisiteDAO.getInstance().getPrerequisiteList(); ArrayList<Bid> bidList = BidDAO.getInstance().getBidList(); ArrayList<CompletedCourse> completeList = CompletedCourseDAO.getInstance().getCompletedCourseList(); ArrayList<SectionStudent> sectionStudentList = SectionStudentDAO.getInstance().getSectionStudentList(); courseList.sort(new CourseComparator()); sectionList.sort(new SectionComparator()); studentList.sort(new StudentComparator()); prerequisiteList.sort(new PrerequisiteComparator()); bidList.sort(new BidJSONComparator()); completeList.sort(new CourseCompletedComparator()); sectionStudentList.sort(new SectionStudentComparator()); JsonArray courseArray = new JsonArray(); for (Course course : courseList) { JsonObject courseObj = new JsonObject(); courseObj.add("course", new JsonPrimitive(course.getCourseID())); courseObj.add("school", new JsonPrimitive(course.getSchool())); courseObj.add("title", new JsonPrimitive(course.getTitle())); courseObj.add("description", new JsonPrimitive(course.getDescription())); courseObj.add("exam date", new JsonPrimitive(course.getExamDate())); courseObj.add("exam start", new JsonPrimitive(course.getExamStart().replace(":", ""))); courseObj.add("exam end", new JsonPrimitive(course.getExamEnd().replace(":", ""))); courseArray.add(courseObj); } object.add("course", courseArray); JsonArray sectionArray = new JsonArray(); for (Section section : sectionList) { JsonObject obj = new JsonObject(); obj.add("course", new JsonPrimitive(section.getCourseID())); obj.add("section", new JsonPrimitive(section.getSectionID())); obj.add("day", new JsonPrimitive(getDay(section.getDay()))); obj.add("start", new JsonPrimitive(section.getStart().replace(":", ""))); obj.add("end", new JsonPrimitive(section.getEnd().replace(":", ""))); obj.add("instructor", new JsonPrimitive(section.getInstructor())); obj.add("venue", new JsonPrimitive(section.getVenue())); obj.add("size", new JsonPrimitive(section.getSize())); sectionArray.add(obj); } object.add("section", sectionArray); JsonArray studentArray = new JsonArray(); for (Student student : studentList) { Student stud = StudentDAO.getInstance().retrieve(student.getId()); JsonObject obj = new JsonObject(); obj.add("userid", new JsonPrimitive(student.getId())); obj.add("password", new JsonPrimitive(student.getPassword())); obj.add("name", new JsonPrimitive(student.getNameOfUser())); obj.add("school", new JsonPrimitive(student.getSchool())); obj.add("edollar", new JsonPrimitive(stud.geteDollar())); studentArray.add(obj); } object.add("student", studentArray); JsonArray prerequisiteArray = new JsonArray(); for (Prerequisite pre : prerequisiteList) { JsonObject obj = new JsonObject(); obj.add("course", new JsonPrimitive(pre.getCourseID())); obj.add("prerequisite", new JsonPrimitive(pre.getPrerequisiteID())); prerequisiteArray.add(obj); } object.add("prerequisite", prerequisiteArray); JsonArray bidArray = new JsonArray(); for (Bid bid : bidList) { JsonObject obj = new JsonObject(); obj.add("userid", new JsonPrimitive(bid.getUserID())); obj.add("amount", new JsonPrimitive(bid.getAmount())); obj.add("course", new JsonPrimitive(bid.getCourseID())); obj.add("section", new JsonPrimitive(bid.getSectionID())); bidArray.add(obj); } object.add("bid", bidArray); JsonArray completedArray = new JsonArray(); for (CompletedCourse cc : completeList) { JsonObject obj = new JsonObject(); obj.add("userid", new JsonPrimitive(cc.getUserID())); obj.add("course", new JsonPrimitive(cc.getCourseID())); completedArray.add(obj); } object.add("completed-course", completedArray); JsonArray sectionStudentArray = new JsonArray(); for (SectionStudent stud : sectionStudentList) { JsonObject obj = new JsonObject(); obj.add("userid", new JsonPrimitive(stud.getUserID())); obj.add("course", new JsonPrimitive(stud.getCourseID())); obj.add("section", new JsonPrimitive(stud.getSectionID())); obj.add("amount", new JsonPrimitive(stud.getAmount())); sectionStudentArray.add(obj); } object.add("section-student", sectionStudentArray); System.out.println(object); response.setContentType("application/json"); response.getWriter().write(object.toString()); }
From source file:com.bios.controller.services.DumpUserService.java
/** * Handles the HTTP <code>GET</code> method which takes in the userid of a * user and a token of an administrator. After the userid of the student and * token of the administrator is passed in, the method first verifies that * the token is valid. If the token is invalid, a json object is created * with the status "error" and an error message "invalid username/token". * Subsequently, if the token is valid, the method checks the respective * DAOs and retrieves the respective information of the user and places it * in the json object. If an error occurs, such as a missing input, the json * object will also reflect the error without displaying the user's * information./* w w w. j a v a 2 s.c o m*/ * @param request servlet requesty * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ConnectionManager manager = new ConnectionManager(); String requestObject = request.getParameter("r"); String token = request.getParameter("token"); ArrayList<String> errorList = new ArrayList<String>(); JsonObject responseObject = new JsonObject(); JsonArray allErrors = new JsonArray(); if (requestObject == null) { errorList.add("missing r"); } else if (requestObject.length() == 0) { errorList.add("blank r"); } if (token == null) { errorList.add("missing token"); } else if (token.length() == 0) { errorList.add("blank token"); } else { try { String result = JWTUtility.verify(token, "abcdefgh12345678"); if (!result.equals("admin")) { errorList.add("invalid token"); } } catch (JWTException e) { // do we need this? // This means not an admin, or token expired errorList.add("invalid username/token"); } } JsonElement jelementRequest = new JsonParser().parse(requestObject); JsonObject json = jelementRequest.getAsJsonObject(); JsonElement userID = json.get("userid"); if (userID == null) { errorList.add("missing userid"); } else if (userID.getAsString().length() == 0) { errorList.add("blank userid"); } if (errorList.size() > 0) { errorList.sort(new ErrorMsgComparator()); for (String s : errorList) { allErrors.add(new JsonPrimitive(s)); } responseObject.addProperty("status", "error"); responseObject.add("message", allErrors); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } Student s = StudentDAO.getInstance().retrieve(userID.getAsString()); if (s == null) { allErrors.add(new JsonPrimitive("invalid userid")); responseObject.addProperty("status", "error"); responseObject.add("message", allErrors); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } else { responseObject.addProperty("status", "success"); responseObject.addProperty("userid", s.getId()); responseObject.addProperty("password", s.getPassword()); responseObject.addProperty("name", s.getNameOfUser()); responseObject.addProperty("school", s.getSchool()); DecimalFormat df = new DecimalFormat("0.00"); String result = df.format(s.geteDollar()); double eDollar = Double.parseDouble(result); responseObject.addProperty("edollar", eDollar); } response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; }
From source file:com.bios.controller.services.StartRoundService.java
/** * Handles the HTTP <code>GET</code> method which starts a bidding round. It checks that the user is a verified admin * and proceeds to start the round if the round is not yet started. Subsequently, it creates a json object with the status * "success" and stores the current bidding round. However, if round 2 has already ended, it creates a json object with the * status "error" and stores the error message "round 2 ended" in a json array. Additionally, if the user is not a verified admin with a valid * token, it creates a json object with the status "error" and stores the error message in a json array "invalid username/token". * * @param request servlet request//from w w w . j a va 2 s. co m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // String token = request.getParameter("token"); JsonObject object = new JsonObject(); if (request.getParameter("token") == null) { JsonArray array = new JsonArray(); object.addProperty("status", "error"); array.add(new JsonPrimitive("missing token")); object.add("message", array); } else if (request.getParameter("token").length() == 0) { JsonArray array = new JsonArray(); object.addProperty("status", "error"); array.add(new JsonPrimitive("blank token")); object.add("message", array); } else { String token = request.getParameter("token"); try { String result = JWTUtility.verify(token, "abcdefgh12345678"); if (result.equals("admin")) { JsonArray array = new JsonArray(); ConnectionManager manager = new ConnectionManager(); if (manager.getBiddingRoundStatus().equals("started")) { // Stop the round here object.addProperty("status", "success"); object.add("round", new JsonPrimitive(manager.getBiddingRound())); } else if (manager.getBiddingRound() == 1) { BiddingRoundServlet.incrementBiddingRound("true"); object.addProperty("status", "success"); object.add("round", new JsonPrimitive(manager.getBiddingRound())); } else { object.addProperty("status", "error"); array.add(new JsonPrimitive("round 2 ended")); object.add("message", array); } } } catch (JWTException e) { // This means not an admin, or token expired JsonArray array = new JsonArray(); JsonPrimitive value = new JsonPrimitive("invalid token"); array.add(value); object.addProperty("status", "error"); object.add("message", array); } } System.out.println(object); response.setContentType( "application/json"); response.getWriter() .write(object.toString()); }
From source file:com.bios.controller.services.StopRoundService.java
/** * Handles the HTTP <code>GET</code> method which stops a bidding round. It checks that the user is a verified admin * and proceeds to stop the round if the round is started. Subsequently, it creates a json object with the status * "success". However, if a round is already stopped, it creates a json object with the status "error" and stores the error * message "round already ended" in a json array. Additionally, if the user is not a verified admin with a valid token, it * creates a json object with the status "error" and stores the error message in a json array "invalid username/token". * * @param request servlet request// ww w. j a va2s .c om * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { JsonObject object = new JsonObject(); if (request.getParameter("token") == null) { JsonArray array = new JsonArray(); object.addProperty("status", "error"); array.add(new JsonPrimitive("missing token")); object.add("message", array); } else if (request.getParameter("token").length() == 0) { JsonArray array = new JsonArray(); object.addProperty("status", "error"); array.add(new JsonPrimitive("blank token")); object.add("message", array); } else { String token = request.getParameter("token"); try { String result = JWTUtility.verify(token, "abcdefgh12345678"); if (result.equals("admin")) { JsonArray array = new JsonArray(); ConnectionManager manager = new ConnectionManager(); if (manager.getBiddingRoundStatus().equals("started") && manager.getBiddingRound() == 1) { // Stop the round here object.addProperty("status", "success"); // RoundOneBidDAO.getInstance().drop(); // // add all round 1 pending bids into roundonebiddao // ArrayList<Bid> allBids = BidDAO.getInstance().getPendingBids(); // for (Bid bid : allBids){ // RoundOneBidDAO.getInstance().addBid(bid); // } BiddingRoundServlet.incrementBiddingRound("true"); } else if (manager.getBiddingRoundStatus().equals("started") && manager.getBiddingRound() == 2) { // Stop the round here object.addProperty("status", "success"); // RoundTwoBidDAO.getInstance().drop(); // // add all round 1 pending bids into roundonebiddao // ArrayList<Bid> allBids = BidDAO.getInstance().getPendingBids(); // for (Bid bid : allBids){ // RoundTwoBidDAO.getInstance().addBid(bid); // } BiddingRoundServlet.incrementBiddingRound("true"); } else { object.addProperty("status", "error"); array.add(new JsonPrimitive("round already ended")); object.add("message", array); } } } catch (JWTException e) { // This means not an admin, or token expired JsonArray array = new JsonArray(); JsonPrimitive value = new JsonPrimitive("invalid token"); array.add(value); object.addProperty("status", "error"); object.add("message", array); } } System.out.println(object); response.setContentType("application/json"); response.getWriter().write(object.toString()); return; }
From source file:com.bios.controller.services.StudentCalendarBidService.java
/** * Handles the HTTP <code>GET</code> method uses the userID of a student to * generates a json object with a timetable of that particular student that * consists of both the student's exams and classes. * * @param request servlet request//from w w w.j a v a2 s. c o m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String userID = request.getParameter("userID"); System.out.println(userID); ArrayList<Bid> allBids = BidDAO.getInstance().getStudentBids(userID); JsonArray arr = new JsonArray(); System.out.println(allBids.size()); for (Bid bid : allBids) { if (bid.getStatus().equals("success") || bid.getStatus().equals("pending")) { JsonObject obj = new JsonObject(); Course course = CourseDAO.getInstance().findCourse(bid.getCourseID()); Section section = SectionDAO.getInstance().findSection(bid.getCourseID(), bid.getSectionID()); DateFormat sourceFormat = new SimpleDateFormat("yyyyMMdd"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); // Adding a Course exam object obj.add("title", new JsonPrimitive(course.getTitle())); try { Date source = sourceFormat.parse(course.getExamDate()); String target = df.format(source); System.out.println(target); System.out.println(sourceFormat.parse(course.getExamDate()).toString()); obj.add("start", new JsonPrimitive(target)); obj.add("end", new JsonPrimitive(target)); obj.add("className", new JsonPrimitive("bg-blue")); } catch (Exception e) { e.printStackTrace(); } obj.add("description", new JsonPrimitive(course.getDescription())); arr.add(obj); // Adding a Section class timetable object obj = new JsonObject(); obj.add("title", new JsonPrimitive(course.getTitle())); obj.add("start", new JsonPrimitive(section.getStart())); System.out.println(section.getStart()); obj.add("end", new JsonPrimitive(section.getEnd())); obj.add("className", new JsonPrimitive("bg-green")); obj.add("description", new JsonPrimitive(section.getCourseID())); JsonArray dow = new JsonArray(); dow.add(new JsonPrimitive(section.getDay())); obj.add("dow", dow); arr.add(obj); } } response.setContentType("application/json"); response.getWriter().write(arr.toString()); }