@Override
public void start() {
try {
H2HSession session = networkManager.getSession();
String randomPID = UUID.randomUUID().toString();
UserProfileManager profileManager = session.getProfileManager();
UserProfile userProfile = profileManager.getUserProfile(randomPID, true);
// get and check the file nodes to be rearranged
FolderIndex oldParent = (FolderIndex) userProfile.getFileById(oldParentKey);
if (oldParent == null) {
logger.error("Could not find the old parent.");
return;
} else if (!oldParent.canWrite(sender)) {
logger.error("User was not allowed to change the source directory.");
return;
}
Index child = oldParent.getChildByName(sourceFileName);
if (child == null) {
logger.error("File node that should be moved not found.");
return;
}
FolderIndex newParent = (FolderIndex) userProfile.getFileById(newParentKey);
if (newParent == null) {
logger.error("Could not find the new parent.");
return;
} else if (!newParent.canWrite(sender)) {
logger.error("User was not allowed to change the destination directory.");
return;
}
// rearrange
oldParent.removeChild(child);
newParent.addChild(child);
child.setParent(newParent);
// change the child's name
child.setName(destFileName);
profileManager.readyToPut(userProfile, randomPID);
// move the file on disk
FileUtil.moveFile(session.getRoot(), sourceFileName, destFileName, oldParent, newParent);
} catch (Hive2HiveException | IOException e) {
logger.error("Could not process the user profile task.", e);