Search in sources :

Example 36 with WorkspaceUserEntity

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

the class CommunicatorController method createMessage.

public CommunicatorMessage createMessage(CommunicatorMessageId communicatorMessageId, UserEntity sender, List<UserEntity> userRecipients, List<UserGroupEntity> userGroupRecipients, List<WorkspaceEntity> workspaceStudentRecipients, List<WorkspaceEntity> workspaceTeacherRecipients, CommunicatorMessageCategory category, String caption, String content, Set<Tag> tags) {
    CommunicatorMessage message = communicatorMessageDAO.create(communicatorMessageId, sender.getId(), category, caption, clean(content), new Date(), tags);
    // Clean duplicates from recipient list
    cleanDuplicateRecipients(userRecipients);
    Set<Long> recipientIds = new HashSet<Long>();
    for (UserEntity recipient : userRecipients) {
        // #3758: Only send messages to active users
        if (!isActiveUser(recipient)) {
            continue;
        }
        if (!recipientIds.contains(recipient.getId())) {
            recipientIds.add(recipient.getId());
            communicatorMessageRecipientDAO.create(message, recipient, null);
        }
    }
    if (!CollectionUtils.isEmpty(userGroupRecipients)) {
        for (UserGroupEntity userGroup : userGroupRecipients) {
            List<UserGroupUserEntity> groupUsers = userGroupEntityController.listUserGroupUserEntitiesByUserGroupEntity(userGroup);
            if (!CollectionUtils.isEmpty(groupUsers)) {
                CommunicatorMessageRecipientUserGroup groupRecipient = createUserGroupRecipient(userGroup);
                for (UserGroupUserEntity groupUser : groupUsers) {
                    UserSchoolDataIdentifier userSchoolDataIdentifier = groupUser.getUserSchoolDataIdentifier();
                    UserEntity recipient = userSchoolDataIdentifier.getUserEntity();
                    // #3758: Only send messages to active users
                    if (!isActiveUser(recipient)) {
                        continue;
                    }
                    if ((recipient != null) && !Objects.equals(sender.getId(), recipient.getId())) {
                        if (!recipientIds.contains(recipient.getId())) {
                            recipientIds.add(recipient.getId());
                            communicatorMessageRecipientDAO.create(message, recipient, groupRecipient);
                        }
                    }
                }
            }
        }
    }
    if (!CollectionUtils.isEmpty(workspaceStudentRecipients)) {
        for (WorkspaceEntity workspaceEntity : workspaceStudentRecipients) {
            List<WorkspaceUserEntity> workspaceUsers = workspaceUserEntityController.listActiveWorkspaceStudents(workspaceEntity);
            if (!CollectionUtils.isEmpty(workspaceUsers)) {
                CommunicatorMessageRecipientWorkspaceGroup groupRecipient = createWorkspaceGroupRecipient(workspaceEntity, WorkspaceRoleArchetype.STUDENT);
                for (WorkspaceUserEntity workspaceUserEntity : workspaceUsers) {
                    UserEntity recipient = workspaceUserEntity.getUserSchoolDataIdentifier().getUserEntity();
                    // #3758: Only send messages to active users
                    if (!isActiveUser(recipient)) {
                        continue;
                    }
                    if ((recipient != null) && !Objects.equals(sender.getId(), recipient.getId())) {
                        if (!recipientIds.contains(recipient.getId())) {
                            recipientIds.add(recipient.getId());
                            communicatorMessageRecipientDAO.create(message, recipient, groupRecipient);
                        }
                    }
                }
            }
        }
    }
    if (!CollectionUtils.isEmpty(workspaceTeacherRecipients)) {
        for (WorkspaceEntity workspaceEntity : workspaceTeacherRecipients) {
            List<WorkspaceUserEntity> workspaceUsers = workspaceUserEntityController.listActiveWorkspaceStaffMembers(workspaceEntity);
            if (!CollectionUtils.isEmpty(workspaceUsers)) {
                CommunicatorMessageRecipientWorkspaceGroup groupRecipient = createWorkspaceGroupRecipient(workspaceEntity, WorkspaceRoleArchetype.TEACHER);
                for (WorkspaceUserEntity wosu : workspaceUsers) {
                    UserEntity recipient = wosu.getUserSchoolDataIdentifier().getUserEntity();
                    // #3758: Workspace teachers are considered active, no need to check
                    if ((recipient != null) && !Objects.equals(sender.getId(), recipient.getId())) {
                        if (!recipientIds.contains(recipient.getId())) {
                            recipientIds.add(recipient.getId());
                            communicatorMessageRecipientDAO.create(message, recipient, groupRecipient);
                        }
                    }
                }
            }
        }
    }
    return message;
}
Also used : UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) CommunicatorMessage(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage) UserGroupUserEntity(fi.otavanopisto.muikku.model.users.UserGroupUserEntity) UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity) Date(java.util.Date) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) UserGroupUserEntity(fi.otavanopisto.muikku.model.users.UserGroupUserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) CommunicatorMessageRecipientUserGroup(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageRecipientUserGroup) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) CommunicatorMessageRecipientWorkspaceGroup(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageRecipientWorkspaceGroup) HashSet(java.util.HashSet)

