Search in sources :

Example 1 with StudentGroupStudent

use of fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent in project pyramus by otavanopisto.

the class ViewStudentGroupViewController method process.

/**
 * Processes the page request by including the corresponding JSP page to the response.
 *
 * @param pageRequestContext Page request context
 */
public void process(PageRequestContext pageRequestContext) {
    StudentGroupDAO studentGroupDAO = DAOFactory.getInstance().getStudentGroupDAO();
    StudentGroupContactLogEntryDAO contactLogEntryDAO = DAOFactory.getInstance().getStudentGroupContactLogEntryDAO();
    StudentGroupContactLogEntryCommentDAO contactLogEntryCommentDAO = DAOFactory.getInstance().getStudentGroupContactLogEntryCommentDAO();
    // The student group to be edited
    StudentGroup studentGroup = studentGroupDAO.findById(pageRequestContext.getLong("studentgroup"));
    pageRequestContext.getRequest().setAttribute("studentGroup", studentGroup);
    List<StudentGroupStudent> studentGroupStudents = new ArrayList<>(studentGroup.getStudents());
    Collections.sort(studentGroupStudents, new Comparator<StudentGroupStudent>() {

        @Override
        public int compare(StudentGroupStudent o1, StudentGroupStudent o2) {
            int cmp = o1.getStudent().getLastName().compareToIgnoreCase(o2.getStudent().getLastName());
            if (cmp == 0)
                cmp = o1.getStudent().getFirstName().compareToIgnoreCase(o2.getStudent().getFirstName());
            return cmp;
        }
    });
    final Map<Long, List<StudentGroupContactLogEntryComment>> contactEntryComments = new HashMap<>();
    List<StudentGroupContactLogEntry> contactLogEntries = contactLogEntryDAO.listByStudentGroup(studentGroup);
    for (int j = 0; j < contactLogEntries.size(); j++) {
        StudentGroupContactLogEntry entry = contactLogEntries.get(j);
        List<StudentGroupContactLogEntryComment> listComments = contactLogEntryCommentDAO.listByEntry(entry);
        Collections.sort(listComments, new Comparator<StudentGroupContactLogEntryComment>() {

            public int compare(StudentGroupContactLogEntryComment o1, StudentGroupContactLogEntryComment o2) {
                Date d1 = o1.getCommentDate();
                Date d2 = o2.getCommentDate();
                int val = d1 == null ? d2 == null ? 0 : 1 : d2 == null ? -1 : d1.compareTo(d2);
                if (val == 0)
                    return o1.getId().compareTo(o2.getId());
                else
                    return val;
            }
        });
        contactEntryComments.put(entry.getId(), listComments);
    }
    // Now we can sort entries based on date of entry and/or dates of the comments on the entry
    Collections.sort(contactLogEntries, new Comparator<StudentGroupContactLogEntry>() {

        private Date getDateForEntry(StudentGroupContactLogEntry entry) {
            Date d = entry.getEntryDate();
            List<StudentGroupContactLogEntryComment> comments = contactEntryComments.get(entry.getId());
            for (int i = 0; i < comments.size(); i++) {
                StudentGroupContactLogEntryComment comment = comments.get(i);
                if (d == null) {
                    d = comment.getCommentDate();
                } else {
                    if (d.before(comment.getCommentDate()))
                        d = comment.getCommentDate();
                }
            }
            return d;
        }

        public int compare(StudentGroupContactLogEntry o1, StudentGroupContactLogEntry o2) {
            Date d1 = getDateForEntry(o1);
            Date d2 = getDateForEntry(o2);
            int val = d1 == null ? d2 == null ? 0 : 1 : d2 == null ? -1 : d2.compareTo(d1);
            if (val == 0)
                return o2.getId().compareTo(o1.getId());
            else
                return val;
        }
    });
    pageRequestContext.getRequest().setAttribute("contactEntries", contactLogEntries);
    pageRequestContext.getRequest().setAttribute("contactEntryComments", contactEntryComments);
    pageRequestContext.getRequest().setAttribute("studentGroupStudents", studentGroupStudents);
    pageRequestContext.setIncludeJSP("/templates/students/viewstudentgroup.jsp");
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StudentGroupContactLogEntry(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupContactLogEntry) Date(java.util.Date) StudentGroupStudent(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent) StudentGroupDAO(fi.otavanopisto.pyramus.dao.students.StudentGroupDAO) StudentGroupContactLogEntryDAO(fi.otavanopisto.pyramus.dao.students.StudentGroupContactLogEntryDAO) ArrayList(java.util.ArrayList) List(java.util.List) StudentGroupContactLogEntryCommentDAO(fi.otavanopisto.pyramus.dao.students.StudentGroupContactLogEntryCommentDAO) StudentGroup(fi.otavanopisto.pyramus.domainmodel.students.StudentGroup) StudentGroupContactLogEntryComment(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupContactLogEntryComment)

Example 2 with StudentGroupStudent

use of fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent in project pyramus by otavanopisto.

the class MuikkuRESTService method removeStudentGroupMembers.

@Path("/removestudentgroupmembers")
@PUT
@RESTPermit(MuikkuPermissions.MUIKKU_REMOVE_STUDENT_GROUP_MEMBERS)
public Response removeStudentGroupMembers(@Context HttpServletRequest request, StudentGroupMembersPayload payload) {
    // Prerequisites
    User loggedUser = sessionController.getUser();
    if (loggedUser.getOrganization() == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Current user lacks organization").build();
    }
    // Basic payload validation
    Long groupId = new Long(payload.getGroupIdentifier());
    StudentGroup studentGroup = studentGroupController.findStudentGroupById(groupId);
    if (studentGroup == null) {
        return Response.status(Status.BAD_REQUEST).entity(String.format("Student group %d not found", groupId)).build();
    } else if (!UserUtils.canAccessOrganization(loggedUser, studentGroup.getOrganization())) {
        logger.log(Level.SEVERE, String.format("Organization mismatch. User %d attempted to access group %d", loggedUser.getId(), groupId));
        return Response.status(Status.BAD_REQUEST).entity("No student group access").build();
    }
    for (String userIdentifier : payload.getUserIdentifiers()) {
        Long userId = new Long(userIdentifier);
        User user = userController.findUserById(userId);
        if (!UserUtils.canAccessOrganization(loggedUser, user.getOrganization())) {
            logger.log(Level.SEVERE, String.format("Organization mismatch. User %d attempted to remove user %d from group %d", loggedUser.getId(), userId, groupId));
            return Response.status(Status.BAD_REQUEST).entity("No user access").build();
        }
    }
    for (String userIdentifier : payload.getUserIdentifiers()) {
        Long userId = new Long(userIdentifier);
        User user = userController.findUserById(userId);
        if (user instanceof Student) {
            StudentGroupStudent studentGroupStudent = studentGroupController.findStudentGroupStudentByStudentGroupAndStudent(studentGroup, (Student) user);
            if (studentGroupStudent != null) {
                studentGroupController.deleteStudentGroupStudent(studentGroupStudent);
            }
        } else if (user instanceof StaffMember) {
            StudentGroupUser studentGroupUser = studentGroupController.findStudentGroupUserByStudentGroupAndUser(studentGroup, (StaffMember) user);
            if (studentGroupUser != null) {
                studentGroupController.deleteStudentGroupUser(studentGroupUser);
            }
        }
    }
    return Response.noContent().build();
}
Also used : StudentGroupStudent(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent) StudentGroupUser(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupUser) User(fi.otavanopisto.pyramus.domainmodel.users.User) CourseStudent(fi.otavanopisto.pyramus.domainmodel.courses.CourseStudent) StudentGroupStudent(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent) Student(fi.otavanopisto.pyramus.domainmodel.students.Student) StaffMember(fi.otavanopisto.pyramus.domainmodel.users.StaffMember) StudentGroupUser(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupUser) StudentGroup(fi.otavanopisto.pyramus.domainmodel.students.StudentGroup) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit) PUT(javax.ws.rs.PUT)

