use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage 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.plugins.communicator.model.CommunicatorMessage 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();
}
use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage in project muikku by otavanopisto.
the class CommunicatorRESTService method listUserSentCommunicatorItems.
@GET
@Path("/sentitems")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response listUserSentCommunicatorItems(@QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("10") Integer maxResults) {
UserEntity user = sessionController.getLoggedUserEntity();
List<CommunicatorMessage> sentItems = communicatorController.listSentItems(user, firstResult, maxResults);
List<CommunicatorThreadRESTModel> result = new ArrayList<CommunicatorThreadRESTModel>();
for (CommunicatorMessage sentItem : sentItems) {
String categoryName = sentItem.getCategory() != null ? sentItem.getCategory().getName() : null;
boolean hasUnreadMsgs = false;
Date latestMessageDate = sentItem.getCreated();
List<CommunicatorMessageRecipient> recipients = communicatorController.listCommunicatorMessageRecipientsByUserAndMessage(user, sentItem.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(sentItem);
Long messageCountInThread = communicatorController.countMessagesByUserAndMessageId(user, sentItem.getCommunicatorMessageId(), false);
List<CommunicatorMessageIdLabel> labels = communicatorController.listMessageIdLabelsByUserEntity(user, sentItem.getCommunicatorMessageId());
List<CommunicatorMessageIdLabelRESTModel> restLabels = restModels.restLabel(labels);
List<CommunicatorMessageRecipient> messageRecipients = communicatorController.listCommunicatorMessageRecipients(sentItem);
List<CommunicatorMessageRecipientUserGroup> userGroupRecipients = communicatorController.listCommunicatorMessageUserGroupRecipients(sentItem);
List<CommunicatorMessageRecipientWorkspaceGroup> workspaceGroupRecipients = communicatorController.listCommunicatorMessageWorkspaceGroupRecipients(sentItem);
List<CommunicatorMessageRecipientRESTModel> restRecipients = restModels.restRecipient(messageRecipients);
List<fi.otavanopisto.muikku.rest.model.UserGroup> restUserGroupRecipients = restModels.restUserGroupRecipients(userGroupRecipients);
List<CommunicatorMessageRecipientWorkspaceGroupRESTModel> restWorkspaceRecipients = restModels.restWorkspaceGroupRecipients(workspaceGroupRecipients);
Long recipientCount = (long) messageRecipients.size() + userGroupRecipients.size() + workspaceGroupRecipients.size();
result.add(new CommunicatorSentThreadRESTModel(sentItem.getId(), sentItem.getCommunicatorMessageId().getId(), sentItem.getSender(), senderBasicInfo, categoryName, sentItem.getCaption(), sentItem.getCreated(), tagIdsToStr(sentItem.getTags()), hasUnreadMsgs, latestMessageDate, messageCountInThread, restLabels, restRecipients, restUserGroupRecipients, restWorkspaceRecipients, recipientCount));
}
return Response.ok(result).build();
}
use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage in project muikku by otavanopisto.
the class CommunicatorRESTService method postMessageReply.
@POST
@Path("/messages/{COMMUNICATORMESSAGEID}")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response postMessageReply(@PathParam("COMMUNICATORMESSAGEID") Long communicatorMessageId, CommunicatorNewMessageRESTModel newMessage) throws AuthorizationException {
UserEntity userEntity = sessionController.getLoggedUserEntity();
CommunicatorMessageId communicatorMessageId2 = communicatorController.findCommunicatorMessageId(communicatorMessageId);
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();
}
}
// TODO Category not existing at this point would technically indicate an invalid state
CommunicatorMessageCategory categoryEntity = communicatorController.persistCategory(newMessage.getCategoryName());
CommunicatorMessage message = communicatorController.createMessage(communicatorMessageId2, userEntity, recipients, userGroupRecipients, workspaceStudentRecipients, workspaceTeacherRecipients, categoryEntity, newMessage.getCaption(), newMessage.getContent(), tagList);
sendNewMessageNotifications(message);
return Response.ok(restModels.restFullMessage(message)).build();
}
use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage in project muikku by otavanopisto.
the class CommunicatorTrashRESTService method listUserTrashItems.
@GET
@Path("/trash")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response listUserTrashItems(@QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("10") Integer maxResults) {
UserEntity user = sessionController.getLoggedUserEntity();
List<CommunicatorMessage> trashItems = communicatorController.listTrashItems(user, firstResult, maxResults);
List<CommunicatorThreadRESTModel> result = new ArrayList<CommunicatorThreadRESTModel>();
for (CommunicatorMessage receivedItem : trashItems) {
String categoryName = receivedItem.getCategory() != null ? receivedItem.getCategory().getName() : null;
boolean hasUnreadMsgs = false;
Date latestMessageDate = receivedItem.getCreated();
List<CommunicatorMessageRecipient> recipients = communicatorController.listCommunicatorMessageRecipientsByUserAndMessage(user, receivedItem.getCommunicatorMessageId(), true);
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(), true);
List<CommunicatorMessageRecipient> messageRecipients = communicatorController.listCommunicatorMessageRecipients(receivedItem);
List<CommunicatorMessageRecipientUserGroup> userGroupRecipients = communicatorController.listCommunicatorMessageUserGroupRecipients(receivedItem);
List<CommunicatorMessageRecipientWorkspaceGroup> workspaceGroupRecipients = communicatorController.listCommunicatorMessageWorkspaceGroupRecipients(receivedItem);
List<CommunicatorMessageIdLabel> labels = communicatorController.listMessageIdLabelsByUserEntity(user, receivedItem.getCommunicatorMessageId());
List<CommunicatorMessageIdLabelRESTModel> restLabels = restModels.restLabel(labels);
List<CommunicatorMessageRecipientRESTModel> restRecipients = restModels.restRecipient(messageRecipients);
List<fi.otavanopisto.muikku.rest.model.UserGroup> restUserGroupRecipients = restModels.restUserGroupRecipients(userGroupRecipients);
List<CommunicatorMessageRecipientWorkspaceGroupRESTModel> restWorkspaceRecipients = restModels.restWorkspaceGroupRecipients(workspaceGroupRecipients);
Long recipientCount = (long) messageRecipients.size() + userGroupRecipients.size() + workspaceGroupRecipients.size();
result.add(new CommunicatorSentThreadRESTModel(receivedItem.getId(), receivedItem.getCommunicatorMessageId().getId(), receivedItem.getSender(), senderBasicInfo, categoryName, receivedItem.getCaption(), receivedItem.getCreated(), restModels.tagIdsToStr(receivedItem.getTags()), hasUnreadMsgs, latestMessageDate, messageCountInThread, restLabels, restRecipients, restUserGroupRecipients, restWorkspaceRecipients, recipientCount));
}
return Response.ok(result).build();
}
Aggregations