Search in sources :

Example 1 with SchoolDataEnvironmentRoleRemovedEvent

use of fi.otavanopisto.muikku.schooldata.events.SchoolDataEnvironmentRoleRemovedEvent 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)

Aggregations

RoleSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.RoleSchoolDataIdentifier)1 WorkspaceRoleEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceRoleEntity)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 CourseStaffMemberRole (fi.otavanopisto.pyramus.rest.model.CourseStaffMemberRole)1 UserRole (fi.otavanopisto.pyramus.rest.model.UserRole)1 HashMap (java.util.HashMap)1