2015-06-24 15:16:13 +02:00
package org.gcube.portlets.widgets.wsexplorer.server ;
2015-06-25 12:33:18 +02:00
import java.util.ArrayList ;
2018-05-29 15:16:45 +02:00
import java.util.Arrays ;
2015-06-24 15:16:13 +02:00
import java.util.Collections ;
2018-06-22 12:05:49 +02:00
import java.util.HashMap ;
2015-06-24 15:16:13 +02:00
import java.util.List ;
2016-05-17 16:27:15 +02:00
import java.util.Map ;
2015-06-24 15:16:13 +02:00
2018-05-18 18:31:46 +02:00
import org.gcube.common.authorization.library.provider.SecurityTokenProvider ;
2016-11-23 15:35:15 +01:00
import org.gcube.common.portal.PortalContext ;
2018-06-22 12:05:49 +02:00
import org.gcube.common.storagehub.client.dsl.StorageHubClient ;
2018-05-22 16:47:23 +02:00
import org.gcube.common.storagehub.model.items.AbstractFileItem ;
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 ;
2015-06-25 17:22:13 +02:00
import org.gcube.portlets.widgets.wsexplorer.client.WorkspaceExplorerConstants ;
2015-06-24 15:16:13 +02:00
import org.gcube.portlets.widgets.wsexplorer.client.rpc.WorkspaceExplorerService ;
2018-05-29 11:39:17 +02:00
import org.gcube.portlets.widgets.wsexplorer.server.stohub.StorageHubServiceUtil ;
2015-06-24 15:16:13 +02:00
import org.gcube.portlets.widgets.wsexplorer.shared.FilterCriteria ;
2015-06-30 12:31:49 +02:00
import org.gcube.portlets.widgets.wsexplorer.shared.ItemCategory ;
2016-02-26 18:28:40 +01:00
import org.gcube.portlets.widgets.wsexplorer.shared.ItemInterface ;
2015-06-24 15:16:13 +02:00
import org.gcube.portlets.widgets.wsexplorer.shared.ItemType ;
2017-07-13 11:17:07 +02:00
import org.gcube.portlets.widgets.wsexplorer.shared.SearchedFolder ;
2015-06-24 15:16:13 +02:00
import org.gcube.portlets.widgets.wsexplorer.shared.WorkspaceNavigatorServiceException ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import com.google.gwt.user.server.rpc.RemoteServiceServlet ;
2016-09-21 15:46:49 +02:00
2015-06-24 15:16:13 +02:00
/ * *
2016-09-21 15:46:49 +02:00
* The Class WorkspaceExplorerServiceImpl .
2015-06-25 12:33:18 +02:00
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
2016-11-23 15:35:15 +01:00
* @author Massimiliano Assante , CNR - ISTI
2015-06-24 15:16:13 +02:00
* /
@SuppressWarnings ( " serial " )
public class WorkspaceExplorerServiceImpl extends RemoteServiceServlet implements WorkspaceExplorerService {
2018-05-22 16:47:23 +02:00
public static final Logger _log = LoggerFactory . getLogger ( WorkspaceExplorerServiceImpl . class ) ;
2015-06-24 15:16:13 +02:00
/ * *
* { @inheritDoc }
* /
@Override
2018-05-22 16:47:23 +02:00
public org . gcube . portlets . widgets . wsexplorer . shared . Item getRoot (
2018-10-08 11:50:17 +02:00
List < ItemType > showableTypes ,
boolean purgeEmpyFolders ,
FilterCriteria filterCriteria )
2018-05-22 16:47:23 +02:00
throws WorkspaceNavigatorServiceException {
_log . trace ( " getRoot showableTypes: " + showableTypes + " purgeEmpyFolders: " + purgeEmpyFolders + " filterCriteria: " + filterCriteria ) ;
2015-06-24 15:16:13 +02:00
try {
2018-05-18 18:31:46 +02:00
PortalContext pContext = PortalContext . getConfiguration ( ) ;
String userName = pContext . getCurrentUser ( getThreadLocalRequest ( ) ) . getUsername ( ) ;
String scope = pContext . getCurrentScope ( getThreadLocalRequest ( ) ) ;
String authorizationToken = pContext . getCurrentUserToken ( scope , userName ) ;
SecurityTokenProvider . instance . set ( authorizationToken ) ;
2018-05-22 19:01:28 +02:00
2018-05-22 16:47:23 +02:00
_log . trace ( " Start getRoot... " ) ;
2018-05-29 11:39:17 +02:00
Item root = StorageHubServiceUtil . getRoot ( getThreadLocalRequest ( ) ) ;
2015-06-24 15:16:13 +02:00
2018-05-21 18:45:16 +02:00
org . gcube . portlets . widgets . wsexplorer . shared . Item rootItem = ItemBuilder . getItem ( null , root , root . getPath ( ) , showableTypes , filterCriteria , true , false ) ;
2015-06-30 12:31:49 +02:00
rootItem . setName ( WorkspaceExplorerConstants . HOME_LABEL ) ;
2015-07-13 17:15:21 +02:00
rootItem . setIsRoot ( true ) ;
2016-01-15 11:13:48 +01:00
if ( purgeEmpyFolders ) {
rootItem = ItemBuilder . purgeEmptyFolders ( rootItem ) ;
}
2015-06-24 15:16:13 +02:00
Collections . sort ( rootItem . getChildren ( ) , new ItemComparator ( ) ) ;
2018-05-22 16:47:23 +02:00
_log . info ( " ->Returning children size: " + rootItem . getChildren ( ) . size ( ) ) ;
2015-06-24 15:16:13 +02:00
return rootItem ;
} catch ( Exception e ) {
2018-05-22 16:47:23 +02:00
_log . error ( " Error during root retrieving " , e ) ;
e . printStackTrace ( ) ;
2015-06-30 12:31:49 +02:00
throw new WorkspaceNavigatorServiceException ( " Sorry, an error occurred when performing get root " ) ;
2015-06-24 15:16:13 +02:00
}
}
/ * *
* { @inheritDoc }
* /
@Override
2018-05-22 16:47:23 +02:00
public org . gcube . portlets . widgets . wsexplorer . shared . Item getFolder (
2018-10-08 11:50:17 +02:00
ItemInterface item , List < ItemType > showableTypes ,
boolean purgeEmpyFolders ,
FilterCriteria filterCriteria ,
boolean loadGcubeProperties )
2018-05-22 16:47:23 +02:00
throws WorkspaceNavigatorServiceException {
_log . trace ( " getFolder folderId: " + item . getId ( ) + " showableTypes: " + showableTypes + " purgeEmpyFolders: " + purgeEmpyFolders + " filterCriteria: " + filterCriteria ) ;
2015-06-24 15:16:13 +02:00
try {
2018-05-18 18:31:46 +02:00
PortalContext pContext = PortalContext . getConfiguration ( ) ;
String userName = pContext . getCurrentUser ( getThreadLocalRequest ( ) ) . getUsername ( ) ;
String scope = pContext . getCurrentScope ( getThreadLocalRequest ( ) ) ;
String authorizationToken = pContext . getCurrentUserToken ( scope , userName ) ;
2018-10-08 11:50:17 +02:00
SecurityTokenProvider . instance . set ( authorizationToken ) ;
2018-05-29 11:39:17 +02:00
Item folder = StorageHubServiceUtil . getItem ( getThreadLocalRequest ( ) , item . getId ( ) ) ;
2018-05-22 16:47:23 +02:00
_log . trace ( " GetFolder - Replying folder " ) ;
2018-05-22 19:01:28 +02:00
2016-02-26 18:28:40 +01:00
//TO AVOID SLOW CALL getPATH()
String folderPath = item . getPath ( ) ! = null & & ! item . getPath ( ) . isEmpty ( ) ? item . getPath ( ) : folder . getPath ( ) ;
2018-05-21 18:45:16 +02:00
org . gcube . portlets . widgets . wsexplorer . shared . Item itemFolder = ItemBuilder . getItem ( null , folder , folderPath , showableTypes , filterCriteria , true , loadGcubeProperties ) ;
2018-05-22 19:01:28 +02:00
// _log.trace("Only showable types:");
2016-01-15 11:13:48 +01:00
if ( purgeEmpyFolders ) {
itemFolder = ItemBuilder . purgeEmptyFolders ( itemFolder ) ;
}
2015-06-24 15:16:13 +02:00
2015-06-25 12:33:18 +02:00
Collections . sort ( itemFolder . getChildren ( ) , new ItemComparator ( ) ) ;
2018-05-22 16:47:23 +02:00
_log . info ( " Returning children size: " + itemFolder . getChildren ( ) . size ( ) ) ;
2017-11-17 16:47:29 +01:00
2015-06-25 12:33:18 +02:00
return itemFolder ;
2015-06-24 15:16:13 +02:00
} catch ( Exception e ) {
2018-05-22 16:47:23 +02:00
_log . error ( " Error during folder retrieving " , e ) ;
2015-06-30 12:31:49 +02:00
throw new WorkspaceNavigatorServiceException ( " Sorry, an error occurred when performing get folder " ) ;
}
}
2016-01-15 11:13:48 +01:00
2015-07-06 17:18:23 +02:00
/ * ( non - Javadoc )
* @see org . gcube . portlets . widgets . wsexplorer . client . rpc . WorkspaceExplorerService # getItemByCategory ( org . gcube . portlets . widgets . wsexplorer . shared . ItemCategory )
* /
2016-09-21 15:46:49 +02:00
/ * *
* Gets the item by category .
*
* @param category the category
* @return the item by category
* @throws WorkspaceNavigatorServiceException the workspace navigator service exception
* /
2015-06-30 12:31:49 +02:00
@Override
2018-05-21 18:45:16 +02:00
public org . gcube . portlets . widgets . wsexplorer . shared . Item getItemByCategory ( ItemCategory category ) throws WorkspaceNavigatorServiceException {
2018-05-22 16:47:23 +02:00
_log . trace ( " GetItemByCategory category: " + category ) ;
2018-05-18 18:31:46 +02:00
PortalContext pContext = PortalContext . getConfiguration ( ) ;
String userName = pContext . getCurrentUser ( getThreadLocalRequest ( ) ) . getUsername ( ) ;
String scope = pContext . getCurrentScope ( getThreadLocalRequest ( ) ) ;
String authorizationToken = pContext . getCurrentUserToken ( scope , userName ) ;
SecurityTokenProvider . instance . set ( authorizationToken ) ;
2015-06-30 12:31:49 +02:00
try {
2018-05-22 19:01:28 +02:00
2018-05-21 18:45:16 +02:00
org . gcube . portlets . widgets . wsexplorer . shared . Item item = null ;
2016-01-15 11:13:48 +01:00
2015-06-30 12:31:49 +02:00
switch ( category ) {
2018-05-22 19:01:28 +02:00
case HOME : {
2018-05-29 11:39:17 +02:00
Item root = StorageHubServiceUtil . getRoot ( getThreadLocalRequest ( ) ) ;
2018-05-22 19:01:28 +02:00
String fullName = pContext . getCurrentUser ( getThreadLocalRequest ( ) ) . getFullname ( ) ;
if ( fullName . indexOf ( " " ) > 0 ) {
fullName = fullName . substring ( 0 , fullName . indexOf ( " " ) ) ;
} else if ( fullName . indexOf ( " . " ) > 0 ) {
fullName = fullName . substring ( 0 , fullName . indexOf ( " . " ) ) ;
2015-06-30 12:31:49 +02:00
}
2018-05-22 19:01:28 +02:00
item = new org . gcube . portlets . widgets . wsexplorer . shared . Item ( null , root . getId ( ) , fullName + " 's " , ItemType . FOLDER , root . getPath ( ) , root . getOwner ( ) , null , true , true ) ;
break ;
}
case VRE_FOLDER : {
2018-05-29 11:39:17 +02:00
String vreFolderId = StorageHubServiceUtil . getVREFoldersId ( getThreadLocalRequest ( ) ) ;
Item folder = StorageHubServiceUtil . getItem ( getThreadLocalRequest ( ) , vreFolderId ) ;
2018-05-22 19:01:28 +02:00
item = new org . gcube . portlets . widgets . wsexplorer . shared . Item (
2018-10-08 11:50:17 +02:00
null ,
folder . getId ( ) ,
WorkspaceExplorerConstants . VRE_FOLDERS_LABEL ,
ItemType . FOLDER ,
folder . getPath ( ) ,
folder . getOwner ( ) ,
null ,
true ,
2018-05-22 19:01:28 +02:00
false ) ;
//SET SPECIAL FOLDER /Workspace/MySpecialFolders
item . setSpecialFolder ( true ) ;
break ;
}
2015-06-30 12:31:49 +02:00
}
return item ;
} catch ( Exception e ) {
2018-05-22 16:47:23 +02:00
_log . error ( " Error during get item by category " , e ) ;
2015-06-30 12:31:49 +02:00
throw new WorkspaceNavigatorServiceException ( " Sorry, an error occurred when performing get item by category " ) ;
2015-06-24 15:16:13 +02:00
}
}
2016-01-15 11:13:48 +01:00
2015-06-25 17:22:13 +02:00
/ * *
* { @inheritDoc }
* /
@Override
2018-05-22 16:47:23 +02:00
public org . gcube . portlets . widgets . wsexplorer . shared . Item getMySpecialFolder (
2018-10-08 11:50:17 +02:00
List < ItemType > showableTypes ,
boolean purgeEmpyFolders ,
FilterCriteria filterCriteria )
2018-05-22 16:47:23 +02:00
throws WorkspaceNavigatorServiceException {
_log . trace ( " GetMySpecialFolder showableTypes: " + showableTypes + " purgeEmpyFolders: " + purgeEmpyFolders + " filterCriteria: " + filterCriteria ) ;
2015-06-25 17:22:13 +02:00
try {
2018-05-18 18:31:46 +02:00
PortalContext pContext = PortalContext . getConfiguration ( ) ;
String userName = pContext . getCurrentUser ( getThreadLocalRequest ( ) ) . getUsername ( ) ;
String scope = pContext . getCurrentScope ( getThreadLocalRequest ( ) ) ;
String authorizationToken = pContext . getCurrentUserToken ( scope , userName ) ;
SecurityTokenProvider . instance . set ( authorizationToken ) ;
2018-05-29 11:39:17 +02:00
String vreFolderId = StorageHubServiceUtil . getVREFoldersId ( getThreadLocalRequest ( ) ) ;
Item folder = StorageHubServiceUtil . getItem ( getThreadLocalRequest ( ) , vreFolderId ) ;
2015-06-25 17:22:13 +02:00
2018-05-21 18:45:16 +02:00
org . gcube . portlets . widgets . wsexplorer . shared . Item itemFolder = ItemBuilder . getItem ( null , folder , folder . getPath ( ) , showableTypes , filterCriteria , true , false ) ;
2015-07-13 17:15:21 +02:00
//OVERRIDING VRE FOLDERS NAME - SET SPECIAL FOLDER /Workspace/MySpecialFolders
2015-06-25 18:17:10 +02:00
itemFolder . setName ( WorkspaceExplorerConstants . VRE_FOLDERS_LABEL ) ;
2015-06-25 17:22:13 +02:00
itemFolder . setSpecialFolder ( true ) ;
2016-01-15 11:13:48 +01:00
2018-05-22 16:47:23 +02:00
_log . trace ( " Builded MySpecialFolder: " + itemFolder ) ;
2015-06-25 17:22:13 +02:00
2018-05-22 16:47:23 +02:00
_log . trace ( " Only showable types: " ) ;
2015-06-25 17:22:13 +02:00
2016-01-15 11:13:48 +01:00
if ( purgeEmpyFolders ) {
itemFolder = ItemBuilder . purgeEmptyFolders ( itemFolder ) ;
}
2015-06-25 17:22:13 +02:00
Collections . sort ( itemFolder . getChildren ( ) , new ItemComparator ( ) ) ;
return itemFolder ;
} catch ( Exception e ) {
2018-05-22 16:47:23 +02:00
_log . error ( " Error during special folders retrieving " , e ) ;
2015-06-30 12:31:49 +02:00
throw new WorkspaceNavigatorServiceException ( " Sorry, an error occurred when performing get My Special Folder " ) ;
2015-06-25 17:22:13 +02:00
}
}
2015-06-24 15:16:13 +02:00
/ * *
* { @inheritDoc }
* /
@Override
public boolean checkName ( String name ) throws WorkspaceNavigatorServiceException {
2018-05-22 16:47:23 +02:00
_log . warn ( " checkName name NOT IMPLEMENTED: " + name ) ;
2018-05-18 18:31:46 +02:00
return true ; //TODO:
2015-06-24 15:16:13 +02:00
}
/ * *
2018-05-22 16:47:23 +02:00
* Gets Breadcrumbs ( the list of parents ) by item identifier and name ( the name is added as last item of the breadcrumb )
2015-06-24 15:16:13 +02:00
*
* @param itemIdentifier the item identifier
* @param includeItemAsParent - if parameter is true and item passed in input is a folder , the folder is included in path returned as last parent
* @return the list parents by item identifier
* @throws Exception the exception
* /
@Override
2018-05-22 16:47:23 +02:00
public ArrayList < org . gcube . portlets . widgets . wsexplorer . shared . Item > getBreadcrumbsByItemIdentifier (
2018-10-08 11:50:17 +02:00
String itemIdentifier , String itemName , boolean includeItemAsParent )
2018-05-22 16:47:23 +02:00
throws Exception {
System . out . println ( " ListParents By Item id " + itemIdentifier + " name= " + itemName ) ;
2015-06-24 15:16:13 +02:00
try {
2018-05-29 11:39:17 +02:00
List < ? extends Item > parents = StorageHubServiceUtil . getParents ( getThreadLocalRequest ( ) , itemIdentifier ) ;
2018-05-21 18:45:16 +02:00
ArrayList < org . gcube . portlets . widgets . wsexplorer . shared . Item > toReturn = new ArrayList < > ( parents . size ( ) ) ;
2018-05-22 16:47:23 +02:00
for ( Item item : parents ) {
2018-05-22 19:01:28 +02:00
if ( item instanceof FolderItem )
2018-05-22 16:47:23 +02:00
toReturn . add ( ItemBuilder . buildFolderForBreadcrumbs ( ( FolderItem ) item , null ) ) ;
System . out . println ( " -> " + item . getTitle ( ) ) ;
}
2018-05-21 18:45:16 +02:00
Collections . reverse ( toReturn ) ;
2018-05-22 19:01:28 +02:00
2018-10-08 11:50:17 +02:00
Item theFolder ;
if ( itemName = = null | | itemName . isEmpty ( ) ) {
theFolder = StorageHubServiceUtil . getItem ( getThreadLocalRequest ( ) , itemIdentifier ) ;
itemName = theFolder . getName ( ) ;
}
String theClickedFolderName = itemName = = null | | itemName . equals ( " " ) ? " current folder " : itemName ;
2018-05-21 18:45:16 +02:00
toReturn . add ( new org . gcube . portlets . widgets . wsexplorer . shared . Item ( itemIdentifier , theClickedFolderName , true ) ) ; //this is the last non clickable item on the BC
return toReturn ;
2015-06-24 15:16:13 +02:00
} catch ( Exception e ) {
2018-05-22 16:47:23 +02:00
_log . error ( " Error in get List Parents By Item Identifier " , e ) ;
2015-12-03 15:41:16 +01:00
throw new Exception ( " Sorry, an error occurred during path retrieving! " ) ;
2015-06-24 15:16:13 +02:00
}
2015-12-03 15:41:16 +01:00
}
2015-06-24 15:16:13 +02:00
2015-12-03 15:41:16 +01:00
/ * *
* Gets the parents by item identifier to limit .
*
2016-09-21 15:46:49 +02:00
* @param itemIdentifier the item identifier
* @param parentLimit the parent limit
* @param includeItemAsParent the include item as parent
2015-12-03 15:41:16 +01:00
* @return the parents by item identifier to limit
2016-09-21 15:46:49 +02:00
* @throws Exception the exception
2015-12-03 15:41:16 +01:00
* /
@Override
2018-05-21 18:45:16 +02:00
public List < org . gcube . portlets . widgets . wsexplorer . shared . Item > getBreadcrumbsByItemIdentifierToParentLimit ( String itemIdentifier , String parentLimit , boolean includeItemAsParent ) throws Exception {
2018-05-22 16:47:23 +02:00
_log . trace ( " getBreadcrumbsByItemIdentifierToParentLimit by Item Identifier " + itemIdentifier + " and limit: " + parentLimit ) ;
2015-12-03 15:41:16 +01:00
2018-05-22 19:01:28 +02:00
parentLimit = parentLimit ! = null ? parentLimit : " " ;
try {
2018-05-29 11:39:17 +02:00
List < ? extends Item > parents = StorageHubServiceUtil . getParents ( getThreadLocalRequest ( ) , itemIdentifier ) ;
2018-05-22 19:01:28 +02:00
ArrayList < org . gcube . portlets . widgets . wsexplorer . shared . Item > toWorkOn = new ArrayList < > ( parents . size ( ) ) ;
for ( Item item : parents ) {
if ( item instanceof FolderItem )
toWorkOn . add ( ItemBuilder . buildFolderForBreadcrumbs ( ( FolderItem ) item , null ) ) ;
2015-12-03 15:41:16 +01:00
}
2018-05-22 19:01:28 +02:00
Collections . reverse ( toWorkOn ) ;
2018-05-29 11:39:17 +02:00
Item folderItem = StorageHubServiceUtil . getItem ( getThreadLocalRequest ( ) , itemIdentifier ) ;
2018-05-22 19:01:28 +02:00
String theClickedFolderName = folderItem . getTitle ( ) ;
2018-10-08 11:50:17 +02:00
if ( theClickedFolderName . compareTo ( WorkspaceExplorerConstants . SPECIAL_FOLDERS_NAME ) = = 0 ) {
2018-05-22 19:01:28 +02:00
theClickedFolderName = WorkspaceExplorerConstants . VRE_FOLDERS_LABEL ;
} else {
ItemType type = ItemBuilder . getItemType ( folderItem ) ;
2018-10-08 11:50:17 +02:00
boolean isSharedFolder = type . equals ( ItemType . SHARED_FOLDER ) | | type . equals ( ItemType . VRE_FOLDER ) ? true : false ;
2018-05-22 19:01:28 +02:00
if ( isSharedFolder ) {
SharedFolder shared = ( SharedFolder ) folderItem ;
theClickedFolderName = shared . isVreFolder ( ) ? shared . getDisplayName ( ) : folderItem . getTitle ( ) ;
2016-01-15 11:13:48 +01:00
}
2015-12-03 15:41:16 +01:00
}
2016-01-15 11:13:48 +01:00
2018-05-22 19:01:28 +02:00
toWorkOn . add ( new org . gcube . portlets . widgets . wsexplorer . shared . Item ( itemIdentifier , theClickedFolderName , true ) ) ; //this is the last non clickable item on the BC
_log . debug ( " parentLimit id is= " + parentLimit ) ;
boolean found = false ;
ArrayList < org . gcube . portlets . widgets . wsexplorer . shared . Item > toReturn = new ArrayList < > ( parents . size ( ) ) ;
for ( org . gcube . portlets . widgets . wsexplorer . shared . Item item : toWorkOn ) {
if ( item . getId ( ) . compareTo ( parentLimit ) = = 0 ) found = true ;
if ( found )
2018-10-08 11:50:17 +02:00
toReturn . add ( item ) ;
2016-01-15 11:13:48 +01:00
}
2018-05-22 19:01:28 +02:00
return toReturn ;
2015-12-03 15:41:16 +01:00
} catch ( Exception e ) {
2018-05-22 16:47:23 +02:00
_log . error ( " Error in get List Parents By Item Identifier " , e ) ;
2015-12-03 15:41:16 +01:00
throw new Exception ( " Sorry, an error occurred during path retrieving! " ) ;
}
2015-06-24 15:16:13 +02:00
}
2016-01-15 11:13:48 +01:00
2015-07-06 17:18:23 +02:00
/ * ( non - Javadoc )
* @see org . gcube . portlets . widgets . wsexplorer . client . rpc . WorkspaceExplorerService # loadSizeByItemId ( java . lang . String )
* /
2016-09-21 15:46:49 +02:00
/ * *
* Gets the size by item id .
*
* @param itemId the item id
* @return the size by item id
* @throws Exception the exception
* /
2015-07-06 17:18:23 +02:00
@Override
2015-07-10 11:57:39 +02:00
public Long getSizeByItemId ( String itemId ) throws Exception {
2018-05-23 16:34:06 +02:00
_log . debug ( " get Size By ItemId " + itemId ) ;
2018-06-28 10:33:54 +02:00
Item wsItem = StorageHubServiceUtil . getItem ( getThreadLocalRequest ( ) , itemId ) ;
_log . trace ( " workspace retrieved item name: " + wsItem . getName ( ) ) ;
2018-10-08 11:50:17 +02:00
if ( wsItem instanceof FolderItem ) //if is a folder no Size
2018-06-28 10:33:54 +02:00
return 0L ;
2018-05-23 16:34:06 +02:00
2018-06-28 10:33:54 +02:00
AbstractFileItem file = ( AbstractFileItem ) wsItem ;
return file . getContent ( ) . getSize ( ) ;
2015-07-06 17:18:23 +02:00
}
2016-01-15 11:13:48 +01:00
2015-07-06 17:18:23 +02:00
/ * ( non - Javadoc )
* @see org . gcube . portlets . widgets . wsexplorer . client . rpc . WorkspaceExplorerService # getMimeType ( java . lang . String )
* /
2016-09-21 15:46:49 +02:00
/ * *
* Gets the mime type .
*
* @param itemId the item id
* @return the mime type
* @throws Exception the exception
* /
2015-07-06 17:18:23 +02:00
@Override
public String getMimeType ( String itemId ) throws Exception {
2018-05-22 16:47:23 +02:00
_log . debug ( " get MimeType By ItemId " + itemId ) ;
2018-06-28 10:33:54 +02:00
Item wsItem = StorageHubServiceUtil . getItem ( getThreadLocalRequest ( ) , itemId ) ;
_log . trace ( " workspace retrieved item name: " + wsItem . getName ( ) ) ;
2018-10-08 11:50:17 +02:00
if ( wsItem instanceof FolderItem ) //if is a folder no mime
2018-06-28 10:33:54 +02:00
return null ;
AbstractFileItem file = ( AbstractFileItem ) wsItem ;
return file . getContent ( ) . getMimeType ( ) ;
2015-07-06 17:18:23 +02:00
}
2016-01-15 11:13:48 +01:00
2015-07-06 17:18:23 +02:00
/ * *
* Gets the user acl for folder id .
*
* @param folderId the folder id
* @return the user acl for folder id
* @throws Exception the exception
* /
@Override
public String getUserACLForFolderId ( String folderId ) throws Exception {
2018-05-29 11:39:17 +02:00
try {
return StorageHubServiceUtil . getUserACLForFolderId ( getThreadLocalRequest ( ) , folderId ) ;
} catch ( Exception e ) {
2019-04-09 12:07:09 +02:00
_log . info ( " Error on getUserACLForFolderId " , e ) ;
2018-05-29 11:39:17 +02:00
}
return " UNKNOWN " ;
2015-07-06 17:18:23 +02:00
}
2015-07-10 11:57:39 +02:00
/ * ( non - Javadoc )
* @see org . gcube . portlets . widgets . wsexplorer . client . rpc . WorkspaceExplorerService # getFormattedSizeByItemId ( java . lang . String , org . gcube . portlets . widgets . wsexplorer . shared . SizeFormatter )
* /
2016-09-21 15:46:49 +02:00
/ * *
* Gets the readable size by item id .
*
* @param itemId the item id
* @return the readable size by item id
* @throws Exception the exception
* /
2015-07-10 11:57:39 +02:00
@Override
public String getReadableSizeByItemId ( String itemId ) throws Exception {
2016-01-15 11:13:48 +01:00
2015-07-10 11:57:39 +02:00
try {
2018-05-22 19:01:28 +02:00
_log . info ( " getFormattedSize ByItemId " + itemId ) ;
long size = getSizeByItemId ( itemId ) ;
return StringUtil . readableFileSize ( size ) ;
2015-07-10 11:57:39 +02:00
} catch ( Exception e ) {
2018-05-22 16:47:23 +02:00
_log . error ( " getFormattedSize By ItemId " , e ) ;
2015-07-10 11:57:39 +02:00
throw new Exception ( e . getMessage ( ) ) ;
}
}
2016-02-01 16:03:11 +01:00
2016-09-21 15:46:49 +02:00
/ * *
* Creates the folder .
*
* @param nameFolder the name folder
* @param description the description
* @param parentId the parent id
* @return the item
* @throws Exception the exception
* /
2016-02-01 16:03:11 +01:00
@Override
2018-05-29 15:16:45 +02:00
public org . gcube . portlets . widgets . wsexplorer . shared . Item createFolder ( String folderName , String description , String parentId ) throws Exception {
_log . debug ( " creating folder: " + folderName + " , parent id: " + parentId ) ;
if ( parentId = = null | | parentId . isEmpty ( ) )
throw new Exception ( " Parent id is null or empty " ) ;
if ( folderName = = null )
folderName = " New Folder " ;
try {
FolderItem createdFolder = StorageHubServiceUtil . createFolder ( getThreadLocalRequest ( ) , parentId , folderName , description ) ;
_log . info ( " Path returned by StoHub: " + createdFolder . getPath ( ) ) ;
List < ItemType > allTypes = Arrays . asList ( ItemType . values ( ) ) ;
return ItemBuilder . getItem ( null , createdFolder , createdFolder . getPath ( ) , allTypes , null , false , false ) ;
2018-06-28 10:33:54 +02:00
// } catch(InsufficientPrivilegesException e){
// String error = "Insufficient Privileges to create the folder";
// _log.error(error, e);
// throw new Exception(error);
// } catch (ItemAlreadyExistException e) {
// String error = "An error occurred on creating folder, " +e.getMessage();
// _log.error(error, e);
// throw new Exception(error);
2018-05-29 15:16:45 +02:00
} catch ( Exception e ) {
String error = " An error occurred on the sever during creating folder. Try again " ;
_log . error ( error , e ) ;
throw new Exception ( error ) ;
}
2016-02-01 16:03:11 +01:00
}
2016-05-17 16:27:15 +02:00
/ * ( non - Javadoc )
* @see org . gcube . portlets . widgets . wsexplorer . client . rpc . WorkspaceExplorerService # getGcubePropertiesForWorspaceId ( java . lang . String )
* /
2016-09-21 15:46:49 +02:00
/ * *
* Gets the gcube properties for worspace id .
*
* @param id the id
* @return the gcube properties for worspace id
* @throws Exception the exception
* /
2016-05-17 16:27:15 +02:00
@Override
2018-06-22 12:05:49 +02:00
public Map < String , String > getGcubePropertiesForWorspaceId ( String itemId ) throws Exception {
_log . trace ( " getGcubePropertiesForWorspaceId " + itemId ) ;
try {
2018-06-28 10:33:54 +02:00
StorageHubClient shc = new StorageHubClient ( ) ;
Item item = shc . open ( itemId ) . asItem ( ) . get ( ) ;
return ItemBuilder . getGcubePropertiesForItem ( item ) ;
} catch ( Throwable e ) {
_log . error ( e . getLocalizedMessage ( ) , e ) ;
return new HashMap < String , String > ( ) ;
}
2016-05-17 16:27:15 +02:00
}
2017-07-06 16:16:37 +02:00
/ * ( non - Javadoc )
* @see org . gcube . portlets . widgets . wsexplorer . client . rpc . WorkspaceExplorerService # getFolder ( org . gcube . portlets . widgets . wsexplorer . shared . Item , java . util . List , boolean , org . gcube . portlets . widgets . wsexplorer . shared . FilterCriteria , boolean , int , int )
* /
@Override
2017-07-13 11:17:07 +02:00
public SearchedFolder getFolder (
2018-05-22 19:01:28 +02:00
org . gcube . portlets . widgets . wsexplorer . shared . Item item , List < ItemType > showableTypes , boolean purgeEmpyFolders ,
FilterCriteria filterCriteria , boolean loadGcubeProperties ,
final int startIndex , final int limit , final int serverStartIndex ) throws WorkspaceNavigatorServiceException {
2017-07-06 16:16:37 +02:00
2018-05-22 16:47:23 +02:00
_log . trace ( " getFolder folderId: " + item . getId ( ) + " showableTypes: " + showableTypes + " purgeEmpyFolders: " + purgeEmpyFolders + " filterCriteria: " + filterCriteria ) ;
2017-07-06 16:16:37 +02:00
try {
2018-05-29 11:39:17 +02:00
Item folder = StorageHubServiceUtil . getItem ( getThreadLocalRequest ( ) , item . getId ( ) ) ;
2018-05-22 19:01:28 +02:00
2017-07-13 11:17:07 +02:00
int searchStartIndex = startIndex < serverStartIndex ? serverStartIndex : startIndex ;
2018-05-29 11:39:17 +02:00
_log . debug ( " MyLg getFolder searchIndex: " + searchStartIndex + " , limit: " + limit ) ;
2017-07-06 16:16:37 +02:00
//TO AVOID SLOW CALL getPATH()
String folderPath = item . getPath ( ) ! = null & & ! item . getPath ( ) . isEmpty ( ) ? item . getPath ( ) : folder . getPath ( ) ;
2018-05-21 18:45:16 +02:00
org . gcube . portlets . widgets . wsexplorer . shared . Item itemFolderToReturn = ItemBuilder . getItem ( null , folder , folderPath , showableTypes , filterCriteria , true , loadGcubeProperties , searchStartIndex , limit ) ;
2017-07-06 16:16:37 +02:00
2017-07-13 11:17:07 +02:00
SearchedFolder sf = new SearchedFolder ( itemFolderToReturn , startIndex , limit , searchStartIndex , false ) ;
int currentListCount = sf . getFolder ( ) . getChildren ( ) . size ( ) ;
2018-05-22 16:47:23 +02:00
_log . debug ( " MyLg Total item returning is: " + currentListCount ) ;
2017-07-06 16:16:37 +02:00
2018-05-18 18:31:46 +02:00
FolderItem hlFolder = ( FolderItem ) folder ;
2018-05-29 11:39:17 +02:00
int folderChildrenCount = StorageHubServiceUtil . getItemChildrenCount ( getThreadLocalRequest ( ) , hlFolder . getId ( ) ) ;
2018-05-22 16:47:23 +02:00
_log . debug ( " MyLg Folder children count is: " + folderChildrenCount ) ;
2017-07-13 11:17:07 +02:00
if ( currentListCount = = limit | | folderChildrenCount = = 0 ) {
2018-05-22 16:47:23 +02:00
_log . debug ( " Page completed returning " + currentListCount + " items " ) ;
2017-07-13 11:17:07 +02:00
int offset = searchStartIndex + limit ;
2017-07-13 12:37:12 +02:00
Collections . sort ( sf . getFolder ( ) . getChildren ( ) , new ItemComparator ( ) ) ;
2017-07-13 11:17:07 +02:00
sf . setServerSearchFinished ( offset > folderChildrenCount | | folderChildrenCount = = 0 ) ;
2018-05-22 16:47:23 +02:00
_log . debug ( " is Search finished: " + sf . isServerSearchFinished ( ) ) ;
2017-07-13 11:17:07 +02:00
return sf ;
}
2018-05-21 18:45:16 +02:00
ArrayList < org . gcube . portlets . widgets . wsexplorer . shared . Item > childrenToReturn = new ArrayList < org . gcube . portlets . widgets . wsexplorer . shared . Item > ( limit ) ;
2017-07-13 11:17:07 +02:00
childrenToReturn . addAll ( sf . getFolder ( ) . getChildren ( ) ) ;
int offsetStartIndex = searchStartIndex ;
boolean pageOffsetOut = false ;
while ( currentListCount < limit & & ! sf . isServerSearchFinished ( ) & & ! pageOffsetOut ) { //&& SEARCH NOT ULTIMATED
2018-05-22 16:47:23 +02:00
_log . debug ( " MyLg new WHILE Items count: " + currentListCount + " is less than limit.. " ) ;
2017-07-13 11:17:07 +02:00
int newstartIndex = offsetStartIndex + limit + 1 ;
2018-05-22 16:47:23 +02:00
_log . debug ( " MyLg NewStartIndex is startIndex+limit: " + newstartIndex ) ;
2017-07-13 11:17:07 +02:00
//THERE ARE OTHER CHILDREN OVER NEW START INDEX
if ( newstartIndex < folderChildrenCount ) {
//newLimit = limit - childrenToReturn.size();
2018-05-22 16:47:23 +02:00
_log . debug ( " MyLg getting items with index start: " + newstartIndex + " , limit: " + limit ) ;
2018-05-21 18:45:16 +02:00
org . gcube . portlets . widgets . wsexplorer . shared . Item newItemFolder = ItemBuilder . getItem ( null , folder , folderPath , showableTypes , filterCriteria , true , loadGcubeProperties , newstartIndex , limit ) ;
2017-07-13 11:17:07 +02:00
int diff = limit - currentListCount ; //How items are remaining
//int offset = 0;
2018-05-22 16:47:23 +02:00
_log . debug ( " MyLg new search start: " + newstartIndex + " , diff: " + diff + " , retrieved: " + newItemFolder . getChildren ( ) . size ( ) ) ;
2017-07-13 11:17:07 +02:00
if ( diff > = newItemFolder . getChildren ( ) . size ( ) ) {
2018-05-22 16:47:23 +02:00
_log . debug ( " MyLg Adding sublist from 0 to 'diff' " + diff + " to children " ) ;
2017-07-13 11:17:07 +02:00
childrenToReturn . addAll ( newItemFolder . getChildren ( ) . subList ( 0 , newItemFolder . getChildren ( ) . size ( ) ) ) ;
//offset = diff;
} else {
2018-05-22 16:47:23 +02:00
_log . debug ( " MyLg PageOffsetOut, the sublist size: " + newItemFolder . getChildren ( ) . size ( ) + " is greather than (limit-currentListCount) " + diff + " leaving WHILE... " ) ;
2017-07-13 11:17:07 +02:00
//childrenToReturn.addAll(newItemFolder.getChildren().subList(0, newItemFolder.getChildren().size()));
//offset = newItemFolder.getChildren().size();
pageOffsetOut = true ;
}
offsetStartIndex = newstartIndex ;
currentListCount = childrenToReturn . size ( ) ;
//int realServerEndIndex = newstartIndex+offset;
2018-05-22 16:47:23 +02:00
_log . debug ( " MyLg New items count is: " + currentListCount + " serverEndIndex: " + offsetStartIndex ) ;
2017-07-13 11:17:07 +02:00
sf . setServerEndIndex ( offsetStartIndex ) ;
} else {
2018-05-22 16:47:23 +02:00
_log . debug ( " MyLg New start index (oldStartIndex+limit) is grather than folder children count, search is finished " ) ;
2017-07-13 11:17:07 +02:00
sf . setServerSearchFinished ( true ) ;
}
}
sf . getFolder ( ) . setChildren ( childrenToReturn ) ;
//sf.setServerEndIndex(sf.getServerEndIndex());
2017-07-06 16:16:37 +02:00
if ( purgeEmpyFolders ) {
2017-07-13 11:17:07 +02:00
itemFolderToReturn = ItemBuilder . purgeEmptyFolders ( sf . getFolder ( ) ) ;
2017-07-06 16:16:37 +02:00
}
2017-07-13 12:37:12 +02:00
Collections . sort ( itemFolderToReturn . getChildren ( ) , new ItemComparator ( ) ) ;
2018-05-22 16:47:23 +02:00
_log . debug ( " Returning: " + sf ) ;
2017-07-13 11:17:07 +02:00
return sf ;
2017-07-06 16:16:37 +02:00
} catch ( Exception e ) {
2018-05-22 16:47:23 +02:00
_log . error ( " Error during folder retrieving " , e ) ;
2017-07-06 16:16:37 +02:00
throw new WorkspaceNavigatorServiceException ( " Sorry, an error occurred when performing get folder " ) ;
}
}
2018-05-22 19:01:28 +02:00
2017-07-06 16:16:37 +02:00
@Override
2018-05-21 18:45:16 +02:00
public int getFolderChildrenCount ( org . gcube . portlets . widgets . wsexplorer . shared . Item item ) {
2019-04-09 12:07:09 +02:00
try {
return StorageHubServiceUtil . getItemChildrenCount ( getThreadLocalRequest ( ) , item . getId ( ) ) ;
} catch ( Exception e ) {
_log . error ( " Error on getFolderChildrenCount " , e ) ;
return 0 ;
}
2017-07-06 16:16:37 +02:00
}
2018-05-22 19:01:28 +02:00
2015-06-24 15:16:13 +02:00
}