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();
}
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();
}
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();
}
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;
}
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();
}
}
Aggregations