Search in sources :

Example 1 with CommunicatorMessageIdLabel

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

Example 2 with CommunicatorMessageIdLabel

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

Example 3 with CommunicatorMessageIdLabel

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

Example 4 with CommunicatorMessageIdLabel

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

Example 5 with CommunicatorMessageIdLabel

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();
}
Also used : CommunicatorMessageIdLabel(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageIdLabel) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Aggregations

CommunicatorMessageIdLabel (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageIdLabel)18 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)11 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)11 Path (javax.ws.rs.Path)11 CommunicatorMessage (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage)10 GET (javax.ws.rs.GET)10 CommunicatorMessageId (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageId)8 CommunicatorMessageRecipient (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageRecipient)5 EntityManager (javax.persistence.EntityManager)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 UserBasicInfo (fi.otavanopisto.muikku.rest.model.UserBasicInfo)3 CommunicatorLabel (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorLabel)2 CommunicatorMessageRecipientUserGroup (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageRecipientUserGroup)2 CommunicatorMessageRecipientWorkspaceGroup (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageRecipientWorkspaceGroup)2 CommunicatorUserLabel (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorUserLabel)1 Join (javax.persistence.criteria.Join)1 Predicate (javax.persistence.criteria.Predicate)1 Root (javax.persistence.criteria.Root)1