Example 37 with WorkspaceUserEntity

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

the class DefaultSchoolDataWorkspaceListener method onSchoolDataWorkspaceUserDiscoveredEvent.

public synchronized void onSchoolDataWorkspaceUserDiscoveredEvent(@Observes SchoolDataWorkspaceUserDiscoveredEvent event) {
    String discoverId = "WSU-" + event.getDataSource() + "/" + event.getIdentifier();
    if (discoveredWorkspaceUsers.containsKey(discoverId)) {
        event.setDiscoveredWorkspaceUserEntityId(discoveredWorkspaceUsers.get(discoverId));
        return;
    }
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceByDataSourceAndIdentifier(event.getWorkspaceDataSource(), event.getWorkspaceIdentifier());
    if (workspaceEntity != null) {
        WorkspaceRoleEntity workspaceUserRole = workspaceController.findWorkspaceRoleEntityByDataSourceAndIdentifier(event.getRoleDataSource(), event.getRoleIdentifier());
        if (workspaceUserRole != null) {
            UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifier(event.getUserDataSource(), event.getUserIdentifier());
            if (userSchoolDataIdentifier != null) {
                WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceAndUserSchoolDataIdentifierIncludeArchived(workspaceEntity, userSchoolDataIdentifier);
                if (workspaceUserEntity == null) {
                    workspaceUserEntity = workspaceUserEntityController.createWorkspaceUserEntity(userSchoolDataIdentifier, workspaceEntity, event.getIdentifier(), workspaceUserRole);
                    discoveredWorkspaceUsers.put(discoverId, workspaceUserEntity.getId());
                    event.setDiscoveredWorkspaceUserEntityId(workspaceUserEntity.getId());
                } else {
                    if (!workspaceUserEntity.getIdentifier().equals(event.getIdentifier())) {
                        workspaceUserEntityController.updateIdentifier(workspaceUserEntity, event.getIdentifier());
                    }
                    // #3806: WorkspaceUserEntity exists; check if role has changed
                    if (workspaceUserEntity.getWorkspaceUserRole() == null || !workspaceUserRole.getId().equals(workspaceUserEntity.getWorkspaceUserRole().getId())) {
                        workspaceUserEntityController.updateWorkspaceUserRole(workspaceUserEntity, workspaceUserRole);
                    }
                    workspaceUserEntityController.unarchiveWorkspaceUserEntity(workspaceUserEntity);
                }
                if (!workspaceUserEntity.getActive().equals(event.getIsActive())) {
                    workspaceUserEntityController.updateActive(workspaceUserEntity, event.getIsActive());
                }
            } else {
                logger.warning("could not add workspace user because userSchoolDataIdentifier #" + event.getUserIdentifier() + '/' + event.getUserDataSource() + " could not be found");
            }
        } else {
            logger.warning("could not init workspace user because workspace role #" + event.getRoleIdentifier() + '/' + event.getRoleDataSource() + " could not be found");
        }
    } else {
        logger.warning("could not init workspace user because workspace entity #" + event.getWorkspaceIdentifier() + '/' + event.getWorkspaceDataSource() + " could not be found");
    }
}
Also used : 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)

Example 38 with WorkspaceUserEntity

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

the class DefaultSchoolDataWorkspaceListener method onSchoolDataWorkspaceUserUpdatedEvent.

