Search in sources :

Example 11 with UserGroupEntity

use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.

the class NotificationController method sendNotification.

public void sendNotification(String category, String subject, String content, UserEntity recipient, SchoolDataIdentifier recipientIdentifier, String notificationType) {
    HashMap<String, Object> map = new HashMap<>();
    map.put("notificationType", notificationType);
    map.put("recipient", recipient.getId());
    map.put("recipientIdentifier", recipientIdentifier.toId());
    map.put("time", String.valueOf(System.currentTimeMillis()));
    UserEntity guidanceCounselor = null;
    SchoolDataIdentifier userIdentifier = new SchoolDataIdentifier(recipient.getDefaultIdentifier(), recipient.getDefaultSchoolDataSource().getIdentifier());
    List<UserGroupEntity> userGroupEntities = userGroupEntityController.listUserGroupsByUserIdentifier(userIdentifier);
    // #3089: An awkward workaround to use the latest guidance group based on its identifier. Assumes a larger
    // identifier means a more recent entity. A more proper fix would be to sync group creation dates from
    // Pyramus and include them in the Elastic index. Then again, user groups would have to be refactored
    // entirely, as Pyramus handles group members as students (one study programme) while Muikku handles
    // them as user entities (all study programmes)...
    userGroupEntities.sort(new Comparator<UserGroupEntity>() {

        public int compare(UserGroupEntity o1, UserGroupEntity o2) {
            long l1 = NumberUtils.toLong(StringUtils.substringAfterLast(o1.getIdentifier(), "-"), -1);
            long l2 = NumberUtils.toLong(StringUtils.substringAfterLast(o2.getIdentifier(), "-"), -1);
            return (int) (l2 - l1);
        }
    });
    userGroupEntities: for (UserGroupEntity userGroupEntity : userGroupEntities) {
        UserGroup userGroup = userGroupController.findUserGroup(userGroupEntity);
        if (userGroup.isGuidanceGroup()) {
            List<GroupUser> groupUsers = userGroupController.listUserGroupStaffMembers(userGroup);
            for (GroupUser groupUser : groupUsers) {
                User user = userGroupController.findUserByGroupUser(groupUser);
                guidanceCounselor = userEntityController.findUserEntityByUser(user);
                break userGroupEntities;
            }
        }
    }
    LogProvider provider = getProvider(LOG_PROVIDER);
    if (provider != null) {
        provider.log(COLLECTION_NAME, map);
    }
    if (isDryRun()) {
        String recipientEmail = getRecipientEmail();
        if (recipientEmail == null) {
            logger.log(Level.INFO, String.format("Sending notification %s - %s to %s", category, subject, recipient.getDefaultIdentifier()));
        } else {
            mailer.sendMail(MailType.HTML, Arrays.asList(recipientEmail), subject, "SENT TO: " + recipient.getDefaultIdentifier() + "<br/><br/><br/>" + content);
        }
    } else {
        ArrayList<UserEntity> recipients = new ArrayList<>();
        recipients.add(recipient);
        if (guidanceCounselor != null) {
            recipients.add(guidanceCounselor);
        }
        String studentEmail = userEmailEntityController.getUserDefaultEmailAddress(recipient, Boolean.FALSE);
        if (studentEmail != null) {
            mailer.sendMail(MailType.HTML, Arrays.asList(studentEmail), subject, content);
        } else {
            logger.log(Level.WARNING, String.format("Cannot send email notification to student %s because no email address was found", recipient.getDefaultIdentifier()));
        }
        communicatorController.postMessage(recipient, category, subject, content, recipients);
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) User(fi.otavanopisto.muikku.schooldata.entity.User) GroupUser(fi.otavanopisto.muikku.schooldata.entity.GroupUser) GroupUser(fi.otavanopisto.muikku.schooldata.entity.GroupUser) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) UserGroup(fi.otavanopisto.muikku.schooldata.entity.UserGroup) LogProvider(fi.otavanopisto.muikku.plugins.commonlog.LogProvider) ArrayList(java.util.ArrayList) List(java.util.List)

Example 12 with UserGroupEntity

use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.

the class VopsLister method processCourse.

