Search in sources :

Example 1 with UserRole

use of fi.otavanopisto.pyramus.rest.model.UserRole in project muikku by otavanopisto.

the class PyramusUserSchoolDataBridge method findRole.

@Override
public Role findRole(String identifier) {
    UserRole pyramusUserRole = identifierMapper.getPyramusUserRole(identifier);
    if (pyramusUserRole != null) {
        return entityFactory.createEntity(pyramusUserRole);
    }
    String id = identifierMapper.getPyramusCourseRoleId(identifier);
    if (StringUtils.isBlank(id)) {
        throw new SchoolDataBridgeInternalException("Malformed role identifier");
    }
    if ("STUDENT".equals(id)) {
        return entityFactory.createCourseStudentRoleEntity();
    }
    return entityFactory.createEntity(pyramusClient.get("/courses/staffMemberRoles/" + id, CourseStaffMemberRole.class));
}
Also used : CourseStaffMemberRole(fi.otavanopisto.pyramus.rest.model.CourseStaffMemberRole) UserRole(fi.otavanopisto.pyramus.rest.model.UserRole) SchoolDataBridgeInternalException(fi.otavanopisto.muikku.schooldata.SchoolDataBridgeInternalException)

Example 2 with UserRole

use of fi.otavanopisto.pyramus.rest.model.UserRole in project muikku by otavanopisto.

the class PyramusUpdater method updateUserRoles.

/**
 * Updates user roles from Pyramus
 *
 * @return count of updates roles
 */
public int updateUserRoles() {
    int count = 0;
    List<RoleSchoolDataIdentifier> existingRoleIdentifiers = roleSchoolDataIdentifierController.listRoleSchoolDataIdentifiersByDataSource(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE);
    Map<String, RoleSchoolDataIdentifier> removedIdentifiers = new HashMap<>();
    for (RoleSchoolDataIdentifier existingRoleIdentifier : existingRoleIdentifiers) {
        removedIdentifiers.put(existingRoleIdentifier.getIdentifier(), existingRoleIdentifier);
    }
    for (fi.otavanopisto.pyramus.rest.model.UserRole userRole : fi.otavanopisto.pyramus.rest.model.UserRole.values()) {
        String roleIdentifier = identifierMapper.getEnvironmentRoleIdentifier(userRole);
        removedIdentifiers.remove(roleIdentifier);
        if (environmentRoleEntityController.findEnvironmentRoleEntity(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, roleIdentifier) == null) {
            EnvironmentRole environmentRole = entityFactory.createEntity(userRole);
            schoolDataEnvironmentRoleDiscoveredEvent.fire(new SchoolDataEnvironmentRoleDiscoveredEvent(environmentRole.getSchoolDataSource(), environmentRole.getIdentifier(), environmentRole.getArchetype(), environmentRole.getName()));
            count++;
        }
    }
    CourseStaffMemberRole[] staffMemberRoles = pyramusClient.get().get("/courses/staffMemberRoles", CourseStaffMemberRole[].class);
    if (staffMemberRoles == null || staffMemberRoles.length == 0) {
        logger.warning("Aborting role synchronization because Pyramus has no course staff member roles");
        return count;
    }
    for (CourseStaffMemberRole staffMemberRole : staffMemberRoles) {
        String identifier = identifierMapper.getWorkspaceStaffRoleIdentifier(staffMemberRole.getId());
        removedIdentifiers.remove(identifier);
        WorkspaceRoleEntity workspaceRoleEntity = workspaceRoleEntityController.findWorkspaceRoleEntityByDataSourceAndIdentifier(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, identifier);
        if (workspaceRoleEntity == null) {
            WorkspaceRole workspaceRole = entityFactory.createEntity(staffMemberRole);
            schoolDataWorkspaceRoleDiscoveredEvent.fire(new SchoolDataWorkspaceRoleDiscoveredEvent(workspaceRole.getSchoolDataSource(), workspaceRole.getIdentifier(), workspaceRole.getArchetype(), workspaceRole.getName()));
            count++;
        }
    }
    WorkspaceRole studentRole = entityFactory.createCourseStudentRoleEntity();
    removedIdentifiers.remove(studentRole.getIdentifier());
    WorkspaceRoleEntity studentWorkspaceRoleEntity = workspaceRoleEntityController.findWorkspaceRoleEntityByDataSourceAndIdentifier(studentRole.getSchoolDataSource(), studentRole.getIdentifier());
    if (studentWorkspaceRoleEntity == null) {
        schoolDataWorkspaceRoleDiscoveredEvent.fire(new SchoolDataWorkspaceRoleDiscoveredEvent(studentRole.getSchoolDataSource(), studentRole.getIdentifier(), studentRole.getArchetype(), studentRole.getName()));
        count++;
    }
    Set<String> removedIdentifierIds = removedIdentifiers.keySet();
    for (String removedIdentifierId : removedIdentifierIds) {
        RoleSchoolDataIdentifier removedIdentifier = removedIdentifiers.get(removedIdentifierId);
        if (removedIdentifier.getRoleEntity().getType() == UserRoleType.ENVIRONMENT) {
            schoolDataEnvironmentRoleRemovedEvent.fire(new SchoolDataEnvironmentRoleRemovedEvent(removedIdentifier.getDataSource().getIdentifier(), removedIdentifier.getIdentifier()));
        } else if (removedIdentifier.getRoleEntity().getType() == UserRoleType.WORKSPACE) {
            schoolDataWorkspaceRoleRemovedEvent.fire(new SchoolDataWorkspaceRoleRemovedEvent(removedIdentifier.getDataSource().getIdentifier(), removedIdentifier.getIdentifier()));
        }
    }
    return count;
}
Also used : CourseStaffMemberRole(fi.otavanopisto.pyramus.rest.model.CourseStaffMemberRole) SchoolDataWorkspaceRoleDiscoveredEvent(fi.otavanopisto.muikku.schooldata.events.SchoolDataWorkspaceRoleDiscoveredEvent) UserRole(fi.otavanopisto.pyramus.rest.model.UserRole) HashMap(java.util.HashMap) SchoolDataEnvironmentRoleDiscoveredEvent(fi.otavanopisto.muikku.schooldata.events.SchoolDataEnvironmentRoleDiscoveredEvent) SchoolDataEnvironmentRoleRemovedEvent(fi.otavanopisto.muikku.schooldata.events.SchoolDataEnvironmentRoleRemovedEvent) WorkspaceRole(fi.otavanopisto.muikku.schooldata.entity.WorkspaceRole) RoleSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.RoleSchoolDataIdentifier) SchoolDataWorkspaceRoleRemovedEvent(fi.otavanopisto.muikku.schooldata.events.SchoolDataWorkspaceRoleRemovedEvent) EnvironmentRole(fi.otavanopisto.muikku.schooldata.entity.EnvironmentRole) WorkspaceRoleEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceRoleEntity)

