Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isRoot boolean should be true in notifications if root directory is updated #161

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 70 additions & 62 deletions src/main/java/org/gridsuite/directory/server/DirectoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public Consumer<Message<String>> consumeStudyUpdate() {
if (error != null && elementName != null) {
deleteElement(studyUuid, userId);
}
notificationService.emitDirectoryChanged(parentUuid, elementName, userId, error, parentUuid == null, NotificationType.UPDATE_DIRECTORY);
// At study creation, if the corresponding element doesn't exist here yet and doesn't have parent
// then avoid sending a notification with parentUuid=null and isRoot=true
if (parentUuid != null) {
notifyDirectoryHasChanged(parentUuid, userId, elementName, error);
}
}
} catch (Exception e) {
LOGGER.error(e.toString(), e);
Expand All @@ -109,14 +113,8 @@ public ElementAttributes createElement(ElementAttributes elementAttributes, UUID
assertDirectoryExist(parentDirectoryUuid);
DirectoryElementEntity elementEntity = insertElement(elementAttributes, parentDirectoryUuid);

notificationService.emitDirectoryChanged(
parentDirectoryUuid,
elementAttributes.getElementName(),
userId,
null,
false,
NotificationType.UPDATE_DIRECTORY
);
// Here we know that parentDirectoryUuid can't be null
notifyDirectoryHasChanged(parentDirectoryUuid, userId, elementAttributes.getElementName());

return toElementAttributes(elementEntity);
}
Expand All @@ -138,14 +136,9 @@ public ElementAttributes duplicateElement(UUID elementId, UUID newElementId, UUI
assertDirectoryExist(parentDirectoryUuid);
DirectoryElementEntity elementEntity = insertElement(elementAttributes, parentDirectoryUuid);

notificationService.emitDirectoryChanged(
parentDirectoryUuid,
elementAttributes.getElementName(),
userId,
null,
false,
NotificationType.UPDATE_DIRECTORY
);
// Here we know that parentDirectoryUuid can't be null
notifyDirectoryHasChanged(parentDirectoryUuid, userId, elementAttributes.getElementName());

return toElementAttributes(elementEntity);
}

Expand Down Expand Up @@ -193,6 +186,7 @@ public ElementAttributes createRootDirectory(RootDirectoryAttributes rootDirecto
assertRootDirectoryNotExist(rootDirectoryAttributes.getElementName());
ElementAttributes elementAttributes = toElementAttributes(insertElement(toElementAttributes(rootDirectoryAttributes), null));

// here we know a root directory has no parent
notificationService.emitDirectoryChanged(
elementAttributes.getElementUuid(),
elementAttributes.getElementName(),
Expand Down Expand Up @@ -297,14 +291,7 @@ public void updateElement(UUID elementUuid, ElementAttributes newElementAttribut

DirectoryElementEntity elementEntity = repositoryService.saveElement(directoryElement.update(newElementAttributes));

notificationService.emitDirectoryChanged(
elementEntity.getParentId() == null ? elementUuid : elementEntity.getParentId(),
elementEntity.getName(),
userId,
null,
elementEntity.getParentId() == null,
NotificationType.UPDATE_DIRECTORY
);
notifyDirectoryHasChanged(elementEntity.getParentId() == null ? elementUuid : elementEntity.getParentId(), userId, elementEntity.getName());
}

@Transactional
Expand Down Expand Up @@ -332,7 +319,8 @@ private void moveElementDirectory(UUID elementUuid, UUID newDirectoryUuid, Strin

DirectoryElementEntity oldDirectory = repositoryService.getElementEntity(element.getParentId()).orElseThrow();
updateElementParentDirectory(element, newDirectoryUuid);
emitDirectoryChangedNotifications(element, oldDirectory, userId);
notifyDirectoryHasChanged(element.getParentId() == null ? element.getId() : element.getParentId(), userId, element.getName());
notifyDirectoryHasChanged(oldDirectory.getId(), userId, element.getName());

}

Expand All @@ -351,11 +339,6 @@ private void updateElementParentDirectory(DirectoryElementEntity element, UUID n
repositoryService.saveElement(element);
}

private void emitDirectoryChangedNotifications(DirectoryElementEntity element, DirectoryElementEntity oldDirectory, String userId) {
notificationService.emitDirectoryChanged(element.getParentId(), element.getName(), userId, null, repositoryService.isRootDirectory(element.getId()), NotificationType.UPDATE_DIRECTORY);
notificationService.emitDirectoryChanged(oldDirectory.getId(), element.getName(), userId, null, repositoryService.isRootDirectory(element.getId()), NotificationType.UPDATE_DIRECTORY);
}

private void validateNewDirectory(UUID newDirectoryUuid) {
DirectoryElementEntity newDirectory = repositoryService.getElementEntity(newDirectoryUuid)
.orElseThrow(() -> DirectoryException.createElementNotFound(DIRECTORY, newDirectoryUuid));
Expand Down Expand Up @@ -394,15 +377,13 @@ public void deleteElement(UUID elementUuid, String userId) {
}
UUID parentUuid = repositoryService.getParentUuid(elementUuid);
deleteElement(elementAttributes, userId);

notificationService.emitDirectoryChanged(
parentUuid == null ? elementUuid : parentUuid,
elementAttributes.getElementName(),
userId,
null,
parentUuid == null,
parentUuid == null ? NotificationType.DELETE_DIRECTORY : NotificationType.UPDATE_DIRECTORY
);
if (parentUuid == null) {
// We can't notify to update the parent directory of a deleted root directory
// Then we send a specific notification
notifyRootDirectoryDeleted(elementUuid, userId, elementAttributes.getElementName());
} else {
notifyDirectoryHasChanged(parentUuid, userId, elementAttributes.getElementName());
}
}

private void deleteElement(ElementAttributes elementAttributes, String userId) {
Expand Down Expand Up @@ -446,14 +427,7 @@ public void deleteElements(List<UUID> elementsUuids, UUID parentDirectoryUuid, S
.forEach(elementUuid -> notificationService.emitDeletedElement(elementUuid, userId));

// sending directory update notification
notificationService.emitDirectoryChanged(
parentDirectoryUuid,
null,
userId,
null,
false,
NotificationType.UPDATE_DIRECTORY
);
notifyDirectoryHasChanged(parentDirectoryUuid, userId);
}

/***
Expand Down Expand Up @@ -518,25 +492,15 @@ public void notify(@NonNull String notificationName, @NonNull UUID elementUuid,
}

if (notification == NotificationType.UPDATE_DIRECTORY) {
emitDirectoryChangedNotification(elementUuid, userId);
ElementAttributes elementAttributes = getElement(elementUuid);
UUID parentUuid = repositoryService.getParentUuid(elementUuid);

notifyDirectoryHasChanged(parentUuid != null ? parentUuid : elementUuid, userId, elementAttributes.getElementName());
} else {
throw DirectoryException.createNotificationUnknown(notification.name());
}
}

public void emitDirectoryChangedNotification(UUID elementUuid, String userId) {
ElementAttributes elementAttributes = getElement(elementUuid);
UUID parentUuid = repositoryService.getParentUuid(elementUuid);
notificationService.emitDirectoryChanged(
parentUuid,
elementAttributes.getElementName(),
userId,
null,
parentUuid == null,
NotificationType.UPDATE_DIRECTORY
);
}

public boolean areDirectoryElementsAccessible(@NonNull List<UUID> elementUuids, @NonNull String userId) {
// TODO : check in the gateway should be modified. Now all directories are public. See with Slimane
/* getElements(elementUuids, true, List.of()).stream()
Expand Down Expand Up @@ -590,4 +554,48 @@ public UUID getDirectoryUuidFromPath(List<String> directoryPath) {
}
return parentDirectoryUuid;
}

// Everytime we make a change inside a directory, we must send a notification
// to tell the directory has changed, please update its metadata and content data (UPDATE_DIRECTORY)
// Note: for a root directory without parent directory, if one of its metadata changes (name,...)
// then we send a notification on this directory
private void notifyDirectoryHasChanged(UUID directoryUuid, String userId) {
Objects.requireNonNull(directoryUuid);
notifyDirectoryHasChanged(directoryUuid, userId, null, null);
}

private void notifyDirectoryHasChanged(UUID directoryUuid, String userId, String elementName) {
Objects.requireNonNull(directoryUuid);
notifyDirectoryHasChanged(directoryUuid, userId, elementName, null);
}

private void notifyDirectoryHasChanged(UUID directoryUuid, String userId, String elementName, String error) {
Objects.requireNonNull(directoryUuid);
notificationService.emitDirectoryChanged(
directoryUuid,
elementName,
userId,
error,
repositoryService.isRootDirectory(directoryUuid),
NotificationType.UPDATE_DIRECTORY
);
}

// Root directories don't have parent directories. Then if on is deleted, we must send a specific notification
private void notifyRootDirectoryDeleted(UUID rootDirectoryUuid, String userId, String elementName) {
Objects.requireNonNull(rootDirectoryUuid);
notifyRootDirectoryDeleted(rootDirectoryUuid, userId, elementName, null);
}

private void notifyRootDirectoryDeleted(UUID rootDirectoryUuid, String userId, String elementName, String error) {
Objects.requireNonNull(rootDirectoryUuid);
notificationService.emitDirectoryChanged(
rootDirectoryUuid,
elementName,
userId,
error,
true,
NotificationType.DELETE_DIRECTORY
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void testDeleteMultipleElementsFromOneDirectory() {
verify(notificationService, times(1)).emitDeletedElement(element2.getId(), "user1");
verify(notificationService, times(1)).emitDeletedElement(element0.getId(), "user1");
// notification for updated directory
verify(notificationService, times(1)).emitDirectoryChanged(parentDirectoryUuid, null, "user1", null, false, NotificationType.UPDATE_DIRECTORY);
verify(notificationService, times(1)).emitDirectoryChanged(parentDirectoryUuid, null, "user1", null, true, NotificationType.UPDATE_DIRECTORY);

verifyNoMoreInteractions(notificationService);
}
Expand Down
Loading
Loading