private VopsRESTModel.VopsEntry processCourse(Subject subject, int courseNumber) {
    VopsRESTModel.VopsEntry transferCreditEntry = processTransferCredits(subject, courseNumber);
    if (transferCreditEntry != null) {
        return transferCreditEntry;
    }
    List<VopsWorkspace> workspaces = vopsController.listWorkspaceIdentifiersBySubjectIdentifierAndCourseNumber(subject.getSchoolDataSource(), subject.getIdentifier(), courseNumber);
    List<WorkspaceAssessment> workspaceAssessments = new ArrayList<>();
    if (curriculumIdentifier != null) {
        workspaces.removeIf((VopsWorkspace workspace) -> !workspace.getCurriculumIdentifiers().contains(curriculumIdentifier));
    }
    if (!workspaces.isEmpty()) {
        SchoolDataIdentifier educationSubtypeIdentifier = null;
        boolean workspaceUserExists = false;
        String name = "";
        String description = "";
        boolean canSignUp = false;
        for (VopsWorkspace workspace : workspaces) {
            WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspace.getWorkspaceIdentifier());
            WorkspaceUserEntity workspaceUser = workspaceUserEntityController.findWorkspaceUserByWorkspaceEntityAndUserIdentifier(workspaceEntity, studentIdentifier);
            WorkspaceAssessment workspaceAssesment = studentAssessments.get(workspace.getWorkspaceIdentifier());
            List<UserGroupEntity> userGroupEntities = userGroupEntityController.listUserGroupsByUserIdentifier(studentIdentifier);
            Permission permission = permissionController.findByName(MuikkuPermissions.WORKSPACE_SIGNUP);
            for (UserGroupEntity userGroupEntity : userGroupEntities) {
                if (permissionController.hasWorkspaceGroupPermission(workspaceEntity, userGroupEntity, permission)) {
                    canSignUp = true;
                    break;
                }
            }
            if (workspaceAssesment != null) {
                workspaceAssessments.add(workspaceAssesment);
            }
            if (workspaceUser != null) {
                workspaceUserExists = true;
            }
        }
        for (VopsWorkspace workspace : workspaces) {
            name = workspace.getName();
            if (name != null) {
                break;
            }
        }
        for (VopsWorkspace workspace : workspaces) {
            description = workspace.getDescription();
            if (description != null) {
                break;
            }
        }
        for (VopsWorkspace workspace : workspaces) {
            educationSubtypeIdentifier = workspace.getEducationSubtypeIdentifier();
            if (educationSubtypeIdentifier != null) {
                break;
            }
        }
        Mandatority mandatority = educationTypeMapping.getMandatority(educationSubtypeIdentifier);
        CourseCompletionState state = CourseCompletionState.NOT_ENROLLED;
        String grade = null;
        if (workspaceUserExists) {
            state = CourseCompletionState.ENROLLED;
        }
        for (WorkspaceAssessment workspaceAssessment : workspaceAssessments) {
            if (!Boolean.TRUE.equals(workspaceAssessment.getPassing())) {
                state = CourseCompletionState.FAILED;
                break;
            }
        }
        for (WorkspaceAssessment workspaceAssessment : workspaceAssessments) {
            if (Boolean.TRUE.equals(workspaceAssessment.getPassing())) {
                state = CourseCompletionState.ASSESSED;
                numCourses++;
                if (mandatority == Mandatority.MANDATORY) {
                    numMandatoryCourses++;
                }
                SchoolDataIdentifier gradingScaleIdentifier = workspaceAssessment.getGradingScaleIdentifier();
                if (gradingScaleIdentifier == null) {
                    break;
                }
                SchoolDataIdentifier gradeIdentifier = workspaceAssessment.getGradeIdentifier();
                if (gradeIdentifier == null) {
                    break;
                }
                GradingScaleItem gradingScaleItem = findGradingScaleItemCached(gradingScaleIdentifier, gradeIdentifier);
                String gradeName = gradingScaleItem.getName();
                if (!StringUtils.isBlank(gradeName)) {
                    if (gradeName.length() > 2)
                        grade = gradeName.substring(0, 2);
                    else
                        grade = gradeName;
                }
                break;
            }
        }
        if (state == CourseCompletionState.NOT_ENROLLED && !canSignUp) {
            return new VopsRESTModel.VopsPlaceholder();
        }
        StudiesViewCourseChoice courseChoice = studiesViewCourseChoiceController.find(new SchoolDataIdentifier(subject.getIdentifier(), subject.getSchoolDataSource()).toId(), courseNumber, studentIdentifierString);
        if (state == CourseCompletionState.NOT_ENROLLED && courseChoice != null) {
            state = CourseCompletionState.PLANNED;
        }
        return new VopsRESTModel.VopsItem(courseNumber, state, educationSubtypeIdentifier != null ? educationSubtypeIdentifier.toId() : null, mandatority, grade, workspaceUserExists, clean(name), clean(description));
    }
    return new VopsRESTModel.VopsPlaceholder();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) WorkspaceAssessment(fi.otavanopisto.muikku.schooldata.entity.WorkspaceAssessment) GradingScaleItem(fi.otavanopisto.muikku.schooldata.entity.GradingScaleItem) ArrayList(java.util.ArrayList) UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity) StudiesViewCourseChoice(fi.otavanopisto.muikku.plugins.transcriptofrecords.model.StudiesViewCourseChoice) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) Mandatority(fi.otavanopisto.muikku.plugins.transcriptofrecords.rest.Mandatority) Permission(fi.otavanopisto.muikku.model.security.Permission) VopsRESTModel(fi.otavanopisto.muikku.plugins.transcriptofrecords.rest.VopsRESTModel) CourseCompletionState(fi.otavanopisto.muikku.plugins.transcriptofrecords.rest.CourseCompletionState)

Example 13 with UserGroupEntity

use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.

the class WorkspacePermissionsManagementBackingBean method hasUserGroupPermission.

