Search in sources :

Example 1 with WorkspaceStaffMember

use of fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceStaffMember 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)

Aggregations

UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)1 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)1 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)1 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)1 WorkspaceStaffMember (fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceStaffMember)1 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)1 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)1 User (fi.otavanopisto.muikku.schooldata.entity.User)1 WorkspaceUser (fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser)1 ArrayList (java.util.ArrayList)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1