use of fi.otavanopisto.muikku.model.workspace.WorkspaceEntity 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.workspace.WorkspaceEntity in project muikku by otavanopisto.
the class CommunicatorAssessmentRequestController method sendAssessmentRequestCancelledMessage.
public void sendAssessmentRequestCancelledMessage(WorkspaceUserEntity workspaceUserEntity) {
CommunicatorMessageId communicatorMessageId = assessmentRequestController.findCommunicatorMessageId(workspaceUserEntity);
if (communicatorMessageId == null) {
communicatorMessageId = communicatorController.createMessageId();
}
UserSchoolDataIdentifier userSchoolDataIdentifier = workspaceUserEntity.getUserSchoolDataIdentifier();
if (userSchoolDataIdentifier == null) {
logger.severe(String.format("Could not find userSchoolDataIdentifier for workspace user entity %d", workspaceUserEntity.getId()));
return;
}
WorkspaceEntity workspaceEntity = workspaceUserEntity.getWorkspaceEntity();
if (workspaceEntity == null) {
logger.severe(String.format("Could not find workspaceEntity for workspace user entity %d", workspaceUserEntity.getId()));
return;
}
UserEntity studentEntity = userSchoolDataIdentifier.getUserEntity();
if (studentEntity == null) {
logger.severe(String.format("Could not find studentEntity for workspace user entity %d", workspaceUserEntity.getId()));
return;
}
schoolDataBridgeSessionController.startSystemSession();
try {
List<UserEntity> teachers = new ArrayList<UserEntity>();
List<WorkspaceUserEntity> workspaceTeachers = workspaceUserEntityController.listActiveWorkspaceStaffMembers(workspaceEntity);
for (WorkspaceUserEntity workspaceTeacher : workspaceTeachers) {
teachers.add(workspaceTeacher.getUserSchoolDataIdentifier().getUserEntity());
}
SchoolDataIdentifier studentIdentifier = new SchoolDataIdentifier(userSchoolDataIdentifier.getIdentifier(), userSchoolDataIdentifier.getDataSource().getIdentifier());
User student = userController.findUserByIdentifier(studentIdentifier);
if (student == null) {
logger.severe(String.format("Could not find student %s", studentIdentifier));
return;
}
Workspace workspace = workspaceController.findWorkspace(workspaceEntity);
if (workspace == null) {
logger.severe(String.format("Could not find workspace for workspace entity %d", workspaceEntity.getId()));
return;
}
String messageCategory = getText("plugin.communicator.assessmentrequest.category");
String messageTitle = assessmentCancelledTitle(workspace, student);
String messageBody = assessmentCancelledBody(workspace, student);
List<String> teacherEmails = new ArrayList<>(teachers.size());
for (UserEntity teacher : teachers) {
String teacherEmail = userEmailEntityController.getUserDefaultEmailAddress(teacher, false);
if (StringUtils.isNotBlank(teacherEmail)) {
teacherEmails.add(teacherEmail);
}
}
if (!teacherEmails.isEmpty()) {
mailer.sendMail(MailType.HTML, teacherEmails, messageTitle, messageBody);
}
communicatorController.replyToMessage(studentEntity, messageCategory, messageTitle, messageBody, teachers, communicatorMessageId);
} finally {
schoolDataBridgeSessionController.endSystemSession();
}
}
use of fi.otavanopisto.muikku.model.workspace.WorkspaceEntity 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.workspace.WorkspaceEntity in project muikku by otavanopisto.
the class CoursePickerRESTService method getWorkspace.
@GET
@Path("/workspaces/{ID}")
@RESTPermitUnimplemented
public Response getWorkspace(@PathParam("ID") Long workspaceEntityId) {
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
if (workspaceEntity == null) {
return Response.status(Status.NOT_FOUND).build();
}
Workspace workspace = null;
schoolDataBridgeSessionController.startSystemSession();
try {
workspace = workspaceController.findWorkspace(workspaceEntity);
} finally {
schoolDataBridgeSessionController.endSystemSession();
}
if (workspace == null) {
return Response.status(Status.NOT_FOUND).build();
}
boolean canSignup = getCanSignup(workspaceEntity);
boolean isCourseMember = getIsAlreadyOnWorkspace(workspaceEntity);
String educationTypeName = null;
if (workspace.getWorkspaceTypeId() != null) {
EducationType educationType = courseMetaController.findEducationType(workspace.getWorkspaceTypeId());
if (educationType != null) {
educationTypeName = educationType.getName();
}
}
return Response.ok(createRestModel(workspaceEntity, workspace.getName(), workspace.getNameExtension(), workspace.getDescription(), educationTypeName, canSignup, isCourseMember)).build();
}
use of fi.otavanopisto.muikku.model.workspace.WorkspaceEntity 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");
}
}
Aggregations