use of fi.otavanopisto.muikku.model.users.UserEntity 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();
}
use of fi.otavanopisto.muikku.model.users.UserEntity in project muikku by otavanopisto.
the class CommunicatorRESTService method createUserMessageTemplate.
@POST
@Path("/templates")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response createUserMessageTemplate(CommunicatorMessageTemplateRESTModel template) throws AuthorizationException {
UserEntity userEntity = sessionController.getLoggedUserEntity();
CommunicatorMessageTemplate messageTemplate = communicatorController.createMessageTemplate(template.getName(), template.getContent(), userEntity);
CommunicatorMessageTemplateRESTModel result = new CommunicatorMessageTemplateRESTModel(messageTemplate.getId(), messageTemplate.getName(), messageTemplate.getContent());
return Response.ok(result).build();
}
use of fi.otavanopisto.muikku.model.users.UserEntity in project muikku by otavanopisto.
the class CommunicatorRESTService method listUserCommunicatorMessagesByMessageId.
@GET
@Path("/messages/{COMMUNICATORMESSAGEID}")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response listUserCommunicatorMessagesByMessageId(@PathParam("COMMUNICATORMESSAGEID") Long communicatorMessageId) {
UserEntity user = sessionController.getLoggedUserEntity();
CommunicatorMessageId threadId = communicatorController.findCommunicatorMessageId(communicatorMessageId);
List<CommunicatorMessage> receivedItems = communicatorController.listMessagesByMessageId(user, threadId, false);
List<CommunicatorMessageIdLabel> labels = communicatorController.listMessageIdLabelsByUserEntity(user, threadId);
List<CommunicatorMessageIdLabelRESTModel> restLabels = restModels.restLabel(labels);
CommunicatorMessageId olderThread = communicatorController.findOlderThreadId(user, threadId, CommunicatorFolderType.INBOX, null);
CommunicatorMessageId newerThread = communicatorController.findNewerThreadId(user, threadId, CommunicatorFolderType.INBOX, null);
return Response.ok(restModels.restThreadViewModel(receivedItems, olderThread, newerThread, restLabels)).build();
}
use of fi.otavanopisto.muikku.model.users.UserEntity in project muikku by otavanopisto.
the class CommunicatorRESTService method getCommunicatorMessageMessageCount.
@GET
@Path("/messages/{COMMUNICATORMESSAGEID}/messagecount")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response getCommunicatorMessageMessageCount(@PathParam("COMMUNICATORMESSAGEID") Long communicatorMessageId) {
UserEntity user = sessionController.getLoggedUserEntity();
CommunicatorMessageId messageId = communicatorController.findCommunicatorMessageId(communicatorMessageId);
Long result = communicatorController.countMessagesByUserAndMessageId(user, messageId, false);
return Response.ok(result).build();
}
use of fi.otavanopisto.muikku.model.users.UserEntity in project muikku by otavanopisto.
the class CommunicatorRESTService method postMessage.
@POST
@Path("/messages")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response postMessage(CommunicatorNewMessageRESTModel newMessage) throws AuthorizationException {
UserEntity userEntity = sessionController.getLoggedUserEntity();
CommunicatorMessageId communicatorMessageId = communicatorController.createMessageId();
Set<Tag> tagList = parseTags(newMessage.getTags());
List<UserEntity> recipients = new ArrayList<UserEntity>();
for (Long recipientId : newMessage.getRecipientIds()) {
UserEntity recipient = userEntityController.findUserEntityById(recipientId);
if (recipient != null)
recipients.add(recipient);
}
List<UserGroupEntity> userGroupRecipients = null;
List<WorkspaceEntity> workspaceStudentRecipients = null;
List<WorkspaceEntity> workspaceTeacherRecipients = null;
if (!CollectionUtils.isEmpty(newMessage.getRecipientGroupIds())) {
if (sessionController.hasEnvironmentPermission(CommunicatorPermissionCollection.COMMUNICATOR_GROUP_MESSAGING)) {
userGroupRecipients = new ArrayList<UserGroupEntity>();
for (Long groupId : newMessage.getRecipientGroupIds()) {
UserGroupEntity group = userGroupEntityController.findUserGroupEntityById(groupId);
userGroupRecipients.add(group);
}
} else {
// Trying to feed group ids when you don't have permission greets you with bad request
return Response.status(Status.BAD_REQUEST).build();
}
}
if (!CollectionUtils.isEmpty(newMessage.getRecipientStudentsWorkspaceIds())) {
workspaceStudentRecipients = new ArrayList<WorkspaceEntity>();
for (Long workspaceId : newMessage.getRecipientStudentsWorkspaceIds()) {
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceId);
if (sessionController.hasPermission(CommunicatorPermissionCollection.COMMUNICATOR_WORKSPACE_MESSAGING, workspaceEntity))
workspaceStudentRecipients.add(workspaceEntity);
else
return Response.status(Status.BAD_REQUEST).build();
}
}
if (!CollectionUtils.isEmpty(newMessage.getRecipientTeachersWorkspaceIds())) {
workspaceTeacherRecipients = new ArrayList<WorkspaceEntity>();
for (Long workspaceId : newMessage.getRecipientTeachersWorkspaceIds()) {
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceId);
if (sessionController.hasPermission(CommunicatorPermissionCollection.COMMUNICATOR_WORKSPACE_MESSAGING, workspaceEntity))
workspaceTeacherRecipients.add(workspaceEntity);
else
return Response.status(Status.BAD_REQUEST).build();
}
}
if (StringUtils.isBlank(newMessage.getCategoryName())) {
return Response.status(Status.BAD_REQUEST).entity("CategoryName missing").build();
}
// TODO Category not existing at this point would technically indicate an invalid state
CommunicatorMessageCategory categoryEntity = communicatorController.persistCategory(newMessage.getCategoryName());
CommunicatorMessage message = communicatorController.createMessage(communicatorMessageId, userEntity, recipients, userGroupRecipients, workspaceStudentRecipients, workspaceTeacherRecipients, categoryEntity, newMessage.getCaption(), newMessage.getContent(), tagList);
sendNewMessageNotifications(message);
return Response.ok(restModels.restFullMessage(message)).build();
}
Aggregations