2015-06-24 15:16:13 +02:00
|
|
|
/**
|
2016-01-15 11:13:48 +01:00
|
|
|
*
|
2015-06-24 15:16:13 +02:00
|
|
|
*/
|
|
|
|
package org.gcube.portlets.widgets.wsexplorer.server;
|
|
|
|
|
2016-03-10 16:53:35 +01:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
2015-06-24 15:16:13 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2018-05-18 18:31:46 +02:00
|
|
|
import org.gcube.common.storagehub.client.plugins.AbstractPlugin;
|
|
|
|
import org.gcube.common.storagehub.client.proxies.ItemManagerClient;
|
|
|
|
import org.gcube.common.storagehub.model.items.AbstractFileItem;
|
2018-05-23 16:34:06 +02:00
|
|
|
import org.gcube.common.storagehub.model.items.ExternalURL;
|
2018-05-18 18:31:46 +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;
|
2015-06-24 15:16:13 +02:00
|
|
|
import org.gcube.portlets.widgets.wsexplorer.client.Util;
|
2015-06-24 15:18:42 +02:00
|
|
|
import org.gcube.portlets.widgets.wsexplorer.client.WorkspaceExplorerConstants;
|
2018-05-18 18:31:46 +02:00
|
|
|
import org.gcube.portlets.widgets.wsexplorer.server.stohub.Whorespace;
|
2015-06-24 15:16:13 +02:00
|
|
|
import org.gcube.portlets.widgets.wsexplorer.shared.FilterCriteria;
|
|
|
|
import org.gcube.portlets.widgets.wsexplorer.shared.ItemType;
|
2015-06-30 18:10:45 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-06-24 15:16:13 +02:00
|
|
|
|
|
|
|
/**
|
2015-06-25 12:33:18 +02:00
|
|
|
* The Class ItemBuilder.
|
|
|
|
*
|
2018-05-21 18:45:16 +02:00
|
|
|
* @author Francesco Mangiacrapa, CNR-ISTI
|
|
|
|
* @author M. Assante, CNR-ISTI
|
2015-06-24 15:16:13 +02:00
|
|
|
*/
|
|
|
|
public class ItemBuilder {
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2018-05-21 18:45:16 +02:00
|
|
|
public static final Logger _log = LoggerFactory.getLogger(ItemBuilder.class);
|
2015-06-24 15:16:13 +02:00
|
|
|
/**
|
2015-07-29 16:23:29 +02:00
|
|
|
* Gets the item.
|
2015-06-25 12:33:18 +02:00
|
|
|
*
|
|
|
|
* @param parent the parent
|
|
|
|
* @param workspaceItem the workspace item
|
2016-02-03 17:08:33 +01:00
|
|
|
* @param workspaceItemPath the workspace item path
|
2015-06-25 12:33:18 +02:00
|
|
|
* @param showableTypes the showable types
|
|
|
|
* @param filterCriteria the filter criteria
|
|
|
|
* @param loadChildren the load children
|
2017-02-06 17:33:04 +01:00
|
|
|
* @param loadGcubeProperties the load gcube properties
|
2015-06-25 12:33:18 +02:00
|
|
|
* @return the item
|
|
|
|
* @throws InternalErrorException the internal error exception
|
2015-06-24 15:16:13 +02:00
|
|
|
*/
|
2018-05-21 18:45:16 +02:00
|
|
|
public static org.gcube.portlets.widgets.wsexplorer.shared.Item getItem(org.gcube.portlets.widgets.wsexplorer.shared.Item parent, Item workspaceItem, String workspaceItemPath,
|
2018-05-18 18:31:46 +02:00
|
|
|
List<ItemType> showableTypes, FilterCriteria filterCriteria,
|
|
|
|
boolean loadChildren, boolean loadGcubeProperties) {
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2015-06-24 15:16:13 +02:00
|
|
|
ItemType type = getItemType(workspaceItem);
|
2016-01-15 11:13:48 +01:00
|
|
|
|
|
|
|
if (!showableTypes.contains(type)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!filterItem(type, workspaceItem, filterCriteria)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-18 18:31:46 +02:00
|
|
|
boolean isFolder = type.equals(ItemType.PRIVATE_FOLDER)?true:false;
|
|
|
|
boolean isSharedFolder = (type.equals(ItemType.SHARED_FOLDER) || type.equals(ItemType.VRE_FOLDER )) ? true : false;
|
2018-05-21 18:45:16 +02:00
|
|
|
if (isSharedFolder)
|
|
|
|
isFolder = true;
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2015-10-05 16:55:30 +02:00
|
|
|
String itemName = workspaceItem.getName();
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2015-10-05 16:55:30 +02:00
|
|
|
if(isSharedFolder){
|
2018-05-21 18:45:16 +02:00
|
|
|
_log.info("Is shared folder: "+workspaceItem.getTitle());
|
2018-05-18 18:31:46 +02:00
|
|
|
SharedFolder shared = (SharedFolder) workspaceItem;
|
2018-05-21 18:45:16 +02:00
|
|
|
itemName = shared.isVreFolder() ? shared.getDisplayName() : workspaceItem.getTitle();
|
2015-10-05 16:55:30 +02:00
|
|
|
}
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2018-05-21 18:45:16 +02:00
|
|
|
org.gcube.portlets.widgets.wsexplorer.shared.Item item = null;
|
2016-02-03 12:20:42 +01:00
|
|
|
try{
|
2018-05-21 18:45:16 +02:00
|
|
|
item = new org.gcube.portlets.widgets.wsexplorer.shared.Item(parent, workspaceItem.getId(), itemName, type, workspaceItemPath, UserUtil.getUserFullName(workspaceItem.getOwner()), toDate(workspaceItem.getCreationTime()), isFolder, false);
|
2016-02-03 12:20:42 +01:00
|
|
|
item.setSharedFolder(isSharedFolder);
|
2016-05-03 15:29:24 +02:00
|
|
|
|
|
|
|
if(loadGcubeProperties){
|
|
|
|
Map<String, String> itemProperties = getGcubePropertiesForItem(workspaceItem);
|
|
|
|
item.setGcubeProperties(itemProperties);
|
|
|
|
}
|
2016-02-03 12:20:42 +01:00
|
|
|
}catch(Exception e){
|
2018-05-21 18:45:16 +02:00
|
|
|
_log.error("Error on getting item: "+itemName+" with id: "+workspaceItem.getId()+", from HL, so skipping item");
|
2016-02-03 12:20:42 +01:00
|
|
|
return null;
|
2016-01-15 11:13:48 +01:00
|
|
|
}
|
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
if(loadChildren){
|
2018-05-18 18:31:46 +02:00
|
|
|
String itemId = item.getId();
|
|
|
|
ItemManagerClient client = AbstractPlugin.item().build();
|
|
|
|
List<? extends Item> theChildren = null;
|
|
|
|
theChildren = client.getChildren(itemId, Whorespace.ACCOUNTING_HL_NODE_NAME);
|
|
|
|
for (Item child : theChildren) {
|
2016-02-29 17:15:57 +01:00
|
|
|
String itemPath = workspaceItemPath+"/"+child.getName();
|
2018-05-21 18:45:16 +02:00
|
|
|
org.gcube.portlets.widgets.wsexplorer.shared.Item itemChild = getItem(item, child, itemPath, showableTypes, filterCriteria, false, loadGcubeProperties);
|
|
|
|
_log.trace("Item: "+child.getName() +" converted!!!");
|
2015-06-25 12:33:18 +02:00
|
|
|
if (itemChild!=null){
|
|
|
|
item.addChild(itemChild);
|
|
|
|
}
|
2015-06-24 15:16:13 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2015-06-24 15:16:13 +02:00
|
|
|
return item;
|
|
|
|
}
|
2015-06-25 12:33:18 +02:00
|
|
|
|
2017-07-06 16:16:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the item.
|
|
|
|
*
|
|
|
|
* @param parent the parent
|
|
|
|
* @param workspaceItem the workspace item
|
|
|
|
* @param workspaceItemPath the workspace item path
|
|
|
|
* @param showableTypes the showable types
|
|
|
|
* @param filterCriteria the filter criteria
|
|
|
|
* @param loadChildren the load children
|
|
|
|
* @param loadGcubeProperties the load gcube properties
|
|
|
|
* @param startIdx the start idx
|
|
|
|
* @param limit the limit
|
|
|
|
* @return the item
|
|
|
|
* @throws InternalErrorException the internal error exception
|
|
|
|
*/
|
2018-05-21 18:45:16 +02:00
|
|
|
public static org.gcube.portlets.widgets.wsexplorer.shared.Item getItem(org.gcube.portlets.widgets.wsexplorer.shared.Item parent, Item workspaceItem, String workspaceItemPath,
|
2018-05-18 18:31:46 +02:00
|
|
|
List<ItemType> showableTypes, FilterCriteria filterCriteria,
|
|
|
|
boolean loadChildren, boolean loadGcubeProperties, int startIdx, int limit) {
|
2017-07-06 16:16:37 +02:00
|
|
|
|
|
|
|
ItemType type = getItemType(workspaceItem);
|
|
|
|
|
|
|
|
if (!showableTypes.contains(type)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!filterItem(type, workspaceItem, filterCriteria)) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-05-18 18:31:46 +02:00
|
|
|
boolean isFolder = type.equals(ItemType.PRIVATE_FOLDER)?true:false;
|
|
|
|
boolean isSharedFolder = (type.equals(ItemType.SHARED_FOLDER) || type.equals(ItemType.VRE_FOLDER )) ? true : false;
|
2017-07-06 16:16:37 +02:00
|
|
|
|
|
|
|
String itemName = workspaceItem.getName();
|
|
|
|
|
|
|
|
if(isSharedFolder){
|
2018-05-21 18:45:16 +02:00
|
|
|
_log.info("Is shared folder: "+workspaceItem.getTitle());
|
2018-05-18 18:31:46 +02:00
|
|
|
SharedFolder shared = (SharedFolder) workspaceItem;
|
|
|
|
itemName = shared.isVreFolder()?shared.getDisplayName():workspaceItem.getTitle();
|
2018-05-21 18:45:16 +02:00
|
|
|
isFolder = true;
|
2017-07-06 16:16:37 +02:00
|
|
|
}
|
2018-05-22 16:47:23 +02:00
|
|
|
|
2018-05-18 18:31:46 +02:00
|
|
|
// _log.debug("Building Item for: "+itemName);
|
2018-05-21 18:45:16 +02:00
|
|
|
org.gcube.portlets.widgets.wsexplorer.shared.Item item = null;
|
2017-07-06 16:16:37 +02:00
|
|
|
try{
|
2018-05-21 18:45:16 +02:00
|
|
|
item = new org.gcube.portlets.widgets.wsexplorer.shared.Item(
|
|
|
|
parent, workspaceItem.getId(), itemName, type, workspaceItemPath,
|
|
|
|
UserUtil.getUserFullName(workspaceItem.getOwner()), toDate(workspaceItem.getCreationTime()), isFolder, false);
|
2017-07-06 16:16:37 +02:00
|
|
|
item.setSharedFolder(isSharedFolder);
|
|
|
|
|
|
|
|
if(loadGcubeProperties){
|
|
|
|
Map<String, String> itemProperties = getGcubePropertiesForItem(workspaceItem);
|
|
|
|
item.setGcubeProperties(itemProperties);
|
|
|
|
}
|
|
|
|
}catch(Exception e){
|
2018-05-21 18:45:16 +02:00
|
|
|
_log.error("Error on getting item: "+itemName+" with id: "+workspaceItem.getId()+", from HL, so skipping item");
|
2017-07-06 16:16:37 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(loadChildren){
|
2018-05-18 18:31:46 +02:00
|
|
|
String itemId = item.getId();
|
|
|
|
ItemManagerClient client = AbstractPlugin.item().build();
|
|
|
|
List<? extends Item> theChildren = null;
|
2018-05-23 12:18:06 +02:00
|
|
|
theChildren = client.getChildren(itemId, startIdx, limit, Whorespace.ACCOUNTING_HL_NODE_NAME);
|
2018-05-18 18:31:46 +02:00
|
|
|
for (Item child : theChildren) {
|
2017-07-06 16:16:37 +02:00
|
|
|
String itemPath = workspaceItemPath+"/"+child.getName();
|
2018-05-21 18:45:16 +02:00
|
|
|
org.gcube.portlets.widgets.wsexplorer.shared.Item itemChild = getItem(item, child, itemPath, showableTypes, filterCriteria, false, loadGcubeProperties);
|
|
|
|
_log.trace("Item: "+child.getName() +" converted!!!");
|
2017-07-06 16:16:37 +02:00
|
|
|
if (itemChild!=null){
|
|
|
|
item.addChild(itemChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
/**
|
|
|
|
* Gets the item type.
|
|
|
|
*
|
|
|
|
* @param item the item
|
|
|
|
* @return the item type
|
|
|
|
* @throws InternalErrorException the internal error exception
|
|
|
|
*/
|
2018-05-22 19:01:28 +02:00
|
|
|
public static ItemType getItemType(Item item) {
|
2018-05-18 18:31:46 +02:00
|
|
|
if (item instanceof AbstractFileItem) {
|
2018-05-21 18:45:16 +02:00
|
|
|
return getFileIconImageType( item);
|
2015-06-24 15:16:13 +02:00
|
|
|
}
|
2018-05-18 18:31:46 +02:00
|
|
|
else if (item instanceof FolderItem) {
|
|
|
|
return getFolderItemType(item);
|
|
|
|
}
|
2018-05-23 16:34:06 +02:00
|
|
|
_log.warn("Item Type non found: " + item.toString());
|
2018-05-18 18:31:46 +02:00
|
|
|
return ItemType.UNKNOWN_TYPE;
|
|
|
|
|
2015-06-24 15:16:13 +02:00
|
|
|
}
|
2018-05-21 18:45:16 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param item
|
|
|
|
* @return the item type based on the extension of the file
|
|
|
|
*/
|
|
|
|
private static ItemType getFileIconImageType(Item item) {
|
2018-05-23 16:34:06 +02:00
|
|
|
if (item instanceof ExternalURL)
|
|
|
|
return ItemType.EXTERNAL_URL;
|
|
|
|
AbstractFileItem aItem = (AbstractFileItem) item;
|
|
|
|
if (aItem.getContent().getMimeType().compareTo("application/xml") == 0) {
|
|
|
|
return ItemType.XML;
|
|
|
|
}
|
2018-05-21 18:45:16 +02:00
|
|
|
String[] splits = item.getName().split("\\.");
|
|
|
|
String extension = "";
|
|
|
|
if (splits.length > 0) {
|
|
|
|
extension = splits[splits.length-1];
|
|
|
|
}
|
|
|
|
if (extension == null || extension.compareTo("") == 0)
|
|
|
|
return ItemType.UNKNOWN_TYPE;
|
|
|
|
extension = extension.toLowerCase();
|
|
|
|
switch (extension) {
|
|
|
|
case "doc":
|
|
|
|
case "docx":
|
|
|
|
return ItemType.DOCUMENT;
|
|
|
|
case "rtf":
|
|
|
|
case "txt":
|
|
|
|
return ItemType.TEXT_PLAIN;
|
|
|
|
case "xls":
|
|
|
|
case "xlsx":
|
|
|
|
return ItemType.SPREADSHEET;
|
|
|
|
case "csv":
|
|
|
|
return ItemType.CSV;
|
|
|
|
case "ics":
|
|
|
|
return ItemType.CALENDAR;
|
|
|
|
case "ppt":
|
|
|
|
case "pptx":
|
|
|
|
return ItemType.PRESENTATION;
|
|
|
|
case "pdf":
|
|
|
|
return ItemType.PDF_DOCUMENT;
|
|
|
|
case "jpg":
|
|
|
|
case "jpeg":
|
|
|
|
case "gif":
|
|
|
|
case "bmp":
|
|
|
|
case "png":
|
|
|
|
case "tif":
|
|
|
|
case "tiff":
|
|
|
|
return ItemType.IMAGE_DOCUMENT;
|
|
|
|
case "avi":
|
|
|
|
case "mp4":
|
|
|
|
case "mpeg":
|
|
|
|
return ItemType.MOVIE;
|
|
|
|
case "html":
|
|
|
|
case "htm":
|
|
|
|
case "jsp":
|
|
|
|
return ItemType.HTML;
|
|
|
|
case "rar":
|
|
|
|
return ItemType.RAR;
|
|
|
|
case "zip":
|
|
|
|
case "tar":
|
|
|
|
case "tar.gz":
|
|
|
|
case ".cpgz":
|
|
|
|
case ".gz":
|
|
|
|
return ItemType.ZIP;
|
|
|
|
default:
|
|
|
|
return ItemType.UNKNOWN_TYPE;
|
|
|
|
}
|
|
|
|
}
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
/**
|
|
|
|
* Gets the folder item type.
|
|
|
|
*
|
|
|
|
* @param item the item
|
|
|
|
* @return the folder item type
|
|
|
|
*/
|
2018-05-18 18:31:46 +02:00
|
|
|
protected static ItemType getFolderItemType(Item item){
|
|
|
|
if (item instanceof SharedFolder || item instanceof VreFolder) {
|
|
|
|
SharedFolder folder = (SharedFolder) item;
|
2018-05-21 18:45:16 +02:00
|
|
|
if (folder.isVreFolder())
|
2018-05-18 18:31:46 +02:00
|
|
|
return ItemType.VRE_FOLDER;
|
|
|
|
return ItemType.SHARED_FOLDER;
|
|
|
|
} else if (item instanceof FolderItem) {
|
|
|
|
return ItemType.PRIVATE_FOLDER;
|
2015-07-10 12:31:47 +02:00
|
|
|
}
|
2018-05-21 18:45:16 +02:00
|
|
|
_log.warn("Item Type non found: ");
|
|
|
|
return ItemType.PRIVATE_FOLDER;
|
2015-06-24 15:16:13 +02:00
|
|
|
}
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2015-07-29 16:23:29 +02:00
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
/**
|
|
|
|
* Filter item.
|
|
|
|
*
|
|
|
|
* @param type the type
|
|
|
|
* @param item the item
|
|
|
|
* @param filterCriteria the filter criteria
|
|
|
|
* @return true, if successful
|
|
|
|
* @throws InternalErrorException the internal error exception
|
|
|
|
*/
|
2018-05-18 18:31:46 +02:00
|
|
|
protected static boolean filterItem(ItemType type, Item item, FilterCriteria filterCriteria) {
|
2016-01-15 11:13:48 +01:00
|
|
|
if(filterCriteria==null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:16:13 +02:00
|
|
|
boolean mimeTypeCheck = checkAllowedMimeTypes(type, item, filterCriteria.getAllowedMimeTypes());
|
2016-01-15 11:13:48 +01:00
|
|
|
if (!mimeTypeCheck) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-29 16:23:29 +02:00
|
|
|
boolean fileExtensionCheck = checkAllowedFileExtension(type, item, filterCriteria.getAllowedFileExtensions());
|
2016-01-15 11:13:48 +01:00
|
|
|
if(!fileExtensionCheck) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean propertiesCheck = checkProperties(item, filterCriteria.getRequiredProperties());
|
2015-06-24 15:16:13 +02:00
|
|
|
return propertiesCheck;
|
|
|
|
}
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
/**
|
|
|
|
* Check allowed mime types.
|
|
|
|
*
|
|
|
|
* @param type the type
|
|
|
|
* @param item the item
|
|
|
|
* @param allowedMimeTypes the allowed mime types
|
|
|
|
* @return true, if successful
|
|
|
|
*/
|
2018-05-18 18:31:46 +02:00
|
|
|
protected static boolean checkAllowedMimeTypes(ItemType type, Item item, List<String> allowedMimeTypes){
|
2016-01-15 11:13:48 +01:00
|
|
|
if (allowedMimeTypes==null || allowedMimeTypes.size()==0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-18 18:31:46 +02:00
|
|
|
if (type == ItemType.EXTERNAL_FILE) {
|
|
|
|
String mimeType = "unknown"; //TODO
|
2015-06-24 15:16:13 +02:00
|
|
|
return allowedMimeTypes.contains(mimeType);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-15 11:13:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-07-29 16:23:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check allowed file extension.
|
|
|
|
*
|
|
|
|
* @param type the type
|
|
|
|
* @param item the item
|
|
|
|
* @param allowedFileExtension the allowed mime types
|
|
|
|
* @return true, if successful
|
|
|
|
*/
|
2018-05-18 18:31:46 +02:00
|
|
|
protected static boolean checkAllowedFileExtension(ItemType type, Item item, List<String> allowedFileExtension){
|
2016-01-15 11:13:48 +01:00
|
|
|
if (allowedFileExtension==null || allowedFileExtension.size()==0) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-29 16:23:29 +02:00
|
|
|
try {
|
2018-05-22 19:01:28 +02:00
|
|
|
if (! (item instanceof FolderItem)) {
|
2015-07-29 16:23:29 +02:00
|
|
|
String name = item.getName();
|
|
|
|
return checkFileExtension(name, allowedFileExtension);
|
|
|
|
}
|
|
|
|
return true;
|
2018-05-18 18:31:46 +02:00
|
|
|
} catch (Exception e) {
|
2018-05-21 18:45:16 +02:00
|
|
|
_log.error("checkAllowedFileExtension, InternalErrorException: ",e);
|
2015-07-29 16:23:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2015-07-29 16:23:29 +02:00
|
|
|
/**
|
|
|
|
* Check file extension.
|
|
|
|
*
|
|
|
|
* @param fileName the file name
|
|
|
|
* @param allowedFileExtension the allowed file extension
|
|
|
|
* @return true, if successful
|
|
|
|
*/
|
|
|
|
protected static boolean checkFileExtension(String fileName, List<String> allowedFileExtension){
|
2016-01-15 11:13:48 +01:00
|
|
|
|
|
|
|
if(fileName==null || fileName.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-29 16:23:29 +02:00
|
|
|
int dot = fileName.lastIndexOf(".");
|
2016-01-15 11:13:48 +01:00
|
|
|
if(dot>=0 && dot+1<=fileName.length()){
|
|
|
|
|
2015-07-29 16:23:29 +02:00
|
|
|
String ext = fileName.substring(dot+1, fileName.length());
|
2018-05-21 18:45:16 +02:00
|
|
|
_log.trace("Extension found: "+ext +" for: "+fileName);
|
2018-05-18 18:31:46 +02:00
|
|
|
// if(ext.isEmpty())
|
|
|
|
// return false;
|
2015-07-29 16:23:29 +02:00
|
|
|
for (String fe : allowedFileExtension) {
|
2016-01-15 11:13:48 +01:00
|
|
|
if(ext.compareTo(fe)==0) {
|
2015-07-29 16:23:29 +02:00
|
|
|
return true;
|
2016-01-15 11:13:48 +01:00
|
|
|
}
|
2015-07-29 16:23:29 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-21 18:45:16 +02:00
|
|
|
_log.trace("Extension not found for: "+fileName);
|
2015-07-29 16:23:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-15 11:13:48 +01:00
|
|
|
|
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
/**
|
|
|
|
* Check properties.
|
|
|
|
*
|
|
|
|
* @param item the item
|
|
|
|
* @param requestedProperties the requested properties
|
|
|
|
* @return true, if successful
|
|
|
|
* @throws InternalErrorException the internal error exception
|
|
|
|
*/
|
2018-05-18 18:31:46 +02:00
|
|
|
protected static boolean checkProperties(Item item, Map<String, String> requestedProperties)
|
2015-06-24 15:16:13 +02:00
|
|
|
{
|
2018-05-18 18:31:46 +02:00
|
|
|
if (requestedProperties==null || requestedProperties.size()==0 || (item instanceof FolderItem)) {
|
2016-01-15 11:13:48 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-03 15:29:24 +02:00
|
|
|
Map<String, String> itemProperties = getGcubePropertiesForItem(item);
|
|
|
|
|
|
|
|
if(itemProperties==null)
|
|
|
|
return false;
|
|
|
|
|
2015-06-24 15:16:13 +02:00
|
|
|
for (Entry<String, String> requestProperty:requestedProperties.entrySet()) {
|
|
|
|
String propertyValue = itemProperties.get(requestProperty.getKey());
|
2016-01-15 11:13:48 +01:00
|
|
|
if (propertyValue == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!propertyValue.equals(requestProperty.getValue())) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-24 15:16:13 +02:00
|
|
|
}
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2015-06-24 15:16:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
|
2016-05-03 15:29:24 +02:00
|
|
|
/**
|
|
|
|
* Gets the gcube properties for item.
|
|
|
|
*
|
|
|
|
* @param item the item
|
|
|
|
* @return the gcube properties for item
|
2017-02-06 17:33:04 +01:00
|
|
|
* @throws InternalErrorException the internal error exception
|
2016-05-03 15:29:24 +02:00
|
|
|
*/
|
2018-05-18 18:31:46 +02:00
|
|
|
protected static Map<String,String> getGcubePropertiesForItem(Item item) {
|
|
|
|
return null;
|
|
|
|
//TODO: see how to return this later
|
|
|
|
// try {
|
|
|
|
// return item.getProperties().getProperties();
|
|
|
|
// }
|
|
|
|
// catch (InternalErrorException e) {
|
|
|
|
// logger.warn("An error occurred during get properties for item: "+item.getId()+", returning null");
|
|
|
|
// return null;
|
|
|
|
// }
|
2016-05-03 15:29:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the folder to breadcrumbs.
|
|
|
|
*
|
|
|
|
* @param wsFolder the ws folder
|
|
|
|
* @param parent the parent
|
|
|
|
* @return the item
|
|
|
|
* @throws InternalErrorException the internal error exception
|
|
|
|
*/
|
2018-05-21 18:45:16 +02:00
|
|
|
public static org.gcube.portlets.widgets.wsexplorer.shared.Item buildFolderForBreadcrumbs(FolderItem wsFolder, org.gcube.portlets.widgets.wsexplorer.shared.Item parent){
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
String name = "";
|
2015-06-25 18:17:10 +02:00
|
|
|
boolean isSpecialFolder = false;
|
2015-06-30 18:10:45 +02:00
|
|
|
boolean isRoot = false;
|
2015-06-30 14:52:40 +02:00
|
|
|
|
2018-05-18 18:31:46 +02:00
|
|
|
if(wsFolder.getParentId() == null){ //IS ROOT
|
2015-06-30 14:52:40 +02:00
|
|
|
name = WorkspaceExplorerConstants.HOME_LABEL;
|
2015-06-30 18:10:45 +02:00
|
|
|
isRoot = true;
|
2018-05-22 16:47:23 +02:00
|
|
|
}
|
2018-05-22 19:01:28 +02:00
|
|
|
if(isSpecialFolder(wsFolder)){
|
|
|
|
name = WorkspaceExplorerConstants.VRE_FOLDERS_LABEL;
|
|
|
|
isSpecialFolder = true;
|
|
|
|
}
|
2018-05-22 16:47:23 +02:00
|
|
|
else if(wsFolder.isShared()){ //MANAGEMENT SHARED FOLDER NAME
|
|
|
|
if (wsFolder instanceof SharedFolder) {
|
|
|
|
SharedFolder shared = (SharedFolder) wsFolder;
|
|
|
|
name = shared.isVreFolder() ? shared.getDisplayName() : shared.getTitle();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FolderItem shared = (FolderItem) wsFolder;
|
|
|
|
name = shared.getTitle();
|
|
|
|
}
|
2018-05-18 18:31:46 +02:00
|
|
|
//MANAGEMENT SPECIAL FOLDER
|
2016-01-15 11:13:48 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-06-25 12:33:18 +02:00
|
|
|
name = wsFolder.getName();
|
2016-01-15 11:13:48 +01:00
|
|
|
}
|
|
|
|
|
2015-06-30 14:52:40 +02:00
|
|
|
//BUILDS A SIMPLE ITEM FOR BREADCRUMB
|
2016-02-26 11:16:50 +01:00
|
|
|
String path = null; //wsFolder.getPath(); FORCED TO NULL BECAUSE IS SLOW CALL
|
2018-05-21 18:45:16 +02:00
|
|
|
org.gcube.portlets.widgets.wsexplorer.shared.Item item = new org.gcube.portlets.widgets.wsexplorer.shared.Item(null, wsFolder.getId(), name, ItemType.FOLDER, path, null, null, true, isRoot);
|
2015-06-25 18:17:10 +02:00
|
|
|
item.setSpecialFolder(isSpecialFolder);
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2018-05-21 18:45:16 +02:00
|
|
|
_log.debug("breadcrumb returning: "+item);
|
2015-06-25 18:17:10 +02:00
|
|
|
return item;
|
2015-06-25 12:33:18 +02:00
|
|
|
}
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2015-07-29 16:23:29 +02:00
|
|
|
/**
|
|
|
|
* Checks if is special folder.
|
|
|
|
*
|
|
|
|
* @param wsFolder the ws folder
|
|
|
|
* @return true, if is special folder
|
|
|
|
*/
|
2018-05-18 18:31:46 +02:00
|
|
|
public static boolean isSpecialFolder(FolderItem wsFolder){
|
2018-05-22 19:01:28 +02:00
|
|
|
return (wsFolder.getName().compareTo(WorkspaceExplorerConstants.SPECIAL_FOLDERS_NAME) == 0);
|
2015-06-30 18:10:45 +02:00
|
|
|
}
|
2016-03-10 16:53:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To date.
|
|
|
|
*
|
|
|
|
* @param calendar the calendar
|
|
|
|
* @return the date
|
|
|
|
*/
|
2018-05-21 18:45:16 +02:00
|
|
|
public static Date toDate(Calendar calendar) {
|
2016-03-10 16:53:35 +01:00
|
|
|
if (calendar == null) return null;
|
|
|
|
return calendar.getTime();
|
|
|
|
|
|
|
|
}
|
2018-05-21 18:45:16 +02:00
|
|
|
/**
|
|
|
|
* Purge empty folders.
|
|
|
|
*
|
|
|
|
* @param item the item
|
|
|
|
* @return the item
|
|
|
|
*/
|
|
|
|
public static org.gcube.portlets.widgets.wsexplorer.shared.Item purgeEmptyFolders(org.gcube.portlets.widgets.wsexplorer.shared.Item item) {
|
|
|
|
List<org.gcube.portlets.widgets.wsexplorer.shared.Item> toRemoveList = new LinkedList<org.gcube.portlets.widgets.wsexplorer.shared.Item>();
|
|
|
|
for (org.gcube.portlets.widgets.wsexplorer.shared.Item child:item.getChildren()) {
|
|
|
|
boolean toRemove = isAnEmptyFolder(child);
|
|
|
|
if (toRemove) {
|
|
|
|
toRemoveList.add(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (org.gcube.portlets.widgets.wsexplorer.shared.Item child:toRemoveList) {
|
|
|
|
item.removeChild(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if is an empty folder.
|
|
|
|
*
|
|
|
|
* @param item the item
|
|
|
|
* @return true, if is an empty folder
|
|
|
|
*/
|
|
|
|
protected static boolean isAnEmptyFolder(org.gcube.portlets.widgets.wsexplorer.shared.Item item) {
|
|
|
|
return Util.isFolder(item.getType()) && item.getChildren().size() == 0;
|
|
|
|
}
|
2015-07-29 16:23:29 +02:00
|
|
|
/*
|
|
|
|
public static void main(String[] args) {
|
|
|
|
List<String> allowedFileExtension = new ArrayList<String>();
|
|
|
|
allowedFileExtension.add("csv");
|
|
|
|
allowedFileExtension.add("");
|
2016-01-15 11:13:48 +01:00
|
|
|
|
2015-07-29 16:23:29 +02:00
|
|
|
String fileName = "t";
|
|
|
|
System.out.println(checkFileExtension(fileName, allowedFileExtension));
|
|
|
|
}*/
|
2017-07-06 16:16:37 +02:00
|
|
|
|
2015-06-24 15:16:13 +02:00
|
|
|
}
|