Search in sources :

Example 26 with CommunicatorMessage

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) CommunicatorMessageRecipient(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageRecipient) EntityManager(javax.persistence.EntityManager) CommunicatorMessage(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage)

Example 27 with CommunicatorMessage

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) CommunicatorMessage(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage)

Example 28 with CommunicatorMessage

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) CommunicatorMessageRecipient(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageRecipient) EntityManager(javax.persistence.EntityManager) CommunicatorMessage(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage)

Aggregations

CommunicatorMessage (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage)28 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)14 CommunicatorMessageRecipient (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageRecipient)13 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)12 Path (javax.ws.rs.Path)12 CommunicatorMessageId (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageId)10 CommunicatorMessageIdLabel (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageIdLabel)10 GET (javax.ws.rs.GET)10 EntityManager (javax.persistence.EntityManager)8 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)8 ArrayList (java.util.ArrayList)7 Date (java.util.Date)6 UserBasicInfo (fi.otavanopisto.muikku.rest.model.UserBasicInfo)5 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)4 Tag (fi.otavanopisto.muikku.model.base.Tag)3 UserGroupEntity (fi.otavanopisto.muikku.model.users.UserGroupEntity)3 CommunicatorMessageCategory (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageCategory)3 CommunicatorMessageRecipientUserGroup (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageRecipientUserGroup)3 CommunicatorMessageRecipientWorkspaceGroup (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageRecipientWorkspaceGroup)3 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)2