Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -312,129 +312,158 @@ public Mono<Boolean> updateExecutablesRunBehaviour(
Flux<Executable> creatorContextExecutablesFlux =
this.getAllExecutablesByCreatorIdFlux(creatorId, creatorType).cache();

// Before we update the actions, fetch all the actions which are currently set to execute on load.
Mono<List<Executable>> existingOnLoadExecutablesMono = creatorContextExecutablesFlux
.flatMap(executable -> {
if (RunBehaviourEnum.ON_PAGE_LOAD.equals(executable.getRunBehaviour())) {
return Mono.just(executable);
}
return Mono.empty();
})
.collectList();

return existingOnLoadExecutablesMono
.zipWith(creatorContextExecutablesFlux.collectList())
.flatMap(tuple -> featureFlagService
.check(FeatureFlagEnum.release_reactive_actions_enabled)
.flatMap(isReactiveActionsEnabled -> {
List<Executable> existingOnLoadExecutables = tuple.getT1();
List<Executable> creatorContextExecutables = tuple.getT2();

// There are no actions in this page. No need to proceed further since no actions would get
// updated
if (CollectionUtils.isNullOrEmpty(creatorContextExecutables)) {
return Mono.just(FALSE);
}

// No actions require an update if no actions have been found as page load actions as well
// as
// existing on load page actions are empty
if (CollectionUtils.isNullOrEmpty(existingOnLoadExecutables)
&& (onLoadExecutables == null
|| CollectionUtils.isNullOrEmpty(onLoadExecutables))) {
return Mono.just(FALSE);
}
// Check the feature flag first to determine which RunBehaviourEnum to use for comparison
return featureFlagService
.check(FeatureFlagEnum.release_reactive_actions_enabled)
.flatMap(isReactiveActionsEnabled -> {
RunBehaviourEnum runBehaviourToCheck =
isReactiveActionsEnabled ? RunBehaviourEnum.AUTOMATIC : RunBehaviourEnum.ON_PAGE_LOAD;

// Before we update the actions, fetch all the actions which are currently set to execute on load.
Mono<List<Executable>> existingOnLoadExecutablesMono = creatorContextExecutablesFlux
.flatMap(executable -> {
if (runBehaviourToCheck.equals(executable.getRunBehaviour())) {
return Mono.just(executable);
}
return Mono.empty();
})
.collectList();

// Extract names of existing page load actions and new page load actions for quick lookup.
Set<String> existingOnLoadExecutableNames = existingOnLoadExecutables.stream()
.map(Executable::getUserExecutableName)
.collect(Collectors.toSet());
return existingOnLoadExecutablesMono
.zipWith(creatorContextExecutablesFlux.collectList())
.flatMap(tuple -> {
List<Executable> existingOnLoadExecutables = tuple.getT1();
List<Executable> creatorContextExecutables = tuple.getT2();

// There are no actions in this page. No need to proceed further since no actions would
// get
// updated
if (CollectionUtils.isNullOrEmpty(creatorContextExecutables)) {
return Mono.just(FALSE);
}

Set<String> newOnLoadExecutableNames = onLoadExecutables.stream()
.map(Executable::getUserExecutableName)
.collect(Collectors.toSet());
// No actions require an update if no actions have been found as page load actions as
// well
// as
// existing on load page actions are empty
if (CollectionUtils.isNullOrEmpty(existingOnLoadExecutables)
&& (onLoadExecutables == null
|| CollectionUtils.isNullOrEmpty(onLoadExecutables))) {
return Mono.just(FALSE);
}

// Calculate the actions which would need to be updated from execute on load TRUE to FALSE.
Set<String> turnedOffExecutableNames = new HashSet<>();
turnedOffExecutableNames.addAll(existingOnLoadExecutableNames);
turnedOffExecutableNames.removeAll(newOnLoadExecutableNames);

// Calculate the actions which would need to be updated from execute on load FALSE to TRUE
Set<String> turnedOnExecutableNames = new HashSet<>();
turnedOnExecutableNames.addAll(newOnLoadExecutableNames);
turnedOnExecutableNames.removeAll(existingOnLoadExecutableNames);

for (Executable executable : creatorContextExecutables) {

String executableName = executable.getUserExecutableName();
// If a user has ever set execute on load, this field can not be changed automatically.
// It has
// to be explicitly changed by the user again. Add the executable to update only if this
// condition is false.
if (FALSE.equals(executable.getUserSetOnLoad())) {

// If this executable is no longer an onload executable, turn the execute on load to
// false
if (turnedOffExecutableNames.contains(executableName)) {
executable.setRunBehaviour(RunBehaviourEnum.MANUAL);
toUpdateExecutables.add(executable);
}
// Extract names of existing page load actions and new page load actions for quick
// lookup.
Set<String> existingOnLoadExecutableNames = existingOnLoadExecutables.stream()
.map(Executable::getUserExecutableName)
.collect(Collectors.toSet());

Set<String> newOnLoadExecutableNames = onLoadExecutables.stream()
.map(Executable::getUserExecutableName)
.collect(Collectors.toSet());

// Calculate the actions which would need to be updated from execute on load TRUE to
// FALSE.
Set<String> turnedOffExecutableNames = new HashSet<>();
turnedOffExecutableNames.addAll(existingOnLoadExecutableNames);
turnedOffExecutableNames.removeAll(newOnLoadExecutableNames);

// Calculate the actions which would need to be updated from execute on load FALSE to
// TRUE
Set<String> turnedOnExecutableNames = new HashSet<>();
turnedOnExecutableNames.addAll(newOnLoadExecutableNames);
turnedOnExecutableNames.removeAll(existingOnLoadExecutableNames);

for (Executable executable : creatorContextExecutables) {

String executableName = executable.getUserExecutableName();
// If a user has ever set execute on load, this field can not be changed
// automatically.
// It has
// to be explicitly changed by the user again. Add the executable to update only if
// this
// condition is false.
if (FALSE.equals(executable.getUserSetOnLoad())) {

// If this executable is no longer an onload executable, turn the execute on
// load to
// false
if (turnedOffExecutableNames.contains(executableName)) {
executable.setRunBehaviour(RunBehaviourEnum.MANUAL);
toUpdateExecutables.add(executable);
}

// If this executable is newly found to be on load, turn execute on load to true
if (turnedOnExecutableNames.contains(executableName)) {
// Use the prefetched feature flag value
if (isReactiveActionsEnabled) {
executable.setRunBehaviour(RunBehaviourEnum.AUTOMATIC);
} else {
executable.setRunBehaviour(RunBehaviourEnum.ON_PAGE_LOAD);
// If this executable is newly found to be on load, turn execute on load to true
if (turnedOnExecutableNames.contains(executableName)) {
// Use the prefetched feature flag value
if (isReactiveActionsEnabled) {
executable.setRunBehaviour(RunBehaviourEnum.AUTOMATIC);
} else {
executable.setRunBehaviour(RunBehaviourEnum.ON_PAGE_LOAD);
}
toUpdateExecutables.add(executable);
}
toUpdateExecutables.add(executable);
}

} else {
// Remove the executable name from either of the lists (if present) because this
// executable
// should not be updated
turnedOnExecutableNames.remove(executableName);
turnedOffExecutableNames.remove(executableName);
} else {
// Remove the executable name from either of the lists (if present) because this
// executable
// should not be updated
turnedOnExecutableNames.remove(executableName);
turnedOffExecutableNames.remove(executableName);
}
}
}

// Add newly turned on page actions to report back to the caller
executableUpdatesRef.addAll(addExecutableUpdatesForExecutableNames(
creatorContextExecutables, turnedOnExecutableNames));
// Add newly turned on page actions to report back to the caller
executableUpdatesRef.addAll(addExecutableUpdatesForExecutableNames(
creatorContextExecutables, turnedOnExecutableNames));

// Add newly turned off page actions to report back to the caller
executableUpdatesRef.addAll(addExecutableUpdatesForExecutableNames(
creatorContextExecutables, turnedOffExecutableNames));
// Add newly turned off page actions to report back to the caller
executableUpdatesRef.addAll(addExecutableUpdatesForExecutableNames(
creatorContextExecutables, turnedOffExecutableNames));

for (Executable executable : creatorContextExecutables) {
String executableName = executable.getUserExecutableName();
if (Boolean.FALSE.equals(executable.isOnLoadMessageAllowed())) {
turnedOffExecutableNames.remove(executableName);
turnedOnExecutableNames.remove(executableName);
for (Executable executable : creatorContextExecutables) {
String executableName = executable.getUserExecutableName();
if (Boolean.FALSE.equals(executable.isOnLoadMessageAllowed())) {
turnedOffExecutableNames.remove(executableName);
turnedOnExecutableNames.remove(executableName);
}
}
}

// Now add messagesRef that would eventually be displayed to the developer user informing
// them
// about the action setting change.
if (!turnedOffExecutableNames.isEmpty()) {
messagesRef.add(turnedOffExecutableNames.toString()
+ " will no longer be executed on page load");
}
// Now add messagesRef that would eventually be displayed to the developer user
// informing
// them
// about the action setting change.
if (isReactiveActionsEnabled) {
if (!turnedOffExecutableNames.isEmpty()) {
messagesRef.add(
turnedOffExecutableNames.toString()
+ " will no longer run automatically. You can run it manually when needed.");
}

if (!turnedOnExecutableNames.isEmpty()) {
messagesRef.add(turnedOnExecutableNames.toString()
+ " will be executed automatically on page load");
}
if (!turnedOnExecutableNames.isEmpty()) {
messagesRef.add(
turnedOnExecutableNames.toString()
+ " will run automatically on page load or when a variable it depends on changes");
}
} else {
if (!turnedOffExecutableNames.isEmpty()) {
messagesRef.add(turnedOffExecutableNames.toString()
+ " will no longer be executed on page load");
}

if (!turnedOnExecutableNames.isEmpty()) {
messagesRef.add(turnedOnExecutableNames.toString()
+ " will be executed automatically on page load");
}
}

// Finally update the actions which require an update
return Flux.fromIterable(toUpdateExecutables)
.flatMap(executable -> this.updateUnpublishedExecutable(
executable.getId(), executable, creatorType))
.then(Mono.just(TRUE));
}));
// Finally update the actions which require an update
return Flux.fromIterable(toUpdateExecutables)
.flatMap(executable -> this.updateUnpublishedExecutable(
executable.getId(), executable, creatorType))
.then(Mono.just(TRUE));
});
});
}

@Override
Expand Down
Loading
Loading