2018-04-13 16:59:33 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.portlets.user.wswidget;
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.gcube.common.storagehub.model.items.AbstractFileItem;
|
2019-11-15 16:40:37 +01:00
|
|
|
import org.gcube.common.storagehub.model.items.ExternalLink;
|
2018-05-23 17:06:09 +02:00
|
|
|
import org.gcube.common.storagehub.model.items.ExternalURL;
|
2018-04-13 16:59:33 +02:00
|
|
|
import org.gcube.common.storagehub.model.items.FolderItem;
|
|
|
|
import org.gcube.common.storagehub.model.items.Item;
|
|
|
|
import org.gcube.common.storagehub.model.items.SharedFolder;
|
|
|
|
import org.gcube.common.storagehub.model.items.VreFolder;
|
2018-05-23 17:06:09 +02:00
|
|
|
import org.gcube.portal.stohubicons.IconsManager;
|
|
|
|
import org.gcube.portal.stohubicons.shared.MDIcon;
|
2018-04-13 16:59:33 +02:00
|
|
|
import org.gcube.portlets.user.wswidget.shared.ItemType;
|
|
|
|
import org.gcube.portlets.user.wswidget.shared.WSItem;
|
|
|
|
import org.gcube.vomanagement.usermanagement.UserManager;
|
|
|
|
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
|
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
2021-04-06 10:39:35 +02:00
|
|
|
@SuppressWarnings("deprecation")
|
2018-04-13 16:59:33 +02:00
|
|
|
public class ItemBuilder {
|
|
|
|
|
|
|
|
public static final Logger _log = LoggerFactory.getLogger(ItemBuilder.class);
|
|
|
|
private static UserManager UMAN = new LiferayUserManager();
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param parent
|
|
|
|
* @param workspaceItem
|
|
|
|
* @param workspaceItemPath
|
|
|
|
* @param currentUserName
|
|
|
|
* @return
|
|
|
|
*/
|
2021-04-06 10:39:35 +02:00
|
|
|
@SuppressWarnings("deprecation")
|
2018-04-13 16:59:33 +02:00
|
|
|
public static WSItem getItem(WSItem parent, Item workspaceItem, String workspaceItemPath, String currentUserName) {
|
|
|
|
ItemType type = getItemType(workspaceItem);
|
|
|
|
boolean isFolder = type.equals(ItemType.PRIVATE_FOLDER)?true:false;
|
|
|
|
boolean isSharedFolder = (type.equals(ItemType.SHARED_FOLDER) || type.equals(ItemType.VRE_FOLDER )) ? true : false;
|
|
|
|
|
|
|
|
String itemName = workspaceItem.getName();
|
|
|
|
if(isSharedFolder){
|
2021-01-28 18:59:09 +01:00
|
|
|
_log.debug("Is shared folder: "+workspaceItem.getTitle());
|
2018-04-13 16:59:33 +02:00
|
|
|
SharedFolder shared = (SharedFolder) workspaceItem;
|
|
|
|
itemName = shared.isVreFolder()?shared.getDisplayName():workspaceItem.getTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
_log.debug("Building Item for: "+itemName + " id="+workspaceItem.getId());
|
|
|
|
WSItem item = null;
|
|
|
|
try{
|
|
|
|
String fullName = "me";
|
|
|
|
if (currentUserName.compareTo(workspaceItem.getOwner()) != 0) {
|
|
|
|
fullName = workspaceItem.getOwner();
|
|
|
|
try {
|
|
|
|
fullName = UMAN.getUserByUsername(workspaceItem.getOwner()).getFullname();
|
|
|
|
}
|
|
|
|
catch (UserRetrievalFault f) {
|
|
|
|
_log.warn("The user does not exist in this portal: " + fullName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
item = new WSItem(parent, workspaceItem.getId(), itemName, type, workspaceItemPath, fullName, toDate(workspaceItem.getCreationTime()), toDate(workspaceItem.getLastModificationTime()), isFolder, false);
|
|
|
|
item.setSharedFolder(isSharedFolder);
|
|
|
|
if (isSharedFolder) {
|
|
|
|
item.setIconNameAndColor("folder_shared", "#8F8F8F"); //gray
|
|
|
|
} else if (isFolder) {
|
|
|
|
item.setIconNameAndColor("folder", "#8F8F8F");// #gray
|
|
|
|
}
|
2018-05-23 17:06:09 +02:00
|
|
|
else { //is a file, a Link or an XML
|
|
|
|
MDIcon mdIcon = IconsManager.getDefault();
|
2019-11-15 16:40:37 +01:00
|
|
|
if (workspaceItem instanceof ExternalLink || workspaceItem instanceof ExternalURL) {
|
2018-05-23 17:06:09 +02:00
|
|
|
mdIcon = IconsManager.getIconTypeLink();
|
|
|
|
} else {
|
|
|
|
AbstractFileItem aItem = (AbstractFileItem) workspaceItem;
|
2018-06-06 15:13:12 +02:00
|
|
|
try {
|
|
|
|
String mimeType = aItem.getContent().getMimeType();
|
|
|
|
if (mimeType.compareTo("application/xml") == 0) {
|
|
|
|
mdIcon = IconsManager.getXMLTypeLink();
|
|
|
|
}
|
|
|
|
else if (mimeType.compareTo("application/zip") == 0
|
|
|
|
|| mimeType.compareTo("application/tar") == 0
|
|
|
|
|| mimeType.compareTo("application/x-gzip") == 0
|
|
|
|
|| mimeType.compareTo("application/tar+gzip") == 0
|
|
|
|
|| mimeType.compareTo("application/octet-stream") == 0
|
|
|
|
|| mimeType.compareTo("application/x-rar-compressed") == 0
|
|
|
|
|| mimeType.compareTo("application/x-tgz") == 0) {
|
|
|
|
mdIcon = new MDIcon("archive", "#ffc107"); //amber
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
String[] splits = item.getName().split("\\.");
|
|
|
|
String extension = "";
|
|
|
|
if (splits.length > 0) {
|
|
|
|
extension = splits[splits.length-1];
|
|
|
|
}
|
|
|
|
mdIcon = IconsManager.getMDIconTextualName(extension);
|
|
|
|
}
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
_log.warn("could not get mimeType for " + itemName);
|
2018-05-23 17:06:09 +02:00
|
|
|
String[] splits = item.getName().split("\\.");
|
|
|
|
String extension = "";
|
|
|
|
if (splits.length > 0) {
|
|
|
|
extension = splits[splits.length-1];
|
|
|
|
}
|
2018-06-06 15:13:12 +02:00
|
|
|
mdIcon = IconsManager.getMDIconTextualName(extension);
|
2018-05-23 17:06:09 +02:00
|
|
|
}
|
2018-04-13 16:59:33 +02:00
|
|
|
}
|
2018-05-23 17:06:09 +02:00
|
|
|
item.setIconNameAndColor(mdIcon.getTextualName(), mdIcon.getColor());
|
2018-04-13 16:59:33 +02:00
|
|
|
}
|
|
|
|
} catch(Exception e){
|
2018-06-06 15:13:12 +02:00
|
|
|
_log.error("Error on getting item: "+itemName+" with id: "+workspaceItem.getId()+", from StorageHub, so skipping item " + e.getMessage());
|
|
|
|
e.printStackTrace();
|
2018-04-13 16:59:33 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the item type.
|
|
|
|
*
|
|
|
|
* @param item the item
|
|
|
|
* @return the item type
|
|
|
|
* @throws InternalErrorException the internal error exception
|
|
|
|
*/
|
|
|
|
protected static ItemType getItemType(Item item) {
|
|
|
|
if (item instanceof AbstractFileItem) {
|
|
|
|
return ItemType.EXTERNAL_FILE;
|
|
|
|
}
|
|
|
|
else if (item instanceof FolderItem) {
|
|
|
|
return getFolderItemType(item);
|
|
|
|
}
|
|
|
|
_log.warn("Item Type non found: ");
|
|
|
|
return ItemType.UNKNOWN_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the folder item type.
|
|
|
|
*
|
|
|
|
* @param item the item
|
|
|
|
* @return the folder item type
|
|
|
|
*/
|
|
|
|
protected static ItemType getFolderItemType(Item item){
|
|
|
|
if (item instanceof SharedFolder || item instanceof VreFolder) {
|
|
|
|
SharedFolder folder = (SharedFolder) item;
|
|
|
|
if (folder.isVreFolder())
|
|
|
|
return ItemType.VRE_FOLDER;
|
|
|
|
return ItemType.SHARED_FOLDER;
|
|
|
|
} else if (item instanceof FolderItem) {
|
|
|
|
return ItemType.PRIVATE_FOLDER;
|
|
|
|
}
|
|
|
|
_log.warn("Item Type non found: ");
|
|
|
|
return ItemType.UNKNOWN_TYPE;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Check allowed mime types.
|
|
|
|
*
|
|
|
|
* @param type the type
|
|
|
|
* @param item the item
|
|
|
|
* @param allowedMimeTypes the allowed mime types
|
|
|
|
* @return true, if successful
|
|
|
|
*/
|
|
|
|
protected static boolean checkAllowedMimeTypes(ItemType type, Item item, List<String> allowedMimeTypes){
|
|
|
|
if (allowedMimeTypes==null || allowedMimeTypes.size()==0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (type == ItemType.EXTERNAL_FILE || type == ItemType.EXTERNAL_IMAGE || type == ItemType.EXTERNAL_PDF_FILE) {
|
|
|
|
AbstractFileItem externalFile = (AbstractFileItem)item;
|
|
|
|
String mimeType = externalFile.getContent().getMimeType();
|
|
|
|
return allowedMimeTypes.contains(mimeType);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* To date.
|
|
|
|
*
|
|
|
|
* @param calendar the calendar
|
|
|
|
* @return the date
|
|
|
|
*/
|
|
|
|
public static Date toDate(Calendar calendar) {
|
|
|
|
if (calendar == null) return null;
|
|
|
|
return calendar.getTime();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|