Search in sources :

Example 1 with CommunicatorLabel

use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorLabel in project muikku by otavanopisto.

the class CommunicatorRESTService method listUserInboxMessages.

@GET
@Path("/items")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response listUserInboxMessages(@QueryParam("labelId") Long labelId, @QueryParam("onlyUnread") @DefaultValue("false") Boolean onlyUnread, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("10") Integer maxResults) {
    UserEntity user = sessionController.getLoggedUserEntity();
    CommunicatorLabel label;
    if (labelId != null) {
        label = communicatorController.findUserLabelById(labelId);
        if (label == null)
            return Response.status(Status.NOT_FOUND).build();
    } else
        label = null;
    List<CommunicatorMessage> receivedItems;
    if (label != null)
        receivedItems = communicatorController.listReceivedItems(user, label, onlyUnread, firstResult, maxResults);
    else
        receivedItems = communicatorController.listReceivedItems(user, onlyUnread, firstResult, maxResults);
    List<CommunicatorThreadRESTModel> result = new ArrayList<CommunicatorThreadRESTModel>();
    for (CommunicatorMessage receivedItem : receivedItems) {
        String categoryName = receivedItem.getCategory() != null ? receivedItem.getCategory().getName() : null;
        boolean hasUnreadMsgs = false;
        Date latestMessageDate = receivedItem.getCreated();
        List<CommunicatorMessageRecipient> recipients = communicatorController.listCommunicatorMessageRecipientsByUserAndMessage(user, receivedItem.getCommunicatorMessageId(), false);
        for (CommunicatorMessageRecipient recipient : recipients) {
            hasUnreadMsgs = hasUnreadMsgs || Boolean.FALSE.equals(recipient.getReadByReceiver());
            Date created = recipient.getCommunicatorMessage().getCreated();
            latestMessageDate = latestMessageDate == null || latestMessageDate.before(created) ? created : latestMessageDate;
        }
        UserBasicInfo senderBasicInfo = restModels.getSenderBasicInfo(receivedItem);
        Long messageCountInThread = communicatorController.countMessagesByUserAndMessageId(user, receivedItem.getCommunicatorMessageId(), false);
        List<CommunicatorMessageIdLabel> labels = communicatorController.listMessageIdLabelsByUserEntity(user, receivedItem.getCommunicatorMessageId());
        List<CommunicatorMessageIdLabelRESTModel> restLabels = restModels.restLabel(labels);
        result.add(new CommunicatorThreadRESTModel(receivedItem.getId(), receivedItem.getCommunicatorMessageId().getId(), receivedItem.getSender(), senderBasicInfo, categoryName, receivedItem.getCaption(), receivedItem.getCreated(), tagIdsToStr(receivedItem.getTags()), hasUnreadMsgs, latestMessageDate, messageCountInThread, restLabels));
    }
    return Response.ok(result).build();
}
Also used : CommunicatorMessage(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage) ArrayList(java.util.ArrayList) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Date(java.util.Date) CommunicatorMessageRecipient(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageRecipient) CommunicatorMessageIdLabel(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageIdLabel) UserBasicInfo(fi.otavanopisto.muikku.rest.model.UserBasicInfo) CommunicatorLabel(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorLabel) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 2 with CommunicatorLabel

use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorLabel in project muikku by otavanopisto.

the class CommunicatorLabelRESTService method createMessageIdLabel.

@POST
@Path("/messages/{COMMUNICATORMESSAGEID}/labels")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response createMessageIdLabel(@PathParam("COMMUNICATORMESSAGEID") Long communicatorMessageId, CommunicatorMessageIdLabelRESTModel newLabel) throws AuthorizationException {
    CommunicatorMessageId messageId = communicatorController.findCommunicatorMessageId(communicatorMessageId);
    UserEntity userEntity = sessionController.getLoggedUserEntity();
    CommunicatorLabel label = communicatorController.findUserLabelById(newLabel.getLabelId());
    if ((label != null) && canAccessLabel(userEntity, label)) {
        CommunicatorMessageIdLabel userLabel = communicatorController.findMessageIdLabel(userEntity, messageId, label);
        if (userLabel == null) {
            userLabel = communicatorController.createMessageIdLabel(userEntity, messageId, label);
            return Response.ok(restModels.restLabel(userLabel)).build();
        } else {
            return Response.status(Status.BAD_REQUEST).build();
        }
    } else {
        return Response.status(Status.NOT_FOUND).build();
    }
}
Also used : CommunicatorMessageIdLabel(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageIdLabel) CommunicatorMessageId(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageId) CommunicatorLabel(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorLabel) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) POST(javax.ws.rs.POST)

Aggregations

UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)2 CommunicatorLabel (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorLabel)2 CommunicatorMessageIdLabel (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageIdLabel)2 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)2 Path (javax.ws.rs.Path)2 CommunicatorMessage (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage)1 CommunicatorMessageId (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageId)1 CommunicatorMessageRecipient (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageRecipient)1 UserBasicInfo (fi.otavanopisto.muikku.rest.model.UserBasicInfo)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1