Example 3 with StudentGroupStudent

use of fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent in project pyramus by otavanopisto.

the class StudentRESTService method findStudentGroupStudent.

@Path("/studentGroups/{GROUPID:[0-9]*}/students/{ID:[0-9]*}")
@GET
@RESTPermit(StudentGroupPermissions.FIND_STUDENTGROUPSTUDENT)
public Response findStudentGroupStudent(@PathParam("GROUPID") Long studentGroupId, @PathParam("ID") Long id) {
    StudentGroup studentGroup = studentGroupController.findStudentGroupById(studentGroupId);
    if (studentGroup == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    if (studentGroup.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
    }
    if (!UserUtils.canAccessOrganization(sessionController.getUser(), studentGroup.getOrganization())) {
        return Response.status(Status.FORBIDDEN).build();
    }
    StudentGroupStudent studentGroupStudent = studentGroupController.findStudentGroupStudentById(id);
    if (studentGroupStudent == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    if (!studentGroupStudent.getStudentGroup().getId().equals(studentGroup.getId())) {
        return Response.status(Status.NOT_FOUND).build();
    }
    return Response.ok(objectFactory.createModel(studentGroupStudent)).build();
}
Also used : StudentGroupStudent(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent) StudentGroup(fi.otavanopisto.pyramus.domainmodel.students.StudentGroup) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit) GET(javax.ws.rs.GET)

Example 4 with StudentGroupStudent

use of fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent in project pyramus by otavanopisto.

the class EditStudentGroupJSONRequestController method process.

/**
 * Processes the request to edit a student group.
 *
 * @param requestContext
 *          The JSON request context
 */
public void process(JSONRequestContext requestContext) {
    StaffMemberDAO staffMemberDAO = DAOFactory.getInstance().getStaffMemberDAO();
    StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO();
    StudentGroupDAO studentGroupDAO = DAOFactory.getInstance().getStudentGroupDAO();
    StudentGroupStudentDAO studentGroupStudentDAO = DAOFactory.getInstance().getStudentGroupStudentDAO();
    StudentGroupUserDAO studentGroupUserDAO = DAOFactory.getInstance().getStudentGroupUserDAO();
    TagDAO tagDAO = DAOFactory.getInstance().getTagDAO();
    OrganizationDAO organizationDAO = DAOFactory.getInstance().getOrganizationDAO();
    // StudentGroup basic information
    String name = requestContext.getString("name");
    String description = requestContext.getString("description");
    Date beginDate = requestContext.getDate("beginDate");
    String tagsText = requestContext.getString("tags");
    Boolean guidanceGroup = requestContext.getBoolean("guidanceGroup");
    Set<Tag> tagEntities = new HashSet<>();
    if (!StringUtils.isBlank(tagsText)) {
        List<String> tags = Arrays.asList(tagsText.split("[\\ ,]"));
        for (String tag : tags) {
            if (!StringUtils.isBlank(tag)) {
                Tag tagEntity = tagDAO.findByText(tag.trim());
                if (tagEntity == null)
                    tagEntity = tagDAO.create(tag);
                tagEntities.add(tagEntity);
            }
        }
    }
    StudentGroup studentGroup = studentGroupDAO.findById(requestContext.getLong("studentGroupId"));
    User loggedUser = staffMemberDAO.findById(requestContext.getLoggedUserId());
    if (!UserUtils.canAccessOrganization(loggedUser, studentGroup.getOrganization())) {
        throw new SmvcRuntimeException(PyramusStatusCode.UNAUTHORIZED, "Can not access student group from another organization.");
    }
    // Version check
    Long version = requestContext.getLong("version");
    if (!studentGroup.getVersion().equals(version))
        throw new StaleObjectStateException(StudentGroup.class.getName(), studentGroup.getId());
    Organization organization = organizationDAO.findById(requestContext.getLong("organizationId"));
    if (!UserUtils.canAccessOrganization(loggedUser, organization)) {
        throw new SmvcRuntimeException(PyramusStatusCode.UNAUTHORIZED, "Invalid organization.");
    }
    studentGroupDAO.update(studentGroup, organization, name, description, beginDate, guidanceGroup, loggedUser);
    // Tags
    studentGroupDAO.setStudentGroupTags(studentGroup, tagEntities);
    // Personnel
    StudentGroupUser[] users = studentGroup.getUsers().toArray(new StudentGroupUser[0]);
    StudentGroupStudent[] students = studentGroup.getStudents().toArray(new StudentGroupStudent[0]);
    Set<Long> removables = studentGroup.getUsers().stream().map(StudentGroupUser::getId).collect(Collectors.toSet());
    int rowCount = requestContext.getInteger("usersTable.rowCount").intValue();
    for (int i = 0; i < rowCount; i++) {
        String colPrefix = "usersTable." + i;
        Long userId = requestContext.getLong(colPrefix + ".userId");
        Long studentGroupUserId = requestContext.getLong(colPrefix + ".studentGroupUserId");
        StaffMember staffMember = staffMemberDAO.findById(userId);
        if (!UserUtils.canAccessOrganization(loggedUser, staffMember.getOrganization())) {
            throw new SmvcRuntimeException(PyramusStatusCode.UNAUTHORIZED, "Can not access student group from another organization.");
        }
        if (studentGroupUserId == null) {
            // New User
            studentGroupUserDAO.create(studentGroup, staffMember, loggedUser);
        } else {
            // Old User, still in list
            removables.remove(studentGroupUserId);
        }
    }
    // Remove the ones that were deleted from group
    for (int i = 0; i < users.length; i++) {
        if (removables.contains(users[i].getId())) {
            studentGroupUserDAO.remove(studentGroup, users[i], loggedUser);
        }
    }
    // Students
    removables = studentGroup.getStudents().stream().map(StudentGroupStudent::getId).collect(Collectors.toSet());
    rowCount = requestContext.getInteger("studentsTable.rowCount");
    for (int i = 0; i < rowCount; i++) {
        String colPrefix = "studentsTable." + i;
        Long studentId = requestContext.getLong(colPrefix + ".studentId");
        Long studentGroupStudentId = requestContext.getLong(colPrefix + ".studentGroupStudentId");
        Student student = studentDAO.findById(studentId);
        if (!UserUtils.canAccessOrganization(loggedUser, student.getOrganization())) {
            throw new SmvcRuntimeException(PyramusStatusCode.UNAUTHORIZED, "Can not access student from another organization.");
        }
        if (studentGroupStudentId == null) {
            // New Student
            studentGroupStudentDAO.create(studentGroup, student, loggedUser);
        } else {
            // Old User, still in list, we'll update if the student has changed student group
            removables.remove(studentGroupStudentId);
            StudentGroupStudent sgStudent = studentGroupStudentDAO.findById(studentGroupStudentId);
            if (!sgStudent.getStudent().getId().equals(studentId)) {
                studentGroupStudentDAO.update(sgStudent, studentDAO.findById(studentId), loggedUser);
            }
        }
    }
    // Remove the ones that were deleted from group
    for (int i = 0; i < students.length; i++) {
        if (removables.contains(students[i].getId())) {
            studentGroupStudentDAO.remove(studentGroup, students[i], loggedUser);
        }
    }
    requestContext.setRedirectURL(requestContext.getReferer(true));
}
Also used : User(fi.otavanopisto.pyramus.domainmodel.users.User) StudentGroupUser(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupUser) Organization(fi.otavanopisto.pyramus.domainmodel.base.Organization) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) StaffMember(fi.otavanopisto.pyramus.domainmodel.users.StaffMember) StudentGroupStudent(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) StudentGroupStudentDAO(fi.otavanopisto.pyramus.dao.students.StudentGroupStudentDAO) OrganizationDAO(fi.otavanopisto.pyramus.dao.base.OrganizationDAO) HashSet(java.util.HashSet) StudentGroupUserDAO(fi.otavanopisto.pyramus.dao.students.StudentGroupUserDAO) TagDAO(fi.otavanopisto.pyramus.dao.base.TagDAO) Student(fi.otavanopisto.pyramus.domainmodel.students.Student) StudentGroupStudent(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent) Date(java.util.Date) StudentGroupStudentDAO(fi.otavanopisto.pyramus.dao.students.StudentGroupStudentDAO) StudentDAO(fi.otavanopisto.pyramus.dao.students.StudentDAO) StudentGroupDAO(fi.otavanopisto.pyramus.dao.students.StudentGroupDAO) Tag(fi.otavanopisto.pyramus.domainmodel.base.Tag) StudentGroupUser(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupUser) StaleObjectStateException(org.hibernate.StaleObjectStateException) StudentGroup(fi.otavanopisto.pyramus.domainmodel.students.StudentGroup)

