Search in sources :

Example 1 with UserGroupEntity

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

the class AcceptanceTestsRESTService method deleteUserGroup.

@DELETE
@Path("/userGroups/{USERGROUPID}")
@RESTPermit(handling = Handling.UNSECURED)
public Response deleteUserGroup(@PathParam("USERGROUPID") Long userGroupId) {
    UserGroupEntity userGroup = userGroupEntityController.findUserGroupEntityById(userGroupId);
    for (UserGroupUserEntity userGroupUser : userGroupEntityController.listUserGroupUserEntitiesByUserGroupEntity(userGroup)) {
        userGroupEntityController.deleteUserGroupUserEntity(userGroupUser);
    }
    userGroupEntityController.deleteUserGroupEntity(userGroup);
    return Response.noContent().build();
}
Also used : UserGroupUserEntity(fi.otavanopisto.muikku.model.users.UserGroupUserEntity) UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RESTPermit(fi.otavanopisto.security.rest.RESTPermit)

Example 2 with UserGroupEntity

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

the class AnnouncerRESTService method updateAnnouncement.

@PUT
@Path("/announcements/{ID}")
@RESTPermit(handling = Handling.INLINE)
public Response updateAnnouncement(@PathParam("ID") Long announcementId, AnnouncementRESTModel restModel) {
    UserEntity userEntity = sessionController.getLoggedUserEntity();
    if (announcementId == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    Announcement oldAnnouncement = announcementController.findById(announcementId);
    if (oldAnnouncement == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    // Check that the user has permission to update the old announcement
    if (!canEdit(oldAnnouncement, userEntity))
        return Response.status(Status.FORBIDDEN).entity("You don't have the permission to update this announcement.").build();
    List<Long> workspaceEntityIds = restModel.getWorkspaceEntityIds();
    if (workspaceEntityIds == null) {
        workspaceEntityIds = Collections.emptyList();
    }
    List<Long> userGroupEntityIds = restModel.getUserGroupEntityIds();
    if (userGroupEntityIds == null) {
        userGroupEntityIds = Collections.emptyList();
    }
    if (workspaceEntityIds.isEmpty() && !sessionController.hasEnvironmentPermission(AnnouncerPermissions.UPDATE_ANNOUNCEMENT)) {
        return Response.status(Status.FORBIDDEN).entity("You don't have the permission to update environment announcements").build();
    }
    for (Long workspaceEntityId : workspaceEntityIds) {
        WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
        if (workspaceEntity == null) {
            return Response.status(Status.BAD_REQUEST).entity("Invalid workspaceEntityId").build();
        }
        if (!sessionController.hasWorkspacePermission(AnnouncerPermissions.UPDATE_WORKSPACE_ANNOUNCEMENT, workspaceEntity)) {
            return Response.status(Status.FORBIDDEN).entity("You don't have the permission to update workspace announcement").build();
        }
    }
    Announcement newAnnouncement = announcementController.updateAnnouncement(oldAnnouncement, restModel.getCaption(), restModel.getContent(), restModel.getStartDate(), restModel.getEndDate(), restModel.getPubliclyVisible(), restModel.isArchived());
    announcementController.clearAnnouncementTargetGroups(newAnnouncement);
    for (Long userGroupEntityId : userGroupEntityIds) {
        UserGroupEntity userGroupEntity = userGroupEntityController.findUserGroupEntityById(userGroupEntityId);
        if (userGroupEntity == null) {
            return Response.status(Status.BAD_REQUEST).entity("Invalid userGroupEntityId").build();
        }
        announcementController.addAnnouncementTargetGroup(newAnnouncement, userGroupEntity);
    }
    announcementController.clearAnnouncementWorkspaces(newAnnouncement);
    for (Long workspaceEntityId : workspaceEntityIds) {
        WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
        if (workspaceEntity == null) {
            return Response.status(Status.BAD_REQUEST).entity("Invalid workspaceEntityId").build();
        }
        announcementController.addAnnouncementWorkspace(newAnnouncement, workspaceEntity);
    }
    List<AnnouncementUserGroup> announcementUserGroups = announcementController.listAnnouncementUserGroups(newAnnouncement);
    List<AnnouncementWorkspace> announcementWorkspaces = announcementController.listAnnouncementWorkspaces(newAnnouncement);
    return Response.ok(createRESTModel(newAnnouncement, announcementUserGroups, announcementWorkspaces)).build();
}
Also used : Announcement(fi.otavanopisto.muikku.plugins.announcer.model.Announcement) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) AnnouncementUserGroup(fi.otavanopisto.muikku.plugins.announcer.model.AnnouncementUserGroup) AnnouncementWorkspace(fi.otavanopisto.muikku.plugins.announcer.workspace.model.AnnouncementWorkspace) UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) PUT(javax.ws.rs.PUT)

Example 3 with UserGroupEntity

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

the class AnnouncementDAO method listAnnouncements.

