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 2bf692e
Show file tree
Hide file tree
Showing 26 changed files with 551 additions and 156 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Map;

import com.databasepreservation.common.client.tools.HistoryManager;
import org.fusesource.restygwt.client.MethodCallback;
import org.roda.core.data.v2.index.sublist.Sublist;

Expand Down Expand Up @@ -54,9 +55,11 @@
*/
public class CrossDatabaseList extends BasicAsyncTableCell<ViewerDatabase> {
private static final ClientMessages messages = GWT.create(ClientMessages.class);
private String searchValue;

public CrossDatabaseList() {
this(new Filter(), null, null, false, false);
this.searchValue="";
}

public CrossDatabaseList(Filter filter, Facets facets, String summary, boolean selectable, boolean exportable) {
Expand All @@ -67,6 +70,42 @@ public CrossDatabaseList(Filter filter, Facets facets, String summary, boolean s
protected void configureDisplay(CellTable<ViewerDatabase> display) {
display.setSelectionModel(display.getSelectionModel(), DefaultSelectionEventManager.createBlacklistManager(4, 10));

display.addCellPreviewHandler(event -> {
if (event.getNativeEvent().getType().equals("click")) {
int columnIndex = event.getColumn();
ViewerDatabase database = event.getValue();

UserLogin.getInstance().getAuthenticatedUser(new DefaultAsyncCallback<User>() {
@Override
public void onSuccess(User user) {
if (database != null) {
if (user.isAdmin()) {
if (columnIndex == 7) {
HistoryManager.gotoDatabaseSearchWithValue(database.getUuid(), searchValue);
getSelectionModel().clear();
} else {
HistoryManager.gotoSIARDInfo(database.getUuid());
getSelectionModel().clear();
}
} else {
if (columnIndex == 4) {
if (ApplicationType.getType().equals(ViewerConstants.APPLICATION_ENV_SERVER)) {
HistoryManager.gotoDatabaseSearchWithValue(database.getUuid(), searchValue);
}
getSelectionModel().clear();
} else {
if (ApplicationType.getType().equals(ViewerConstants.APPLICATION_ENV_SERVER)) {
HistoryManager.gotoDatabase(database.getUuid());
}
getSelectionModel().clear();
}
}
}
}
});
}
});

Column<ViewerDatabase, SafeHtml> nameColumn = new TooltipColumn<ViewerDatabase>() {
@Override
public SafeHtml getValue(ViewerDatabase database) {
Expand Down Expand Up @@ -127,21 +166,6 @@ public String getValue(ViewerDatabase database) {
}
});

Column<ViewerDatabase, SafeHtml> sizeColumn = new TooltipColumn<ViewerDatabase>() {
@Override
public SafeHtml getValue(ViewerDatabase database) {
return database != null ? SafeHtmlUtils.fromString(Humanize.readableFileSize(database.getSize()))
: SafeHtmlUtils.fromString("unknown");
}
};

Column<ViewerDatabase, SafeHtml> versionColumn = new TooltipColumn<ViewerDatabase>() {
@Override
public SafeHtml getValue(ViewerDatabase database) {
return database != null ? SafeHtmlUtils.fromString(database.getVersion()) : SafeHtmlUtils.fromString("unknown");
}
};

Column<ViewerDatabase, SafeHtml> validColumn = new Column<ViewerDatabase, SafeHtml>(new SafeHtmlCell()) {
@Override
public SafeHtml getValue(ViewerDatabase database) {
Expand All @@ -159,7 +183,11 @@ public SafeHtml getValue(ViewerDatabase database) {
Column<ViewerDatabase, String> searchHitsColumn = new ButtonColumn<ViewerDatabase>() {
@Override
public String getValue(ViewerDatabase database) {
return database != null ? String.valueOf(database.getSearchHits()) : "unknown";
if (searchValue != null && !searchValue.isEmpty()) {
return database != null ? String.valueOf(database.getSearchHits()) + " results" : "unknown";
} else {
return database != null ? "" : "unknown";
}
}
};

Expand All @@ -173,8 +201,6 @@ public void onSuccess(User user) {
addColumn(archivalDateColumn, messages.managePageTableHeaderTextForArchivalDate(), true, TextAlign.NONE, 5);
if (user.isAdmin()) {
addColumn(dbmsColumn, messages.managePageTableHeaderTextForProductName(), true, TextAlign.NONE, 10);
addColumn(sizeColumn, messages.managePageTableHeaderTextForSIARDSize(), true, TextAlign.NONE, 4);
addColumn(versionColumn, messages.managePageTableHeaderTextForSIARDVersion(), true, TextAlign.NONE, 4);
addColumn(validColumn, messages.managePageTableHeaderTextForSIARDValidationStatus(), true, TextAlign.NONE,
5);
addColumn(statusColumn, messages.managePageTableHeaderTextForDatabaseStatus(), true, TextAlign.NONE, 5);
Expand All @@ -189,8 +215,6 @@ public void onSuccess(User user) {
addColumn(archivalDateColumn, messages.managePageTableHeaderTextForArchivalDate(), true, TextAlign.NONE, 5);
addColumn(locationColumn, messages.managePageTableHeaderTextForSIARDLocation(), true, TextAlign.NONE, 8);
addColumn(dbmsColumn, messages.managePageTableHeaderTextForProductName(), true, TextAlign.NONE, 10);
addColumn(sizeColumn, messages.managePageTableHeaderTextForSIARDSize(), true, TextAlign.NONE, 4);
addColumn(versionColumn, messages.managePageTableHeaderTextForSIARDVersion(), true, TextAlign.NONE, 4);
addColumn(validColumn, messages.managePageTableHeaderTextForSIARDValidationStatus(), true, TextAlign.NONE, 5);
addColumn(statusColumn, messages.managePageTableHeaderTextForDatabaseStatus(), true, TextAlign.NONE, 5);
addColumn(searchHitsColumn, messages.managePageTableHeaderTextForSearchHits(), true, TextAlign.NONE, 5);
Expand All @@ -213,6 +237,14 @@ protected void getData(Sublist sublist, ColumnSortList columnSortList,
DatabaseService.Util.call(callback).findAll(findRequest, LocaleInfo.getCurrentLocale().getLocaleName());
}

public String getSearchValue() {
return searchValue;
}

public void setSearchValue(String searchValue) {
this.searchValue = searchValue;
}

@Override
public void exportClickHandler() {
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,16 @@ private <T extends IsIndexed> void attachComponents(String objectClassSimpleName
}
}

public Components getComponents() {
return components;
}

/**
* Auxiliary manager for inner components (groups of one searchWrapper and one
* BasicAsyncTableCell, at least for now) that is used to enforce type coherence
*/
@SuppressWarnings("unchecked")
private class Components {
public class Components {
private final Map<String, SearchPanel> searchPanels = new LinkedHashMap<>();
private final Map<String, AsyncTableCell<? extends IsIndexed, Void>> lists = new LinkedHashMap<>();

Expand Down
Loading

0 comments on commit 2bf692e

Please sign in to comment.