Example 5 with StudentGroupStudent

use of fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent in project pyramus by otavanopisto.

the class StudentGroupDAO method listByStudent.

public List<StudentGroup> listByStudent(Student student, Integer firstResult, Integer maxResults, Boolean archived) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<StudentGroup> criteria = criteriaBuilder.createQuery(StudentGroup.class);
    Root<StudentGroupStudent> root = criteria.from(StudentGroupStudent.class);
    Join<StudentGroupStudent, StudentGroup> studentGroup = root.join(StudentGroupStudent_.studentGroup);
    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(criteriaBuilder.equal(root.get(StudentGroupStudent_.student), student));
    if (archived != null)
        predicates.add(criteriaBuilder.equal(studentGroup.get(StudentGroup_.archived), archived));
    criteria.select(root.get(StudentGroupStudent_.studentGroup));
    criteria.where(criteriaBuilder.and(predicates.toArray(new Predicate[0])));
    TypedQuery<StudentGroup> query = entityManager.createQuery(criteria);
    if (firstResult != null) {
        query.setFirstResult(firstResult);
    }
    if (maxResults != null) {
        query.setMaxResults(maxResults);
    }
    return query.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) StudentGroupStudent(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent) FullTextEntityManager(org.hibernate.search.jpa.FullTextEntityManager) EntityManager(javax.persistence.EntityManager) ArrayList(java.util.ArrayList) StudentGroup(fi.otavanopisto.pyramus.domainmodel.students.StudentGroup) Predicate(javax.persistence.criteria.Predicate)