public List<Announcement> listAnnouncements(List<UserGroupEntity> userGroupEntities, List<WorkspaceEntity> workspaceEntities, AnnouncementEnvironmentRestriction environment, AnnouncementTimeFrame timeFrame, UserEntity announcementOwner, boolean archived) {
    EntityManager entityManager = getEntityManager();
    Date currentDate = onlyDateFields(new Date());
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Announcement> criteria = criteriaBuilder.createQuery(Announcement.class);
    Root<Announcement> root = criteria.from(Announcement.class);
    criteria.select(root).distinct(true);
    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(criteriaBuilder.equal(root.get(Announcement_.archived), archived));
    switch(timeFrame) {
        case ALL:
            // No restrictions here
            break;
        case CURRENTANDUPCOMING:
            predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(Announcement_.endDate), currentDate));
            break;
        case CURRENTANDEXPIRED:
            predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(Announcement_.startDate), currentDate));
            break;
        case CURRENT:
            predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(Announcement_.startDate), currentDate));
            predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(Announcement_.endDate), currentDate));
            break;
        case UPCOMING:
            predicates.add(criteriaBuilder.greaterThan(root.get(Announcement_.startDate), currentDate));
            break;
        case EXPIRED:
            predicates.add(criteriaBuilder.lessThan(root.get(Announcement_.endDate), currentDate));
            break;
    }
    if (announcementOwner != null) {
        predicates.add(criteriaBuilder.equal(root.get(Announcement_.publisherUserEntityId), announcementOwner.getId()));
    }
    // Predicates for group visibility restrictions (workspace, usergroup or environment)
    List<Predicate> groupPredicates = new ArrayList<Predicate>();
    /**
     * Environment announcements:
     * - All announcements which are not tied to a workspace
     * - Publicly visible and/or group (when requested)
     */
    switch(environment) {
        case NONE:
            // No environment announcements added
            break;
        case PUBLIC:
            groupPredicates.add(criteriaBuilder.and(criteriaBuilder.not(criteriaBuilder.in(root).value(subqueryWorkspaceAnnouncements(criteriaBuilder, criteria))), criteriaBuilder.equal(root.get(Announcement_.publiclyVisible), Boolean.TRUE)));
            break;
        case PUBLICANDGROUP:
            groupPredicates.add(criteriaBuilder.not(criteriaBuilder.in(root).value(subqueryWorkspaceAnnouncements(criteriaBuilder, criteria))));
            break;
    }
    /**
     * Workspace announcements:
     * - All announcements tied to specified workspace(s)
     */
    if (CollectionUtils.isNotEmpty(workspaceEntities)) {
        List<Long> workspaceEntityIds = workspaceEntities.stream().map((WorkspaceEntity workspaceEntity) -> workspaceEntity.getId()).collect(Collectors.toList());
        Subquery<Announcement> subquery = criteria.subquery(Announcement.class);
        Root<AnnouncementWorkspace> announcementWorkspace = subquery.from(AnnouncementWorkspace.class);
        subquery.select(announcementWorkspace.get(AnnouncementWorkspace_.announcement));
        subquery.where(criteriaBuilder.and(criteriaBuilder.equal(announcementWorkspace.get(AnnouncementWorkspace_.archived), Boolean.FALSE), announcementWorkspace.get(AnnouncementWorkspace_.workspaceEntityId).in(workspaceEntityIds)));
        groupPredicates.add(root.in(subquery));
    }
    /**
     * User group announcements:
     * - Environment announcements that are tied to user group.
     */
    if (CollectionUtils.isNotEmpty(userGroupEntities)) {
        List<Long> userGroupEntityIds = userGroupEntities.stream().map((UserGroupEntity userGroupEntity) -> userGroupEntity.getId()).collect(Collectors.toList());
        Subquery<Announcement> subquery = criteria.subquery(Announcement.class);
        Root<AnnouncementUserGroup> announcementUserGroup = subquery.from(AnnouncementUserGroup.class);
        subquery.select(announcementUserGroup.get(AnnouncementUserGroup_.announcement));
        subquery.where(criteriaBuilder.and(criteriaBuilder.equal(announcementUserGroup.get(AnnouncementUserGroup_.archived), Boolean.FALSE), announcementUserGroup.get(AnnouncementUserGroup_.userGroupEntityId).in(userGroupEntityIds)));
        groupPredicates.add(root.in(subquery));
    }
    predicates.add(criteriaBuilder.or(groupPredicates.toArray(new Predicate[0])));
    criteria.where(criteriaBuilder.and(predicates.toArray(new Predicate[0])));
    criteria.orderBy(criteriaBuilder.desc(root.get(Announcement_.startDate)));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Announcement(fi.otavanopisto.muikku.plugins.announcer.model.Announcement) AnnouncementUserGroup(fi.otavanopisto.muikku.plugins.announcer.model.AnnouncementUserGroup) ArrayList(java.util.ArrayList) UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity) Date(java.util.Date) Predicate(javax.persistence.criteria.Predicate) EntityManager(javax.persistence.EntityManager) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) AnnouncementWorkspace(fi.otavanopisto.muikku.plugins.announcer.workspace.model.AnnouncementWorkspace)

Example 4 with UserGroupEntity

use of fi.otavanopisto.muikku.model.users.UserGroupEntity 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 5 with UserGroupEntity

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

the class CommunicatorRESTModels method restUserGroupRecipient.

public fi.otavanopisto.muikku.rest.model.UserGroup restUserGroupRecipient(CommunicatorMessageRecipientUserGroup userGroup) {
    schoolDataBridgeSessionController.startSystemSession();
    try {
        UserGroupEntity entity = userGroupEntityController.findUserGroupEntityById(userGroup.getUserGroupEntityId());
        if (entity != null) {
            Long userCount = userGroupEntityController.getGroupUserCount(entity);
            UserGroup group = userGroupController.findUserGroup(entity);
            if (group != null)
                return new fi.otavanopisto.muikku.rest.model.UserGroup(entity.getId(), group.getName(), userCount);
        }
        return null;
    } finally {
        schoolDataBridgeSessionController.endSystemSession();
    }
}
Also used : UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity) CommunicatorMessageRecipientUserGroup(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageRecipientUserGroup) UserGroup(fi.otavanopisto.muikku.schooldata.entity.UserGroup)

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