use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageIdLabel in project muikku by otavanopisto.
the class CommunicatorMessageDAO method listUserThreadBasicInfos.
private List<CommunicatorThreadBasicInfo> listUserThreadBasicInfos(UserEntity userEntity, CommunicatorFolderType type, CommunicatorLabel label) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<CommunicatorThreadBasicInfo> criteria = criteriaBuilder.createQuery(CommunicatorThreadBasicInfo.class);
switch(type) {
case UNREAD:
{
Root<CommunicatorMessageRecipient> root = criteria.from(CommunicatorMessageRecipient.class);
Join<CommunicatorMessageRecipient, CommunicatorMessage> messageJoin = root.join(CommunicatorMessageRecipient_.communicatorMessage);
criteria.multiselect(messageJoin.get(CommunicatorMessage_.communicatorMessageId), criteriaBuilder.greatest(messageJoin.get(CommunicatorMessage_.created)));
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.recipient), userEntity.getId()), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.readByReceiver), Boolean.FALSE), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.trashedByReceiver), Boolean.FALSE), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.archivedByReceiver), Boolean.FALSE)));
criteria.groupBy(messageJoin.get(CommunicatorMessage_.communicatorMessageId));
}
break;
case LABEL:
{
Root<CommunicatorMessageRecipient> root = criteria.from(CommunicatorMessageRecipient.class);
Join<CommunicatorMessageRecipient, CommunicatorMessage> messageJoin = root.join(CommunicatorMessageRecipient_.communicatorMessage);
Join<CommunicatorMessage, CommunicatorMessageId> threadJoin = messageJoin.join(CommunicatorMessage_.communicatorMessageId);
criteria.multiselect(messageJoin.get(CommunicatorMessage_.communicatorMessageId), criteriaBuilder.greatest(messageJoin.get(CommunicatorMessage_.created)));
Root<CommunicatorMessageIdLabel> labelRoot = criteria.from(CommunicatorMessageIdLabel.class);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.recipient), userEntity.getId()), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.trashedByReceiver), Boolean.FALSE), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.archivedByReceiver), Boolean.FALSE), threadJoin.in(labelRoot.get(CommunicatorMessageIdLabel_.communicatorMessageId)), criteriaBuilder.equal(labelRoot.get(CommunicatorMessageIdLabel_.label), label)));
criteria.groupBy(messageJoin.get(CommunicatorMessage_.communicatorMessageId));
}
break;
case SENT:
{
Root<CommunicatorMessage> root = criteria.from(CommunicatorMessage.class);
criteria.multiselect(root.get(CommunicatorMessage_.communicatorMessageId), criteriaBuilder.greatest(root.get(CommunicatorMessage_.created)));
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(CommunicatorMessage_.sender), userEntity.getId()), criteriaBuilder.equal(root.get(CommunicatorMessage_.archivedBySender), Boolean.FALSE), criteriaBuilder.equal(root.get(CommunicatorMessage_.trashedBySender), Boolean.FALSE)));
criteria.groupBy(root.get(CommunicatorMessage_.communicatorMessageId));
}
break;
case INBOX:
case TRASH:
Root<CommunicatorMessageRecipient> root = criteria.from(CommunicatorMessageRecipient.class);
Join<CommunicatorMessageRecipient, CommunicatorMessage> messageJoin = root.join(CommunicatorMessageRecipient_.communicatorMessage);
criteria.multiselect(messageJoin.get(CommunicatorMessage_.communicatorMessageId), criteriaBuilder.greatest(messageJoin.get(CommunicatorMessage_.created)));
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.recipient), userEntity.getId()), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.trashedByReceiver), type == CommunicatorFolderType.TRASH), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.archivedByReceiver), Boolean.FALSE)));
criteria.groupBy(messageJoin.get(CommunicatorMessage_.communicatorMessageId));
break;
}
List<CommunicatorThreadBasicInfo> threads = entityManager.createQuery(criteria).getResultList();
Collections.sort(threads, new Comparator<CommunicatorThreadBasicInfo>() {
@Override
public int compare(CommunicatorThreadBasicInfo o1, CommunicatorThreadBasicInfo o2) {
return o2.getLatestThread().compareTo(o1.getLatestThread());
}
});
return threads;
}
use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageIdLabel 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.CommunicatorMessageIdLabel in project muikku by otavanopisto.
the class CommunicatorMessageIdLabelDAO method findBy.
public CommunicatorMessageIdLabel findBy(UserEntity userEntity, CommunicatorMessageId messageId, CommunicatorLabel label) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<CommunicatorMessageIdLabel> criteria = criteriaBuilder.createQuery(CommunicatorMessageIdLabel.class);
Root<CommunicatorMessageIdLabel> root = criteria.from(CommunicatorMessageIdLabel.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(CommunicatorMessageIdLabel_.userEntity), userEntity.getId()), criteriaBuilder.equal(root.get(CommunicatorMessageIdLabel_.communicatorMessageId), messageId), criteriaBuilder.equal(root.get(CommunicatorMessageIdLabel_.label), label)));
return getSingleResult(entityManager.createQuery(criteria));
}
use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageIdLabel in project muikku by otavanopisto.
the class CommunicatorMessageIdLabelDAO method listByUserAndMessageId.
public List<CommunicatorMessageIdLabel> listByUserAndMessageId(UserEntity userEntity, CommunicatorMessageId messageId) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<CommunicatorMessageIdLabel> criteria = criteriaBuilder.createQuery(CommunicatorMessageIdLabel.class);
Root<CommunicatorMessageIdLabel> root = criteria.from(CommunicatorMessageIdLabel.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(CommunicatorMessageIdLabel_.userEntity), userEntity.getId()), criteriaBuilder.equal(root.get(CommunicatorMessageIdLabel_.communicatorMessageId), messageId)));
return entityManager.createQuery(criteria).getResultList();
}
use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageIdLabel in project muikku by otavanopisto.
the class CommunicatorLabelRESTService method getMessageIdLabel.
@GET
@Path("/messages/{COMMUNICATORMESSAGEID}/labels/{LABELID}")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response getMessageIdLabel(@PathParam("COMMUNICATORMESSAGEID") Long communicatorMessageId, @PathParam("LABELID") Long labelId) throws AuthorizationException {
CommunicatorMessageIdLabel label = communicatorController.findMessageIdLabelById(labelId);
UserEntity userEntity = sessionController.getLoggedUserEntity();
if (!canAccessLabel(userEntity, label.getLabel())) {
return Response.status(Status.FORBIDDEN).build();
}
return Response.ok(restModels.restLabel(label)).build();
}
Aggregations