Aggregations

StudentGroupStudent (fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent)18 StudentGroup (fi.otavanopisto.pyramus.domainmodel.students.StudentGroup)14 Student (fi.otavanopisto.pyramus.domainmodel.students.Student)9 EntityManager (javax.persistence.EntityManager)7 CourseStudent (fi.otavanopisto.pyramus.domainmodel.courses.CourseStudent)6 StudentGroupUser (fi.otavanopisto.pyramus.domainmodel.students.StudentGroupUser)6 RESTPermit (fi.otavanopisto.pyramus.rest.annotation.RESTPermit)6 ArrayList (java.util.ArrayList)6 Path (javax.ws.rs.Path)6 StaffMember (fi.otavanopisto.pyramus.domainmodel.users.StaffMember)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)5 Organization (fi.otavanopisto.pyramus.domainmodel.base.Organization)4 User (fi.otavanopisto.pyramus.domainmodel.users.User)4 Date (java.util.Date)4 StudentGroupDAO (fi.otavanopisto.pyramus.dao.students.StudentGroupDAO)3 Tag (fi.otavanopisto.pyramus.domainmodel.base.Tag)3 OrganizationDAO (fi.otavanopisto.pyramus.dao.base.OrganizationDAO)2 StaffMemberDAO (fi.otavanopisto.pyramus.dao.users.StaffMemberDAO)2 Email (fi.otavanopisto.pyramus.domainmodel.base.Email)2 StudyProgramme (fi.otavanopisto.pyramus.domainmodel.base.StudyProgramme)2