Skip to content

Commit

Permalink
enabled db search, search all for users and on/off dbs in search all
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioG70 committed Nov 15, 2024
1 parent 97ff3bc commit 959f6e9
Show file tree
Hide file tree
Showing 28 changed files with 562 additions and 163 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up JDK 8
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: 8
java-version: 21
distribution: 'adopt'
- name: Cache
uses: actions/cache@v3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.solr.client.solrj.SolrServerException;
import org.roda.core.data.exceptions.GenericException;
Expand All @@ -34,6 +35,7 @@
import com.databasepreservation.common.client.index.facets.Facets;
import com.databasepreservation.common.client.index.facets.SimpleFacetParameter;
import com.databasepreservation.common.client.index.filter.AndFiltersParameters;
import com.databasepreservation.common.client.index.filter.BasicSearchFilterParameter;
import com.databasepreservation.common.client.index.filter.Filter;
import com.databasepreservation.common.client.index.filter.FilterParameter;
import com.databasepreservation.common.client.index.filter.OrFiltersParameters;
Expand Down Expand Up @@ -81,8 +83,9 @@ public IndexResult<ViewerDatabase> find(FindRequest findRequest, String localeSt
fieldsToReturn.add(ViewerConstants.SOLR_DATABASES_METADATA);
fieldsToReturn.add(ViewerConstants.SOLR_DATABASES_PERMISSIONS);

FindRequest userFindRequest = new FindRequest(findRequest.classToReturn, getDatabaseFilterForUser(user),
findRequest.sorter, findRequest.sublist, findRequest.facets, findRequest.exportFacets, fieldsToReturn);
FindRequest userFindRequest = new FindRequest(findRequest.classToReturn,
getDatabaseFindFilterForUser(user, findRequest.filter), findRequest.sorter, findRequest.sublist,
findRequest.facets, findRequest.exportFacets, fieldsToReturn);
return getViewerDatabaseIndexResult(userFindRequest, fieldsToReturn, controllerAssistant, user, state);
}
} else {
Expand All @@ -93,25 +96,14 @@ public IndexResult<ViewerDatabase> find(FindRequest findRequest, String localeSt
@Override
public IndexResult<ViewerDatabase> findAll(FindRequest findRequest, String localeString) {
ControllerAssistant controllerAssistant = new ControllerAssistant() {};

LogEntryState state = LogEntryState.SUCCESS;
User user = controllerAssistant.checkRoles(request);

if (ViewerConfiguration.getInstance().getApplicationEnvironment().equals(ViewerConstants.APPLICATION_ENV_SERVER)) {
if (user.isAdmin() || user.isWhiteList()) {

return getCrossViewerDatabaseIndexResult(findRequest, controllerAssistant, user, state);
} else {
List<String> fieldsToReturn = new ArrayList<>();
fieldsToReturn.add(ViewerConstants.INDEX_ID);
fieldsToReturn.add(ViewerConstants.SOLR_DATABASES_STATUS);
fieldsToReturn.add(ViewerConstants.SOLR_DATABASES_METADATA);
fieldsToReturn.add(ViewerConstants.SOLR_DATABASES_PERMISSIONS);

FindRequest userFindRequest = new FindRequest(findRequest.classToReturn, getDatabaseFilterForUser(user),
findRequest.sorter, findRequest.sublist, findRequest.facets, findRequest.exportFacets, fieldsToReturn);
return getCrossViewerDatabaseIndexResult(userFindRequest, controllerAssistant, user, state,
getDatabaseFilterForUser(user));
return getCrossViewerDatabaseIndexResult(findRequest, controllerAssistant, user, state,
getDatabaseFindAllFilterForUser(user));
}
} else {
return getCrossViewerDatabaseIndexResult(findRequest, controllerAssistant, user, state);
Expand Down Expand Up @@ -176,15 +168,27 @@ private IndexResult<ViewerDatabase> getViewerDatabaseIndexResult(FindRequest fin

private IndexResult<ViewerDatabase> getCrossViewerDatabaseIndexResult(FindRequest findRequest,
ControllerAssistant controllerAssistant, User user, LogEntryState state) {
return getCrossViewerDatabaseIndexResult(findRequest, controllerAssistant, user, state, new Filter());
return getCrossViewerDatabaseIndexResult(findRequest, controllerAssistant, user, state,
new Filter(new SimpleFilterParameter(ViewerConstants.SOLR_DATABASES_AVAILABLE_TO_SEARCH_ALL, "true")));
}

private IndexResult<ViewerDatabase> getCrossViewerDatabaseIndexResult(FindRequest findRequest,
ControllerAssistant controllerAssistant, User user, LogEntryState state, Filter userFilter) {
long count = 0;
try {
IterableDatabaseResult<ViewerDatabase> databases = ViewerFactory.getSolrManager().findAll(ViewerDatabase.class,
userFilter, Sorter.NONE, findRequest.fieldsToReturn);
IterableDatabaseResult<ViewerDatabase> databases;
if (userFilter != null) {
List<String> fieldsToReturn = new ArrayList<>();
fieldsToReturn.add(ViewerConstants.INDEX_ID);
fieldsToReturn.add(ViewerConstants.SOLR_DATABASES_STATUS);
fieldsToReturn.add(ViewerConstants.SOLR_DATABASES_METADATA);
fieldsToReturn.add(ViewerConstants.SOLR_DATABASES_PERMISSIONS);
databases = ViewerFactory.getSolrManager().findAll(ViewerDatabase.class, userFilter, Sorter.NONE,
fieldsToReturn);
} else {
databases = ViewerFactory.getSolrManager().findAll(ViewerDatabase.class, new Filter(), Sorter.NONE,
findRequest.fieldsToReturn);
}

if (databases.getTotalCount() == 0) {
return new IndexResult<>();
Expand All @@ -196,14 +200,14 @@ private IndexResult<ViewerDatabase> getCrossViewerDatabaseIndexResult(FindReques
for (ViewerDatabase database : databases) {
databaseMap.put(database.getUuid(), database);
// only add the available collections
if(database.getStatus().equals(ViewerDatabaseStatus.AVAILABLE)){
if (database.getStatus().equals(ViewerDatabaseStatus.AVAILABLE)) {
String collectionName = ViewerConstants.SOLR_INDEX_ROW_COLLECTION_NAME_PREFIX + database.getUuid();
collections.add(collectionName);
}
}

String collectionAlias = SolrUtils.crateSearchAllAlias(ViewerFactory.getSolrClient(), "alias-" + user.getUUID(),
collections);
String collectionAlias = SolrUtils.createSearchAllAlias(ViewerFactory.getSolrClient(),
"alias-" + UUID.randomUUID(), collections);

SimpleFacetParameter simpleFacetParameter = new SimpleFacetParameter(ViewerConstants.SOLR_ROWS_DATABASE_UUID,
FacetParameter.SORT.COUNT);
Expand All @@ -213,11 +217,15 @@ private IndexResult<ViewerDatabase> getCrossViewerDatabaseIndexResult(FindReques

final IndexResult<ViewerDatabase> facetsSearch = ViewerFactory.getSolrManager().findHits(ViewerDatabase.class,
collectionAlias, findRequest.filter, findRequest.sorter, findRequest.sublist, new Facets(simpleFacetParameter));

SolrUtils.deleteSearchAllAlias(ViewerFactory.getSolrClient(), collectionAlias);

count = facetsSearch.getTotalCount();
FacetFieldResult facetResults = facetsSearch.getFacetResults().get(0);

IndexResult<ViewerDatabase> searchHitsResult = new IndexResult<>();
if(facetResults.getValues().size() < findRequest.sublist.getMaximumElementCount() && findRequest.sublist.getFirstElementIndex() == 0) {
if (facetResults.getValues().size() < findRequest.sublist.getMaximumElementCount()
&& findRequest.sublist.getFirstElementIndex() == 0) {
searchHitsResult.setTotalCount(facetResults.getValues().size());
} else {
searchHitsResult.setTotalCount(-1);
Expand Down Expand Up @@ -250,18 +258,64 @@ private IndexResult<ViewerDatabase> getCrossViewerDatabaseIndexResult(FindReques
}
}

private Filter getDatabaseFilterForUser(User user) {
private Filter getDatabaseFindAllFilterForUser(User user) {
ArrayList<FilterParameter> permissionFilterParameters = new ArrayList<>();
// Only retrieve databases with AVAILABLE status
SimpleFilterParameter statusFilter = new SimpleFilterParameter(ViewerConstants.SOLR_DATABASES_STATUS,

// Only retrieve databases with AVAILABLE
SimpleFilterParameter statusFilter;
statusFilter = new SimpleFilterParameter(ViewerConstants.SOLR_DATABASES_STATUS,
ViewerDatabaseStatus.AVAILABLE.name());
permissionFilterParameters.add(statusFilter);

permissionFilterParameters
.add(new SimpleFilterParameter(ViewerConstants.SOLR_DATABASES_AVAILABLE_TO_SEARCH_ALL, "true"));

// Add user permissions on filter
ArrayList<FilterParameter> permissionsOrFilterParameters = new ArrayList<>();
for (String role : user.getAllRoles()) {
permissionsOrFilterParameters.add(new SimpleFilterParameter(ViewerConstants.SOLR_DATABASES_PERMISSIONS, role));
}

permissionFilterParameters.add(new OrFiltersParameters(permissionsOrFilterParameters));

return new Filter(new AndFiltersParameters(permissionFilterParameters));
}

private Filter getDatabaseFindFilterForUser(User user, Filter searchFilter) {
ArrayList<FilterParameter> permissionFilterParameters = new ArrayList<>();

// Only retrieve databases with AVAILABLE, this filter has to be default
SimpleFilterParameter statusFilter;
statusFilter = new SimpleFilterParameter(ViewerConstants.SOLR_DATABASES_STATUS,
ViewerDatabaseStatus.AVAILABLE.name());
permissionFilterParameters.add(statusFilter);

// Controlling the search filter
if (!searchFilter.getParameters().isEmpty()) {
for (FilterParameter filterParameter : searchFilter.getParameters()) {
if (filterParameter instanceof SimpleFilterParameter simpleFilterParameter) {
// If there is a status filter, remove the default and add the new one(s).
if (simpleFilterParameter.getName().equals(ViewerConstants.SOLR_DATABASES_STATUS)) {
permissionFilterParameters.remove(statusFilter);
permissionFilterParameters
.add(new SimpleFilterParameter(ViewerConstants.SOLR_DATABASES_STATUS, simpleFilterParameter.getValue()));
} else if (simpleFilterParameter.getName().equals(ViewerConstants.SOLR_DATABASES_AVAILABLE_TO_SEARCH_ALL)) {
permissionFilterParameters.add(new SimpleFilterParameter(
ViewerConstants.SOLR_DATABASES_AVAILABLE_TO_SEARCH_ALL, simpleFilterParameter.getValue()));
}
} else if (filterParameter instanceof BasicSearchFilterParameter searchFilterParameter) {
String searchValue = searchFilterParameter.getValue();
permissionFilterParameters.add(new SimpleFilterParameter(ViewerConstants.INDEX_SEARCH, searchValue));
}
}
}

// Add user permissions on filter
ArrayList<FilterParameter> permissionsOrFilterParameters = new ArrayList<>();
for (String role : user.getAllRoles()) {
permissionsOrFilterParameters.add(new SimpleFilterParameter(ViewerConstants.SOLR_DATABASES_PERMISSIONS, role));
}

permissionFilterParameters.add(new OrFiltersParameters(permissionsOrFilterParameters));

return new Filter(new AndFiltersParameters(permissionFilterParameters));
Expand Down Expand Up @@ -345,4 +399,25 @@ public Set<String> updateDatabasePermissions(String databaseUUID, Set<String> pe
databaseUUID);
}
}

@Override
public boolean updateDatabaseSearchAllAvailability(String databaseUUID) {
ControllerAssistant controllerAssistant = new ControllerAssistant() {};

LogEntryState state = LogEntryState.SUCCESS;
User user = controllerAssistant.checkRoles(request);

try {
return SIARDController.updateDatabaseSearchAllAvailability(databaseUUID);
} catch (GenericException | ViewerException | NotFoundException e) {
state = LogEntryState.FAILURE;
throw new RESTException(e);
} finally {
// register action
controllerAssistant.registerAction(user, databaseUUID, state, ViewerConstants.CONTROLLER_DATABASE_ID_PARAM,
databaseUUID);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public class ViewerConstants {
*/
public static final String SOLR_DATABASES_STATUS = "status";
public static final String SOLR_DATABASES_BROWSE_LOAD_DATE = "browse_loaded_date";
public static final String SOLR_DATABASES_AVAILABLE_TO_SEARCH_ALL = "available_to_search_all";
public static final String SOLR_DATABASES_METADATA = "metadata";
public static final String SOLR_DATABASES_SIARD_PATH = "siard_path";
public static final String SOLR_DATABASES_SIARD_SIZE = "siard_size";
Expand Down
Loading

0 comments on commit 959f6e9

Please sign in to comment.