Example 3 with UserRole

use of fi.otavanopisto.pyramus.rest.model.UserRole in project muikku by otavanopisto.

the class PyramusUpdater method updatePerson.

@Lock(LockType.WRITE)
@AccessTimeout(value = 30000)
public void updatePerson(Person person) {
    Long userEntityId = null;
    String defaultIdentifier = null;
    Long defaultUserId = person.getDefaultUserId();
    UserRole defaultUserPyramusRole = null;
    List<String> identifiers = new ArrayList<>();
    List<String> removedIdentifiers = new ArrayList<>();
    List<String> updatedIdentifiers = new ArrayList<>();
    List<String> discoveredIdentifiers = new ArrayList<>();
    Map<SchoolDataIdentifier, List<String>> emails = new HashMap<>();
    // List all person's students and staffMembers
    Student[] students = pyramusClient.get().get(String.format("/persons/persons/%d/students", person.getId()), Student[].class);
    StaffMember[] staffMembers = pyramusClient.get().get(String.format("/persons/persons/%d/staffMembers", person.getId()), StaffMember[].class);
    // If person does not have a defaultUserId specified, we try to guess something
    if (defaultUserId == null) {
        if ((staffMembers != null) && (staffMembers.length > 0)) {
            // If person has a staffMember instance, lets use that one
            defaultUserId = staffMembers[0].getId();
        } else {
            if (students != null) {
                // Otherwise just use first non archived student (if any)
                for (Student student : students) {
                    if (!student.getArchived()) {
                        defaultUserId = student.getId();
                        break;
                    }
                }
            }
        }
    }
    if (students != null) {
        // Iterate over all student instances
        for (Student student : students) {
            String identifier = identifierMapper.getStudentIdentifier(student.getId());
            SchoolDataIdentifier schoolDataIdentifier = toIdentifier(identifier);
            List<String> identifierEmails = new ArrayList<String>();
            if (!student.getArchived()) {
                // If student is not archived, add it to identifiers list
                identifiers.add(identifier);
                // If it's the specified defaultUserId, update defaultIdentifier and role accordingly
                if ((defaultIdentifier == null) && student.getId().equals(defaultUserId)) {
                    defaultIdentifier = identifier;
                    defaultUserPyramusRole = UserRole.STUDENT;
                }
                // List emails and add all emails that are not specified non unique (e.g. contact persons) to the emails list
                Email[] studentEmails = pyramusClient.get().get("/students/students/" + student.getId() + "/emails", Email[].class);
                if (studentEmails != null) {
                    for (Email studentEmail : studentEmails) {
                        if (studentEmail.getContactTypeId() != null) {
                            ContactType contactType = pyramusClient.get().get("/common/contactTypes/" + studentEmail.getContactTypeId(), ContactType.class);
                            if (!contactType.getNonUnique() && !identifierEmails.contains(studentEmail.getAddress())) {
                                identifierEmails.add(studentEmail.getAddress());
                            }
                        } else {
                            logger.log(Level.WARNING, "ContactType of email is null - email is ignored");
                        }
                    }
                }
            } else {
                // If the student instance if archived, we add it the the removed identifiers list
                removedIdentifiers.add(identifier);
            }
            emails.put(schoolDataIdentifier, identifierEmails);
        }
    }
    if (staffMembers != null) {
        for (StaffMember staffMember : staffMembers) {
            // Add staffMember identifier into the identifier list
            String identifier = identifierMapper.getStaffIdentifier(staffMember.getId());
            SchoolDataIdentifier schoolDataIdentifier = toIdentifier(identifier);
            List<String> identifierEmails = new ArrayList<String>();
            identifiers.add(identifier);
            // If it's the specified defaultUserId, update defaultIdentifier and role accordingly
            if ((defaultIdentifier == null) && staffMember.getId().equals(defaultUserId)) {
                defaultIdentifier = identifier;
                defaultUserPyramusRole = staffMember.getRole();
            }
            // List emails and add all emails that are not specified non unique (e.g. contact persons) to the emails list
            Email[] staffMemberEmails = pyramusClient.get().get("/staff/members/" + staffMember.getId() + "/emails", Email[].class);
            if (staffMemberEmails != null) {
                for (Email staffMemberEmail : staffMemberEmails) {
                    if (staffMemberEmail.getContactTypeId() != null) {
                        ContactType contactType = pyramusClient.get().get("/common/contactTypes/" + staffMemberEmail.getContactTypeId(), ContactType.class);
                        if (!contactType.getNonUnique() && !identifierEmails.contains(staffMemberEmail.getAddress())) {
                            identifierEmails.add(staffMemberEmail.getAddress());
                        }
                    } else {
                        logger.log(Level.WARNING, "ContactType of email is null - email is ignored");
                    }
                }
            }
            emails.put(schoolDataIdentifier, identifierEmails);
        }
    }
    // Iterate over all discovered identifiers (students and staff members)
    for (String identifier : identifiers) {
        UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifier(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, identifier);
        if (userSchoolDataIdentifier == null) {
            // If no user entity can be found by the identifier, add it the the discovered identities list
            discoveredIdentifiers.add(identifier);
        } else {
            // user entity found with given identity, so we need to make sure they all belong to same user
            UserEntity userEntity = userSchoolDataIdentifier.getUserEntity();
            if (userEntityId == null) {
                userEntityId = userEntity.getId();
            } else if (!userEntityId.equals(userEntity.getId())) {
                logger.warning(String.format("Person %d synchronization failed. Found two userEntitys bound to it (%d and %d)", person.getId(), userEntityId, userEntity.getId()));
                return;
            }
        }
    }
    UserEntity userEntity = userEntityId != null ? userEntityController.findUserEntityById(userEntityId) : null;
    if (userEntity != null) {
        // User already exists in the system so we check which of the identifiers have been removed and which just updated
        List<UserSchoolDataIdentifier> existingSchoolDataIdentifiers = userSchoolDataIdentifierController.listUserSchoolDataIdentifiersByUserEntity(userEntity);
        for (UserSchoolDataIdentifier existingSchoolDataIdentifier : existingSchoolDataIdentifiers) {
            if (existingSchoolDataIdentifier.getDataSource().getIdentifier().equals(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE)) {
                if (!identifiers.contains(existingSchoolDataIdentifier.getIdentifier())) {
                    if (!removedIdentifiers.contains(existingSchoolDataIdentifier.getIdentifier())) {
                        removedIdentifiers.add(existingSchoolDataIdentifier.getIdentifier());
                    }
                } else if (!discoveredIdentifiers.contains(existingSchoolDataIdentifier.getIdentifier())) {
                    updatedIdentifiers.add(existingSchoolDataIdentifier.getIdentifier());
                }
            }
        }
    }
    // Resolve the user's desired environment role
    SchoolDataIdentifier environmentRoleIdentifier = null;
    if (defaultUserPyramusRole != null) {
        String roleIdentifier = identifierMapper.getEnvironmentRoleIdentifier(defaultUserPyramusRole);
        environmentRoleIdentifier = new SchoolDataIdentifier(roleIdentifier, SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE);
    }
    // And finally fire the update event
    fireSchoolDataUserUpdated(userEntityId, defaultIdentifier, removedIdentifiers, updatedIdentifiers, discoveredIdentifiers, emails, environmentRoleIdentifier);
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) RoleSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.RoleSchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) Email(fi.otavanopisto.pyramus.rest.model.Email) ContactType(fi.otavanopisto.pyramus.rest.model.ContactType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Student(fi.otavanopisto.pyramus.rest.model.Student) StudentGroupStudent(fi.otavanopisto.pyramus.rest.model.StudentGroupStudent) CourseStudent(fi.otavanopisto.pyramus.rest.model.CourseStudent) CourseStaffMember(fi.otavanopisto.pyramus.rest.model.CourseStaffMember) StaffMember(fi.otavanopisto.pyramus.rest.model.StaffMember) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) UserGroupUserEntity(fi.otavanopisto.muikku.model.users.UserGroupUserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) UserRole(fi.otavanopisto.pyramus.rest.model.UserRole) List(java.util.List) ArrayList(java.util.ArrayList) AccessTimeout(javax.ejb.AccessTimeout) Lock(javax.ejb.Lock)

