Assert.notNull( fileId );
jcrTemplate.execute( new JcrCallback() {
@Override
public Object doInJcr( final Session session ) throws RepositoryException, IOException {
RepositoryFile file = getFileById( fileId );
RepositoryFileAcl acl = aclDao.getAcl( fileId );
if ( !accessVoterManager.hasAccess( file, RepositoryFilePermission.WRITE, acl, PentahoSessionHolder
.getSession() ) ) {
return null;
}
PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants( session );
String destAbsPath = pathConversionHelper.relToAbs( destRelPath );
String cleanDestAbsPath = destAbsPath;
if ( cleanDestAbsPath.endsWith( RepositoryFile.SEPARATOR ) ) {
cleanDestAbsPath.substring( 0, cleanDestAbsPath.length() - 1 );
}
Node srcFileNode = session.getNodeByIdentifier( fileId.toString() );
Serializable srcParentFolderId = JcrRepositoryFileUtils.getParentId( session, fileId );
boolean appendFileName = false;
boolean destExists = true;
Node destFileNode = null;
Node destParentFolderNode = null;
try {
destFileNode = (Node) session.getItem( JcrStringHelper.pathEncode( cleanDestAbsPath ) );
} catch ( PathNotFoundException e ) {
destExists = false;
}
if ( destExists ) {
// make sure it's a file or folder
Assert.isTrue( JcrRepositoryFileUtils.isSupportedNodeType( pentahoJcrConstants, destFileNode ) );
// existing item; make sure src is not a folder if dest is a file
Assert.isTrue(
!( JcrRepositoryFileUtils.isPentahoFolder( pentahoJcrConstants, srcFileNode ) && JcrRepositoryFileUtils
.isPentahoFile( pentahoJcrConstants, destFileNode ) ), Messages.getInstance().getString(
"JcrRepositoryFileDao.ERROR_0002_CANNOT_OVERWRITE_FILE_WITH_FOLDER" ) ); //$NON-NLS-1$
if ( JcrRepositoryFileUtils.isPentahoFolder( pentahoJcrConstants, destFileNode ) ) {
// existing item; caller is not renaming file, only moving it
appendFileName = true;
destParentFolderNode = destFileNode;
} else {
// get parent of existing dest item
int lastSlashIndex = cleanDestAbsPath.lastIndexOf( RepositoryFile.SEPARATOR );
Assert.isTrue( lastSlashIndex > 1, Messages.getInstance().getString(
"JcrRepositoryFileDao.ERROR_0003_ILLEGAL_DEST_PATH" ) ); //$NON-NLS-1$
String absPathToDestParentFolder = cleanDestAbsPath.substring( 0, lastSlashIndex );
destParentFolderNode = (Node) session.getItem( JcrStringHelper.pathEncode( absPathToDestParentFolder ) );
}
} else {
// destination doesn't exist; go up one level to a folder that does exist
int lastSlashIndex = cleanDestAbsPath.lastIndexOf( RepositoryFile.SEPARATOR );
Assert.isTrue( lastSlashIndex > 1, Messages.getInstance().getString(
"JcrRepositoryFileDao.ERROR_0003_ILLEGAL_DEST_PATH" ) ); //$NON-NLS-1$
String absPathToDestParentFolder = cleanDestAbsPath.substring( 0, lastSlashIndex );
// Not need to check the name if we encoded it
// JcrRepositoryFileUtils.checkName( cleanDestAbsPath.substring( lastSlashIndex + 1 ) );
try {
destParentFolderNode = (Node) session.getItem( JcrStringHelper.pathEncode( absPathToDestParentFolder ) );
} catch ( PathNotFoundException e1 ) {
Assert.isTrue( false, Messages.getInstance()
.getString( "JcrRepositoryFileDao.ERROR_0004_PARENT_MUST_EXIST" ) ); //$NON-NLS-1$
}
Assert.isTrue( JcrRepositoryFileUtils.isPentahoFolder( pentahoJcrConstants, destParentFolderNode ), Messages
.getInstance().getString( "JcrRepositoryFileDao.ERROR_0005_PARENT_MUST_BE_FOLDER" ) ); //$NON-NLS-1$
}
if ( !copy ) {
JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary( session, pentahoJcrConstants,
srcParentFolderId );
}
JcrRepositoryFileUtils.checkoutNearestVersionableNodeIfNecessary( session, pentahoJcrConstants,
destParentFolderNode );
String finalEncodedSrcAbsPath = srcFileNode.getPath();
String finalDestAbsPath =
appendFileName && !file.isFolder() ? cleanDestAbsPath + RepositoryFile.SEPARATOR + srcFileNode.getName()
: cleanDestAbsPath;
try {
if ( copy ) {
session.getWorkspace().copy( finalEncodedSrcAbsPath, JcrStringHelper.pathEncode( finalDestAbsPath ) );
} else {
session.getWorkspace().move( finalEncodedSrcAbsPath, JcrStringHelper.pathEncode( finalDestAbsPath ) );
}
} catch ( ItemExistsException iae ) {
throw new UnifiedRepositoryException( ( file.isFolder() ? "Folder " : "File " ) + "with path ["
+ cleanDestAbsPath + "] already exists in the repository" );
}
JcrRepositoryFileUtils.checkinNearestVersionableNodeIfNecessary( session, pentahoJcrConstants,
destParentFolderNode, versionMessage );