porting to storagehub
This commit is contained in:
parent
e85a63e1ba
commit
4eed959544
|
@ -1,7 +1,6 @@
|
|||
package org.gcube.portets.user.message_conversations.server;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -12,20 +11,15 @@ import org.gcube.applicationsupportlayer.social.NotificationsManager;
|
|||
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingSite;
|
||||
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.homelibary.model.items.type.WorkspaceItemType;
|
||||
import org.gcube.common.homelibrary.home.HomeLibrary;
|
||||
import org.gcube.common.homelibrary.home.HomeManager;
|
||||
import org.gcube.common.homelibrary.home.HomeManagerFactory;
|
||||
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
|
||||
import org.gcube.common.homelibrary.home.workspace.Workspace;
|
||||
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
|
||||
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException;
|
||||
import org.gcube.common.homelibrary.home.workspace.folder.FolderItem;
|
||||
import org.gcube.common.homelibrary.home.workspace.sharing.WorkspaceMessage;
|
||||
import org.gcube.common.homelibrary.home.workspace.sharing.WorkspaceMessageManager;
|
||||
import org.gcube.common.portal.PortalContext;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.common.storagehub.client.dsl.FileContainer;
|
||||
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
|
||||
import org.gcube.common.storagehub.client.plugins.AbstractPlugin;
|
||||
import org.gcube.common.storagehub.client.proxies.MessageManagerClient;
|
||||
import org.gcube.common.storagehub.model.items.FolderItem;
|
||||
import org.gcube.common.storagehub.model.items.Item;
|
||||
import org.gcube.common.storagehub.model.messages.Message;
|
||||
import org.gcube.portal.notifications.bean.GenericItemBean;
|
||||
import org.gcube.portal.notifications.thread.MessageNotificationsThread;
|
||||
import org.gcube.portets.user.message_conversations.client.MessageService;
|
||||
|
@ -110,85 +104,78 @@ public class ConvServiceImpl extends RemoteServiceServlet implements MessageServ
|
|||
ScopeProvider.instance.set(scope);
|
||||
String token = pContext.getCurrentUserToken(scope, user.getUserId());
|
||||
SecurityTokenProvider.instance.set(token);
|
||||
Workspace workspace = null;
|
||||
try {
|
||||
HomeManagerFactory factory = HomeLibrary.getHomeManagerFactory();
|
||||
HomeManager manager = factory.getHomeManager();
|
||||
workspace = manager.getHome().getWorkspace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
MessageManagerClient client = AbstractPlugin.messages().build();
|
||||
long start = System.currentTimeMillis();
|
||||
List<Message> listMessages = (sent) ? client.getSentMessages(79)
|
||||
: client.getReceivedMessages(79);
|
||||
if (listMessages == null) {
|
||||
_log.error("listMessages is null, Could not read messages of {}", user.getFullname());
|
||||
return null;
|
||||
}
|
||||
List<WorkspaceMessage> listMessages = null;
|
||||
_log.debug("Got messages of {} how many? {} in {} ms", new Object[] {user.getFullname(), listMessages.size(), (System.currentTimeMillis()-start)});
|
||||
//the messages from shub are returned ordered from the new one to the oldest, so we don't need to reverse them anymore
|
||||
//Collections.reverse(listMessages);
|
||||
LiferayUserManager um = new LiferayUserManager();
|
||||
|
||||
listMessages = (sent) ?
|
||||
workspace.getWorkspaceMessageManager().getSentMessages() : workspace.getWorkspaceMessageManager().getReceivedMessages();
|
||||
if (listMessages == null) {
|
||||
_log.error("listMessages is null, Could not read messages of " + user.getFullname());
|
||||
return null;
|
||||
}
|
||||
_log.debug("Got messages of " + user.getFullname() + " how many? " + listMessages.size());
|
||||
//the messages are returned from the oldest to the new one, so we reverse them
|
||||
Collections.reverse(listMessages);
|
||||
LiferayUserManager um = new LiferayUserManager();
|
||||
for (Message m : listMessages) {
|
||||
String previewBody = m.getBody() + " ...";
|
||||
MessageUserModel mu = null;
|
||||
GCubeUser sender = null;
|
||||
try {
|
||||
if (sent) {
|
||||
if (m.getAddresses().length < 2) {
|
||||
GCubeUser recipient = um.getUserByUsername(m.getAddresses()[0]);
|
||||
mu = new MessageUserModel(recipient.getUserId(), recipient.getUsername(), recipient.getFullname(), recipient.getUserAvatarURL(), "", "");
|
||||
} else {
|
||||
//we have at least 2 recipients
|
||||
GCubeUser recipient1 = um.getUserByUsername(m.getAddresses()[0]);
|
||||
GCubeUser recipient2 = um.getUserByUsername(m.getAddresses()[1]);
|
||||
String label2Display = recipient1.getFirstName() + " & " + recipient2.getFirstName();
|
||||
if (m.getAddresses().length > 2)
|
||||
label2Display += " & ...";
|
||||
mu = new MessageUserModel(recipient1.getUserId(), recipient1.getUsername(), label2Display, null, "", "");
|
||||
|
||||
for (WorkspaceMessage m : listMessages) {
|
||||
String previewBody = m.getBody().length() > 80 ? m.getBody().substring(0, 79) + " ..." : m.getBody();
|
||||
MessageUserModel mu = null;
|
||||
GCubeUser sender = null;
|
||||
try {
|
||||
if (sent) {
|
||||
if (m.getAddresses().size() < 2) {
|
||||
GCubeUser recipient = um.getUserByUsername(m.getAddresses().get(0));
|
||||
mu = new MessageUserModel(recipient.getUserId(), recipient.getUsername(), recipient.getFullname(), recipient.getUserAvatarURL(), "", "");
|
||||
} else {
|
||||
//we have at least 2 recipients
|
||||
GCubeUser recipient1 = um.getUserByUsername(m.getAddresses().get(0));
|
||||
GCubeUser recipient2 = um.getUserByUsername(m.getAddresses().get(1));
|
||||
String label2Display = recipient1.getFirstName() + " & " + recipient2.getFirstName();
|
||||
if (m.getAddresses().size() > 2)
|
||||
label2Display += " & ...";
|
||||
mu = new MessageUserModel(recipient1.getUserId(), recipient1.getUsername(), label2Display, null, "", "");
|
||||
|
||||
}
|
||||
} else { //received message
|
||||
sender = um.getUserByUsername(m.getSender().getPortalLogin());
|
||||
mu = new MessageUserModel(sender.getUserId(), sender.getUsername(), sender.getFullname(), sender.getUserAvatarURL(), "", "");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
if (!sent) {
|
||||
mu = new MessageUserModel(m.getSender().getPortalLogin());
|
||||
} else {
|
||||
mu = new MessageUserModel(m.getAddresses().get(0));
|
||||
}
|
||||
}
|
||||
boolean hasAttachments = !m.getAttachmentsIds().isEmpty();
|
||||
if (!sent) { //received messages
|
||||
|
||||
toReturn.add(new ConvMessage(
|
||||
m.getId(),
|
||||
m.getSubject(),
|
||||
mu,
|
||||
new Date(m.getSendTime().getTimeInMillis()),
|
||||
previewBody,
|
||||
m.isRead(),
|
||||
hasAttachments));
|
||||
} else { //sent messages
|
||||
ArrayList<MessageUserModel> recipients = new ArrayList<>();
|
||||
for (String rec : m.getAddresses()) {
|
||||
recipients.add(new MessageUserModel(rec));
|
||||
}
|
||||
toReturn.add(new ConvMessage(
|
||||
m.getId(),
|
||||
m.getSubject(),
|
||||
mu,
|
||||
recipients,
|
||||
new Date(m.getSendTime().getTimeInMillis()),
|
||||
previewBody,
|
||||
m.isRead(),
|
||||
hasAttachments));
|
||||
}
|
||||
} else { //received message
|
||||
sender = um.getUserByUsername(m.getSender().getUserName());
|
||||
mu = new MessageUserModel(sender.getUserId(), sender.getUsername(), sender.getFullname(), sender.getUserAvatarURL(), "", "");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
if (!sent) {
|
||||
mu = new MessageUserModel(m.getSender().getUserName());
|
||||
} else {
|
||||
mu = new MessageUserModel(m.getAddresses()[0]);
|
||||
}
|
||||
}
|
||||
boolean hasAttachments = m.isWithAttachments();
|
||||
if (!sent) { //received messages
|
||||
|
||||
toReturn.add(new ConvMessage(
|
||||
m.getId(),
|
||||
m.getSubject(),
|
||||
mu,
|
||||
new Date(m.getCreationTime().getTimeInMillis()),
|
||||
previewBody,
|
||||
m.isRead(),
|
||||
hasAttachments));
|
||||
} else { //sent messages
|
||||
ArrayList<MessageUserModel> recipients = new ArrayList<>();
|
||||
for (String rec : m.getAddresses()) {
|
||||
recipients.add(new MessageUserModel(rec));
|
||||
}
|
||||
toReturn.add(new ConvMessage(
|
||||
m.getId(),
|
||||
m.getSubject(),
|
||||
mu,
|
||||
recipients,
|
||||
new Date(m.getCreationTime().getTimeInMillis()),
|
||||
previewBody,
|
||||
m.isRead(),
|
||||
hasAttachments));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
|
@ -203,13 +190,9 @@ public class ConvServiceImpl extends RemoteServiceServlet implements MessageServ
|
|||
String scope = pContext.getCurrentScope(getThreadLocalRequest());
|
||||
SecurityTokenProvider.instance.set(pContext.getCurrentUserToken(scope, user.getUserId()));
|
||||
try {
|
||||
HomeManagerFactory factory = HomeLibrary.getHomeManagerFactory();
|
||||
HomeManager manager = factory.getHomeManager();
|
||||
Workspace workspace = manager.getHome().getWorkspace();
|
||||
WorkspaceMessage m = (sent) ?
|
||||
workspace.getWorkspaceMessageManager().getSentMessage(messageId): workspace.getWorkspaceMessageManager().getReceivedMessage(messageId);
|
||||
m.setStatus(false);
|
||||
return true;
|
||||
MessageManagerClient client = AbstractPlugin.messages().build();
|
||||
client.setRead(messageId, false);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
|
@ -225,50 +208,51 @@ public class ConvServiceImpl extends RemoteServiceServlet implements MessageServ
|
|||
String scope = pContext.getCurrentScope(getThreadLocalRequest());
|
||||
SecurityTokenProvider.instance.set(pContext.getCurrentUserToken(scope, user.getUserId()));
|
||||
LiferayUserManager um = new LiferayUserManager();
|
||||
HomeManagerFactory factory = HomeLibrary.getHomeManagerFactory();
|
||||
HomeManager manager = factory.getHomeManager();
|
||||
Workspace workspace = manager.getHome().getWorkspace();
|
||||
WorkspaceMessage m = (sent) ?
|
||||
workspace.getWorkspaceMessageManager().getSentMessage(messageId): workspace.getWorkspaceMessageManager().getReceivedMessage(messageId);
|
||||
MessageUserModel mu = null;
|
||||
GCubeUser sender = null;
|
||||
try {
|
||||
sender = um.getUserByUsername(m.getSender().getPortalLogin());
|
||||
mu = new MessageUserModel(sender.getUserId(), sender.getUsername(), sender.getFullname(), extractDomainFromEmail(sender.getEmail()));
|
||||
} catch (Exception ex) {
|
||||
mu = new MessageUserModel(m.getSender().getPortalLogin());
|
||||
}
|
||||
ArrayList<MessageUserModel> recipients = new ArrayList<>();
|
||||
for (String recipient : m.getAddresses()) {
|
||||
try {
|
||||
GCubeUser toAdd = um.getUserByUsername(recipient);
|
||||
recipients.add(new MessageUserModel(toAdd.getUserId(), toAdd.getUsername(), toAdd.getFullname(), extractDomainFromEmail(toAdd.getEmail())));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
recipients.add(new MessageUserModel(recipient));
|
||||
}
|
||||
}
|
||||
MessageManagerClient client = AbstractPlugin.messages().build();
|
||||
Message m= client.get(messageId);
|
||||
MessageUserModel mu = null;
|
||||
GCubeUser sender = null;
|
||||
try {
|
||||
sender = um.getUserByUsername(m.getSender().getUserName());
|
||||
mu = new MessageUserModel(sender.getUserId(), sender.getUsername(), sender.getFullname(), extractDomainFromEmail(sender.getEmail()));
|
||||
} catch (Exception ex) {
|
||||
mu = new MessageUserModel(m.getSender().getUserName());
|
||||
}
|
||||
ArrayList<MessageUserModel> recipients = new ArrayList<>();
|
||||
for (String recipient : m.getAddresses()) {
|
||||
try {
|
||||
GCubeUser toAdd = um.getUserByUsername(recipient);
|
||||
recipients.add(new MessageUserModel(toAdd.getUserId(), toAdd.getUsername(), toAdd.getFullname(), extractDomainFromEmail(toAdd.getEmail())));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
recipients.add(new MessageUserModel(recipient));
|
||||
}
|
||||
}
|
||||
|
||||
ArrayList<FileModel> attachments = new ArrayList<>();
|
||||
List<String> attachItems = m.getAttachmentsIds();
|
||||
for (String itemId : attachItems) {
|
||||
WorkspaceItem item = workspace.getItem(itemId);
|
||||
String downloadURL = null; //removed for performanc issue and done on demand
|
||||
attachments.add(new FileModel(item.getId(), item.getName(), null, item.isFolder(), downloadURL));
|
||||
}
|
||||
boolean hasAttachments = !attachItems.isEmpty();
|
||||
toReturn = new ConvMessage(
|
||||
m.getId(),
|
||||
m.getSubject(),
|
||||
mu,
|
||||
recipients,
|
||||
new Date(m.getSendTime().getTimeInMillis()),
|
||||
m.getBody(),
|
||||
m.isRead(),
|
||||
attachments,
|
||||
hasAttachments);
|
||||
if (!sent)
|
||||
m.setStatus(true); //marked as read
|
||||
|
||||
ArrayList<FileModel> attachments = new ArrayList<>();
|
||||
List<? extends Item> attachItems = client.getAttachments(messageId);
|
||||
|
||||
for (Item item : attachItems) {
|
||||
String downloadURL = null; //removed for performanc issue and done on demand
|
||||
attachments.add(new FileModel(item.getId(), item.getName(), null, item instanceof FolderItem, downloadURL));
|
||||
}
|
||||
boolean hasAttachments = !attachItems.isEmpty();
|
||||
toReturn = new ConvMessage(
|
||||
m.getId(),
|
||||
m.getSubject(),
|
||||
mu,
|
||||
recipients,
|
||||
new Date(m.getCreationTime().getTimeInMillis()),
|
||||
m.getBody(),
|
||||
m.isRead(),
|
||||
attachments,
|
||||
hasAttachments);
|
||||
//TODO: update this
|
||||
if (!sent) {
|
||||
m.setRead(true); //marked as rea */
|
||||
client.setRead(messageId, true);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -298,11 +282,10 @@ public class ConvServiceImpl extends RemoteServiceServlet implements MessageServ
|
|||
String scope = pContext.getCurrentScope(getThreadLocalRequest());
|
||||
SecurityTokenProvider.instance.set(pContext.getCurrentUserToken(scope, user.getUserId()));
|
||||
try {
|
||||
HomeManagerFactory factory = HomeLibrary.getHomeManagerFactory();
|
||||
HomeManager manager = factory.getHomeManager();
|
||||
Workspace workspace = manager.getHome().getWorkspace();
|
||||
WorkspaceItem copied = workspace.copy(itemId, destinationFolderId);
|
||||
return (copied != null);
|
||||
StorageHubClient shc = new StorageHubClient();
|
||||
FileContainer fileContainer = shc.open(itemId).asFile();
|
||||
FileContainer copiedFile = fileContainer.copy(shc.open(destinationFolderId).asFolder(), fileContainer.get().getTitle());
|
||||
return (copiedFile.get() != null);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
|
@ -316,14 +299,8 @@ public class ConvServiceImpl extends RemoteServiceServlet implements MessageServ
|
|||
String scope = pContext.getCurrentScope(getThreadLocalRequest());
|
||||
SecurityTokenProvider.instance.set(pContext.getCurrentUserToken(scope, user.getUserId()));
|
||||
try {
|
||||
HomeManagerFactory factory = HomeLibrary.getHomeManagerFactory();
|
||||
HomeManager manager = factory.getHomeManager();
|
||||
WorkspaceMessageManager workspaceMessanger = manager.getHome().getWorkspace().getWorkspaceMessageManager();
|
||||
if (sent)
|
||||
workspaceMessanger.deleteSentMessage(messageId);
|
||||
else {
|
||||
workspaceMessanger.deleteReceivedMessage(messageId);
|
||||
}
|
||||
MessageManagerClient client = AbstractPlugin.messages().build();
|
||||
client.delete(messageId);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
|
@ -381,21 +358,19 @@ public class ConvServiceImpl extends RemoteServiceServlet implements MessageServ
|
|||
public boolean sendToById(ArrayList<String> recipientIds, ArrayList<String> listAttachmentsId, String subject, String body) {
|
||||
PortalContext pContext = PortalContext.getConfiguration();
|
||||
GCubeUser currentUser = pContext.getCurrentUser(getThreadLocalRequest());
|
||||
|
||||
if (listAttachmentsId == null)
|
||||
listAttachmentsId = new ArrayList<String>();
|
||||
MessageManagerClient client = AbstractPlugin.messages().build();
|
||||
try {
|
||||
Workspace workspace = HomeLibrary.getUserWorkspace(currentUser.getUsername());
|
||||
_log.info("Sending message to: " + recipientIds.toString());
|
||||
String checkedSubject = subject;
|
||||
String checkedBody = body;
|
||||
String messageId = workspace.getWorkspaceMessageManager().sendMessageToPortalLogins(checkedSubject, checkedBody, listAttachmentsId, recipientIds);
|
||||
try {
|
||||
body += getPublicLinksForAttachs(workspace, listAttachmentsId);
|
||||
}
|
||||
catch (InternalErrorException|ItemNotFoundException e) {
|
||||
String messageId = client.sendMessage(recipientIds, checkedSubject, checkedBody, listAttachmentsId);
|
||||
|
||||
/*try {
|
||||
body += getPublicLinksForAttachs(listAttachmentsId);
|
||||
|
||||
catch (StorageHubException e) {
|
||||
_log.error("Ops, could not generate publick link for some of the attachments");
|
||||
}
|
||||
}*/
|
||||
_log.debug("Sending message notification to: " + recipientIds.toString());
|
||||
|
||||
List<GenericItemBean> recipients = getUsersbyUserId(recipientIds);
|
||||
|
@ -410,7 +385,7 @@ public class ConvServiceImpl extends RemoteServiceServlet implements MessageServ
|
|||
Thread thread = new Thread(new MessageNotificationsThread(recipients, messageId, checkedSubject, body, nm));
|
||||
thread.start();
|
||||
|
||||
return (messageId != null);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
_log.error("While Sending message to: " + recipientIds.toString());
|
||||
e.printStackTrace();
|
||||
|
@ -454,45 +429,39 @@ public class ConvServiceImpl extends RemoteServiceServlet implements MessageServ
|
|||
* @throws ItemNotFoundException
|
||||
* @throws InternalErrorException
|
||||
*/
|
||||
private String getPublicLinksForAttachs(Workspace workspace, ArrayList<String> listAttachmentsId) throws ItemNotFoundException, InternalErrorException{
|
||||
|
||||
if (listAttachmentsId != null && (!listAttachmentsId.isEmpty()) ) {
|
||||
List<WorkspaceItem> attachments = new ArrayList<WorkspaceItem>();
|
||||
for (String itemId : listAttachmentsId) {
|
||||
attachments.add(workspace.getItem(itemId));
|
||||
}
|
||||
/*
|
||||
private String getPublicLinksForAttachs(ArrayList<String> listAttachments) {
|
||||
|
||||
|
||||
if (listAttachments != null && (!listAttachments.isEmpty()) ) {
|
||||
StorageHubClient client = new StorageHubClient();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
if(attachments!=null && attachments.size() > 0){
|
||||
builder.append("\n\n\nThe following ");
|
||||
String msg = attachments.size()>1?"files were attached to this message:":"file was attached to this message:";
|
||||
builder.append(msg+"\n");
|
||||
for (WorkspaceItem workspaceItem : attachments) {
|
||||
builder.append("\n\n\nThe following ");
|
||||
String msg = listAttachments.size()>1?"files were attached to this message:":"file was attached to this message:";
|
||||
builder.append(msg+"\n");
|
||||
for (String itemId : listAttachments) {
|
||||
try {
|
||||
OpenResolver openResolver = client.open(itemId);
|
||||
|
||||
if(openResolver.asItem().getType()==ContainerType.FOLDER) {
|
||||
|
||||
String publicLink = openResolver.getPublickLink(itemId).toString();;
|
||||
String itemName = item.getTitle();
|
||||
|
||||
if(workspaceItem.getType().equals(WorkspaceItemType.FOLDER_ITEM)) {
|
||||
FolderItem folderItem = (FolderItem) workspaceItem;
|
||||
String publicLink = "";
|
||||
String itemName = "";
|
||||
try {
|
||||
itemName = workspaceItem.getName();
|
||||
publicLink = folderItem.getPublicLink(true);
|
||||
}
|
||||
catch (InternalErrorException e) {
|
||||
_log.warn("An error occurred when creating public link for attachment, skipping file: " + itemName);
|
||||
return "";
|
||||
}
|
||||
builder.append(itemName + " ("+publicLink+")");
|
||||
builder.append("\n");
|
||||
}catch (StorageHubException e) {
|
||||
_log.error("While reading attachments");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
_log.debug("returning public links: "+builder.toString());
|
||||
return builder.toString();
|
||||
}
|
||||
else return "";
|
||||
_log.debug("returning public links: "+builder.toString());
|
||||
return builder.toString();
|
||||
}
|
||||
else return "";
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue