Search in sources :

Example 61 with WorkspaceUserEntity

use of fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity in project muikku by otavanopisto.

the class WorkspaceRESTService method findWorkspaceStudent.

@GET
@Path("/workspaces/{WORKSPACEENTITYID}/students/{ID}")
@RESTPermit(handling = Handling.INLINE)
public Response findWorkspaceStudent(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("ID") String workspaceStudentId) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    SchoolDataIdentifier workspaceUserIdentifier = SchoolDataIdentifier.fromId(workspaceStudentId);
    if (workspaceUserIdentifier == null) {
        return Response.status(Status.BAD_REQUEST).entity("Invalid workspace user id").build();
    }
    WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceUserIdentifierIncludeArchived(workspaceUserIdentifier);
    if (workspaceUserEntity == null) {
        return Response.status(Status.NOT_FOUND).entity("Workspace student not found").build();
    }
    fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser bridgeUser = workspaceController.findWorkspaceUser(workspaceUserEntity);
    if (bridgeUser == null) {
        return Response.status(Status.NOT_FOUND).entity("School data user not found").build();
    }
    SchoolDataIdentifier userIdentifier = bridgeUser.getUserIdentifier();
    User user = userController.findUserByIdentifier(userIdentifier);
    if (user == null) {
        return Response.status(Status.NOT_FOUND).entity("School data user not found").build();
    }
    WorkspaceUser workspaceUser = workspaceController.findWorkspaceUserByWorkspaceEntityAndUser(workspaceEntity, userIdentifier);
    if (workspaceUser == null) {
        return Response.status(Status.NOT_FOUND).entity("School data workspace user not found").build();
    }
    return Response.ok(createRestModel(workspaceUserEntity.getUserSchoolDataIdentifier().getUserEntity(), user, workspaceUser, workspaceUserEntity.getActive())).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) User(fi.otavanopisto.muikku.schooldata.entity.User) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 62 with WorkspaceUserEntity

use of fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity in project muikku by otavanopisto.

the class WorkspaceRESTService method listWorkspaceStaffMembers.

@GET
@Path("/workspaces/{ID}/staffMembers")
@RESTPermitUnimplemented
public Response listWorkspaceStaffMembers(@PathParam("ID") Long workspaceEntityId, @QueryParam("orderBy") String orderBy) {
    // Workspace
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    // Access check
    if (!sessionController.hasWorkspacePermission(MuikkuPermissions.LIST_WORKSPACE_MEMBERS, workspaceEntity)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    // Staff via WorkspaceSchoolDataBridge
    List<fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser> schoolDataUsers = workspaceController.listWorkspaceStaffMembers(workspaceEntity);
    List<WorkspaceStaffMember> workspaceStaffMembers = new ArrayList<>();
    for (fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser workspaceUser : schoolDataUsers) {
        SchoolDataIdentifier userIdentifier = workspaceUser.getUserIdentifier();
        User user = userController.findUserByIdentifier(userIdentifier);
        if (user != null) {
            UserEntity userEntity = userEntityController.findUserEntityByUser(user);
            // #3111: Workspace staff members should be limited to teachers only. A better implementation would support specified workspace roles
            WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findActiveWorkspaceUserByWorkspaceEntityAndUserEntity(workspaceEntity, userEntity);
            if (workspaceUserEntity == null || workspaceUserEntity.getWorkspaceUserRole().getArchetype() != WorkspaceRoleArchetype.TEACHER) {
                continue;
            }
            workspaceStaffMembers.add(new WorkspaceStaffMember(workspaceUser.getIdentifier().toId(), workspaceUser.getUserIdentifier().toId(), userEntity != null ? userEntity.getId() : null, user.getFirstName(), user.getLastName()));
        } else {
            logger.log(Level.SEVERE, String.format("Could not find user %s", userIdentifier));
        }
    }
    // Sorting
    if (StringUtils.equals(orderBy, "name")) {
        Collections.sort(workspaceStaffMembers, new Comparator<WorkspaceStaffMember>() {

            @Override
            public int compare(WorkspaceStaffMember o1, WorkspaceStaffMember o2) {
                String s1 = String.format("%s, %s", StringUtils.defaultString(o1.getLastName(), ""), StringUtils.defaultString(o1.getFirstName(), ""));
                String s2 = String.format("%s, %s", StringUtils.defaultString(o2.getLastName(), ""), StringUtils.defaultString(o2.getFirstName(), ""));
                return s1.compareTo(s2);
            }
        });
    }
    // Response
    return Response.ok(workspaceStaffMembers).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) User(fi.otavanopisto.muikku.schooldata.entity.User) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) ArrayList(java.util.ArrayList) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceStaffMember(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceStaffMember) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Example 63 with WorkspaceUserEntity

use of fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity in project muikku by otavanopisto.

the class WorkspaceRESTService method deleteWorkspaceStudent.

@DELETE
@Path("/workspaces/{WORKSPACEENTITYID}/students/{ID}")
@RESTPermit(handling = Handling.INLINE)
public Response deleteWorkspaceStudent(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("ID") String workspaceStudentId) {
    // Workspace
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    // User (in school data source)
    SchoolDataIdentifier workspaceUserIdentifier = SchoolDataIdentifier.fromId(workspaceStudentId);
    if (workspaceUserIdentifier == null) {
        return Response.status(Status.BAD_REQUEST).entity("Invalid workspace user id").build();
    }
    // Access check
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).entity("Not logged in").build();
    }
    if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE_MEMBERS, workspaceEntity)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceUserIdentifier(workspaceUserIdentifier);
    if (workspaceUserEntity == null) {
        return Response.status(Status.NOT_FOUND).entity("Workspace student not found").build();
    }
    fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser bridgeUser = workspaceController.findWorkspaceUser(workspaceUserEntity);
    if (bridgeUser == null) {
        return Response.status(Status.NOT_FOUND).entity("School data user not found").build();
    }
    workspaceController.updateWorkspaceStudentActivity(bridgeUser, false);
    workspaceUserEntityController.archiveWorkspaceUserEntity(workspaceUserEntity);
    return Response.noContent().build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RESTPermit(fi.otavanopisto.security.rest.RESTPermit)

Example 64 with WorkspaceUserEntity

use of fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity in project muikku by otavanopisto.

the class WorkspaceRESTService method listWorkspaceStudents.

