use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage in project muikku by otavanopisto.
the class CommunicatorMessageDAO method listThreadsInTrash.
public List<CommunicatorMessage> listThreadsInTrash(UserEntity user, Integer firstResult, Integer maxResults) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<CommunicatorMessage> criteria = criteriaBuilder.createQuery(CommunicatorMessage.class);
Root<CommunicatorMessageRecipient> root = criteria.from(CommunicatorMessageRecipient.class);
Join<CommunicatorMessageRecipient, CommunicatorMessage> messageJoin = root.join(CommunicatorMessageRecipient_.communicatorMessage);
Join<CommunicatorMessage, CommunicatorMessageId> threadJoin = messageJoin.join(CommunicatorMessage_.communicatorMessageId);
// SubQuery finds the latest date in a thread when linked to the main query
// and thus allows finding the latest message in the thread.
Subquery<Date> subQuery = criteria.subquery(Date.class);
Root<CommunicatorMessageRecipient> subQueryRoot = subQuery.from(CommunicatorMessageRecipient.class);
Join<CommunicatorMessageRecipient, CommunicatorMessage> subMessageJoin = subQueryRoot.join(CommunicatorMessageRecipient_.communicatorMessage);
subQuery.select(criteriaBuilder.greatest(subMessageJoin.get(CommunicatorMessage_.created)));
subQuery.where(criteriaBuilder.and(criteriaBuilder.equal(subMessageJoin.get(CommunicatorMessage_.communicatorMessageId), messageJoin.get(CommunicatorMessage_.communicatorMessageId)), criteriaBuilder.or(criteriaBuilder.and(criteriaBuilder.equal(subQueryRoot.get(CommunicatorMessageRecipient_.recipient), user.getId()), criteriaBuilder.equal(subQueryRoot.get(CommunicatorMessageRecipient_.trashedByReceiver), Boolean.TRUE), criteriaBuilder.equal(subQueryRoot.get(CommunicatorMessageRecipient_.archivedByReceiver), Boolean.FALSE)), criteriaBuilder.and(criteriaBuilder.equal(subMessageJoin.get(CommunicatorMessage_.sender), user.getId()), criteriaBuilder.equal(subMessageJoin.get(CommunicatorMessage_.trashedBySender), Boolean.TRUE), criteriaBuilder.equal(subMessageJoin.get(CommunicatorMessage_.archivedBySender), Boolean.FALSE)))));
criteria.select(messageJoin);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(messageJoin.get(CommunicatorMessage_.created), subQuery), criteriaBuilder.or(criteriaBuilder.and(criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.recipient), user.getId()), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.trashedByReceiver), Boolean.TRUE), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.archivedByReceiver), Boolean.FALSE)), criteriaBuilder.and(criteriaBuilder.equal(messageJoin.get(CommunicatorMessage_.sender), user.getId()), criteriaBuilder.equal(messageJoin.get(CommunicatorMessage_.trashedBySender), Boolean.TRUE), criteriaBuilder.equal(messageJoin.get(CommunicatorMessage_.archivedBySender), Boolean.FALSE)))));
criteria.groupBy(threadJoin);
criteria.orderBy(criteriaBuilder.desc(messageJoin.get(CommunicatorMessage_.created)));
TypedQuery<CommunicatorMessage> query = entityManager.createQuery(criteria);
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
return query.getResultList();
}
use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage in project muikku by otavanopisto.
the class CommunicatorMessageDAO method listThreadsInInbox.
public List<CommunicatorMessage> listThreadsInInbox(UserEntity recipient, CommunicatorLabel label, boolean onlyUnread, Integer firstResult, Integer maxResults) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<CommunicatorMessage> criteria = criteriaBuilder.createQuery(CommunicatorMessage.class);
Root<CommunicatorMessageRecipient> root = criteria.from(CommunicatorMessageRecipient.class);
Join<CommunicatorMessageRecipient, CommunicatorMessage> messageJoin = root.join(CommunicatorMessageRecipient_.communicatorMessage);
Join<CommunicatorMessage, CommunicatorMessageId> threadJoin = messageJoin.join(CommunicatorMessage_.communicatorMessageId);
criteria.select(messageJoin);
// SubQuery finds the latest date in a thread when linked to the main query
// and thus allows finding the latest message in the thread.
Subquery<Date> subQuery = criteria.subquery(Date.class);
Root<CommunicatorMessageRecipient> subQueryRoot = subQuery.from(CommunicatorMessageRecipient.class);
Join<CommunicatorMessageRecipient, CommunicatorMessage> subMessageJoin = subQueryRoot.join(CommunicatorMessageRecipient_.communicatorMessage);
Join<CommunicatorMessage, CommunicatorMessageId> subThreadJoin = subMessageJoin.join(CommunicatorMessage_.communicatorMessageId);
subQuery.select(criteriaBuilder.greatest(subMessageJoin.get(CommunicatorMessage_.created)));
List<Predicate> subQueryPredicates = new ArrayList<Predicate>();
subQueryPredicates.add(criteriaBuilder.equal(subMessageJoin.get(CommunicatorMessage_.communicatorMessageId), messageJoin.get(CommunicatorMessage_.communicatorMessageId)));
subQueryPredicates.add(criteriaBuilder.equal(subQueryRoot.get(CommunicatorMessageRecipient_.recipient), recipient.getId()));
subQueryPredicates.add(criteriaBuilder.equal(subQueryRoot.get(CommunicatorMessageRecipient_.trashedByReceiver), Boolean.FALSE));
subQueryPredicates.add(criteriaBuilder.equal(subQueryRoot.get(CommunicatorMessageRecipient_.archivedByReceiver), Boolean.FALSE));
if (onlyUnread)
subQueryPredicates.add(criteriaBuilder.equal(subQueryRoot.get(CommunicatorMessageRecipient_.readByReceiver), Boolean.FALSE));
if (label != null) {
Root<CommunicatorMessageIdLabel> subLabelRoot = criteria.from(CommunicatorMessageIdLabel.class);
subQueryPredicates.add(subThreadJoin.in(subLabelRoot.get(CommunicatorMessageIdLabel_.communicatorMessageId)));
subQueryPredicates.add(criteriaBuilder.equal(subLabelRoot.get(CommunicatorMessageIdLabel_.label), label));
}
subQuery.where(criteriaBuilder.and(subQueryPredicates.toArray(new Predicate[0])));
// Main Query
List<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(criteriaBuilder.equal(messageJoin.get(CommunicatorMessage_.created), subQuery));
predicates.add(criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.recipient), recipient.getId()));
predicates.add(criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.trashedByReceiver), Boolean.FALSE));
predicates.add(criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.archivedByReceiver), Boolean.FALSE));
if (onlyUnread)
predicates.add(criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.readByReceiver), Boolean.FALSE));
if (label != null) {
Root<CommunicatorMessageIdLabel> labelRoot = criteria.from(CommunicatorMessageIdLabel.class);
predicates.add(threadJoin.in(labelRoot.get(CommunicatorMessageIdLabel_.communicatorMessageId)));
predicates.add(criteriaBuilder.equal(labelRoot.get(CommunicatorMessageIdLabel_.label), label));
}
criteria.where(criteriaBuilder.and(predicates.toArray(new Predicate[0])));
criteria.groupBy(threadJoin);
criteria.orderBy(criteriaBuilder.desc(messageJoin.get(CommunicatorMessage_.created)));
TypedQuery<CommunicatorMessage> query = entityManager.createQuery(criteria);
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
return query.getResultList();
}
use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage 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.plugins.communicator.model.CommunicatorMessage in project muikku by otavanopisto.
the class CommunicatorController method archiveTrashedMessages.
public void archiveTrashedMessages(UserEntity user, CommunicatorMessageId threadId) {
List<CommunicatorMessageRecipient> received = communicatorMessageRecipientDAO.listByUserAndMessageId(user, threadId, true, false);
for (CommunicatorMessageRecipient recipient : received) {
communicatorMessageRecipientDAO.updateArchivedByReceiver(recipient, true);
}
List<CommunicatorMessage> sent = communicatorMessageDAO.listMessagesInSentThread(user, threadId, true, false);
for (CommunicatorMessage msg : sent) {
communicatorMessageDAO.updateArchivedBySender(msg, true);
}
}
use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage in project muikku by otavanopisto.
the class CommunicatorController method unTrashAllThreadMessages.
public void unTrashAllThreadMessages(UserEntity user, CommunicatorMessageId messageId) {
List<CommunicatorMessageRecipient> received = communicatorMessageRecipientDAO.listByUserAndMessageId(user, messageId, true, false);
for (CommunicatorMessageRecipient recipient : received) {
communicatorMessageRecipientDAO.updateTrashedByReceiver(recipient, false);
}
List<CommunicatorMessage> sentMessages = communicatorMessageDAO.listMessagesInSentThread(user, messageId, true, false);
for (CommunicatorMessage message : sentMessages) {
communicatorMessageDAO.updateTrashedBySender(message, false);
}
}
Aggregations