2018-05-17 12:51:56 +02:00
package org.gcube.data.access.storagehub ;
import java.io.BufferedInputStream ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.OutputStream ;
2019-07-29 16:26:08 +02:00
import java.net.URL ;
2018-05-17 12:51:56 +02:00
import java.security.MessageDigest ;
import java.util.ArrayList ;
2021-03-15 16:01:14 +01:00
import java.util.Arrays ;
2018-10-25 16:33:23 +02:00
import java.util.Calendar ;
2018-05-17 12:51:56 +02:00
import java.util.Deque ;
2020-03-17 13:58:37 +01:00
import java.util.HashSet ;
2018-05-17 12:51:56 +02:00
import java.util.LinkedList ;
import java.util.List ;
2018-10-25 16:33:23 +02:00
import java.util.Set ;
2021-03-12 10:24:45 +01:00
import java.util.function.Predicate ;
2018-05-17 12:51:56 +02:00
import java.util.zip.ZipEntry ;
import java.util.zip.ZipOutputStream ;
import javax.jcr.Node ;
import javax.jcr.NodeIterator ;
2018-10-25 16:33:23 +02:00
import javax.jcr.PathNotFoundException ;
import javax.jcr.RepositoryException ;
2018-05-17 12:51:56 +02:00
import javax.jcr.Session ;
2019-04-01 17:50:43 +02:00
import javax.jcr.lock.Lock ;
import javax.jcr.lock.LockException ;
2021-03-15 16:01:14 +01:00
import javax.jcr.nodetype.NodeType ;
2020-01-22 16:41:12 +01:00
import javax.jcr.query.Query ;
2018-10-25 16:33:23 +02:00
import javax.jcr.version.Version ;
2018-05-17 12:51:56 +02:00
2018-10-25 16:33:23 +02:00
import org.apache.commons.io.FilenameUtils ;
2020-01-22 16:41:12 +01:00
import org.apache.jackrabbit.util.ISO9075 ;
2018-10-25 16:33:23 +02:00
import org.apache.jackrabbit.util.Text ;
2018-05-17 12:51:56 +02:00
import org.gcube.common.authorization.library.provider.AuthorizationProvider ;
2020-01-22 16:41:12 +01:00
import org.gcube.common.scope.api.ScopeProvider ;
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.common.scope.impl.ScopeBean.Type ;
2018-10-25 16:33:23 +02:00
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 ;
2019-04-01 17:50:43 +02:00
import org.gcube.common.storagehub.model.exceptions.ItemLockedException ;
2020-03-17 13:40:41 +01:00
import org.gcube.common.storagehub.model.exceptions.UserNotAuthorizedException ;
2018-05-17 12:51:56 +02:00
import org.gcube.common.storagehub.model.items.AbstractFileItem ;
2019-07-29 16:26:08 +02:00
import org.gcube.common.storagehub.model.items.ExternalLink ;
2018-05-17 12:51:56 +02:00
import org.gcube.common.storagehub.model.items.FolderItem ;
2018-10-25 16:33:23 +02:00
import org.gcube.common.storagehub.model.items.GCubeItem ;
2018-05-17 12:51:56 +02:00
import org.gcube.common.storagehub.model.items.Item ;
2018-06-29 16:59:24 +02:00
import org.gcube.common.storagehub.model.items.SharedFolder ;
2018-10-25 16:33:23 +02:00
import org.gcube.common.storagehub.model.types.ItemAction ;
2018-05-17 12:51:56 +02:00
import org.gcube.common.storagehub.model.types.NodeProperty ;
import org.gcube.data.access.storagehub.accounting.AccountingHandler ;
2019-07-05 12:09:16 +02:00
import org.gcube.data.access.storagehub.handlers.StorageBackendHandler ;
2020-01-22 16:41:12 +01:00
import org.gcube.data.access.storagehub.handlers.VRE ;
import org.gcube.data.access.storagehub.handlers.VREManager ;
2018-10-25 16:33:23 +02:00
import org.gcube.data.access.storagehub.handlers.VersionHandler ;
2020-04-08 21:11:43 +02:00
import org.gcube.data.access.storagehub.handlers.items.Item2NodeConverter ;
import org.gcube.data.access.storagehub.handlers.items.Node2ItemConverter ;
2018-05-17 12:51:56 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2021-03-15 16:01:14 +01:00
import net.bull.javamelody.internal.common.LOG ;
2018-05-17 12:51:56 +02:00
public class Utils {
public final static String SERVICE_NAME = " home-library " ;
public final static String SERVICE_CLASS = " org.gcube.portlets.user " ;
private static final String FOLDERS_TYPE = " nthl:workspaceItem " ;
2018-10-25 16:33:23 +02:00
2018-05-17 12:51:56 +02:00
private static final Logger logger = LoggerFactory . getLogger ( Utils . class ) ;
public static String getSecurePassword ( String user ) throws Exception {
String digest = null ;
try {
MessageDigest md = MessageDigest . getInstance ( " MD5 " ) ;
byte [ ] hash = md . digest ( user . getBytes ( " UTF-8 " ) ) ;
//converting byte array to Hexadecimal String
StringBuilder sb = new StringBuilder ( 2 * hash . length ) ;
for ( byte b : hash ) {
sb . append ( String . format ( " %02x " , b & 0xff ) ) ;
}
digest = sb . toString ( ) ;
} catch ( Exception e ) {
logger . error ( " error getting secure password " , e ) ;
}
return digest ;
}
2018-10-25 16:33:23 +02:00
public static long getItemCount ( Node parent , boolean showHidden , Class < ? extends Item > nodeType ) throws RepositoryException , BackendGenericError {
return getItemList ( parent , Excludes . ALL , null , showHidden , nodeType ) . size ( ) ;
2018-05-17 12:51:56 +02:00
}
2018-10-25 16:33:23 +02:00
2019-04-01 17:50:43 +02:00
public static void acquireLockWithWait ( Session ses , String nodePath , boolean isDeep , String login , int maxTries ) throws RepositoryException , ItemLockedException {
Lock lock = null ;
int tries = 0 ;
while ( lock = = null & & tries < = maxTries ) {
try {
lock = ses . getWorkspace ( ) . getLockManager ( ) . lock ( nodePath , isDeep , true , 0 , login ) ;
logger . info ( " lock acquired(try n. {}) " , tries ) ;
} catch ( LockException e ) {
try {
if ( maxTries > = tries ) {
int waitTime = ( int ) ( Math . random ( ) * 5000 ) ;
logger . info ( " lock NOT acquired, waiting (try n. {}) for {} " , tries , waitTime ) ;
Thread . sleep ( waitTime ) ;
}
} catch ( InterruptedException e1 ) { }
tries + + ;
}
}
if ( lock = = null ) throw new ItemLockedException ( " the item is locked " ) ;
}
2018-10-25 16:33:23 +02:00
2020-03-17 13:40:41 +01:00
public static < T extends Item > List < T > serachByNameOnFolder ( Session ses , AuthorizationChecker authChecker , Node parent , List < String > excludes , Range range , boolean showHidden , boolean excludeTrashed , Class < ? extends Item > nodeTypeToInclude , String nameParam ) throws RepositoryException , BackendGenericError {
2020-01-24 12:30:05 +01:00
String xpath = String . format ( " /jcr:root%s//element(*,nthl:workspaceItem)[jcr:like(fn:lower-case(@jcr:title), '%s')] " , ISO9075 . encodePath ( parent . getPath ( ) ) , nameParam . toLowerCase ( ) ) ;
2018-10-25 16:33:23 +02:00
2020-01-22 16:41:12 +01:00
//String query = String.format("SELECT * FROM [nthl:workspaceLeafItem] AS node WHERE ISDESCENDANTNODE('%s') ORDER BY node.[jcr:lastModified] DESC ",vreFolder.getPath());
2020-03-17 13:40:41 +01:00
logger . debug ( " query for search is {} " , xpath ) ;
long start = System . currentTimeMillis ( ) ;
2020-01-22 16:41:12 +01:00
Query jcrQuery = ses . getWorkspace ( ) . getQueryManager ( ) . createQuery ( xpath , Query . XPATH ) ;
2020-04-14 18:52:59 +02:00
NodeChildrenFilterIterator iterator = new NodeChildrenFilterIterator ( jcrQuery . execute ( ) . getNodes ( ) ) ;
2020-03-17 13:40:41 +01:00
logger . debug ( " [SEARCH] real search took {} millis " , ( System . currentTimeMillis ( ) - start ) ) ;
2021-03-12 10:24:45 +01:00
Predicate < Node > checker = new Predicate < Node > ( ) {
@Override
public boolean test ( Node t ) {
try {
authChecker . checkReadAuthorizationControl ( t . getSession ( ) , t . getIdentifier ( ) ) ;
return true ;
} catch ( UserNotAuthorizedException | BackendGenericError | RepositoryException e ) {
return false ;
}
2021-03-15 16:01:14 +01:00
2021-03-12 10:24:45 +01:00
}
2021-03-15 16:01:14 +01:00
2021-03-12 10:24:45 +01:00
} ;
2021-03-15 16:01:14 +01:00
2021-03-12 10:24:45 +01:00
return getItemListFromNodeIterator ( checker , iterator , excludes , range , showHidden , excludeTrashed , nodeTypeToInclude ) ;
2020-01-22 16:41:12 +01:00
}
2021-03-15 16:01:14 +01:00
2020-01-22 16:41:12 +01:00
public static < T extends Item > List < T > getItemList ( Node parent , List < String > excludes , Range range , boolean showHidden , Class < ? extends Item > nodeTypeToInclude ) throws RepositoryException , BackendGenericError {
2021-03-12 10:24:45 +01:00
return getItemList ( null , parent , excludes , range , showHidden , nodeTypeToInclude ) ;
}
2021-03-15 16:01:14 +01:00
2021-03-12 10:24:45 +01:00
public static < T extends Item > List < T > getItemList ( Predicate < Node > checker , Node parent , List < String > excludes , Range range , boolean showHidden , Class < ? extends Item > nodeTypeToInclude ) throws RepositoryException , BackendGenericError {
2020-01-22 16:41:12 +01:00
logger . trace ( " getting children of node {} " , parent . getIdentifier ( ) ) ;
2018-05-17 12:51:56 +02:00
long start = System . currentTimeMillis ( ) ;
2020-04-14 18:52:59 +02:00
NodeChildrenFilterIterator iterator = new NodeChildrenFilterIterator ( parent ) ;
2018-05-17 12:51:56 +02:00
logger . trace ( " time to get iterator {} " , ( System . currentTimeMillis ( ) - start ) ) ;
2021-03-12 10:24:45 +01:00
return getItemListFromNodeIterator ( checker , iterator , excludes , range , showHidden , false , nodeTypeToInclude ) ;
2020-01-22 16:41:12 +01:00
}
2021-03-15 16:01:14 +01:00
2021-03-12 10:24:45 +01:00
private static < T extends Item > List < T > getItemListFromNodeIterator ( Predicate < Node > checker , NodeChildrenFilterIterator iterator , List < String > excludes , Range range , boolean showHidden , boolean excludeTrashed , Class < ? extends Item > nodeTypeToInclude ) throws RepositoryException , BackendGenericError {
2020-01-22 16:41:12 +01:00
List < T > returnList = new ArrayList < T > ( ) ;
2021-03-15 16:01:14 +01:00
2018-10-25 16:33:23 +02:00
logger . trace ( " nodeType is {} " , nodeTypeToInclude ) ;
2018-05-17 12:51:56 +02:00
int count = 0 ;
logger . trace ( " selected range is {} " , range ) ;
2018-10-25 16:33:23 +02:00
Node2ItemConverter node2Item = new Node2ItemConverter ( ) ;
2020-03-17 13:58:37 +01:00
Set < String > duplicateId = new HashSet < String > ( ) ;
2018-05-17 12:51:56 +02:00
while ( iterator . hasNext ( ) ) {
2020-04-14 18:52:59 +02:00
Node current = iterator . next ( ) ;
2021-03-15 16:01:14 +01:00
2020-03-17 13:40:41 +01:00
logger . debug ( " [SEARCH] evaluating node {} " , current . hasProperty ( NodeProperty . TITLE . toString ( ) ) ? current . getProperty ( NodeProperty . TITLE . toString ( ) ) : current . getName ( ) ) ;
2021-03-15 16:01:14 +01:00
2020-03-17 13:58:37 +01:00
//REMOVE duplicate nodes, in case the indexes are not working
2020-04-08 21:11:43 +02:00
if ( duplicateId . contains ( current . getIdentifier ( ) ) ) {
logger . warn ( " duplicated node found " ) ;
continue ;
}
2021-03-15 16:01:14 +01:00
2021-03-12 10:24:45 +01:00
//EXCLUDES node from predicate
if ( checker ! = null & & ! checker . test ( current ) )
continue ;
2021-03-15 16:01:14 +01:00
2018-05-17 12:51:56 +02:00
if ( isToExclude ( current , showHidden ) )
continue ;
2019-04-01 17:50:43 +02:00
2020-03-17 13:40:41 +01:00
logger . debug ( " [SEARCH] current node not excluded {} " , current . hasProperty ( NodeProperty . TITLE . toString ( ) ) ? current . getProperty ( NodeProperty . TITLE . toString ( ) ) : current . getName ( ) ) ;
2019-04-01 17:50:43 +02:00
2018-05-17 12:51:56 +02:00
if ( range = = null | | ( count > = range . getStart ( ) & & returnList . size ( ) < range . getLimit ( ) ) ) {
2018-10-25 16:33:23 +02:00
T item = node2Item . getFilteredItem ( current , excludes , nodeTypeToInclude ) ;
2020-03-16 16:55:26 +01:00
if ( item = = null | | ( item . isTrashed ( ) & & excludeTrashed ) ) continue ;
2018-05-17 12:51:56 +02:00
returnList . add ( item ) ;
}
count + + ;
2020-03-17 13:58:37 +01:00
duplicateId . add ( current . getIdentifier ( ) ) ;
2018-05-17 12:51:56 +02:00
}
return returnList ;
}
2018-10-25 16:33:23 +02:00
private static boolean isToExclude ( Node node , boolean showHidden ) throws RepositoryException {
2018-05-17 12:51:56 +02:00
return ( ( node . getName ( ) . startsWith ( " rep: " ) | | ( node . getName ( ) . startsWith ( " hl: " ) ) ) | |
2018-10-25 16:33:23 +02:00
( ! showHidden & & node . hasProperty ( NodeProperty . HIDDEN . toString ( ) ) & & node . getProperty ( NodeProperty . HIDDEN . toString ( ) ) . getBoolean ( ) ) | |
( node . getPrimaryNodeType ( ) . getName ( ) . equals ( FOLDERS_TYPE ) & & Constants . FOLDERS_TO_EXLUDE . contains ( node . getName ( ) ) ) ) ;
2018-05-17 12:51:56 +02:00
}
2018-10-25 16:33:23 +02:00
2018-12-17 14:55:43 +01:00
public static org . gcube . common . storagehub . model . Path getWorkspacePath ( ) {
2020-04-09 09:44:40 +02:00
return Paths . getPath ( String . format ( " /Home/%s/%s " , AuthorizationProvider . instance . get ( ) . getClient ( ) . getId ( ) , Constants . WORKSPACE_ROOT_FOLDER_NAME ) ) ;
2018-05-17 12:51:56 +02:00
}
2018-12-17 14:55:43 +01:00
public static org . gcube . common . storagehub . model . Path getWorkspacePath ( String login ) {
2020-04-09 09:44:40 +02:00
return Paths . getPath ( String . format ( " /Home/%s/%s " , login , Constants . WORKSPACE_ROOT_FOLDER_NAME ) ) ;
2018-06-29 16:59:24 +02:00
}
2018-10-25 16:33:23 +02:00
2018-12-17 14:55:43 +01:00
public static org . gcube . common . storagehub . model . Path getHome ( String login ) {
return Paths . getPath ( String . format ( " /Home/%s " , login ) ) ;
}
2019-04-01 17:50:43 +02:00
2018-10-25 16:33:23 +02:00
public static Deque < Item > getAllNodesForZip ( FolderItem directory , Session session , AccountingHandler accountingHandler , List < String > excludes ) throws RepositoryException , BackendGenericError {
2018-05-17 12:51:56 +02:00
Deque < Item > queue = new LinkedList < Item > ( ) ;
Node currentNode = session . getNodeByIdentifier ( directory . getId ( ) ) ;
queue . push ( directory ) ;
Deque < Item > tempQueue = new LinkedList < Item > ( ) ;
2020-01-22 16:41:12 +01:00
logger . trace ( " adding directory {} " , currentNode . getPath ( ) ) ;
2018-10-25 16:33:23 +02:00
for ( Item item : Utils . getItemList ( currentNode , Excludes . GET_ONLY_CONTENT , null , false , null ) ) {
if ( excludes . contains ( item . getId ( ) ) ) continue ;
2018-05-17 12:51:56 +02:00
if ( item instanceof FolderItem )
2018-10-25 16:33:23 +02:00
tempQueue . addAll ( getAllNodesForZip ( ( FolderItem ) item , session , accountingHandler , excludes ) ) ;
2018-05-17 12:51:56 +02:00
else if ( item instanceof AbstractFileItem ) {
2020-01-22 16:41:12 +01:00
logger . trace ( " adding file {} " , item . getPath ( ) ) ;
2018-05-17 12:51:56 +02:00
AbstractFileItem fileItem = ( AbstractFileItem ) item ;
accountingHandler . createReadObj ( fileItem . getTitle ( ) , session , session . getNodeByIdentifier ( item . getId ( ) ) , false ) ;
queue . addLast ( item ) ;
}
}
queue . addAll ( tempQueue ) ;
return queue ;
}
2019-07-05 12:09:16 +02:00
public static void zipNode ( ZipOutputStream zos , Deque < Item > queue , String login , org . gcube . common . storagehub . model . Path originalPath , StorageBackendHandler storageHandler ) throws Exception {
2018-05-17 12:51:56 +02:00
logger . trace ( " originalPath is {} " , originalPath . toPath ( ) ) ;
org . gcube . common . storagehub . model . Path actualPath = Paths . getPath ( " " ) ;
while ( ! queue . isEmpty ( ) ) {
Item item = queue . pop ( ) ;
if ( item instanceof FolderItem ) {
actualPath = Paths . getPath ( item . getPath ( ) ) ;
2020-01-22 16:41:12 +01:00
logger . trace ( " actualPath is {} " , actualPath . toPath ( ) ) ;
2018-05-17 12:51:56 +02:00
String name = Paths . remove ( actualPath , originalPath ) . toPath ( ) . replaceFirst ( " / " , " " ) ;
2020-01-22 16:41:12 +01:00
logger . trace ( " writing dir {} " , name ) ;
2018-10-25 16:33:23 +02:00
if ( name . isEmpty ( ) ) continue ;
try {
2018-05-17 12:51:56 +02:00
zos . putNextEntry ( new ZipEntry ( name ) ) ;
2018-10-25 16:33:23 +02:00
} finally {
2018-05-17 12:51:56 +02:00
zos . closeEntry ( ) ;
}
2018-10-25 16:33:23 +02:00
} else if ( item instanceof AbstractFileItem ) {
try {
2019-07-05 12:09:16 +02:00
InputStream streamToWrite = storageHandler . download ( ( ( AbstractFileItem ) item ) . getContent ( ) . getStorageId ( ) ) ;
2018-10-25 16:33:23 +02:00
if ( streamToWrite = = null ) {
logger . warn ( " discarding item {} " , item . getName ( ) ) ;
continue ;
}
try ( BufferedInputStream is = new BufferedInputStream ( streamToWrite ) ) {
String name = ( Paths . remove ( actualPath , originalPath ) . toPath ( ) + item . getName ( ) ) . replaceFirst ( " / " , " " ) ;
2020-01-22 16:41:12 +01:00
logger . trace ( " writing file {} " , name ) ;
2018-10-25 16:33:23 +02:00
zos . putNextEntry ( new ZipEntry ( name ) ) ;
copyStream ( is , zos ) ;
} catch ( Exception e ) {
logger . warn ( " error writing item {} " , item . getName ( ) , e ) ;
} finally {
zos . closeEntry ( ) ;
}
zos . flush ( ) ;
} catch ( Throwable e ) {
logger . warn ( " error reading content for item {} " , item . getPath ( ) , e ) ;
}
2018-05-17 12:51:56 +02:00
}
}
2018-10-25 16:33:23 +02:00
zos . close ( ) ;
2018-05-17 12:51:56 +02:00
}
private static void copyStream ( InputStream in , OutputStream out ) throws IOException {
byte [ ] buffer = new byte [ 2048 ] ;
int readcount = 0 ;
while ( ( readcount = in . read ( buffer ) ) ! = - 1 ) {
out . write ( buffer , 0 , readcount ) ;
}
}
2018-10-25 16:33:23 +02:00
public static boolean hasSharedChildren ( Node node ) throws RepositoryException , BackendGenericError {
Node2ItemConverter node2Item = new Node2ItemConverter ( ) ;
2020-04-14 18:52:59 +02:00
NodeChildrenFilterIterator children = new NodeChildrenFilterIterator ( node ) ;
2019-04-01 17:50:43 +02:00
2018-10-25 16:33:23 +02:00
while ( children . hasNext ( ) ) {
2020-04-14 18:52:59 +02:00
Node child = children . next ( ) ;
2018-10-25 16:33:23 +02:00
if ( node2Item . checkNodeType ( child , SharedFolder . class ) ) return true ;
if ( node2Item . checkNodeType ( child , FolderItem . class ) & & hasSharedChildren ( child ) ) return true ;
2018-06-29 16:59:24 +02:00
}
return false ;
2018-10-25 16:33:23 +02:00
}
public static void getAllContentIds ( Session ses , Set < String > idsToDelete , Item itemToDelete , VersionHandler versionHandler ) throws Exception {
if ( itemToDelete instanceof AbstractFileItem ) {
2021-03-15 16:01:14 +01:00
Node currentNode = ses . getNodeByIdentifier ( itemToDelete . getId ( ) ) ;
List < NodeType > ntList = Arrays . asList ( currentNode . getMixinNodeTypes ( ) ) ;
boolean isVersioned = false ;
logger . debug ( " mixin node type are {} " , ntList ) ;
for ( NodeType nt : ntList )
if ( nt . getName ( ) . equals ( " mix:versionable " ) ) {
isVersioned = true ;
break ;
2018-10-25 16:33:23 +02:00
}
2021-03-15 16:01:14 +01:00
if ( isVersioned ) {
List < Version > versions = versionHandler . getContentVersionHistory ( currentNode , ses ) ;
versions . forEach ( v - > {
try {
String storageId = v . getFrozenNode ( ) . getProperty ( NodeProperty . STORAGE_ID . toString ( ) ) . getString ( ) ;
idsToDelete . add ( storageId ) ;
logger . info ( " retrieved StorageId {} for version {} " , storageId , v . getName ( ) ) ;
} catch ( Exception e ) {
logger . warn ( " error retreiving sotrageId version for item with id {} " , itemToDelete . getId ( ) , e ) ;
}
} ) ;
} else
logger . info ( " not versionable node type found of type {} " , currentNode . getPrimaryNodeType ( ) . toString ( ) ) ;
2018-10-25 16:33:23 +02:00
idsToDelete . add ( ( ( AbstractFileItem ) itemToDelete ) . getContent ( ) . getStorageId ( ) ) ;
} else if ( itemToDelete instanceof FolderItem ) {
List < Item > items = Utils . getItemList ( ses . getNodeByIdentifier ( itemToDelete . getId ( ) ) , Excludes . GET_ONLY_CONTENT , null , true , null ) ;
for ( Item item : items )
getAllContentIds ( ses , idsToDelete , item , versionHandler ) ;
}
}
2019-04-01 17:50:43 +02:00
2018-10-25 16:33:23 +02:00
public static String checkExistanceAndGetUniqueName ( Session ses , Node destination , String name ) throws BackendGenericError {
try {
destination . getNode ( name ) ;
} catch ( PathNotFoundException pnf ) {
return Text . escapeIllegalJcrChars ( name ) ;
} catch ( Exception e ) {
throw new BackendGenericError ( e ) ;
}
try {
String filename = FilenameUtils . getBaseName ( name ) ;
String ext = FilenameUtils . getExtension ( name ) ;
String nameTocheck = ext . isEmpty ( ) ? String . format ( " %s(*) " , filename ) : String . format ( " %s(*).%s " , filename , ext ) ;
2020-01-22 16:41:12 +01:00
logger . trace ( " filename is {}, extension is {} , and name to check is {} " , filename , ext , nameTocheck ) ;
2018-10-25 16:33:23 +02:00
NodeIterator ni = destination . getNodes ( nameTocheck ) ;
int maxval = 0 ;
while ( ni . hasNext ( ) ) {
Node n = ni . nextNode ( ) ;
int actual = Integer . parseInt ( n . getName ( ) . replaceAll ( String . format ( " %s \\ (( \\ d*) \\ ).* " , filename ) , " $1 " ) ) ;
if ( actual > maxval )
maxval = actual ;
}
2018-06-29 16:59:24 +02:00
2018-10-25 16:33:23 +02:00
String newName = ext . isEmpty ( ) ? String . format ( " %s(%d) " , filename , maxval + 1 ) : String . format ( " %s(%d).%s " , filename , maxval + 1 , ext ) ;
return Text . escapeIllegalJcrChars ( newName ) ;
} catch ( Exception e ) {
throw new BackendGenericError ( e ) ;
}
}
2019-04-01 17:50:43 +02:00
2021-03-15 16:01:14 +01:00
2018-11-30 17:49:35 +01:00
public static Node createFolderInternally ( Session ses , Node destinationNode , String name , String description , boolean hidden , String login , AccountingHandler accountingHandler ) throws BackendGenericError {
2020-05-22 17:31:40 +02:00
logger . debug ( " creating folder {} in {} " , name , destinationNode ) ;
2021-03-15 16:01:14 +01:00
2018-10-25 16:33:23 +02:00
String uniqueName = Utils . checkExistanceAndGetUniqueName ( ses , destinationNode , name ) ;
2019-04-01 17:50:43 +02:00
2018-10-25 16:33:23 +02:00
FolderItem item = new FolderItem ( ) ;
Calendar now = Calendar . getInstance ( ) ;
item . setName ( uniqueName ) ;
item . setTitle ( uniqueName ) ;
item . setDescription ( description ) ;
//item.setCreationTime(now);
2021-03-15 16:01:14 +01:00
2020-03-16 16:55:26 +01:00
boolean hiddenDestNode = false ;
try {
hiddenDestNode = destinationNode . getProperty ( NodeProperty . HIDDEN . toString ( ) ) . getBoolean ( ) ;
} catch ( Throwable e ) { }
2021-03-15 16:01:14 +01:00
2020-03-16 16:55:26 +01:00
item . setHidden ( hidden | | hiddenDestNode ) ;
2018-10-25 16:33:23 +02:00
item . setLastAction ( ItemAction . CREATED ) ;
item . setLastModificationTime ( now ) ;
item . setLastModifiedBy ( login ) ;
item . setOwner ( login ) ;
item . setPublicItem ( false ) ;
2019-04-01 17:50:43 +02:00
2019-02-14 12:01:59 +01:00
Node newNode = new Item2NodeConverter ( ) . getNode ( destinationNode , item ) ;
2020-01-22 16:41:12 +01:00
if ( accountingHandler ! = null ) {
accountingHandler . createFolderAddObj ( name , item . getClass ( ) . getSimpleName ( ) , null , ses , destinationNode , false ) ;
accountingHandler . createEntryCreate ( item . getTitle ( ) , ses , newNode , false ) ;
}
2018-10-25 16:33:23 +02:00
return newNode ;
}
2021-03-15 16:01:14 +01:00
2019-07-29 16:26:08 +02:00
public static Node createURLInternally ( Session ses , Node destinationNode , String name , URL value , String description , String login , AccountingHandler accountingHandler ) throws BackendGenericError {
String uniqueName = Utils . checkExistanceAndGetUniqueName ( ses , destinationNode , name ) ;
ExternalLink item = new ExternalLink ( ) ;
Calendar now = Calendar . getInstance ( ) ;
item . setName ( uniqueName ) ;
item . setTitle ( uniqueName ) ;
item . setDescription ( description ) ;
//item.setCreationTime(now);
item . setLastAction ( ItemAction . CREATED ) ;
item . setLastModificationTime ( now ) ;
item . setLastModifiedBy ( login ) ;
item . setOwner ( login ) ;
item . setPublicItem ( false ) ;
item . setValue ( value ) ;
2020-03-16 16:55:26 +01:00
try {
item . setHidden ( destinationNode . getProperty ( NodeProperty . HIDDEN . toString ( ) ) . getBoolean ( ) ) ;
} catch ( Throwable e ) {
item . setHidden ( false ) ;
}
2021-03-15 16:01:14 +01:00
2020-03-16 16:55:26 +01:00
2019-07-29 16:26:08 +02:00
Node newNode = new Item2NodeConverter ( ) . getNode ( destinationNode , item ) ;
2020-01-22 16:41:12 +01:00
if ( accountingHandler ! = null ) {
accountingHandler . createFolderAddObj ( name , item . getClass ( ) . getSimpleName ( ) , null , ses , destinationNode , false ) ;
accountingHandler . createEntryCreate ( item . getTitle ( ) , ses , newNode , false ) ;
}
2019-07-29 16:26:08 +02:00
return newNode ;
}
2019-04-01 17:50:43 +02:00
2018-10-25 16:33:23 +02:00
public static Node createGcubeItemInternally ( Session ses , Node destinationNode , String name , String description , String login , GCubeItem gcubeItem , AccountingHandler accountingHandler ) throws BackendGenericError {
2019-04-01 17:50:43 +02:00
2018-10-25 16:33:23 +02:00
Calendar now = Calendar . getInstance ( ) ;
gcubeItem . setName ( name ) ;
gcubeItem . setTitle ( name ) ;
gcubeItem . setDescription ( description ) ;
//item.setCreationTime(now);
gcubeItem . setHidden ( false ) ;
gcubeItem . setLastAction ( ItemAction . CREATED ) ;
gcubeItem . setLastModificationTime ( now ) ;
gcubeItem . setLastModifiedBy ( login ) ;
gcubeItem . setOwner ( login ) ;
//to inherit hidden property
//item.setHidden(destinationItem.isHidden());
2019-02-14 12:01:59 +01:00
Node newNode = new Item2NodeConverter ( ) . getNode ( destinationNode , gcubeItem ) ;
2018-10-25 16:33:23 +02:00
//TODO: accounting for GCUBEITEM
//accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), null, ses, newNode, false);
return newNode ;
}
2019-04-01 17:50:43 +02:00
2018-10-25 16:33:23 +02:00
public static void setPropertyOnChangeNode ( Node node , String login , ItemAction action ) throws RepositoryException {
node . setProperty ( NodeProperty . LAST_MODIFIED . toString ( ) , Calendar . getInstance ( ) ) ;
node . setProperty ( NodeProperty . LAST_MODIFIED_BY . toString ( ) , login ) ;
node . setProperty ( NodeProperty . LAST_ACTION . toString ( ) , action . name ( ) ) ;
2018-06-29 16:59:24 +02:00
}
2021-03-15 16:01:14 +01:00
2020-01-22 16:41:12 +01:00
public static synchronized VRE getVreFolderItem ( Session ses , Node2ItemConverter node2Item , VREManager vreManager , List < String > excludes ) throws RepositoryException , BackendGenericError {
org . gcube . common . storagehub . model . Path vrePath = Paths . append ( Utils . getWorkspacePath ( ) , Constants . VRE_FOLDER_PARENT_NAME ) ;
ScopeBean bean = new ScopeBean ( ScopeProvider . instance . get ( ) ) ;
if ( ! bean . is ( Type . VRE ) ) throw new BackendGenericError ( " the current scope is not a VRE " ) ;
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 ( ) ;
if ( ! it . hasNext ( ) ) throw new BackendGenericError ( " vre folder not found for context " + entireScopeName ) ;
Node folder = it . nextNode ( ) ;
Item vreFolder = node2Item . getItem ( folder , excludes ) ;
return vreManager . putVRE ( vreFolder ) ;
}
}
2021-03-15 16:01:14 +01:00
2020-01-22 16:41:12 +01:00
public static synchronized VRE getVreFolderItemByGroupNameAndUser ( Session ses , String goupName , String userId , Node2ItemConverter node2Item , VREManager vreManager , List < String > excludes ) throws RepositoryException , BackendGenericError {
org . gcube . common . storagehub . model . Path vrePath = Paths . append ( Utils . getWorkspacePath ( userId ) , Constants . VRE_FOLDER_PARENT_NAME ) ;
VRE vre = vreManager . getVRE ( goupName ) ;
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') " , goupName , vrePath . toPath ( ) ) ;
Query jcrQuery = ses . getWorkspace ( ) . getQueryManager ( ) . createQuery ( query , Constants . QUERY_LANGUAGE ) ;
NodeIterator it = jcrQuery . execute ( ) . getNodes ( ) ;
if ( ! it . hasNext ( ) ) throw new BackendGenericError ( " vre folder not found for context " + goupName ) ;
Node folder = it . nextNode ( ) ;
Item vreFolder = node2Item . getItem ( folder , excludes ) ;
return vreManager . putVRE ( vreFolder ) ;
}
}
2021-03-15 16:01:14 +01:00
2018-05-17 12:51:56 +02:00
}