public boolean hasUserGroupPermission(UserGroupBean userGroup, Permission permission) {
    UserGroupEntity userGroupEntity = userGroupEntityController.findUserGroupEntityById(userGroup.getId());
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(getWorkspaceEntityId());
    return permissionController.hasWorkspaceGroupPermission(workspaceEntity, userGroupEntity, permission);
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity)

Example 14 with UserGroupEntity

use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.

the class DefaultPermissionResolver method hasWorkspaceAccess.

private boolean hasWorkspaceAccess(WorkspaceEntity workspaceEntity, UserEntity userEntity, Permission permission) {
    // Workspace access as an individual
    WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findActiveWorkspaceUserByWorkspaceEntityAndUserEntity(workspaceEntity, userEntity);
    if (workspaceUserEntity != null) {
        if (permissionController.hasPermission(workspaceUserEntity.getWorkspaceUserRole(), permission)) {
            // TODO Override rules for workspace users
            return true;
        }
    }
    // Workspace access as a group member
    List<UserGroupEntity> userGroups = userGroupEntityController.listUserGroupsByUserEntity(userEntity);
    for (UserGroupEntity userGroup : userGroups) {
        // TODO Override rules for user groups
        if (permissionController.hasPermission(workspaceEntity, userGroup, permission)) {
            return true;
        }
    }
    return false;
}
Also used : WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity)

Example 15 with UserGroupEntity

use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.

the class PyramusSchoolDataUserListener method handlePyramusUserUpdated.

private void handlePyramusUserUpdated(SchoolDataIdentifier identifier) {
    Long pyramusStudentId = identifierMapper.getPyramusStudentId(identifier.getIdentifier());
    if (pyramusStudentId != null) {
        Student student = pyramusClient.get().get(String.format("/students/students/%d", pyramusStudentId), Student.class);
        if (student != null) {
            Long pyramusStudyProgrammeId = student.getStudyProgrammeId();
            if (pyramusStudyProgrammeId != null) {
                String userGroupIdentifier = identifierMapper.getStudyProgrammeIdentifier(pyramusStudyProgrammeId);
                boolean found = false;
                boolean isActive = !student.getArchived() && (student.getStudyEndDate() == null || student.getStudyEndDate().isAfter(OffsetDateTime.now()));
                if (!isActive) {
                    schoolDataUserInactiveEvent.fire(new SchoolDataUserInactiveEvent(identifier.getDataSource(), identifier.getIdentifier()));
                }
                // Remove StudyProgrammeGroups
                UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifier(identifier.getDataSource(), identifier.getIdentifier());
                List<UserGroupUserEntity> userGroupUsers = userGroupEntityController.listUserGroupUsersByUserSchoolDataIdentifier(userSchoolDataIdentifier);
                for (UserGroupUserEntity userGroupUser : userGroupUsers) {
                    UserGroupEntity userGroup = userGroupUser.getUserGroupEntity();
                    StudentGroupType studentGroupType = identifierMapper.getStudentGroupType(userGroup.getIdentifier());
                    if (studentGroupType == StudentGroupType.STUDYPROGRAMME) {
                        boolean archived = Boolean.TRUE.equals(userGroupUser.getArchived());
                        if (!archived && !isActive) {
                            fireUserGroupUserRemoved(userGroupUser.getIdentifier(), userGroup.getIdentifier(), identifier.getIdentifier());
                        } else {
                            found = !archived;
                        }
                    }
                }
                if (!found && isActive) {
                    String userGroupUserIdentifier = identifierMapper.getStudyProgrammeStudentIdentifier(pyramusStudentId);
                    fireUserGroupUserDiscovered(userGroupUserIdentifier, userGroupIdentifier, identifier.getIdentifier());
                }
            }
        }
    }
}
Also used : SchoolDataUserInactiveEvent(fi.otavanopisto.muikku.schooldata.events.SchoolDataUserInactiveEvent) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) UserGroupUserEntity(fi.otavanopisto.muikku.model.users.UserGroupUserEntity) UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity) Student(fi.otavanopisto.pyramus.rest.model.Student) StudentGroupType(fi.otavanopisto.muikku.plugins.schooldatapyramus.PyramusIdentifierMapper.StudentGroupType)

Aggregations

UserGroupEntity (fi.otavanopisto.muikku.model.users.UserGroupEntity)40 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)14 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)12 UserGroupUserEntity (fi.otavanopisto.muikku.model.users.UserGroupUserEntity)12 Path (javax.ws.rs.Path)12 ArrayList (java.util.ArrayList)11 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)10 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)9 EntityManager (javax.persistence.EntityManager)8 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)8 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)7 UserGroup (fi.otavanopisto.muikku.schooldata.entity.UserGroup)6 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)5 HashMap (java.util.HashMap)5 POST (javax.ws.rs.POST)5 Permission (fi.otavanopisto.muikku.model.security.Permission)4 Announcement (fi.otavanopisto.muikku.plugins.announcer.model.Announcement)4 Tag (fi.otavanopisto.muikku.model.base.Tag)3 EnvironmentRoleArchetype (fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype)3 CommunicatorMessage (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage)3