Skip to content

Commit

Permalink
#28059 Improving duplicated variables handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgambarios committed May 2, 2024
1 parent cbce59a commit 68d8af3
Show file tree
Hide file tree
Showing 3 changed files with 1,774 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class HostVariableAPIImpl implements HostVariableAPI {

Expand Down Expand Up @@ -106,15 +107,17 @@ public List<HostVariable> save(final List<HostVariable> siteVariables, final Str
);
}

List<HostVariable> processedVariables = new ArrayList<>();
// Removing duplicates from the given list of variables
final var uniqueSiteVariables = getUniqueSiteVariables(siteVariables);

// First we need to get all the existing variables for the host
// Getting all the existing variables for the host
final List<HostVariable> existingVariables = getVariablesForHost(
siteId, user, respectFrontendRoles
);

// Now we need to loop through the new variables save/update them
for (HostVariable siteVariable : siteVariables) {
List<HostVariable> processedVariables = new ArrayList<>();
for (HostVariable siteVariable : uniqueSiteVariables) {

final HostVariable toProcess;

Expand Down Expand Up @@ -157,6 +160,23 @@ public List<HostVariable> save(final List<HostVariable> siteVariables, final Str
return processedVariables;
}

/**
* Retrieves a list of unique site variables from the given list of site variables.
*
* @param siteVariables The list of site variables.
* @return A new ArrayList containing only the unique site variables based on their keys.
*/
private ArrayList<HostVariable> getUniqueSiteVariables(List<HostVariable> siteVariables) {

return new ArrayList<>(siteVariables.stream().
collect(Collectors.toMap(
HostVariable::getKey, // key is the HostVariable's `key`
hostVariable -> hostVariable, // value is the HostVariable itself
(existing, replacement) -> existing)) // if there's a conflict, keep the existing
.values()
);
}

/**
* Checks if two HostVariables are equal based on their key, value, name, and id attributes.
*
Expand Down

0 comments on commit 68d8af3

Please sign in to comment.