if ( mode == null ) {
mode = MODE_RENAME;
}
String path = idToPath( pathId );
RepositoryFile destDir = getRepository().getFile( path );
String[] sourceFileIds = params.split( "[,]" ); //$NON-NLS-1$
if ( mode == MODE_OVERWRITE || mode == MODE_NO_OVERWRITE ) {
for ( String sourceFileId : sourceFileIds ) {
RepositoryFile sourceFile = getRepository().getFileById( sourceFileId );
if ( destDir != null && destDir.isFolder() && sourceFile != null && !sourceFile.isFolder() ) {
String fileName = sourceFile.getName();
String
sourcePath =
sourceFile.getPath().substring( 0, sourceFile.getPath().lastIndexOf( FileUtils.PATH_SEPARATOR ) );
if ( !sourcePath.equals( destDir.getPath() ) ) { // We're saving to a different folder than we're copying
// from
IRepositoryFileData data = getData( sourceFile );
RepositoryFileAcl acl = getRepository().getAcl( sourceFileId );
RepositoryFile
destFile =
getRepository().getFile( destDir.getPath() + FileUtils.PATH_SEPARATOR + fileName );
if ( destFile == null ) { // destFile doesn't exist so we'll create it.
RepositoryFile duplicateFile =
new RepositoryFile.Builder( fileName ).hidden( sourceFile.isHidden() ).versioned(
sourceFile.isVersioned() ).build();
final RepositoryFile repositoryFile =
getRepository().createFile( destDir.getId(), duplicateFile, data, acl, null );
getRepository()
.setFileMetadata( repositoryFile.getId(), getRepository().getFileMetadata( sourceFileId ) );
} else if ( mode == MODE_OVERWRITE ) { // destFile exists so check to see if we want to overwrite it.
RepositoryFileDto destFileDto = toFileDto( destFile, null, false );
destFileDto.setHidden( sourceFile.isHidden() );
destFile = toFile( destFileDto );
final RepositoryFile repositoryFile = getRepository().updateFile( destFile, data, null );
getRepository().updateAcl( acl );
getRepository()
.setFileMetadata( repositoryFile.getId(), getRepository().getFileMetadata( sourceFileId ) );
}
}
}
}
} else {
for ( String sourceFileId : sourceFileIds ) {
RepositoryFile sourceFile = getRepository().getFileById( sourceFileId );
if ( destDir != null && destDir.isFolder() && sourceFile != null && !sourceFile.isFolder() ) {
// First try to see if regular name is available
String fileName = sourceFile.getName();
String copyText = "";
String rootCopyText = "";
String nameNoExtension = fileName;
String extension = "";
int indexOfDot = fileName.lastIndexOf( '.' );
if ( !( indexOfDot == -1 ) ) {
nameNoExtension = fileName.substring( 0, indexOfDot );
extension = fileName.substring( indexOfDot );
}
RepositoryFileDto
testFile =
getRepoWs().getFile( path + FileUtils.PATH_SEPARATOR + nameNoExtension + extension ); //$NON-NLS-1$
if ( testFile != null ) {
// Second try COPY_PREFIX, If the name already ends with a COPY_PREFIX don't append twice
if ( !nameNoExtension
.endsWith( Messages.getInstance().getString( "FileResource.COPY_PREFIX" ) ) ) { //$NON-NLS-1$
copyText = rootCopyText = Messages.getInstance().getString( "FileResource.COPY_PREFIX" );
fileName = nameNoExtension + copyText + extension;
testFile = getRepoWs().getFile( path + FileUtils.PATH_SEPARATOR + fileName );
}
}
// Third try COPY_PREFIX + DUPLICATE_INDICATOR
Integer nameCount = 1;
while ( testFile != null ) {
nameCount++;
copyText =
rootCopyText + Messages.getInstance().getString( "FileResource.DUPLICATE_INDICATOR", nameCount );
fileName = nameNoExtension + copyText + extension;
testFile = getRepoWs().getFile( path + FileUtils.PATH_SEPARATOR + fileName );
}
IRepositoryFileData data = getData( sourceFile );
RepositoryFileAcl acl = getRepository().getAcl( sourceFileId );
RepositoryFile duplicateFile = null;
// If the title is different than the source file, copy it separately
if ( !sourceFile.getName().equals( sourceFile.getTitle() ) ) {
duplicateFile =
new RepositoryFile.Builder( fileName ).title( RepositoryFile.DEFAULT_LOCALE,
sourceFile.getTitle() + copyText ).hidden( sourceFile.isHidden() ).versioned(
sourceFile.isVersioned() ).build();
} else {
duplicateFile = new RepositoryFile.Builder( fileName ).hidden( sourceFile.isHidden() ).build();
}
final RepositoryFile repositoryFile =
getRepository().createFile( destDir.getId(), duplicateFile, data, acl, null );
getRepository().setFileMetadata( repositoryFile.getId(), getRepository().getFileMetadata( sourceFileId ) );
}
}
}
}