Skip to content

Commit

Permalink
#28059 Improving duplicated variables handling. (#28421)
Browse files Browse the repository at this point in the history
* #28059 Improving duplicated variables handling.

* #28059 Refactor HostVariableAPIImpl to use ImmutableList

The HostVariableAPIImpl was refactored to make use of Google's Immutable List implementation for returned site variables in the getUniqueSiteVariables() method. We previously used an ArrayList, but have made the shift for its immutable properties and thread-safety.
  • Loading branch information
jgambarios committed May 3, 2024
1 parent 9195b2f commit 3f7d39b
Show file tree
Hide file tree
Showing 3 changed files with 1,775 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.hostvariable.model.HostVariable;
import com.google.common.collect.ImmutableList;
import com.liferay.portal.language.LanguageException;
import com.liferay.portal.model.User;
import java.util.ArrayList;
import java.util.Comparator;
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 +108,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 +161,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 List<HostVariable> getUniqueSiteVariables(List<HostVariable> siteVariables) {

return ImmutableList.copyOf(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 3f7d39b

Please sign in to comment.