use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage in project muikku by otavanopisto.
the class CommunicatorMessageDAO method countMessagesByUserAndMessageId.
/**
* Returns the number of messages the user can see in a thread in either inbox/sent or in trash
* depending on the value of trashed parameter.
*
* @param user
* @param communicatorMessageId
* @param trashed
* @return
*/
public Long countMessagesByUserAndMessageId(UserEntity user, CommunicatorMessageId communicatorMessageId, boolean trashed) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class);
Root<CommunicatorMessageRecipient> root = criteria.from(CommunicatorMessageRecipient.class);
Join<CommunicatorMessageRecipient, CommunicatorMessage> msgJoin = root.join(CommunicatorMessageRecipient_.communicatorMessage);
criteria.select(criteriaBuilder.countDistinct(msgJoin));
criteria.where(criteriaBuilder.or(criteriaBuilder.and(criteriaBuilder.equal(msgJoin.get(CommunicatorMessage_.communicatorMessageId), communicatorMessageId), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.recipient), user.getId()), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.trashedByReceiver), trashed), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.archivedByReceiver), Boolean.FALSE)), criteriaBuilder.and(criteriaBuilder.equal(msgJoin.get(CommunicatorMessage_.communicatorMessageId), communicatorMessageId), criteriaBuilder.equal(msgJoin.get(CommunicatorMessage_.sender), user.getId()), criteriaBuilder.equal(msgJoin.get(CommunicatorMessage_.trashedBySender), trashed), criteriaBuilder.equal(msgJoin.get(CommunicatorMessage_.archivedBySender), Boolean.FALSE))));
return entityManager.createQuery(criteria).getSingleResult();
}
use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage in project muikku by otavanopisto.
the class CommunicatorMessageDAO method listThreadsInSent.
public List<CommunicatorMessage> listThreadsInSent(UserEntity sender, Integer firstResult, Integer maxResults) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<CommunicatorMessage> criteria = criteriaBuilder.createQuery(CommunicatorMessage.class);
Root<CommunicatorMessage> root = criteria.from(CommunicatorMessage.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(CommunicatorMessage_.sender), sender.getId()), criteriaBuilder.equal(root.get(CommunicatorMessage_.trashedBySender), Boolean.FALSE), criteriaBuilder.equal(root.get(CommunicatorMessage_.archivedBySender), Boolean.FALSE)));
criteria.groupBy(root.get(CommunicatorMessage_.communicatorMessageId));
criteria.orderBy(criteriaBuilder.desc(criteriaBuilder.greatest(root.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 CommunicatorMessageRecipientDAO method listByUserAndMessageId.
public List<CommunicatorMessageRecipient> listByUserAndMessageId(UserEntity user, CommunicatorMessageId messageId, boolean trashed, boolean archived) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<CommunicatorMessageRecipient> criteria = criteriaBuilder.createQuery(CommunicatorMessageRecipient.class);
Root<CommunicatorMessageRecipient> root = criteria.from(CommunicatorMessageRecipient.class);
Join<CommunicatorMessageRecipient, CommunicatorMessage> msgJoin = root.join(CommunicatorMessageRecipient_.communicatorMessage);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(msgJoin.get(CommunicatorMessage_.communicatorMessageId), messageId), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.recipient), user.getId()), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.trashedByReceiver), trashed), criteriaBuilder.equal(root.get(CommunicatorMessageRecipient_.archivedByReceiver), archived)));
return entityManager.createQuery(criteria).getResultList();
}
Aggregations