use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageId 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.CommunicatorMessageId 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.CommunicatorMessageId in project muikku by otavanopisto.
the class CommunicatorMessageIdDAO method create.
public CommunicatorMessageId create() {
CommunicatorMessageId communicatorMessageId = new CommunicatorMessageId();
getEntityManager().persist(communicatorMessageId);
return communicatorMessageId;
}
use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageId 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.plugins.communicator.model.CommunicatorMessageId in project muikku by otavanopisto.
the class CommunicatorLabelRESTService method listMessageIdLabels.
@GET
@Path("/messages/{COMMUNICATORMESSAGEID}/labels")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response listMessageIdLabels(@PathParam("COMMUNICATORMESSAGEID") Long communicatorMessageId) throws AuthorizationException {
CommunicatorMessageId messageId = communicatorController.findCommunicatorMessageId(communicatorMessageId);
// Lists only labels of logged user so we can consider this safe
UserEntity userEntity = sessionController.getLoggedUserEntity();
List<CommunicatorMessageIdLabel> labels = communicatorController.listMessageIdLabelsByUserEntity(userEntity, messageId);
return Response.ok(restModels.restLabel(labels)).build();
}
Aggregations