2018-05-17 12:51:56 +02:00
package org.gcube.data.access.storagehub.services ;
2018-10-25 16:33:23 +02:00
import java.io.IOException ;
2018-05-17 12:51:56 +02:00
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.List ;
import java.util.stream.Collectors ;
import javax.enterprise.context.RequestScoped ;
import javax.inject.Inject ;
import javax.jcr.Node ;
import javax.jcr.NodeIterator ;
2018-10-25 16:33:23 +02:00
import javax.jcr.RepositoryException ;
2018-05-17 12:51:56 +02:00
import javax.jcr.Session ;
import javax.jcr.query.Query ;
import javax.jcr.query.QueryResult ;
2018-06-20 16:59:41 +02:00
import javax.servlet.ServletContext ;
2018-10-25 16:33:23 +02:00
import javax.ws.rs.Consumes ;
import javax.ws.rs.DELETE ;
2018-05-17 12:51:56 +02:00
import javax.ws.rs.GET ;
2018-10-25 16:33:23 +02:00
import javax.ws.rs.PUT ;
2018-05-17 12:51:56 +02:00
import javax.ws.rs.Path ;
import javax.ws.rs.Produces ;
import javax.ws.rs.QueryParam ;
import javax.ws.rs.core.MediaType ;
2019-03-26 17:09:26 +01:00
import javax.ws.rs.core.Response ;
2018-05-17 12:51:56 +02:00
import org.gcube.common.authorization.library.provider.AuthorizationProvider ;
2018-10-25 16:33:23 +02:00
import org.gcube.common.gxrest.response.outbound.GXOutboundErrorResponse ;
import org.gcube.common.storagehub.model.Excludes ;
2018-05-17 12:51:56 +02:00
import org.gcube.common.storagehub.model.Paths ;
2018-10-25 16:33:23 +02:00
import org.gcube.common.storagehub.model.exceptions.BackendGenericError ;
import org.gcube.common.storagehub.model.exceptions.InvalidItemException ;
import org.gcube.common.storagehub.model.exceptions.StorageHubException ;
2018-05-17 12:51:56 +02:00
import org.gcube.common.storagehub.model.expressions.Expression ;
import org.gcube.common.storagehub.model.expressions.logical.And ;
import org.gcube.common.storagehub.model.expressions.logical.ISDescendant ;
import org.gcube.common.storagehub.model.items.Item ;
2018-10-25 16:33:23 +02:00
import org.gcube.common.storagehub.model.items.TrashItem ;
2018-05-17 12:51:56 +02:00
import org.gcube.common.storagehub.model.service.ItemList ;
import org.gcube.common.storagehub.model.service.ItemWrapper ;
2018-06-20 16:59:41 +02:00
import org.gcube.data.access.storagehub.AuthorizationChecker ;
2018-05-17 12:51:56 +02:00
import org.gcube.data.access.storagehub.Constants ;
import org.gcube.data.access.storagehub.Range ;
2020-03-16 16:55:26 +01:00
import org.gcube.data.access.storagehub.StorageHubAppllicationManager ;
2018-05-17 12:51:56 +02:00
import org.gcube.data.access.storagehub.Utils ;
2018-07-03 12:15:35 +02:00
import org.gcube.data.access.storagehub.handlers.CredentialHandler ;
2018-10-25 16:33:23 +02:00
import org.gcube.data.access.storagehub.handlers.Item2NodeConverter ;
import org.gcube.data.access.storagehub.handlers.Node2ItemConverter ;
2019-07-29 16:26:08 +02:00
import org.gcube.data.access.storagehub.handlers.StorageBackendHandler ;
2018-10-25 16:33:23 +02:00
import org.gcube.data.access.storagehub.handlers.TrashHandler ;
2018-06-20 16:59:41 +02:00
import org.gcube.data.access.storagehub.handlers.VRE ;
import org.gcube.data.access.storagehub.handlers.VREManager ;
2018-05-17 12:51:56 +02:00
import org.gcube.data.access.storagehub.query.sql2.evaluators.Evaluators ;
2020-03-16 16:55:26 +01:00
import org.gcube.smartgears.annotations.ManagedBy ;
2018-07-05 16:26:08 +02:00
import org.gcube.smartgears.utils.InnerMethodName ;
2018-05-17 12:51:56 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import com.fasterxml.jackson.databind.ObjectMapper ;
@Path ( " " )
2020-03-16 16:55:26 +01:00
@ManagedBy ( StorageHubAppllicationManager . class )
2018-05-17 12:51:56 +02:00
public class WorkspaceManager {
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
private static final Logger log = LoggerFactory . getLogger ( WorkspaceManager . class ) ;
2020-03-16 16:55:26 +01:00
RepositoryInitializer repository = StorageHubAppllicationManager . repository ;
2018-05-17 12:51:56 +02:00
@Inject
Evaluators evaluator ;
2018-06-20 16:59:41 +02:00
@Inject
AuthorizationChecker authChecker ;
@Inject
ServletContext context ;
@Inject
VREManager vreManager ;
2018-05-17 12:51:56 +02:00
2018-10-25 16:33:23 +02:00
@Inject
TrashHandler trashHandler ;
2018-05-17 12:51:56 +02:00
@RequestScoped
@QueryParam ( " exclude " )
private List < String > excludes = Collections . emptyList ( ) ;
2018-06-29 16:59:24 +02:00
2018-10-25 16:33:23 +02:00
@Inject Node2ItemConverter node2Item ;
@Inject Item2NodeConverter item2Node ;
2018-06-20 16:59:41 +02:00
2019-07-29 16:26:08 +02:00
@Inject StorageBackendHandler storageBackend ;
2018-05-17 12:51:56 +02:00
@Path ( " " )
@GET
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-06-20 16:59:41 +02:00
public ItemWrapper < Item > getWorkspace ( @QueryParam ( " relPath " ) String relPath ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " getWorkspace " ) ;
2018-05-17 12:51:56 +02:00
Session ses = null ;
2018-06-20 16:59:41 +02:00
org . gcube . common . storagehub . model . Path absolutePath ;
if ( relPath = = null )
2018-12-17 14:55:43 +01:00
absolutePath = Utils . getWorkspacePath ( ) ;
else absolutePath = Paths . append ( Utils . getWorkspacePath ( ) , relPath ) ;
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
Item toReturn = null ;
try {
long start = System . currentTimeMillis ( ) ;
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2019-01-21 11:19:04 +01:00
//TODO: remove it when users will be created via storageHub
String user = AuthorizationProvider . instance . get ( ) . getClient ( ) . getId ( ) ;
org . gcube . common . storagehub . model . Path trashPath = Paths . append ( Utils . getWorkspacePath ( ) , Constants . TRASH_ROOT_FOLDER_NAME ) ;
if ( ! ses . nodeExists ( trashPath . toPath ( ) ) ) {
Utils . createFolderInternally ( ses , ses . getNode ( Utils . getWorkspacePath ( ) . toPath ( ) ) , Constants . TRASH_ROOT_FOLDER_NAME , " trash of " + user , false , user , null ) ;
2019-04-11 16:38:41 +02:00
ses . save ( ) ;
2019-01-21 11:19:04 +01:00
}
2018-06-20 16:59:41 +02:00
log . trace ( " time to connect to repo {} " , ( System . currentTimeMillis ( ) - start ) ) ;
Node node = ses . getNode ( absolutePath . toPath ( ) ) ;
authChecker . checkReadAuthorizationControl ( ses , node . getIdentifier ( ) ) ;
2018-10-25 16:33:23 +02:00
toReturn = node2Item . getItem ( node , excludes ) ;
} catch ( RepositoryException re ) {
log . error ( " jcr error getting workspace item " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-05-17 12:51:56 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
return new ItemWrapper < Item > ( toReturn ) ;
}
2018-06-20 16:59:41 +02:00
2020-01-22 16:41:12 +01:00
/ * private synchronized VRE getVreFolderItem ( Session ses ) throws RepositoryException , BackendGenericError {
2018-12-17 14:55:43 +01:00
org . gcube . common . storagehub . model . Path vrePath = Paths . append ( Utils . getWorkspacePath ( ) , Constants . VRE_FOLDER_PARENT_NAME ) ;
2018-06-20 16:59:41 +02:00
ScopeBean bean = new ScopeBean ( ScopeProvider . instance . get ( ) ) ;
2018-10-25 16:33:23 +02:00
if ( ! bean . is ( Type . VRE ) ) throw new BackendGenericError ( " the current scope is not a VRE " ) ;
2018-06-20 16:59:41 +02:00
String entireScopeName = bean . toString ( ) . replaceAll ( " ^/(.*)/?$ " , " $1 " ) . replaceAll ( " / " , " - " ) ;
VRE vre = vreManager . getVRE ( entireScopeName ) ;
if ( vre ! = null ) return vre ;
else {
String query = String . format ( " SELECT * FROM [nthl:workspaceItem] As node WHERE node.[jcr:title] like '%s' AND ISDESCENDANTNODE('%s') " , entireScopeName , vrePath . toPath ( ) ) ;
Query jcrQuery = ses . getWorkspace ( ) . getQueryManager ( ) . createQuery ( query , Constants . QUERY_LANGUAGE ) ;
NodeIterator it = jcrQuery . execute ( ) . getNodes ( ) ;
2018-10-25 16:33:23 +02:00
if ( ! it . hasNext ( ) ) throw new BackendGenericError ( " vre folder not found for context " + entireScopeName ) ;
2018-06-20 16:59:41 +02:00
Node folder = it . nextNode ( ) ;
2018-10-25 16:33:23 +02:00
Item vreFolder = node2Item . getItem ( folder , excludes ) ;
2018-06-20 16:59:41 +02:00
return vreManager . putVRE ( vreFolder ) ;
}
2020-01-22 16:41:12 +01:00
} * /
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
@Path ( " vrefolder " )
@GET
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-05-17 12:51:56 +02:00
public ItemWrapper < Item > getVreRootFolder ( ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " getVreRootFolder " ) ;
2018-05-17 12:51:56 +02:00
Session ses = null ;
2018-10-25 16:33:23 +02:00
Item vreItem = null ;
2018-06-20 16:59:41 +02:00
try {
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2020-01-22 16:41:12 +01:00
vreItem = Utils . getVreFolderItem ( ses , node2Item , vreManager , excludes ) . getVreFolder ( ) ;
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error getting vrefolder " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-06-20 16:59:41 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
2018-10-25 16:33:23 +02:00
}
return new ItemWrapper < Item > ( vreItem ) ;
2018-06-20 16:59:41 +02:00
}
@Path ( " vrefolder/recents " )
@GET
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-06-20 16:59:41 +02:00
public ItemList getVreFolderRecentsDocument ( ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " getVreFolderRecents " ) ;
2018-06-20 16:59:41 +02:00
Session ses = null ;
2018-10-25 16:33:23 +02:00
List < Item > recentItems = Collections . emptyList ( ) ;
2018-05-17 12:51:56 +02:00
try {
2020-03-11 15:00:40 +01:00
//String login = AuthorizationProvider.instance.get().getClient().getId();
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2018-05-17 12:51:56 +02:00
2020-01-22 16:41:12 +01:00
VRE vre = Utils . getVreFolderItem ( ses , node2Item , vreManager , excludes ) ;
2018-06-20 16:59:41 +02:00
log . trace ( " VRE retrieved {} " , vre . getVreFolder ( ) . getTitle ( ) ) ;
2018-10-25 16:33:23 +02:00
recentItems = vre . getRecents ( ) ;
2018-06-20 16:59:41 +02:00
log . trace ( " recents retrieved {} " , vre . getVreFolder ( ) . getTitle ( ) ) ;
return new ItemList ( recentItems ) ;
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error getting recents " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-05-17 12:51:56 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
2018-10-25 16:33:23 +02:00
return new ItemList ( recentItems ) ;
2018-05-17 12:51:56 +02:00
}
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
@Path ( " trash " )
@GET
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-05-17 12:51:56 +02:00
public ItemWrapper < Item > getTrashRootFolder ( ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " getTrashRootFolder " ) ;
2018-05-17 12:51:56 +02:00
Session ses = null ;
2020-03-11 15:00:40 +01:00
//String user = AuthorizationProvider.instance.get().getClient().getId();
2018-12-17 14:55:43 +01:00
org . gcube . common . storagehub . model . Path trashPath = Paths . append ( Utils . getWorkspacePath ( ) , Constants . TRASH_ROOT_FOLDER_NAME ) ;
2018-10-25 16:33:23 +02:00
Item item = null ;
2018-05-17 12:51:56 +02:00
try {
long start = System . currentTimeMillis ( ) ;
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2018-05-17 12:51:56 +02:00
log . info ( " time to connect to repo {} " , ( System . currentTimeMillis ( ) - start ) ) ;
2019-01-18 15:03:10 +01:00
2018-05-17 12:51:56 +02:00
Node folder = ses . getNode ( trashPath . toPath ( ) ) ;
2018-10-25 16:33:23 +02:00
item = node2Item . getItem ( folder , excludes ) ;
} catch ( RepositoryException re ) {
log . error ( " jcr error getting trash " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-05-17 12:51:56 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
2018-10-25 16:33:23 +02:00
return new ItemWrapper < Item > ( item ) ;
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
}
2018-10-25 16:33:23 +02:00
@Path ( " trash/empty " )
@DELETE
public String emptyTrash ( ) {
InnerMethodName . instance . set ( " emptyTrash " ) ;
Session ses = null ;
2018-12-17 14:55:43 +01:00
org . gcube . common . storagehub . model . Path trashPath = Paths . append ( Utils . getWorkspacePath ( ) , Constants . TRASH_ROOT_FOLDER_NAME ) ;
2018-10-25 16:33:23 +02:00
String toReturn = null ;
try {
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
Node trashNode = ses . getNode ( trashPath . toPath ( ) ) ;
List < Item > itemsToDelete = Utils . getItemList ( trashNode , Excludes . ALL , null , true , null ) ;
trashHandler . removeNodes ( ses , itemsToDelete ) ;
toReturn = trashNode . getIdentifier ( ) ;
} catch ( RepositoryException re ) {
log . error ( " jcr error emptying trash " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-10-25 16:33:23 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
return toReturn ;
}
@PUT
@Consumes ( MediaType . TEXT_PLAIN )
@Path ( " trash/restore " )
public String restoreItem ( String identifier ) {
InnerMethodName . instance . set ( " restoreItem " ) ;
Session ses = null ;
String toReturn = null ;
try {
2018-06-20 16:59:41 +02:00
2018-10-25 16:33:23 +02:00
log . info ( " restoring node with id {} " , identifier ) ;
//TODO check if it is possible to change all the ACL on a workspace
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
authChecker . checkWriteAuthorizationControl ( ses , identifier , false ) ;
final Node nodeToRestore = ses . getNodeByIdentifier ( identifier ) ;
Item itemToRestore = node2Item . getItem ( nodeToRestore , Excludes . ALL ) ;
if ( ! ( itemToRestore instanceof TrashItem ) )
throw new InvalidItemException ( " Only trash items can be restored " ) ;
toReturn = trashHandler . restoreItem ( ses , ( TrashItem ) itemToRestore ) ;
} catch ( RepositoryException re ) {
log . error ( " error restoring item with id {} " , identifier , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
} finally {
2018-10-25 16:33:23 +02:00
if ( ses ! = null ) {
ses . logout ( ) ;
}
}
return toReturn ;
}
2018-05-17 12:51:56 +02:00
@Path ( " vrefolders " )
@GET
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-05-17 12:51:56 +02:00
public ItemList getVreFolders ( ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " getVreFolders " ) ;
2018-05-17 12:51:56 +02:00
Session ses = null ;
2018-06-20 16:59:41 +02:00
2018-12-17 14:55:43 +01:00
org . gcube . common . storagehub . model . Path vrePath = Paths . append ( Utils . getWorkspacePath ( ) , Constants . VRE_FOLDER_PARENT_NAME ) ;
2018-05-17 12:51:56 +02:00
List < ? extends Item > toReturn = null ;
try {
2018-12-28 17:58:08 +01:00
log . info ( " vres folder path is {} " , vrePath . toPath ( ) ) ;
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2018-10-25 16:33:23 +02:00
toReturn = Utils . getItemList ( ses . getNode ( vrePath . toPath ( ) ) , excludes , null , false , null ) ;
} catch ( RepositoryException re ) {
log . error ( " error reading the node children of {} " , vrePath , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-05-17 12:51:56 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
return new ItemList ( toReturn ) ;
}
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
@Path ( " vrefolders/paged " )
@GET
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-05-17 12:51:56 +02:00
public ItemList getVreFoldersPaged ( @QueryParam ( " start " ) Integer start , @QueryParam ( " limit " ) Integer limit ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " getVreFoldersPaged " ) ;
2018-05-17 12:51:56 +02:00
Session ses = null ;
2018-12-17 14:55:43 +01:00
org . gcube . common . storagehub . model . Path vrePath = Paths . append ( Utils . getWorkspacePath ( ) , Constants . VRE_FOLDER_PARENT_NAME ) ;
2018-05-17 12:51:56 +02:00
List < ? extends Item > toReturn = null ;
try {
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2018-10-25 16:33:23 +02:00
toReturn = Utils . getItemList ( ses . getNode ( vrePath . toPath ( ) ) , excludes , new Range ( start , limit ) , false , null ) ;
} catch ( RepositoryException re ) {
log . error ( " (paged) error reading the node children of {} " , vrePath , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-05-17 12:51:56 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
return new ItemList ( toReturn ) ;
}
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
@Path ( " query " )
@GET
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-05-17 12:51:56 +02:00
public ItemList searchItems ( @QueryParam ( " n " ) String node , @QueryParam ( " e " ) String jsonExpr , @QueryParam ( " o " ) List < String > orderField , @QueryParam ( " l " ) Integer limit , @QueryParam ( " f " ) Integer offset ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " searchItems " ) ;
2018-05-17 12:51:56 +02:00
Session ses = null ;
List < ? extends Item > toReturn = new ArrayList < > ( ) ;
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
try {
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
ObjectMapper mapper = new ObjectMapper ( ) ;
Expression < Boolean > expression = mapper . readValue ( jsonExpr , Expression . class ) ;
2018-12-17 14:55:43 +01:00
String stringExpression = evaluator . evaluate ( new And ( new ISDescendant ( Utils . getWorkspacePath ( ) ) , expression ) ) ;
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
String orderBy = " " ;
if ( orderField ! = null & & orderField . size ( ) > 0 )
2018-06-20 16:59:41 +02:00
orderBy = String . format ( " ORDER BY %s " , orderField . stream ( ) . collect ( Collectors . joining ( " , " ) ) . toString ( ) ) ;
2018-05-17 12:51:56 +02:00
String sql2Query = String . format ( " SELECT * FROM [%s] AS node WHERE %s %s " , node , stringExpression , orderBy ) ;
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
log . info ( " query sent is {} " , sql2Query ) ;
2018-06-20 16:59:41 +02:00
2018-12-17 14:55:43 +01:00
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2018-05-17 12:51:56 +02:00
Query jcrQuery = ses . getWorkspace ( ) . getQueryManager ( ) . createQuery ( sql2Query , Constants . QUERY_LANGUAGE ) ;
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
if ( limit ! = null & & limit ! = - 1 )
jcrQuery . setLimit ( limit ) ;
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
if ( offset ! = null & & offset ! = - 1 )
jcrQuery . setOffset ( offset ) ;
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
QueryResult result = jcrQuery . execute ( ) ;
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
NodeIterator it = result . getNodes ( ) ;
2018-06-20 16:59:41 +02:00
2018-05-17 12:51:56 +02:00
while ( it . hasNext ( ) )
2018-10-25 16:33:23 +02:00
toReturn . add ( node2Item . getItem ( it . nextNode ( ) , null ) ) ;
} catch ( RepositoryException | IOException re ) {
log . error ( " error executing the query " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-05-17 12:51:56 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
2018-06-20 16:59:41 +02:00
return new ItemList ( toReturn ) ;
2018-05-17 12:51:56 +02:00
}
2019-07-29 16:26:08 +02:00
@Path ( " count " )
@GET
public String getTotalItemsCount ( ) {
InnerMethodName . instance . set ( " getTotalItemsCount " ) ;
return storageBackend . getTotalItemsCount ( ) ;
}
@Path ( " size " )
@GET
public String getTotalVolume ( ) {
InnerMethodName . instance . set ( " getTotalSize " ) ;
return storageBackend . getTotalVolume ( ) ;
}
2018-05-17 12:51:56 +02:00
}