try {
// get the user profile first
String randomPID = UUID.randomUUID().toString();
UserProfileManager profileManager = networkManager.getSession().getProfileManager();
UserProfile userProfile = profileManager.getUserProfile(randomPID, true);
FolderIndex parentNode = (FolderIndex) userProfile.getFileById(parentKey);
if (parentNode == null) {
logger.error("Could not process the task because the parent node has not been found.");
return;
}
// validate if the other sharer has the right to share
if (parentNode.canWrite(sender)) {
logger.debug("Rights of user '{}' checked. User is allowed to modify.", sender);
} else {
logger.error("Permission of user '{}' not found. Deny to apply this user's changes.", sender);
return;
}
// this task is sent when the file has been added or updated, make the difference between them.
// When it's been added, add the index to the user profile, else, simply upldate it's md5 hash
// there.
if (parentNode.getChildByName(index.getName()) == null) {
logger.debug("Newly shared file '{}' received.", index.getName());
// file is new, link parent and new child
parentNode.addChild(index);
index.setParent(parentNode);
} else {
// copy the md5 parameter of the received file
Index existing = parentNode.getChildByName(index.getName());
if (existing.isFile() && index.isFile()) {
logger.debug("File update in a shared folder received: '{}'.", index.getName());
FileIndex existingFile = (FileIndex) existing;
FileIndex newFile = (FileIndex) index;
existingFile.setMD5(newFile.getMD5());