- public void moveFolder(Long userId, Long folderId, Long destId, String destName) throws ObjectNotFoundException, DuplicateNameException, InsufficientPermissionsException, GSSIOException, QuotaExceededException {
- // TODO Simple Move and delete of original folder, in production
- // scenario we must first check individual files and folders permissions
- copyFolderStructure(userId, folderId, destId, destName);
- deleteFolder(userId, folderId);
+ public void moveFolder(Long userId, Long folderId, Long destId, String destName)
+ throws ObjectNotFoundException, InsufficientPermissionsException,
+ QuotaExceededException {
+ Folder source = dao.getEntityById(Folder.class, folderId);
+ Folder destination = dao.getEntityById(Folder.class, destId);
+ User user = dao.getEntityById(User.class, userId);
+ User sourceOwner = source.getOwner();
+ User destinationOwner = destination.getOwner();
+ // Do not move trashed folders and contents.
+ if (source.isDeleted())
+ return;
+ // Check permissions.
+ if (!destination.hasWritePermission(user)
+ || !source.hasReadPermission(user)
+ || !source.hasWritePermission(user))
+ throw new InsufficientPermissionsException("You don't have the " +
+ "necessary permissions");
+ // Use the same timestamp for all subsequent modifications to make
+ // changes appear simultaneous.
+ Date now = new Date();
+ // If source and destination are not in the same user's namespace,
+ // change owners and check quota.
+ if (!sourceOwner.equals(destinationOwner)) {
+ changeOwner(source, destinationOwner, user, now);
+ if (getQuotaLeft(destinationOwner.getId()) < 0)
+ throw new QuotaExceededException("Not enough free space " +
+ "available in destination folder");
+ }
+ // Perform the move.
+ Folder oldParent = source.getParent();
+ oldParent.removeSubfolder(source);
+ destination.addSubfolder(source);
+ // Mark the former parent and destination trees upwards as modified.
+ touchParentFolders(oldParent, user, now);
+ touchParentFolders(source, user, now);