@GET
@Path("/workspaces/{ID}/students")
@RESTPermit(handling = Handling.INLINE)
public Response listWorkspaceStudents(@PathParam("ID") Long workspaceEntityId, @QueryParam("active") Boolean active, @QueryParam("requestedAssessment") Boolean requestedAssessment, @QueryParam("assessed") Boolean assessed, @QueryParam("studentIdentifier") String studentId, @QueryParam("search") String searchString, @QueryParam("flags") Long[] flagIds, @QueryParam("maxResults") Integer maxResults, @QueryParam("orderBy") String orderBy) {
    List<SchoolDataIdentifier> studentIdentifiers = null;
    if (StringUtils.isNotBlank(studentId)) {
        SchoolDataIdentifier studentIdentifier = SchoolDataIdentifier.fromId(studentId);
        if (studentIdentifier == null) {
            return Response.status(Status.BAD_REQUEST).entity(String.format("Malformed student identifier %s", studentId)).build();
        }
        studentIdentifiers = Collections.singletonList(studentIdentifier);
    }
    // Workspace
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    // Access check
    if (!sessionController.hasWorkspacePermission(MuikkuPermissions.LIST_WORKSPACE_MEMBERS, workspaceEntity)) {
        if (studentIdentifiers == null || studentIdentifiers.size() != 1 || !studentIdentifiers.get(0).equals(sessionController.getLoggedUser())) {
            return Response.status(Status.FORBIDDEN).build();
        }
    }
    List<fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser> workspaceUsers = null;
    if (searchString != null) {
        studentIdentifiers = new ArrayList<>();
        Iterator<SearchProvider> searchProviderIterator = searchProviders.iterator();
        if (!searchProviderIterator.hasNext()) {
            return Response.status(Status.INTERNAL_SERVER_ERROR).entity("No search provider found").build();
        }
        SearchProvider elasticSearchProvider = searchProviderIterator.next();
        if (elasticSearchProvider != null) {
            String[] fields = new String[] { "firstName", "lastName", "nickName", "email" };
            SearchResult result = elasticSearchProvider.searchUsers(searchString, fields, Arrays.asList(EnvironmentRoleArchetype.STUDENT), (Collection<Long>) null, Collections.singletonList(workspaceEntityId), (Collection<SchoolDataIdentifier>) null, Boolean.FALSE, Boolean.FALSE, false, 0, maxResults != null ? maxResults : Integer.MAX_VALUE);
            List<Map<String, Object>> results = result.getResults();
            if (results != null && !results.isEmpty()) {
                for (Map<String, Object> o : results) {
                    String foundStudentId = (String) o.get("id");
                    if (StringUtils.isBlank(foundStudentId)) {
                        logger.severe("Could not process user found from search index because it had a null id");
                        continue;
                    }
                    String[] studentIdParts = foundStudentId.split("/", 2);
                    SchoolDataIdentifier foundStudentIdentifier = studentIdParts.length == 2 ? new SchoolDataIdentifier(studentIdParts[0], studentIdParts[1]) : null;
                    if (foundStudentIdentifier == null) {
                        logger.severe(String.format("Could not process user found from search index with id %s", studentId));
                        continue;
                    }
                    studentIdentifiers.add(foundStudentIdentifier);
                }
            }
        }
    }
    List<Flag> flags = null;
    if (flagIds != null && flagIds.length > 0) {
        flags = new ArrayList<>(flagIds.length);
        for (Long flagId : flagIds) {
            Flag flag = flagController.findFlagById(flagId);
            if (flag == null) {
                return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid flag id %d", flagId)).build();
            }
            if (!flagController.hasFlagPermission(flag, sessionController.getLoggedUser())) {
                return Response.status(Status.FORBIDDEN).entity(String.format("You don't have permission to use flag %d", flagId)).build();
            }
            flags.add(flag);
        }
    }
    if (flags != null) {
        List<SchoolDataIdentifier> flaggedStudents = flagController.getFlaggedStudents(flags);
        if (studentIdentifiers != null) {
            studentIdentifiers.retainAll(flaggedStudents);
        } else {
            studentIdentifiers = flaggedStudents;
        }
    }
    List<WorkspaceStudent> result = new ArrayList<>();
    if (studentIdentifiers != null) {
        workspaceUsers = new ArrayList<>();
        for (SchoolDataIdentifier studentIdentifier : studentIdentifiers) {
            WorkspaceUserEntity wue = workspaceUserEntityController.findWorkspaceUserByWorkspaceEntityAndUserIdentifier(workspaceEntity, studentIdentifier);
            if (wue == null) {
                continue;
            }
            if (active != null && !active.equals(wue.getActive())) {
                continue;
            }
            WorkspaceUser workspaceUser = workspaceController.findWorkspaceUser(wue);
            if (workspaceUser == null) {
                continue;
            }
            workspaceUsers.add(workspaceUser);
        }
    } else {
        // Students via WorkspaceSchoolDataBridge
        workspaceUsers = workspaceController.listWorkspaceStudents(workspaceEntity);
    }
    if (workspaceUsers == null || workspaceUsers.isEmpty()) {
        return Response.noContent().build();
    }
    Map<String, WorkspaceUserEntity> workspaceUserEntityMap = new HashMap<>();
    List<WorkspaceUserEntity> workspaceUserEntities = workspaceUserEntityController.listWorkspaceUserEntities(workspaceEntity);
    for (WorkspaceUserEntity workspaceUserEntity : workspaceUserEntities) {
        workspaceUserEntityMap.put(new SchoolDataIdentifier(workspaceUserEntity.getIdentifier(), workspaceUserEntity.getUserSchoolDataIdentifier().getDataSource().getIdentifier()).toId(), workspaceUserEntity);
    }
    if (maxResults != null && workspaceUsers.size() > maxResults) {
        workspaceUsers.subList(maxResults, workspaceUsers.size() - 1).clear();
    }
    for (fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser workspaceUser : workspaceUsers) {
        SchoolDataIdentifier workspaceUserIdentifier = workspaceUser.getIdentifier();
        WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityMap.get(workspaceUserIdentifier.toId());
        if (workspaceUserEntity == null) {
            logger.log(Level.WARNING, String.format("Workspace student %s does not exist in Muikku", workspaceUserIdentifier.toId()));
            continue;
        }
        if (active == null || active.equals(workspaceUserEntity.getActive())) {
            if (requestedAssessment != null) {
                boolean hasAssessmentRequest = workspaceUserEntity != null && !assessmentRequestController.listByWorkspaceUser(workspaceUserEntity).isEmpty();
                if (requestedAssessment != hasAssessmentRequest) {
                    continue;
                }
            }
            if (assessed != null) {
                boolean isAssessed = !gradingController.listWorkspaceAssessments(workspaceUser.getWorkspaceIdentifier(), workspaceUser.getUserIdentifier()).isEmpty();
                if (assessed != isAssessed) {
                    continue;
                }
            }
            SchoolDataIdentifier userIdentifier = workspaceUser.getUserIdentifier();
            User user = userController.findUserByIdentifier(userIdentifier);
            if (user != null) {
                UserEntity userEntity = null;
                if (workspaceUserEntity != null) {
                    userEntity = workspaceUserEntity.getUserSchoolDataIdentifier().getUserEntity();
                } else {
                    userEntity = userEntityController.findUserEntityByDataSourceAndIdentifier(user.getSchoolDataSource(), user.getIdentifier());
                }
                result.add(createRestModel(userEntity, user, workspaceUser, workspaceUserEntity != null && workspaceUserEntity.getActive()));
            } else {
                logger.log(Level.SEVERE, String.format("Could not find user for identifier %s", userIdentifier));
            }
        }
    }
    // Sorting
    if (StringUtils.equals(orderBy, "name")) {
        Collections.sort(result, new Comparator<WorkspaceStudent>() {

            @Override
            public int compare(WorkspaceStudent o1, WorkspaceStudent o2) {
                String s1 = String.format("%s, %s", StringUtils.defaultString(o1.getLastName(), ""), StringUtils.defaultString(o1.getFirstName(), ""));
                String s2 = String.format("%s, %s", StringUtils.defaultString(o2.getLastName(), ""), StringUtils.defaultString(o2.getFirstName(), ""));
                return s1.compareTo(s2);
            }
        });
    }
    // Response
    return Response.ok(result).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) User(fi.otavanopisto.muikku.schooldata.entity.User) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) SearchProvider(fi.otavanopisto.muikku.search.SearchProvider) SearchResult(fi.otavanopisto.muikku.search.SearchResult) Flag(fi.otavanopisto.muikku.model.users.Flag) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceStudent(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceStudent) Map(java.util.Map) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 65 with WorkspaceUserEntity

