String hostName = getHostname(fromPath);
String toParentPath = getFolderName(getPath(toPath));
Host host;
Folder toParentFolder;
try {
host = hostAPI.findByName(hostName, user, false);
toParentFolder = folderAPI.findFolderByPath(toParentPath,host,user,false);
} catch (DotDataException e) {
Logger.error(DotWebdavHelper.class, e.getMessage(), e);
throw new IOException(e.getMessage());
} catch (DotSecurityException e) {
Logger.error(DotWebdavHelper.class, e.getMessage(), e);
throw new IOException(e.getMessage());
}
if (isResource(fromPath,user)) {
try {
if (!perAPI.doesUserHavePermission(toParentFolder,
PermissionAPI.PERMISSION_READ, user, false)) {
throw new IOException("User doesn't have permissions to move file to folder");
}
} catch (DotDataException e1) {
Logger.error(DotWebdavHelper.class,e1.getMessage(),e1);
throw new IOException(e1.getMessage());
}
if (toParentFolder == null || !InodeUtils.isSet(toParentFolder.getInode())) {
throw new IOException("Cannot move a file to the root of the host.");
}
try{
Identifier identifier = APILocator.getIdentifierAPI().find(host, getPath(fromPath));
Identifier identTo = APILocator.getIdentifierAPI().find(host, getPath(toPath));
boolean destinationExists=identTo!=null && InodeUtils.isSet(identTo.getId());
if(identifier!=null && identifier.getAssetType().equals("contentlet")){
Contentlet fileAssetCont = APILocator.getContentletAPI().findContentletByIdentifier(identifier.getId(), false, APILocator.getLanguageAPI().getDefaultLanguage().getId(), user, false);
if(!destinationExists) {
if (getFolderName(fromPath).equals(getFolderName(toPath))) {
String fileName = getFileName(toPath);
if(fileName.contains(".")){
fileName = fileName.substring(0, fileName.lastIndexOf("."));
}
APILocator.getFileAssetAPI().renameFile(fileAssetCont, fileName, user, false);
} else {
APILocator.getFileAssetAPI().moveFile(fileAssetCont, toParentFolder, user, false);
}
}
else {
// if the destination exists lets just create a new version and delete the original file
Contentlet origin = APILocator.getContentletAPI().findContentletByIdentifier(identifier.getId(), false, APILocator.getLanguageAPI().getDefaultLanguage().getId(), user, false);
Contentlet toContentlet = APILocator.getContentletAPI().findContentletByIdentifier(identTo.getId(), false, APILocator.getLanguageAPI().getDefaultLanguage().getId(), user, false);
Contentlet newversion = APILocator.getContentletAPI().checkout(toContentlet.getInode(), user, false);
// get a copy in a tmp folder to avoid filename change
java.io.File tmpDir=new java.io.File(APILocator.getFileAPI().getRealAssetPathTmpBinary()
+java.io.File.separator+UUIDGenerator.generateUuid());
java.io.File tmp=new java.io.File(tmpDir, toContentlet.getBinary(FileAssetAPI.BINARY_FIELD).getName());
FileUtil.copyFile(origin.getBinary(FileAssetAPI.BINARY_FIELD), tmp);
newversion.setBinary(FileAssetAPI.BINARY_FIELD, tmp);
newversion = APILocator.getContentletAPI().checkin(newversion, user, false);
if(autoPublish) {
APILocator.getContentletAPI().publish(newversion, user, false);
}
APILocator.getContentletAPI().unlock(newversion, user, false);
APILocator.getContentletAPI().delete(origin, APILocator.getUserAPI().getSystemUser(), false);
while(APILocator.getContentletAPI().isInodeIndexed(origin.getInode(),1));
}
}else{
File f = fileAPI.getFileByURI(getPath(fromPath), host, false, user, false);
if (getFolderName(fromPath).equals(getFolderName(toPath))) {
String fileName = getFileName(toPath);
if(fileName.contains(".")){
fileName = fileName.substring(0, fileName.lastIndexOf("."));
}
fileAPI.renameFile(f, fileName, user, false);
} else {
fileAPI.moveFile(f, toParentFolder, user, false);
}
if (autoPublish && perAPI.doesUserHavePermission(f, PermissionAPI.PERMISSION_PUBLISH, user)) {
PublishFactory.publishAsset(f, user, false);
}
APILocator.getFileAPI().invalidateCache(f);
CacheLocator.getIdentifierCache().removeFromCacheByVersionable(f);
LiveCache.removeAssetFromCache(f);
WorkingCache.removeAssetFromCache(f);
}
}catch (Exception e) {
throw new DotDataException(e.getMessage(),e);
}
} else {
if (UtilMethods.isSet(toParentPath) && !toParentPath.equals("/")) {
try {
if (!perAPI.doesUserHavePermission(toParentFolder, PermissionAPI.PERMISSION_READ, user, false)) {
throw new IOException("User doesn't have permissions to move file to folder");
}
} catch (DotDataException e1) {
Logger.error(DotWebdavHelper.class,e1.getMessage(),e1);
throw new IOException(e1.getMessage());
}
if (getFolderName(fromPath).equals(getFolderName(toPath))) {
Logger.debug(this, "Calling Folderfactory to rename " + fromPath + " to " + toPath);
try{
Folder folder = folderAPI.findFolderByPath(getPath(toPath), host,user,false);
removeObject(toPath, user);
fc.removeFolder(folder,idapi.find(folder));
}catch (Exception e) {
Logger.debug(this, "Unable to delete toPath " + toPath);
}
boolean renamed = false;
try{
Folder folder = folderAPI.findFolderByPath(getPath(fromPath), host,user,false);
renamed = folderAPI.renameFolder(folder, getFileName(toPath),user,false);
fc.removeFolder(folder,idapi.find(folder));
//folderAPI.updateMovedFolderAssets(folder);
}catch (Exception e) {
throw new DotDataException(e.getMessage(), e);
}
if(!renamed){
Logger.error(this, "Unable to remame folder");
throw new IOException("Unable to rename folder");
}
} else {
Logger.debug(this, "Calling folder factory to move from " + fromPath + " to " + toParentPath);
Folder fromFolder;
try {
fromFolder = folderAPI.findFolderByPath(getPath(fromPath), host,user,false);
} catch (Exception e1) {
Logger.error(DotWebdavHelper.class, e1.getMessage(), e1);
throw new DotRuntimeException(e1.getMessage(), e1);
}
if(fromFolder != null){
Logger.debug(this, "Calling folder factory to move from " + idapi.find(fromFolder).getPath() + " to " + toParentPath);
Logger.debug(this, "the from folder inode is " + fromFolder.getInode());
}else{
Logger.debug(this, "The from folder is null");
}
try {
folderAPI.move(fromFolder, toParentFolder,user,false);
fc.removeFolder(fromFolder,idapi.find(fromFolder));
fc.removeFolder(toParentFolder,idapi.find(toParentFolder));
//folderAPI.updateMovedFolderAssets(fromFolder);
} catch (Exception e) {
Logger.error(DotWebdavHelper.class, e.getMessage(), e);
throw new DotDataException(e.getMessage(), e);
}
}
} else {
try {
if (!perAPI.doesUserHavePermission(host,PermissionAPI.PERMISSION_READ, user, false)) {
throw new IOException("User doesn't have permissions to move file to host");
}
} catch (DotDataException e) {
Logger.error(DotWebdavHelper.class,e.getMessage(),e);
throw new IOException(e.getMessage());
}
if (getFolderName(fromPath).equals(getFolderName(toPath))) {
try{
Folder fromfolder = folderAPI.findFolderByPath(getPath(fromPath), host,user,false);
folderAPI.renameFolder(fromfolder, getFileName(toPath),user,false);
fc.removeFolder(fromfolder,idapi.find(fromfolder));
}catch (Exception e) {
throw new DotDataException(e.getMessage(), e);
}
} else {
Folder fromFolder;
try {
fromFolder = folderAPI.findFolderByPath(getPath(fromPath), host,user,false);
folderAPI.move(fromFolder, host,user,false);
fc.removeFolder(fromFolder,idapi.find(fromFolder));
//folderAPI.updateMovedFolderAssets(fromFolder);