Commit 6d0307d0 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov

Merge branch 'server-side-for-ui-353' into 'master'

Updated controllers and services for using suggestions

See merge request genesys-pgr/genesys-server!416
parents e1d14749 0775f4f6
......@@ -20,6 +20,7 @@ import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
......@@ -35,6 +36,8 @@ import org.genesys.filerepository.model.RepositoryFile;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.impl.SearchException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
......@@ -146,6 +149,16 @@ public interface DatasetService {
*/
Page<Dataset> listDatasets(DatasetFilter filter, Pageable page);
/**
* Get terms by filter.
* @param filter the dataset filter
*
* @return the term result
* @throws SearchException the search exception
* @throws IOException the IO exception
*/
Map<String, ElasticsearchService.TermResult> getSuggestions(DatasetFilter filter) throws SearchException, IOException;
/**
* List datasets matching the filter for current user.
*
......
......@@ -18,11 +18,14 @@ package org.genesys.catalog.service;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.genesys.catalog.model.filters.DescriptorListFilter;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.impl.SearchException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
......@@ -119,6 +122,16 @@ public interface DescriptorListService {
*/
Page<DescriptorList> listDescriptorLists(DescriptorListFilter filters, Pageable page);
/**
* Get terms by filter.
* @param filter the descriptor list filter
*
* @return the term result
* @throws SearchException the search exception
* @throws IOException the IO exception
*/
Map<String, ElasticsearchService.TermResult> getSuggestions(DescriptorListFilter filter) throws SearchException, IOException;
/**
* List Descriptor Lists for current user.
*
......
......@@ -28,8 +28,10 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
......@@ -39,6 +41,7 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.validation.Valid;
import com.google.common.collect.Sets;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.dataset.Dataset;
......@@ -76,6 +79,7 @@ import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.impl.QSubsetAccessionRef;
import org.genesys2.server.service.DownloadService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.impl.SearchException;
import org.genesys2.server.service.worker.AccessionRefMatcher;
import org.genesys2.util.JPAUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -535,6 +539,29 @@ public class DatasetServiceImpl implements DatasetService {
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
}
/**
* {@inheritDoc}
*/
@Override
public Map<String, ElasticsearchService.TermResult> getSuggestions(DatasetFilter filter) throws SearchException, IOException {
Set<String> suggestions = Sets.newHashSet("crops");
Map<String, ElasticsearchService.TermResult> suggestionRes = new HashMap<>(suggestions.size());
for (String suggestionKey: suggestions) {
DatasetFilter suggestionFilter = filter.copy(DatasetFilter.class);
suggestionFilter.state(PublishState.PUBLISHED);
try {
suggestionFilter.clearFilter(suggestionKey);
} catch (NoSuchFieldException | IllegalAccessException e) {
LOG.error("Error while clearing filter: ", e.getMessage());
}
ElasticsearchService.TermResult suggestion = elasticsearchService.termStatisticsAuto(Dataset.class, suggestionFilter, 100, suggestionKey);
suggestionRes.put(suggestionKey, suggestion);
}
return suggestionRes;
}
/**
* {@inheritDoc}
*/
......
......@@ -25,12 +25,14 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.validation.Valid;
import com.google.common.collect.Sets;
import org.apache.commons.lang.StringUtils;
import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.filters.DescriptorListFilter;
......@@ -46,6 +48,7 @@ import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.service.DownloadService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.impl.SearchException;
import org.genesys2.util.JPAUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -353,6 +356,29 @@ public class DescriptorListServiceImpl implements DescriptorListService {
return new PageImpl<DescriptorList>(res.getContent(), page, res.getTotalElements());
}
/**
* {@inheritDoc}
*/
@Override
public Map<String, ElasticsearchService.TermResult> getSuggestions(DescriptorListFilter filter) throws SearchException, IOException {
Set<String> suggestions = Sets.newHashSet("crop");
Map<String, ElasticsearchService.TermResult> suggestionRes = new HashMap<>(suggestions.size());
for (String suggestionKey: suggestions) {
DescriptorListFilter suggestionFilter = filter.copy(DescriptorListFilter.class);
suggestionFilter.state(PublishState.PUBLISHED);
try {
suggestionFilter.clearFilter(suggestionKey);
} catch (NoSuchFieldException | IllegalAccessException e) {
LOG.error("Error while clearing filter: ", e.getMessage());
}
ElasticsearchService.TermResult suggestion = elasticsearchService.termStatisticsAuto(DescriptorList.class, suggestionFilter, 100, suggestionKey);
suggestionRes.put(suggestionKey, suggestion);
}
return suggestionRes;
}
@Override
public Page<DescriptorList> listDescriptorListsForCurrentUser(final DescriptorListFilter filter, final Pageable page) {
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
......
......@@ -26,6 +26,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonView;
import com.google.common.collect.Sets;
import io.swagger.annotations.Api;
......@@ -265,6 +266,30 @@ public class DatasetController extends ApiBaseController {
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, datasetService.listDatasets(filterInfo.filter, page.toPageRequest(100, Sort.Direction.ASC, "id")));
}
/**
* List datasets with suggestions.
*
* @param page the page
* @param filter the dataset filter
* @return the page with suggestions
*/
@PostMapping(value = "/filter")
public DatasetSuggestionPage listSuggestions(@RequestParam(name = "f", required = false) String filterCode, final Pagination page,
@RequestBody(required = false) DatasetFilter filter) throws IOException, SearchException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, DatasetFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
FilterInfo<DatasetFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, DatasetFilter.class);
FilteredPage<Dataset> pageRes = new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, datasetService.listDatasets(filterInfo.filter, page.toPageRequest(100, Sort.Direction.ASC, "id")));
Map<String, ElasticsearchService.TermResult> suggestionRes = datasetService.getSuggestions(filter);
return DatasetSuggestionPage.from(pageRes, suggestionRes);
}
/**
* Get term overview for filters
*
......@@ -289,8 +314,9 @@ public class DatasetController extends ApiBaseController {
Map<String, TermResult> overview = elasticsearchService.termStatisticsAuto(Dataset.class, overviewFilter, 10, terms.toArray(new String[] {}));
long datasetCount = datasetService.countDatasets(filter);
Map<String, ElasticsearchService.TermResult> suggestionRes = datasetService.getSuggestions(filter);
return DatasetOverview.from(filterCode, filter, overview, datasetCount);
return DatasetOverview.from(filterCode, filter, overview, datasetCount, suggestionRes);
}
/**
......@@ -625,13 +651,29 @@ public class DatasetController extends ApiBaseController {
public DatasetFilter filter;
public long datasetCount;
public Map<String, TermResult> overview;
public Map<String, TermResult> suggestions;
private static DatasetOverview from(String filterCode, DatasetFilter filter, Map<String, TermResult> overview, long datasetCount) {
private static DatasetOverview from(String filterCode, DatasetFilter filter, Map<String, TermResult> overview, long datasetCount, Map<String, TermResult> suggestions) {
DatasetOverview res = new DatasetOverview();
res.filterCode = filterCode;
res.filter = filter;
res.overview = overview;
res.datasetCount = datasetCount;
res.suggestions = suggestions;
return res;
}
}
public static class DatasetSuggestionPage {
@JsonUnwrapped
public FilteredPage<Dataset> page;
public Map<String, TermResult> suggestions;
private static DatasetSuggestionPage from(FilteredPage<Dataset> page, Map<String, TermResult> suggestions) {
DatasetSuggestionPage res = new DatasetSuggestionPage();
res.page = page;
res.suggestions = suggestions;
return res;
}
......
......@@ -27,6 +27,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonView;
import com.google.common.collect.Sets;
import io.swagger.annotations.Api;
......@@ -38,6 +39,7 @@ import org.genesys.catalog.mvc.DescriptorListExporter;
import org.genesys.catalog.service.DescriptorListService;
import org.genesys.catalog.service.DescriptorService;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys.catalog.service.ShortFilterService.FilterInfo;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.FilteredPage;
import org.genesys2.server.api.Pagination;
......@@ -209,10 +211,40 @@ public class DescriptorListController {
public FilteredPage<DescriptorList> listDescriptorLists(@RequestParam(name = "f", required = false) String filterCode, final Pagination page,
@RequestBody(required = false) DescriptorListFilter filter) throws IOException {
ShortFilterService.FilterInfo<DescriptorListFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, DescriptorListFilter.class);
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, DescriptorListFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
FilterInfo<DescriptorListFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, DescriptorListFilter.class);
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, descriptorListService.listDescriptorLists(filterInfo.filter, page.toPageRequest(100)));
}
/**
* List descriptor lists with suggestions.
*
* @param filterCode short filter code -- overrides filter in body
* @param page the page
* @param filter the descriptor list filter
* @return the page with suggestions
* @throws IOException Signals that an I/O exception has occurred.
*/
@PostMapping(value = "/filter")
public DescriptorListSuggestionPage listSuggestions(@RequestParam(name = "f", required = false) String filterCode, final Pagination page,
@RequestBody(required = false) DescriptorListFilter filter) throws IOException, SearchException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, DescriptorListFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
FilterInfo<DescriptorListFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, DescriptorListFilter.class);
FilteredPage<DescriptorList> pageRes = new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, descriptorListService.listDescriptorLists(filterInfo.filter, page.toPageRequest(100)));
Map<String, ElasticsearchService.TermResult> suggestionRes = descriptorListService.getSuggestions(filter);
return DescriptorListSuggestionPage.from(pageRes, suggestionRes);
}
/**
* Get term overview for filters
*
......@@ -237,8 +269,9 @@ public class DescriptorListController {
Map<String, TermResult> overview = elasticsearchService.termStatisticsAuto(DescriptorList.class, overviewFilter, 10, terms.toArray(new String[] {}));
long descriptorListCount = descriptorListService.countDescriptorLists(filter);
Map<String, ElasticsearchService.TermResult> suggestionRes = descriptorListService.getSuggestions(filter);
return DescriptorListOverview.from(filterCode, filter, overview, descriptorListCount);
return DescriptorListOverview.from(filterCode, filter, overview, descriptorListCount, suggestionRes);
}
/**
......@@ -368,13 +401,29 @@ public class DescriptorListController {
public DescriptorListFilter filter;
public long descriptorListCount;
public Map<String, TermResult> overview;
public Map<String, TermResult> suggestions;
private static DescriptorListOverview from(String filterCode, DescriptorListFilter filter, Map<String, TermResult> overview, long descriptorListCount) {
private static DescriptorListOverview from(String filterCode, DescriptorListFilter filter, Map<String, TermResult> overview, long descriptorListCount, Map<String, TermResult> suggestions) {
DescriptorListOverview res = new DescriptorListOverview();
res.filterCode = filterCode;
res.filter = filter;
res.overview = overview;
res.descriptorListCount = descriptorListCount;
res.suggestions = suggestions;
return res;
}
}
public static class DescriptorListSuggestionPage {
@JsonUnwrapped
public FilteredPage<DescriptorList> page;
public Map<String, TermResult> suggestions;
private static DescriptorListSuggestionPage from(FilteredPage<DescriptorList> page, Map<String, TermResult> suggestions) {
DescriptorListSuggestionPage res = new DescriptorListSuggestionPage();
res.page = page;
res.suggestions = suggestions;
return res;
}
......
......@@ -19,9 +19,11 @@ package org.genesys2.server.api.v1;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonView;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.service.ShortFilterService;
......@@ -33,9 +35,11 @@ import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.SubsetService;
import org.genesys2.server.service.filter.SubsetFilter;
import org.genesys2.server.service.impl.SearchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
......@@ -149,6 +153,32 @@ public class SubsetController extends ApiBaseController {
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, subsetService.list(filterInfo.filter, page.toPageRequest(100, Sort.Direction.ASC, "id")));
}
/**
* List subsets by filterCode or filter with suggestions
*
* @param page the page
* @param filterCode short filter code
* @param filter the filter
* @return the page with suggestions
* @throws IOException
*/
@PostMapping(value = "/filter", produces = { MediaType.APPLICATION_JSON_VALUE })
public SubsetSuggestionPage listSuggestions(@RequestParam(name = "f", required = false) String filterCode, final Pagination page,
@RequestBody(required = false) SubsetFilter filter) throws IOException, SearchException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, SubsetFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
FilterInfo<SubsetFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, SubsetFilter.class);
FilteredPage<Subset> pageRes = new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, subsetService.list(filterInfo.filter, page.toPageRequest(100, Sort.Direction.ASC, "id")));
Map<String, ElasticsearchService.TermResult> suggestionRes = subsetService.getSuggestions(filter);
return SubsetSuggestionPage.from(pageRes, suggestionRes);
}
/**
* My subsets.
*
......@@ -312,4 +342,18 @@ public class SubsetController extends ApiBaseController {
return subsetService.createNewVersion(subset);
}
public static class SubsetSuggestionPage {
@JsonUnwrapped
public FilteredPage<Subset> page;
public Map<String, ElasticsearchService.TermResult> suggestions;
private static SubsetSuggestionPage from(FilteredPage<Subset> page, Map<String, ElasticsearchService.TermResult> suggestions) {
SubsetSuggestionPage res = new SubsetSuggestionPage();
res.page = page;
res.suggestions = suggestions;
return res;
}
}
}
......@@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
......@@ -30,6 +31,7 @@ import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.model.impl.SubsetCreator;
import org.genesys2.server.service.filter.SubsetFilter;
import org.genesys2.server.service.impl.SearchException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
......@@ -117,6 +119,16 @@ public interface SubsetService {
*/
Subset getSubset(UUID uuid, int version);
/**
* Get terms by filter.
* @param filter the subset filter
*
* @return the term result
* @throws SearchException the search exception
* @throws IOException the IO exception
*/
Map<String, ElasticsearchService.TermResult> getSuggestions(SubsetFilter filter) throws SearchException, IOException;
/**
* Method updating Subset.
*
......
......@@ -22,8 +22,10 @@ import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
......@@ -32,6 +34,7 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.validation.Valid;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.catalog.model.Partner;
......@@ -336,6 +339,29 @@ public class SubsetServiceImpl implements SubsetService {
return subset;
}
/**
* {@inheritDoc}
*/
@Override
public Map<String, ElasticsearchService.TermResult> getSuggestions(SubsetFilter filter) throws SearchException, IOException {
Set<String> suggestions = Sets.newHashSet("crops");
Map<String, ElasticsearchService.TermResult> suggestionRes = new HashMap<>(suggestions.size());
for (String suggestionKey: suggestions) {
SubsetFilter suggestionFilter = filter.copy(SubsetFilter.class);
suggestionFilter.state(PublishState.PUBLISHED);
try {
suggestionFilter.clearFilter(suggestionKey);
} catch (NoSuchFieldException | IllegalAccessException e) {
LOG.error("Error while clearing filter: ", e.getMessage());
}
ElasticsearchService.TermResult suggestion = elasticsearchService.termStatisticsAuto(Subset.class, suggestionFilter, 100, suggestionKey);
suggestionRes.put(suggestionKey, suggestion);
}
return suggestionRes;
}
/**
* {@inheritDoc}
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment