Search in sources :

Example 1 with CPRException

use of edu.oswego.cs.util.CPRException in project CSC480-22S by tenbergen.

the class CourseInterface method addCourse.

public void addCourse(SecurityContext securityContext, CourseDAO dao) {
    String professorID = securityContext.getUserPrincipal().getName().split("@")[0];
    dao.professorID = professorID;
    Document courseDocument = courseCollection.find(and(eq("course_id", dao.courseID), eq("professor_id", professorID))).first();
    if (courseDocument != null)
        throw new CPRException(Response.Status.CONFLICT, "Course already existed.");
    Document professorDocument = professorCollection.find(eq("professor_id", professorID)).first();
    if (professorDocument == null)
        throw new CPRException(Response.Status.NOT_FOUND, "This professor does not exist.");
    List<String> professorDocumentCourses = professorDocument.getList("courses", String.class);
    if (professorDocumentCourses == null)
        throw new CPRException(Response.Status.CONFLICT, "Professor profile is not set up properly.");
    professorDocumentCourses.add(dao.courseID);
    professorCollection.updateOne(eq("professor_id", professorID), Updates.set("courses", professorDocumentCourses));
    Jsonb jsonb = JsonbBuilder.create();
    Entity<String> courseDAOEntity = Entity.entity(jsonb.toJson(dao), MediaType.APPLICATION_JSON_TYPE);
    Document course = Document.parse(courseDAOEntity.getEntity());
    courseCollection.insertOne(course);
    List<String> students = course.getList("students", String.class);
    for (String student : students) {
        Document studentDocument = studentCollection.find(eq("student_id", student)).first();
        if (studentDocument != null)
            studentCollection.updateOne(eq("student_id", student), push("courses", dao.courseID));
    }
}
Also used : Jsonb(javax.json.bind.Jsonb) CPRException(edu.oswego.cs.util.CPRException) Document(org.bson.Document)

Example 2 with CPRException

use of edu.oswego.cs.util.CPRException in project CSC480-22S by tenbergen.

the class CourseInterface method addStudentsFromCSV.

public void addStudentsFromCSV(SecurityContext securityContext, FileDAO fileDAO) {
    String professorID = securityContext.getUserPrincipal().getName().split("@")[0];
    List<StudentDAO> allStudents = parseStudentCSV(fileDAO.getCsvLines());
    String cid = fileDAO.getFilename();
    cid = cid.substring(0, cid.length() - 4);
    Document course = courseCollection.find(and(eq("course_id", cid), eq("professor_id", professorID))).first();
    if (course == null)
        throw new CPRException(Response.Status.BAD_REQUEST, "This course does not exist.");
    List<String> oldStudentList = course.getList("students", String.class);
    String courseID = course.getString("course_id");
    ArrayList<String> newStudentList = new ArrayList<>();
    ArrayList<String> studentsToRemove = new ArrayList<>();
    ArrayList<String> studentsToAdd = new ArrayList<>();
    for (StudentDAO studentDAO : allStudents) newStudentList.add(studentDAO.email.split("@")[0]);
    for (String student : oldStudentList) {
        if (!newStudentList.contains(student))
            studentsToRemove.add(student);
    }
    for (String student : newStudentList) {
        if (!oldStudentList.contains(student))
            studentsToAdd.add(student);
    }
    for (String student : studentsToRemove) removeStudent(securityContext, student, courseID);
    for (StudentDAO student : allStudents.stream().filter(s -> studentsToAdd.contains(s.email.split("@")[0])).collect(Collectors.toList())) {
        addStudent(securityContext, student, courseID);
    }
}
Also used : StudentDAO(edu.oswego.cs.daos.StudentDAO) Document(org.bson.Document) JsonbBuilder(javax.json.bind.JsonbBuilder) Updates.push(com.mongodb.client.model.Updates.push) MongoCollection(com.mongodb.client.MongoCollection) SecurityContext(javax.ws.rs.core.SecurityContext) Updates(com.mongodb.client.model.Updates) MongoDatabase(com.mongodb.client.MongoDatabase) Updates.set(com.mongodb.client.model.Updates.set) CSVUtil.parseStudentCSV(edu.oswego.cs.util.CSVUtil.parseStudentCSV) CourseDAO(edu.oswego.cs.daos.CourseDAO) Entity(javax.ws.rs.client.Entity) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) CourseUtil(edu.oswego.cs.util.CourseUtil) MediaType(javax.ws.rs.core.MediaType) List(java.util.List) Filters.and(com.mongodb.client.model.Filters.and) Response(javax.ws.rs.core.Response) StudentDAO(edu.oswego.cs.daos.StudentDAO) WebApplicationException(javax.ws.rs.WebApplicationException) Filters.eq(com.mongodb.client.model.Filters.eq) FileDAO(edu.oswego.cs.daos.FileDAO) Jsonb(javax.json.bind.Jsonb) CPRException(edu.oswego.cs.util.CPRException) CPRException(edu.oswego.cs.util.CPRException) ArrayList(java.util.ArrayList) Document(org.bson.Document)

Example 3 with CPRException

use of edu.oswego.cs.util.CPRException in project CSC480-22S by tenbergen.

the class CourseInterface method removeStudent.

public void removeStudent(SecurityContext securityContext, String studentID, String courseID) {
    String professorID = securityContext.getUserPrincipal().getName().split("@")[0];
    Document studentDocument = studentCollection.find(and(eq("student_id", studentID), eq("courses", courseID))).first();
    if (studentDocument == null)
        throw new CPRException(Response.Status.NOT_FOUND, "This student does not exist.");
    Document courseDocument = courseCollection.find(and(eq("course_id", courseID), eq("professor_id", professorID))).first();
    if (courseDocument == null)
        throw new CPRException(Response.Status.NOT_FOUND, "This course does not exist.");
    List<String> courses = studentDocument.getList("courses", String.class);
    courses.remove(courseID);
    studentCollection.updateOne(eq("student_id", studentID), set("courses", courses));
    List<String> students = courseDocument.getList("students", String.class);
    students.remove(studentID);
    courseCollection.updateOne(eq("course_id", courseID), set("students", students));
}
Also used : CPRException(edu.oswego.cs.util.CPRException) Document(org.bson.Document)

Example 4 with CPRException

use of edu.oswego.cs.util.CPRException in project CSC480-22S by tenbergen.

the class CourseInterface method updateCourse.

public void updateCourse(SecurityContext securityContext, CourseDAO dao) {
    String professorID = securityContext.getUserPrincipal().getName().split("@")[0];
    Document courseDocument = courseCollection.find(and(eq("course_id", dao.getCourseID()), eq("professor_id", professorID))).first();
    if (courseDocument == null)
        throw new CPRException(Response.Status.NOT_FOUND, "This course does not exist.");
    String originalCourseID = dao.courseID;
    String newCourseID = dao.abbreviation + "-" + dao.courseSection + "-" + dao.crn + "-" + dao.semester + "-" + dao.year;
    int originalTeamSize = courseDocument.getInteger("team_size");
    int newTeamSize = dao.teamSize;
    dao.courseID = newCourseID;
    dao.students = courseDocument.getList("students", String.class);
    dao.professorID = professorID;
    if (!originalCourseID.equals(newCourseID)) {
        Document duplicatedCourseDocument = courseCollection.find(and(eq("course_id", newCourseID), eq("professor_id", professorID))).first();
        if (duplicatedCourseDocument != null)
            throw new CPRException(Response.Status.CONFLICT, "This course_id already exist.");
    }
    if (originalTeamSize != newTeamSize) {
        new CourseUtil().updateTeamSize(teamCollection, originalCourseID, newTeamSize);
    }
    new CourseUtil().updateCoursesArrayInProfessorDb(securityContext, professorCollection, originalCourseID, newCourseID, "UPDATE");
    new CourseUtil().updateCoursesArrayInStudentDb(studentCollection, originalCourseID, newCourseID, "UPDATE");
    new CourseUtil().updateCoursesKeyInDBs(assignmentCollection, originalCourseID, newCourseID, "UPDATE");
    new CourseUtil().updateCoursesKeyInDBs(submissionCollection, originalCourseID, newCourseID, "UPDATE");
    new CourseUtil().updateCoursesKeyInDBs(teamCollection, originalCourseID, newCourseID, "UPDATE");
    Jsonb jsonb = JsonbBuilder.create();
    Entity<String> courseDAOEntity = Entity.entity(jsonb.toJson(dao), MediaType.APPLICATION_JSON_TYPE);
    Document course = Document.parse(courseDAOEntity.getEntity());
    courseCollection.replaceOne(eq("course_id", originalCourseID), course);
}
Also used : Jsonb(javax.json.bind.Jsonb) CPRException(edu.oswego.cs.util.CPRException) CourseUtil(edu.oswego.cs.util.CourseUtil) Document(org.bson.Document)

Example 5 with CPRException

use of edu.oswego.cs.util.CPRException in project CSC480-22S by tenbergen.

the class CourseInterface method removeCourse.

public void removeCourse(SecurityContext securityContext, String courseID) {
    String professorID = securityContext.getUserPrincipal().getName().split("@")[0];
    Document courseDocument = courseCollection.find(and(eq("course_id", courseID), eq("professor_id", professorID))).first();
    if (courseDocument == null)
        throw new CPRException(Response.Status.BAD_REQUEST, "This course does not exist.");
    new CourseUtil().updateCoursesArrayInProfessorDb(securityContext, professorCollection, courseID, null, "DELETE");
    new CourseUtil().updateCoursesArrayInStudentDb(studentCollection, courseID, null, "DELETE");
    new CourseUtil().updateCoursesKeyInDBs(assignmentCollection, courseID, null, "DELETE");
    new CourseUtil().updateCoursesKeyInDBs(submissionCollection, courseID, null, "DELETE");
    new CourseUtil().updateCoursesKeyInDBs(teamCollection, courseID, null, "DELETE");
    courseCollection.deleteOne(eq("course_id", courseID));
}
Also used : CPRException(edu.oswego.cs.util.CPRException) CourseUtil(edu.oswego.cs.util.CourseUtil) Document(org.bson.Document)

Aggregations

CPRException (edu.oswego.cs.util.CPRException)28 Document (org.bson.Document)26 SecurityService (edu.oswego.cs.services.SecurityService)15 Bson (org.bson.conversions.Bson)15 UpdateOptions (com.mongodb.client.model.UpdateOptions)8 IdentifyingService (edu.oswego.cs.services.IdentifyingService)7 ArrayList (java.util.ArrayList)4 Jsonb (javax.json.bind.Jsonb)4 CourseUtil (edu.oswego.cs.util.CourseUtil)3 StudentDAO (edu.oswego.cs.daos.StudentDAO)2 IAttachment (com.ibm.websphere.jaxrs20.multipart.IAttachment)1 MongoCollection (com.mongodb.client.MongoCollection)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 Filters.and (com.mongodb.client.model.Filters.and)1 Filters.eq (com.mongodb.client.model.Filters.eq)1 Updates (com.mongodb.client.model.Updates)1 Updates.push (com.mongodb.client.model.Updates.push)1 Updates.set (com.mongodb.client.model.Updates.set)1 CourseDAO (edu.oswego.cs.daos.CourseDAO)1 FileDAO (edu.oswego.cs.daos.FileDAO)1