Aggregations

UserRole (fi.otavanopisto.pyramus.rest.model.UserRole)3 RoleSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.RoleSchoolDataIdentifier)2 CourseStaffMemberRole (fi.otavanopisto.pyramus.rest.model.CourseStaffMemberRole)2 HashMap (java.util.HashMap)2 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)1 UserGroupUserEntity (fi.otavanopisto.muikku.model.users.UserGroupUserEntity)1 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)1 WorkspaceRoleEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceRoleEntity)1 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)1 SchoolDataBridgeInternalException (fi.otavanopisto.muikku.schooldata.SchoolDataBridgeInternalException)1 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)1 EnvironmentRole (fi.otavanopisto.muikku.schooldata.entity.EnvironmentRole)1 WorkspaceRole (fi.otavanopisto.muikku.schooldata.entity.WorkspaceRole)1 SchoolDataEnvironmentRoleDiscoveredEvent (fi.otavanopisto.muikku.schooldata.events.SchoolDataEnvironmentRoleDiscoveredEvent)1 SchoolDataEnvironmentRoleRemovedEvent (fi.otavanopisto.muikku.schooldata.events.SchoolDataEnvironmentRoleRemovedEvent)1 SchoolDataWorkspaceRoleDiscoveredEvent (fi.otavanopisto.muikku.schooldata.events.SchoolDataWorkspaceRoleDiscoveredEvent)1 SchoolDataWorkspaceRoleRemovedEvent (fi.otavanopisto.muikku.schooldata.events.SchoolDataWorkspaceRoleRemovedEvent)1 ContactType (fi.otavanopisto.pyramus.rest.model.ContactType)1 CourseStaffMember (fi.otavanopisto.pyramus.rest.model.CourseStaffMember)1 CourseStudent (fi.otavanopisto.pyramus.rest.model.CourseStudent)1