use of fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity in project muikku by otavanopisto.

the class WorkspaceSystemRESTService method synchronizeWorkspaceUsers.

@GET
@Path("/syncworkspaceusers/{ID}")
@RESTPermit(handling = Handling.INLINE)
public Response synchronizeWorkspaceUsers(@PathParam("ID") Long workspaceEntityId, @Context Request request) {
    // Admins only
    if (!sessionController.isSuperuser()) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    logger.info(String.format("Synchronizing users of workspace entity %d", workspaceEntityId));
    // Workspace
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    // Student role
    WorkspaceRoleEntity workspaceStudentRole = roleController.findWorkspaceRoleEntityById(getWorkspaceStudentRoleId());
    // Workspace students in Muikku
    List<WorkspaceUserEntity> muikkuWorkspaceStudents = workspaceUserEntityController.listWorkspaceUserEntitiesByRole(workspaceEntity, workspaceStudentRole);
    logger.info(String.format("Before synchronizing, Muikku course has %d active students", muikkuWorkspaceStudents.size()));
    // Course students in Pyramus
    List<WorkspaceUser> pyramusCourseStudents = workspaceController.listWorkspaceStudents(workspaceEntity);
    logger.info(String.format("Before synchronizing, Pyramus course has %d active students", pyramusCourseStudents.size()));
    // Loop through Pyramus students
    for (WorkspaceUser pyramusCourseStudent : pyramusCourseStudents) {
        String pyramusStudentId = pyramusCourseStudent.getUserIdentifier().getIdentifier();
        String pyramusCourseStudentId = pyramusCourseStudent.getIdentifier().getIdentifier();
        // Find Muikku student corresponding to Pyramus student
        String muikkuWorkspaceStudentId = null;
        WorkspaceUserEntity muikkuWorkspaceStudent = null;
        for (int i = 0; i < muikkuWorkspaceStudents.size(); i++) {
            muikkuWorkspaceStudentId = muikkuWorkspaceStudents.get(i).getIdentifier();
            if (StringUtils.equals(muikkuWorkspaceStudentId, pyramusCourseStudentId)) {
                muikkuWorkspaceStudent = muikkuWorkspaceStudents.get(i);
                muikkuWorkspaceStudents.remove(i);
                break;
            }
        }
        if (muikkuWorkspaceStudent == null) {
            // Restore an archived workspace student, if possible
            muikkuWorkspaceStudent = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceUserIdentifierAndArchived(pyramusCourseStudent.getIdentifier(), Boolean.TRUE);
            if (muikkuWorkspaceStudent != null) {
                workspaceUserEntityController.unarchiveWorkspaceUserEntity(muikkuWorkspaceStudent);
                logger.info(String.format("Unarchived workspace student %s", pyramusCourseStudentId));
                SchoolDataIdentifier muikkuStudentIdentifier = new SchoolDataIdentifier(muikkuWorkspaceStudent.getUserSchoolDataIdentifier().getIdentifier(), muikkuWorkspaceStudent.getUserSchoolDataIdentifier().getDataSource().getIdentifier());
                ensureCorrectWorkspaceStudent(muikkuWorkspaceStudent, muikkuStudentIdentifier, pyramusCourseStudent.getUserIdentifier());
            } else {
                // Workspace student with workspace student identifier still not found, even amongst archived workspace students
                UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierBySchoolDataIdentifier(pyramusCourseStudent.getUserIdentifier());
                if (userSchoolDataIdentifier == null) {
                    logger.severe(String.format("Unable to fix missing workspace student: UserSchoolDataIdentifier for Pyramus student %s not found", pyramusStudentId));
                } else {
                    // Try to find workspace student with workspace + student combo
                    muikkuWorkspaceStudent = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceAndUserSchoolDataIdentifierIncludeArchived(workspaceEntity, userSchoolDataIdentifier);
                    if (muikkuWorkspaceStudent != null) {
                        // Found. Might be archived but definitely has the wrong workspace student identifier
                        if (muikkuWorkspaceStudent.getArchived()) {
                            workspaceUserEntityController.unarchiveWorkspaceUserEntity(muikkuWorkspaceStudent);
                        }
                        if (!muikkuWorkspaceStudent.getIdentifier().equals(pyramusCourseStudent.getIdentifier().getIdentifier())) {
                            workspaceUserEntityController.updateIdentifier(muikkuWorkspaceStudent, pyramusCourseStudent.getIdentifier().getIdentifier());
                        }
                    } else {
                        // Not found. Create a new workspace student
                        muikkuWorkspaceStudent = workspaceUserEntityController.createWorkspaceUserEntity(userSchoolDataIdentifier, workspaceEntity, pyramusCourseStudentId, workspaceStudentRole);
                        logger.info(String.format("Created workspace student %s", muikkuWorkspaceStudent.getIdentifier()));
                    }
                }
            }
        } else {
            // Workspace student found with workspace student identifier. We still need to ensure that the underlying student is the same as in Pyramus
            SchoolDataIdentifier muikkuStudentIdentifier = new SchoolDataIdentifier(muikkuWorkspaceStudent.getUserSchoolDataIdentifier().getIdentifier(), muikkuWorkspaceStudent.getUserSchoolDataIdentifier().getDataSource().getIdentifier());
            ensureCorrectWorkspaceStudent(muikkuWorkspaceStudent, muikkuStudentIdentifier, pyramusCourseStudent.getUserIdentifier());
        }
    }
    // The remaining Muikku students in muikkuWorkspaceStudents were not in Pyramus so archive them from Muikku
    if (!muikkuWorkspaceStudents.isEmpty()) {
        for (WorkspaceUserEntity muikkuWorkspaceStudent : muikkuWorkspaceStudents) {
            workspaceUserEntityController.archiveWorkspaceUserEntity(muikkuWorkspaceStudent);
        }
        logger.info(String.format("Archived %d Muikku workspace students that were not present in Pyramus", muikkuWorkspaceStudents.size()));
    }
    // Student count in Muikku after synchronizing, which should be the same as in Pyramus before synchronization
    muikkuWorkspaceStudents = workspaceUserEntityController.listWorkspaceUserEntitiesByRole(workspaceEntity, workspaceStudentRole);
    logger.info(String.format("After synchronizing, Muikku course has %d active students", pyramusCourseStudents.size()));
    // Course staff maintenance
    // Teacher role
    WorkspaceRoleEntity workspaceTeacherRole = roleController.findWorkspaceRoleEntityById(getWorkspaceTeacherRoleId());
    // Workspace teachers in Muikku
    List<WorkspaceUserEntity> muikkuWorkspaceTeachers = workspaceUserEntityController.listWorkspaceUserEntitiesByRole(workspaceEntity, workspaceTeacherRole);
    logger.info(String.format("Before synchronizing, Muikku course has %d active teachers", muikkuWorkspaceTeachers.size()));
    // Course teachers in Pyramus
    List<WorkspaceUser> pyramusCourseTeachers = workspaceController.listWorkspaceStaffMembers(workspaceEntity);
    logger.info(String.format("Before synchronizing, Pyramus course has %d active teachers", pyramusCourseTeachers.size()));
    for (WorkspaceUser pyramusCourseTeacher : pyramusCourseTeachers) {
        String pyramusCourseTeacherId = pyramusCourseTeacher.getIdentifier().getIdentifier();
        String pyramusTeacherId = pyramusCourseTeacher.getUserIdentifier().getIdentifier();
        WorkspaceUserEntity muikkuWorkspaceTeacher = null;
        for (int i = 0; i < muikkuWorkspaceTeachers.size(); i++) {
            String muikkuCourseTeacherId = muikkuWorkspaceTeachers.get(i).getIdentifier();
            if (muikkuCourseTeacherId.equals(pyramusCourseTeacherId)) {
                muikkuWorkspaceTeacher = muikkuWorkspaceTeachers.get(i);
                muikkuWorkspaceTeachers.remove(i);
                break;
            }
        }
        if (muikkuWorkspaceTeacher == null) {
            muikkuWorkspaceTeacher = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceUserIdentifierAndArchived(pyramusCourseTeacher.getIdentifier(), Boolean.TRUE);
            if (muikkuWorkspaceTeacher != null) {
                workspaceUserEntityController.unarchiveWorkspaceUserEntity(muikkuWorkspaceTeacher);
                logger.info(String.format("Unarchived workspace teacher %s", pyramusCourseTeacherId));
                if (!muikkuWorkspaceTeacher.getIdentifier().equals(pyramusCourseTeacher.getIdentifier().getIdentifier())) {
                    workspaceUserEntityController.updateIdentifier(muikkuWorkspaceTeacher, pyramusCourseTeacher.getIdentifier().getIdentifier());
                }
            } else {
                UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierBySchoolDataIdentifier(pyramusCourseTeacher.getUserIdentifier());
                if (userSchoolDataIdentifier == null) {
                    logger.severe(String.format("Unable to fix missing workspace teacher: UserSchoolDataIdentifier for Pyramus teacher %s not found", pyramusTeacherId));
                } else {
                    muikkuWorkspaceTeacher = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceAndUserSchoolDataIdentifierIncludeArchived(workspaceEntity, userSchoolDataIdentifier);
                    if (muikkuWorkspaceTeacher != null) {
                        if (muikkuWorkspaceTeacher.getArchived()) {
                            workspaceUserEntityController.unarchiveWorkspaceUserEntity(muikkuWorkspaceTeacher);
                        }
                        if (!muikkuWorkspaceTeacher.getIdentifier().equals(pyramusCourseTeacher.getIdentifier().getIdentifier())) {
                            workspaceUserEntityController.updateIdentifier(muikkuWorkspaceTeacher, pyramusCourseTeacher.getIdentifier().getIdentifier());
                        }
                    } else {
                        muikkuWorkspaceTeacher = workspaceUserEntityController.createWorkspaceUserEntity(userSchoolDataIdentifier, workspaceEntity, pyramusCourseTeacherId, workspaceTeacherRole);
                        logger.info(String.format("Created workspace teacher %", muikkuWorkspaceTeacher.getIdentifier()));
                    }
                }
            }
        }
    }
    // The remaining Muikku teachers in muikkuWorkspaceTeachers were not in Pyramus so archive them from Muikku
    if (!muikkuWorkspaceTeachers.isEmpty()) {
        for (WorkspaceUserEntity muikkuWorkspaceTeacher : muikkuWorkspaceTeachers) {
            workspaceUserEntityController.archiveWorkspaceUserEntity(muikkuWorkspaceTeacher);
        }
        logger.info(String.format("Archived %d Muikku workspace teachers that were not present in Pyramus", muikkuWorkspaceTeachers.size()));
    }
    return null;
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceRoleEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceRoleEntity) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Aggregations

WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)65 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)43 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)33 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)25 Path (javax.ws.rs.Path)20 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)17 ArrayList (java.util.ArrayList)17 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)16 EntityManager (javax.persistence.EntityManager)14 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)14 GET (javax.ws.rs.GET)10 User (fi.otavanopisto.muikku.schooldata.entity.User)9 WorkspaceUser (fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser)7 WorkspaceRoleEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceRoleEntity)6 Workspace (fi.otavanopisto.muikku.schooldata.entity.Workspace)6 RoleSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.RoleSchoolDataIdentifier)5 Date (java.util.Date)5 UserGroupEntity (fi.otavanopisto.muikku.model.users.UserGroupEntity)4 HashMap (java.util.HashMap)4 DELETE (javax.ws.rs.DELETE)4