Skip to content

Commit

Permalink
#28362 : changed HostSearchOptions class to Search Type enum for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
dsolistorres committed Nov 5, 2024
1 parent 0ee4b8b commit 92d2804
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.containers.business.ContainerStructureFinderStrategyResolver;
import com.dotmarketing.portlets.contentlet.business.HostSearchOptions;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.folders.business.ApplicationContainerFolderListener;
import com.dotmarketing.portlets.folders.business.ApplicationTemplateFolderListener;
import com.dotmarketing.portlets.folders.model.Folder;
Expand Down Expand Up @@ -87,8 +87,7 @@ public void initApplicationContainerFolderListener() {

final User user = APILocator.systemUser();
final List<Host> hosts = APILocator.getHostAPI().findAllFromDB(user,
new HostSearchOptions().withIncludeSystemHost(true)
.withRespectFrontendRoles(false));
HostAPI.SearchType.INCLUDE_SYSTEM_HOST);
final ApplicationContainerFolderListener listener = new ApplicationContainerFolderListener();
for (final Host host : hosts) {

Expand All @@ -111,8 +110,7 @@ public void initApplicationTemplateFolderListener() {

final User user = APILocator.systemUser();
final List<Host> hosts = APILocator.getHostAPI().findAllFromDB(user,
new HostSearchOptions().withIncludeSystemHost(true)
.withRespectFrontendRoles(false));
HostAPI.SearchType.INCLUDE_SYSTEM_HOST);
final ApplicationTemplateFolderListener listener = new ApplicationTemplateFolderListener();
for (final Host host : hosts) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.dotmarketing.exception.DotRuntimeException;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
import com.dotmarketing.portlets.contentlet.business.DotContentletValidationException;
import com.dotmarketing.portlets.contentlet.business.HostSearchOptions;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.languagesmanager.business.LanguageAPI;
import com.dotmarketing.portlets.languagesmanager.model.Language;
Expand Down Expand Up @@ -117,8 +117,7 @@ private void populateVanityURLsCacheBySite(final Host site) {
@Override
public void populateAllVanityURLsCache() throws DotDataException {
for (final Host site : Try.of(() -> APILocator.getHostAPI().findAllFromDB(APILocator.systemUser(),
new HostSearchOptions().withIncludeSystemHost(true)
.withRespectFrontendRoles(false))).getOrElse(List.of())) {
HostAPI.SearchType.INCLUDE_SYSTEM_HOST)).getOrElse(List.of())) {
populateVanityURLsCacheBySite(site);
}
populateVanityURLsCacheBySite(APILocator.getHostAPI().findSystemHost());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import com.dotcms.repackage.org.directwebremoting.WebContext;
import com.dotcms.repackage.org.directwebremoting.WebContextFactory;
import com.dotcms.util.CollectionsUtils;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.CacheLocator;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.business.PermissionAPI;
import com.dotmarketing.business.Versionable;
import com.dotmarketing.business.util.HostNameComparator;
import com.dotmarketing.business.web.UserWebAPI;
import com.dotmarketing.business.web.WebAPILocator;
Expand All @@ -17,7 +15,6 @@
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.contentlet.business.HostSearchOptions;
import com.dotmarketing.portlets.contentlet.model.ContentletVersionInfo;
import com.dotmarketing.portlets.structure.model.Field;
import com.dotmarketing.portlets.structure.model.Structure;
Expand Down Expand Up @@ -127,10 +124,11 @@ public Map<String, Object> findHostsForDataStore(String filter, boolean showArch
public Map<String, Object> findHostsPaginated(final String filter, final boolean showArchived, int offset, int count) throws DotDataException, DotSecurityException, PortalException, SystemException {
final User user = this.getLoggedInUser();
final boolean respectFrontend = !this.userWebAPI.isLoggedToBackend(this.getHttpRequest());
final List<HostAPI.SearchType> searchTypes = respectFrontend ?
List.of(HostAPI.SearchType.RESPECT_FRONT_END_ROLES, HostAPI.SearchType.LIVE_ONLY) :
List.of(HostAPI.SearchType.LIVE_ONLY);
final List<Host> sitesFromDb = this.hostAPI.findAllFromDB(user,
new HostSearchOptions().withIncludeSystemHost(false)
.withRetrieveLiveVersion(true)
.withRespectFrontendRoles(respectFrontend));
searchTypes.toArray(new HostAPI.SearchType[0]));
final List<Field> fields = FieldsCache.getFieldsByStructureVariableName(Host.HOST_VELOCITY_VAR_NAME);
final List<Field> searchableFields = fields.stream().filter(field -> field.isListed() && field
.getFieldType().startsWith("text")).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
public interface HostAPI {

/**
* Will return a List of a host's aliases If no host aliases will return empty list
* @param host Host
* @return List
* This class is used to define the options for the search of a host.
*/
enum SearchType {
LIVE_ONLY, RESPECT_FRONT_END_ROLES, INCLUDE_SYSTEM_HOST
}

/**
* Returns the aliases of a given Site in the form of a list. In the Site's properties, aliases can be separated by:
Expand Down Expand Up @@ -184,7 +185,7 @@ Host find(Contentlet contentlet,
/**
* @deprecated This method is basically duplicated code. Use one of the following methods instead:
* <ul>
* <li>{@link #findAllFromDB(User, HostSearchOptions)}</li>
* <li>{@link #findAllFromDB(User, SearchType...)}</li>
* <li>{@link #findAllFromCache(User, boolean)}</li>
* <li>{@link #search(String, boolean, boolean, int, int, User, boolean)}</li>
* </ul>
Expand Down Expand Up @@ -223,15 +224,14 @@ Host find(Contentlet contentlet,
* method allows you to <b>EXCLUDE</b> the System Host from the result list. Additionally, you can specify whether
* you want to retrieve the live version of the Site over its working version if available.
*
* @param user The {@link User} that is calling this method.
* @param searchOptions The search options to be used when retrieving the list of Sites.
*
* @param user The {@link User} that is calling this method.
* @param searchTypes The search options to be used when retrieving the list of Sites.
* @return The list of {@link Host} objects.
* @throws DotDataException An error occurred when accessing the data source.
* @throws DotDataException An error occurred when accessing the data source.
* @throws DotSecurityException The specified User does not have the required permissions to perform this
* operation.
* operation.
*/
List<Host> findAllFromDB(final User user, final HostSearchOptions searchOptions) throws DotDataException, DotSecurityException;
List<Host> findAllFromDB(final User user, final SearchType... searchTypes) throws DotDataException, DotSecurityException;

/**
* Returns the complete list of Sites in your dotCMS repository retrieved from the cache. If no data is currently
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.vavr.control.Try;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -340,77 +341,58 @@ public Host find(final Contentlet contentlet,
@Override
@Deprecated
public List<Host> findAll(final User user, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
return this.findAllFromDB(user,
new HostSearchOptions().withIncludeSystemHost(true)
.withRespectFrontendRoles(respectFrontendRoles));
final List<SearchType> searchTypes = respectFrontendRoles ?
List.of(SearchType.INCLUDE_SYSTEM_HOST, SearchType.RESPECT_FRONT_END_ROLES) :
List.of(SearchType.INCLUDE_SYSTEM_HOST);
return this.findAllFromDB(user, searchTypes.toArray(new SearchType[0]));
}

@Override
public List<Host> findAll(final User user, final int limit, final int offset, final String sortBy, final boolean respectFrontendRoles)
throws DotDataException, DotSecurityException {
return this.findPaginatedSitesFromDB(user, limit, offset, sortBy, respectFrontendRoles);
final List<SearchType> searchTypes = respectFrontendRoles ?
List.of(SearchType.INCLUDE_SYSTEM_HOST, SearchType.RESPECT_FRONT_END_ROLES) :
List.of(SearchType.INCLUDE_SYSTEM_HOST);
return this.findPaginatedSitesFromDB(user, limit, offset, sortBy,
searchTypes.toArray(new SearchType[0]));
}

@Override
@CloseDBIfOpened
public List<Host> findAllFromDB(final User user, final HostSearchOptions searchOptions) throws DotDataException, DotSecurityException {
return this.findPaginatedSitesFromDB(user, 0, 0, null,
searchOptions.isIncludeSystemHost(),
searchOptions.isRetrieveLiveVersion(),
searchOptions.isRespectFrontendRoles());
public List<Host> findAllFromDB(final User user, final SearchType... searchTypes) throws DotDataException, DotSecurityException {
return this.findPaginatedSitesFromDB(user, 0, 0, null, searchTypes);
}

/**
* Returns an optionally paginated list of all Sites in your dotCMS content repository, including the System Host.
*
* @param user The {@link User} performing this action.
* @param limit Limit of results returned in the response, for pagination purposes. If set equal or
* lower than zero, this parameter will be ignored.
* @param offset Expected offset of results in the response, for pagination purposes. If set equal or
* lower than zero, this parameter will be ignored.
* @param sortBy Optional sorting criterion, as specified by the available columns in: {@link
* com.dotmarketing.common.util.SQLUtil#ORDERBY_WHITELIST}
* @param respectFrontendRoles If the User's front-end roles need to be taken into account in order to perform this
* operation, set to {@code true}. Otherwise, set to {@code false}.
* Returns an optionally paginated list of all Sites in your dotCMS content repository. This method allows you to
* * <b>EXCLUDE</b> the System Host from the result list. It also allows you to get live versions of the sites
* when they are available.
*
* @param user The {@link User} performing this action.
* @param limit Limit of results returned in the response, for pagination purposes. If set equal or
* lower than zero, this parameter will be ignored.
* @param offset Expected offset of results in the response, for pagination purposes. If set equal or
* lower than zero, this parameter will be ignored.
* @param sortBy Optional sorting criterion, as specified by the available columns in: {@link
* com.dotmarketing.common.util.SQLUtil#ORDERBY_WHITELIST}
* @param searchTypes The search types to be used in the query.
* @return The list of {@link Host} objects.
*
* @throws DotDataException An error occurred when accessing the data source.
* @throws DotSecurityException The specified User does not have the required permissions to perform this
* operation.
*/
@CloseDBIfOpened
private List<Host> findPaginatedSitesFromDB(final User user, final int limit, final int offset, final String
sortBy, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
return this.findPaginatedSitesFromDB(user, limit, offset, sortBy, true, false, respectFrontendRoles);
}
sortBy, final SearchType... searchTypes) throws DotDataException, DotSecurityException {

final boolean respectFrontEndRoles = Arrays.stream(searchTypes).anyMatch(t -> t == SearchType.RESPECT_FRONT_END_ROLES);
final boolean includeSystemHost = Arrays.stream(searchTypes).anyMatch(t -> t == SearchType.INCLUDE_SYSTEM_HOST);
final boolean liveOnly = Arrays.stream(searchTypes).anyMatch(t -> t == SearchType.LIVE_ONLY);

/**
* Returns an optionally paginated list of all Sites in your dotCMS content repository. This method allows you to
* <b>EXCLUDE</b> the System Host from the result list.
*
* @param user The {@link User} performing this action.
* @param limit Limit of results returned in the response, for pagination purposes. If set equal or
* lower than zero, this parameter will be ignored.
* @param offset Expected offset of results in the response, for pagination purposes. If set equal or
* lower than zero, this parameter will be ignored.
* @param sortBy Optional sorting criterion, as specified by the available columns in: {@link
* com.dotmarketing.common.util.SQLUtil#ORDERBY_WHITELIST}
* @param includeSystemHost If the System Host should be included in the result list, set to {@code true}.
* @param retrieveLiveVersion If the live version of the Site should be retrieved, set to {@code true}.
* @param respectFrontendRoles If the User's front-end roles need to be taken into account in order to perform this
* operation, set to {@code true}. Otherwise, set to {@code false}.
* @return The list of {@link Host} objects.
* @throws DotDataException An error occurred when accessing the data source.
* @throws DotSecurityException The specified User does not have the required permissions to perform this
* operation.
*/
private List<Host> findPaginatedSitesFromDB(final User user, final int limit, final int offset,
final String sortBy, final boolean includeSystemHost,
final boolean retrieveLiveVersion, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
final List<Host> siteList = this.getHostFactory().findAll(
limit, offset, sortBy, includeSystemHost, retrieveLiveVersion);
return filterHostsByPermissions(user, includeSystemHost, respectFrontendRoles, siteList);
limit, offset, sortBy, includeSystemHost, liveOnly);

return filterHostsByPermissions(user, includeSystemHost, respectFrontEndRoles, siteList);
}

/**
Expand Down Expand Up @@ -451,14 +433,10 @@ public List<Host> findAllFromCache(final User user,
final boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
Set<Host> cachedSites = hostCache.getAllSites(respectFrontendRoles);
if(null == cachedSites){
final List<Host> allFromDB = findAllFromDB(APILocator.systemUser(),
new HostSearchOptions().withIncludeSystemHost(true)
.withRetrieveLiveVersion(false)
.withRespectFrontendRoles(false));
final List<Host> allFromDBLive = findAllFromDB(APILocator.systemUser(),
new HostSearchOptions().withIncludeSystemHost(true)
.withRetrieveLiveVersion(true)
.withRespectFrontendRoles(false));
final List<Host> allFromDB = findAllFromDB(
APILocator.systemUser(), SearchType.INCLUDE_SYSTEM_HOST);
final List<Host> allFromDBLive = findAllFromDB(
APILocator.systemUser(), SearchType.INCLUDE_SYSTEM_HOST, SearchType.LIVE_ONLY);
hostCache.addAll(allFromDB, allFromDBLive);
final List<Host> filteredSiteList = filterHostsByPermissions(
user, true, respectFrontendRoles,
Expand Down Expand Up @@ -527,10 +505,11 @@ public Host save(final Host hostToBeSaved, User user, boolean respectFrontendRol
public void updateDefaultHost(Host host, User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException{
// If host is marked as default make sure that no other host is already set to be the default
if(host.isDefault()) {
ContentletAPI conAPI = APILocator.getContentletAPI();
List<Host> hosts= findAllFromDB(user,
new HostSearchOptions().withIncludeSystemHost(true)
.withRespectFrontendRoles(respectFrontendRoles));
final ContentletAPI conAPI = APILocator.getContentletAPI();
final List<SearchType> searchTypes = respectFrontendRoles ?
List.of(SearchType.INCLUDE_SYSTEM_HOST, SearchType.RESPECT_FRONT_END_ROLES) :
List.of(SearchType.INCLUDE_SYSTEM_HOST);
final List<Host> hosts= findAllFromDB(user, searchTypes.toArray(new SearchType[0]));
Host otherHost;
Contentlet otherHostContentlet;
for(Host h : hosts){
Expand All @@ -539,7 +518,7 @@ public void updateDefaultHost(Host host, User user, boolean respectFrontendRoles
}
// if this host is the default as well then ours should become the only one
if(h.isDefault()){
boolean isHostRunning = h.isLive();
final boolean isHostRunning = h.isLive();
otherHostContentlet = APILocator.getContentletAPI().checkout(h.getInode(), user, respectFrontendRoles);
otherHost = new Host(otherHostContentlet);
hostCache.remove(otherHost);
Expand All @@ -549,7 +528,7 @@ public void updateDefaultHost(Host host, User user, boolean respectFrontendRoles
if(host.getMap().containsKey(Contentlet.DISABLE_WORKFLOW))
otherHost.setProperty(Contentlet.DISABLE_WORKFLOW,true);

Contentlet cont = conAPI.checkin(otherHost, user, respectFrontendRoles);
final Contentlet cont = conAPI.checkin(otherHost, user, respectFrontendRoles);
if(isHostRunning) {
otherHost = new Host(cont);
publish(otherHost, user, respectFrontendRoles);
Expand Down
Loading

0 comments on commit 92d2804

Please sign in to comment.