public void onSchoolDataWorkspaceUserUpdatedEvent(@Observes SchoolDataWorkspaceUserUpdatedEvent event) {
    UserEntity userEntity = userEntityController.findUserEntityByDataSourceAndIdentifier(event.getUserDataSource(), event.getUserIdentifier());
    if (userEntity != null) {
        WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceByDataSourceAndIdentifier(event.getWorkspaceDataSource(), event.getWorkspaceIdentifier());
        if (workspaceEntity != null) {
            SchoolDataIdentifier workspaceUserIdentifier = new SchoolDataIdentifier(event.getIdentifier(), event.getDataSource());
            WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceUserIdentifierIncludeArchived(workspaceUserIdentifier);
            if (workspaceUserEntity != null) {
                String currentUserIdentifier = workspaceUserEntity.getUserSchoolDataIdentifier().getIdentifier();
                if (!StringUtils.equals(currentUserIdentifier, event.getUserIdentifier())) {
                    // WorkspaceUserEntity found, but it points to a new study program
                    UserSchoolDataIdentifier newUserIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifier(event.getUserDataSource(), event.getUserIdentifier());
                    if (newUserIdentifier == null) {
                        logger.warning(String.format("Unable to update workspace user. UserSchoolDataIdentifier for %s/%s not found", event.getUserDataSource(), event.getUserIdentifier()));
                    } else {
                        WorkspaceUserEntity existingUser = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceAndUserSchoolDataIdentifierIncludeArchived(workspaceEntity, newUserIdentifier);
                        if (existingUser != null) {
                            if (!existingUser.getArchived().equals(workspaceUserEntity.getArchived())) {
                                if (existingUser.getArchived()) {
                                    workspaceUserEntityController.unarchiveWorkspaceUserEntity(existingUser);
                                } else {
                                    workspaceUserEntityController.archiveWorkspaceUserEntity(existingUser);
                                }
                            }
                            workspaceUserEntityController.updateIdentifier(existingUser, workspaceUserEntity.getIdentifier());
                            // #3308: If the new study program is active, reactivate the corresponding workspace student in Muikku
                            if (event.getIsActive() && !existingUser.getActive()) {
                                workspaceUserEntityController.updateActive(existingUser, event.getIsActive());
                            }
                            workspaceUserEntityController.archiveWorkspaceUserEntity(workspaceUserEntity);
                        } else {
                            workspaceUserEntityController.updateUserSchoolDataIdentifier(workspaceUserEntity, newUserIdentifier);
                            // #3308: If the new study program is active, reactivate the corresponding workspace student in Muikku
                            if (event.getIsActive() && !workspaceUserEntity.getActive()) {
                                workspaceUserEntityController.updateActive(workspaceUserEntity, event.getIsActive());
                            }
                        }
                    }
                } else {
                    WorkspaceRoleEntity workspaceRoleEntity = workspaceController.findWorkspaceRoleEntityByDataSourceAndIdentifier(event.getRoleDataSource(), event.getRoleIdentifier());
                    if (workspaceRoleEntity != null && !workspaceRoleEntity.getId().equals(workspaceUserEntity.getWorkspaceUserRole().getId())) {
                        workspaceUserEntityController.updateWorkspaceUserRole(workspaceUserEntity, workspaceRoleEntity);
                    }
                }
                if (!event.getIsActive() && workspaceUserEntity.getActive()) {
                    workspaceUserEntityController.updateActive(workspaceUserEntity, event.getIsActive());
                }
            }
        } else {
            logger.warning("could not update workspace user because workspace entity #" + event.getWorkspaceIdentifier() + '/' + event.getWorkspaceDataSource() + " could not be found");
        }
    } else {
        logger.warning("could not update workspace user because user entity #" + event.getUserIdentifier() + '/' + event.getUserDataSource() + " could not be found");
    }
}
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) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity)

Example 39 with WorkspaceUserEntity

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

the class WorkspaceUserEntityController method listWorkspaceEntitiesByUserIdentifier.

public List<WorkspaceEntity> listWorkspaceEntitiesByUserIdentifier(SchoolDataIdentifier userIdentifier) {
    List<WorkspaceEntity> result = new ArrayList<>();
    List<WorkspaceUserEntity> workspaceUserEntities = listWorkspaceUserEntitiesByUserIdentifier(userIdentifier);
    for (WorkspaceUserEntity workspaceUserEntity : workspaceUserEntities) {
        result.add(workspaceUserEntity.getWorkspaceEntity());
    }
    return result;
}
Also used : WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) ArrayList(java.util.ArrayList)

Example 40 with WorkspaceUserEntity

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

the class WorkspaceUserEntityController method listActiveWorkspaceEntitiesByUserIdentifier.

public List<WorkspaceEntity> listActiveWorkspaceEntitiesByUserIdentifier(SchoolDataIdentifier userIdentifier) {
    List<WorkspaceEntity> result = new ArrayList<>();
    List<WorkspaceUserEntity> workspaceUserEntities = listActiveWorkspaceUserEntitiesByUserIdentifier(userIdentifier);
    for (WorkspaceUserEntity workspaceUserEntity : workspaceUserEntities) {
        result.add(workspaceUserEntity.getWorkspaceEntity());
    }
    return result;
}
Also used : WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) ArrayList(java.util.ArrayList)

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