2015-06-24 15:16:13 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.portlets.widgets.wsexplorer.server;
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
|
|
import org.gcube.common.homelibary.model.items.type.WorkspaceItemType;
|
|
|
|
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
|
2015-06-25 12:33:18 +02:00
|
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder;
|
2015-06-24 15:16:13 +02:00
|
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
|
2015-06-25 12:33:18 +02:00
|
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceSharedFolder;
|
2015-06-24 15:16:13 +02:00
|
|
|
import org.gcube.common.homelibrary.home.workspace.folder.FolderItem;
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalFile;
|
|
|
|
import org.gcube.portlets.widgets.wsexplorer.client.Util;
|
2015-06-24 15:18:42 +02:00
|
|
|
import org.gcube.portlets.widgets.wsexplorer.client.WorkspaceExplorerConstants;
|
2015-06-24 15:16:13 +02:00
|
|
|
import org.gcube.portlets.widgets.wsexplorer.shared.FilterCriteria;
|
|
|
|
import org.gcube.portlets.widgets.wsexplorer.shared.Item;
|
|
|
|
import org.gcube.portlets.widgets.wsexplorer.shared.ItemType;
|
|
|
|
|
|
|
|
/**
|
2015-06-25 12:33:18 +02:00
|
|
|
* The Class ItemBuilder.
|
|
|
|
*
|
2015-06-24 15:16:13 +02:00
|
|
|
* @author Federico De Faveri defaveri@isti.cnr.it
|
|
|
|
* Modified by Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
|
|
*/
|
|
|
|
public class ItemBuilder {
|
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Purge empty folders.
|
|
|
|
*
|
|
|
|
* @param item the item
|
|
|
|
* @return the item
|
|
|
|
*/
|
2015-06-24 15:16:13 +02:00
|
|
|
public static Item purgeEmptyFolders(Item item)
|
|
|
|
{
|
2015-06-25 12:33:18 +02:00
|
|
|
//for (Item child:item.getChildren()) purgeEmptyFolders(child); ONLY FIRST LEVEL
|
2015-06-24 15:16:13 +02:00
|
|
|
|
|
|
|
List<Item> toRemoveList = new LinkedList<Item>();
|
|
|
|
for (Item child:item.getChildren()) {
|
|
|
|
boolean toRemove = isAnEmptyFolder(child);
|
|
|
|
if (toRemove) toRemoveList.add(child);
|
|
|
|
}
|
|
|
|
for (Item child:toRemoveList) item.removeChild(child);
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if is an empty folder.
|
|
|
|
*
|
|
|
|
* @param item the item
|
|
|
|
* @return true, if is an empty folder
|
|
|
|
*/
|
2015-06-24 15:16:13 +02:00
|
|
|
protected static boolean isAnEmptyFolder(Item item)
|
|
|
|
{
|
|
|
|
return Util.isFolder(item.getType()) && item.getChildren().size() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-25 12:33:18 +02:00
|
|
|
* Gets the item.
|
|
|
|
*
|
|
|
|
* @param parent the parent
|
|
|
|
* @param workspaceItem the workspace item
|
|
|
|
* @param showableTypes the showable types
|
|
|
|
* @param filterCriteria the filter criteria
|
|
|
|
* @param loadChildren the load children
|
|
|
|
* @return the item
|
|
|
|
* @throws InternalErrorException the internal error exception
|
2015-06-24 15:16:13 +02:00
|
|
|
*/
|
2015-06-25 12:33:18 +02:00
|
|
|
public static Item getItem(Item parent, WorkspaceItem workspaceItem, List<ItemType> showableTypes, FilterCriteria filterCriteria, boolean loadChildren) throws InternalErrorException
|
2015-06-24 15:16:13 +02:00
|
|
|
{
|
2015-06-25 12:33:18 +02:00
|
|
|
|
2015-06-24 15:16:13 +02:00
|
|
|
ItemType type = getItemType(workspaceItem);
|
|
|
|
|
|
|
|
if (!showableTypes.contains(type)) return null;
|
|
|
|
if (!filterItem(type, workspaceItem, filterCriteria)) return null;
|
|
|
|
|
|
|
|
// Added in date 26/06/2012 by Francesco Mangiacrapa
|
|
|
|
// Item item = new Item(parent, workspaceItem.getId(), workspaceItem.getName(), type, workspaceItem.getPath());
|
|
|
|
|
|
|
|
String itemName = workspaceItem.getName();
|
2015-06-24 15:18:42 +02:00
|
|
|
if ( workspaceItem.getName().equals(WorkspaceExplorerConstants.SPECIAL_FOLDERS_LABEL)) {
|
2015-06-24 15:16:13 +02:00
|
|
|
itemName = "My VRE Folders";
|
|
|
|
}
|
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
Item item = new Item(parent, workspaceItem.getId(), itemName, type, workspaceItem.getPath(), workspaceItem.getOwner().getPortalLogin(), workspaceItem.isFolder());
|
|
|
|
item.setSharedFolder(workspaceItem.getType()==WorkspaceItemType.SHARED_FOLDER);
|
2015-06-24 15:16:13 +02:00
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
/*
|
2015-06-24 15:16:13 +02:00
|
|
|
// Add temporary item
|
|
|
|
if(Util.isFolder(item.getType())){
|
|
|
|
empty = new Item(parent, UUID.randomUUID().toString(), "empty", ItemType.UNKNOWN_TYPE, "");
|
|
|
|
item.addChild(empty);
|
2015-06-25 12:33:18 +02:00
|
|
|
}*/
|
2015-06-24 15:16:13 +02:00
|
|
|
|
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
if(loadChildren){
|
|
|
|
for (WorkspaceItem child: workspaceItem.getChildren()){
|
|
|
|
Item itemChild = getItem(item, child, showableTypes, filterCriteria, false);
|
|
|
|
if (itemChild!=null){
|
|
|
|
// item.removeChild(empty);
|
|
|
|
item.addChild(itemChild);
|
|
|
|
}
|
2015-06-24 15:16:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
// // Remove the temporary item when we finish loading
|
|
|
|
// if(Util.isFolder(item.getType())&& item.getChildren().size()>1)
|
|
|
|
// item.removeChild(empty);
|
2015-06-24 15:16:13 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2015-06-24 15:16:13 +02:00
|
|
|
protected static ItemType getItemType(WorkspaceItem item) throws InternalErrorException
|
|
|
|
{
|
|
|
|
switch(item.getType())
|
|
|
|
{
|
|
|
|
case SHARED_FOLDER:
|
|
|
|
case FOLDER:{
|
|
|
|
if (item.isRoot()) return ItemType.ROOT;
|
|
|
|
return ItemType.FOLDER;
|
|
|
|
}
|
|
|
|
case FOLDER_ITEM: return getFolderItemType((FolderItem) item);
|
2015-06-25 12:33:18 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
return null;
|
2015-06-24 15:16:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 12:33:18 +02:00
|
|
|
/**
|
|
|
|
* Gets the folder item type.
|
|
|
|
*
|
|
|
|
* @param item the item
|
|
|
|
* @return the folder item type
|
|
|
|
*/
|
2015-06-24 15:16:13 +02:00
|
|
|
protected static ItemType getFolderItemType(FolderItem item)
|
|
|
|
{
|
|
|
|
//System.out.println("getFolderItemType "+item.getFolderItemType().toString());
|
|
|
|
return ItemType.valueOf(item.getFolderItemType().toString());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2015-06-24 15:16:13 +02:00
|
|
|
protected static boolean filterItem(ItemType type, WorkspaceItem item, FilterCriteria filterCriteria) throws InternalErrorException
|
|
|
|
{
|
|
|
|
boolean mimeTypeCheck = checkAllowedMimeTypes(type, item, filterCriteria.getAllowedMimeTypes());
|
|
|
|
if (!mimeTypeCheck) return false;
|
|
|
|
|
|
|
|
boolean propertiesCheck = checkProperties(item, filterCriteria.getRequiredProperties());
|
|
|
|
return propertiesCheck;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2015-06-24 15:16:13 +02:00
|
|
|
protected static boolean checkAllowedMimeTypes(ItemType type, WorkspaceItem item, List<String> allowedMimeTypes)
|
|
|
|
{
|
|
|
|
if (allowedMimeTypes.size()==0) return true;
|
|
|
|
|
|
|
|
if (type == ItemType.EXTERNAL_FILE || type == ItemType.EXTERNAL_IMAGE || type == ItemType.EXTERNAL_PDF_FILE) {
|
|
|
|
ExternalFile externalFile = (ExternalFile)item;
|
|
|
|
String mimeType = externalFile.getMimeType();
|
|
|
|
return allowedMimeTypes.contains(mimeType);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2015-06-24 15:16:13 +02:00
|
|
|
protected static boolean checkProperties(WorkspaceItem item, Map<String, String> requestedProperties) throws InternalErrorException
|
|
|
|
{
|
|
|
|
if (requestedProperties.size()==0 || item.getType()!=WorkspaceItemType.FOLDER_ITEM) return true;
|
|
|
|
|
|
|
|
Map<String, String> itemProperties = item.getProperties().getProperties();
|
|
|
|
for (Entry<String, String> requestProperty:requestedProperties.entrySet()) {
|
|
|
|
String propertyValue = itemProperties.get(requestProperty.getKey());
|
|
|
|
if (propertyValue == null) return false;
|
|
|
|
if (!propertyValue.equals(requestProperty.getValue())) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
protected static Item buildFolderToBreadcrumbs(WorkspaceFolder wsFolder, Item parent) throws InternalErrorException {
|
|
|
|
|
|
|
|
String name = "";
|
|
|
|
|
|
|
|
//MANAGEMENT SHARED FOLDER NAME
|
|
|
|
if(wsFolder.isShared() && wsFolder.getType().equals(WorkspaceItemType.SHARED_FOLDER)){
|
|
|
|
WorkspaceSharedFolder shared = (WorkspaceSharedFolder) wsFolder;
|
|
|
|
name = shared.isVreFolder()?shared.getDisplayName():wsFolder.getName();
|
|
|
|
//MANAGEMENT SPECIAL FOLDER
|
|
|
|
}else if(wsFolder.getName().compareTo(WorkspaceExplorerConstants.SPECIAL_FOLDERS_LABEL)==0 && wsFolder.getParent()!=null && wsFolder.getParent().isRoot()){
|
|
|
|
name = WorkspaceExplorerConstants.VRE_FOLDERS_LABEL;
|
|
|
|
}else
|
|
|
|
name = wsFolder.getName();
|
|
|
|
|
|
|
|
return new Item(null, wsFolder.getId(), name, ItemType.FOLDER, "", wsFolder.getOwner().getPortalLogin(), wsFolder.isFolder());
|
|
|
|
}
|
2015-06-24 15:16:13 +02:00
|
|
|
}
|