+ // if the destination folder belongs to another user:
+ if (!file.getOwner().equals(destination.getOwner())) {
+ // (a) check if the destination quota allows the move
+ if(getQuotaLeft(destination.getOwner().getId()) < file.getTotalSize())
+ throw new QuotaExceededException("Not enough free space available");
+ User newOwner = destination.getOwner();
+ // (b) if quota OK, change the owner of the file
+ file.setOwner(newOwner);
+ // if the file has no permission for the new owner, add it
+ Permission ownerPermission = null;
+ for (final Permission p : file.getPermissions())
+ if (p.getUser() != null)
+ if (p.getUser().equals(newOwner)) {
+ ownerPermission = p;
+ break;
+ }
+ if (ownerPermission == null) {
+ ownerPermission = new Permission();
+ ownerPermission.setUser(newOwner);
+ file.addPermission(ownerPermission);
+ }
+ ownerPermission.setRead(true);
+ ownerPermission.setWrite(true);
+ ownerPermission.setModifyACL(true);
+ }
+ // move the file to the destination folder
+ file.setFolder(destination);