Commit 0f517feb authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza

Added suggestions for Overview and MapInfo

parent 2d9faed7
......@@ -126,7 +126,6 @@ public class AccessionController {
private final Set<String> terms = Sets.newHashSet("institute.code", "institute.country.code3", "cropName", "crop.shortName", "taxonomy.genus", "taxonomy.species",
"taxonomy.genusSpecies", "countryOfOrigin.code3", "sampStat", "available", "mlsStatus", "donorCode", "sgsv", "storage", "duplSite", "breederCode");
private final Set<String> suggestions = Sets.newHashSet("crop.shortName", "institute.code", "taxonomy.genus", "countryOfOrigin.code3", "historic", "sampStat", "available", "mlsStatus", "sgsv", "storage");
private ObjectMapper objectMapper = new ObjectMapper();
......@@ -198,7 +197,7 @@ public class AccessionController {
* @return the page
* @throws IOException
*/
@PostMapping(value = "/list-suggestions", produces = { MediaType.APPLICATION_JSON_VALUE })
@PostMapping(value = "/filter", produces = { MediaType.APPLICATION_JSON_VALUE })
@JsonView({ JsonViews.Public.class })
public AccessionSuggestionPage listSuggestions(@RequestParam(name = "f", required = false) String filterCode, final Pagination page,
@RequestBody(required = false) AccessionFilter filter) throws IOException, SearchException {
......@@ -211,19 +210,7 @@ public class AccessionController {
FilterInfo<AccessionFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, AccessionFilter.class);
FilteredPage<Accession> pageRes = new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, accessionService.list(filterInfo.filter, page.toPageRequest(100, Sort.Direction.ASC, "seqNo")));
Map<String, TermResult> suggestionRes = new HashMap<>(suggestions.size());
for (String suggestionKey: suggestions) {
AccessionFilter suggestionFilter = filter.copy(AccessionFilter.class);
try {
suggestionFilter.clearFilter(suggestionKey);
} catch (NoSuchFieldException | IllegalAccessException e) {
LOG.error("Error while clearing filter: ", e.getMessage());
}
TermResult suggestion = elasticsearchService.termStatisticsAuto(Accession.class, suggestionFilter, 100, suggestionKey);
suggestionRes.put(suggestionKey, suggestion);
}
Map<String, TermResult> suggestionRes = accessionService.getSuggestions(filter);
return AccessionSuggestionPage.from(pageRes, suggestionRes);
}
......@@ -249,8 +236,9 @@ public class AccessionController {
Map<String, TermResult> overview = elasticsearchService.termStatisticsAuto(Accession.class, filter, 10, terms.toArray(new String[] {}));
long accessionCount = accessionService.countAccessions(filter);
Map<String, TermResult> suggestions = accessionService.getSuggestions(filter);
return AccessionOverview.from(filterCode, filter, overview, accessionCount);
return AccessionOverview.from(filterCode, filter, overview, accessionCount, suggestions);
}
/**
......@@ -307,7 +295,7 @@ public class AccessionController {
@PostMapping(value = "/mapinfo", produces = MediaType.APPLICATION_JSON_VALUE)
@JsonView({ JsonViews.Public.class })
public AccessionMapInfo mapInfo(@RequestParam(value = "f", required = false) String filterCode, @RequestBody(required = false) AccessionFilter filter) throws IOException {
public AccessionMapInfo mapInfo(@RequestParam(value = "f", required = false) String filterCode, @RequestBody(required = false) AccessionFilter filter) throws IOException, SearchException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, AccessionFilter.class);
} else {
......@@ -325,6 +313,7 @@ public class AccessionController {
}
mapInfo.accessionCount = accessionService.countAccessions(filter);
mapInfo.tileServers = cdnServers;
mapInfo.suggestions= accessionService.getSuggestions(filter);
return mapInfo;
}
......@@ -381,64 +370,6 @@ public class AccessionController {
return geoJson;
}
/**
* Get term overview for filters
*
* @param filterCode short filter code
* @param filter the filter
* @return the page
* @throws SearchException
*/
@PostMapping(value = "/filter-suggest", produces = { MediaType.APPLICATION_JSON_VALUE })
@JsonView({ JsonViews.Public.class })
public Map<String, TermResult> filterSuggestions(@RequestParam(name = "f", required = false) String filterCode, @RequestBody(required = false) AccessionFilter filter)
throws IOException, SearchException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, AccessionFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
Map<String, TermResult> termResults = elasticsearchService.termStatisticsAuto(Accession.class, filter, 10, terms.toArray(new String[] {}));
// "institute.code", "institute.country.code3", "cropName", "crop.shortName",
// "taxonomy.genus", "taxonomy.genusSpecies", "taxonomy.species",
// "countryOfOrigin.code3", "sampStat", "available", "mlsStatus", "donorCode",
// "sgsv", "storage", "duplSite", "breederCode"
if (filter.taxonomy != null) {
if (CollectionUtil.isNotEmpty(filter.taxonomy.species)) {
AccessionFilter modifiedFilter = copyFilter(filter);
modifiedFilter.taxonomy.species.clear();
termResults.put("taxonomy.species", elasticsearchService.termStatisticsAuto(Accession.class, modifiedFilter, 10, "taxonomy.species"));
}
if (CollectionUtil.isNotEmpty(filter.taxonomy.genus)) {
AccessionFilter modifiedFilter = copyFilter(filter);
modifiedFilter.taxonomy.genus.clear();
termResults.put("taxonomy.genus", elasticsearchService.termStatisticsAuto(Accession.class, modifiedFilter, 10, "taxonomy.genus"));
}
}
if (filter.countryOfOrigin != null) {
AccessionFilter modifiedFilter = copyFilter(filter);
modifiedFilter.countryOfOrigin = null;
termResults.put("countryOfOrigin.code3", elasticsearchService.termStatisticsAuto(Accession.class, modifiedFilter, 10, "countryOfOrigin.code3"));
}
if (filter.institute != null) {
AccessionFilter modifiedFilter = copyFilter(filter);
modifiedFilter.institute = null;
termResults.putAll(elasticsearchService.termStatisticsAuto(Accession.class, modifiedFilter, 10, "institute.code", "institute.country.code3"));
}
if (filter.crop != null) {
AccessionFilter modifiedFilter = copyFilter(filter);
modifiedFilter.crop = null;
termResults.putAll(elasticsearchService.termStatisticsAuto(Accession.class, modifiedFilter, 10, "cropName", "crop.shortName"));
}
return termResults;
}
private AccessionFilter copyFilter(AccessionFilter filter) throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
return objectMapper.readValue(objectMapper.writeValueAsString(filter), AccessionFilter.class);
}
......@@ -591,6 +522,7 @@ public class AccessionController {
public Number[][] bounds;
public long accessionCount;
public String[] tileServers;
public Map<String, TermResult> suggestions;
}
public static class AccessionOverview {
......@@ -598,13 +530,15 @@ public class AccessionController {
public AccessionFilter filter;
public long accessionCount;
public Map<String, TermResult> overview;
public Map<String, TermResult> suggestions;
private static AccessionOverview from(String filterCode, AccessionFilter filter, Map<String, TermResult> overview, long accessionCount) {
private static AccessionOverview from(String filterCode, AccessionFilter filter, Map<String, TermResult> overview, long accessionCount, Map<String, TermResult> suggestions) {
AccessionOverview res = new AccessionOverview();
res.filterCode = filterCode;
res.filter = filter;
res.overview = overview;
res.accessionCount = accessionCount;
res.suggestions = suggestions;
return res;
}
......
......@@ -15,6 +15,7 @@
*/
package org.genesys2.server.service;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -30,6 +31,7 @@ import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.impl.SearchException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.genesys.blocks.auditlog.model.AuditLog;
......@@ -75,6 +77,9 @@ public interface AccessionService {
*/
Page<Accession> list(AccessionFilter filter, Pageable page);
Map<String, ElasticsearchService.TermResult> getSuggestions(AccessionFilter filter) throws SearchException, IOException;
/**
* Gets the geo bounds of maximum and minimum accession localities
*
......
......@@ -15,6 +15,7 @@
*/
package org.genesys2.server.service.impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
......@@ -27,6 +28,7 @@ import java.util.UUID;
import java.util.regex.Matcher;
import java.util.stream.Collectors;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.genesys.blocks.auditlog.service.AuditTrailService;
import org.genesys.catalog.model.dataset.Dataset;
......@@ -73,6 +75,8 @@ public class AccessionServiceImpl implements AccessionService {
public static final Logger LOG = LoggerFactory.getLogger(AccessionServiceImpl.class);
private final Set<String> suggestions = Sets.newHashSet("crop.shortName", "institute.code", "taxonomy.genus", "countryOfOrigin.code3", "historic", "sampStat", "available", "mlsStatus", "sgsv", "storage");
/** The accession repository. */
@Autowired
private AccessionRepository accessionRepository;
......@@ -321,6 +325,25 @@ public class AccessionServiceImpl implements AccessionService {
return new PageImpl<>(content, page, total);
}
@Override
public Map<String, ElasticsearchService.TermResult> getSuggestions(AccessionFilter filter) throws SearchException, IOException {
Map<String, ElasticsearchService.TermResult> suggestionRes = new HashMap<>(suggestions.size());
for (String suggestionKey: suggestions) {
AccessionFilter suggestionFilter = filter.copy(AccessionFilter.class);
try {
suggestionFilter.clearFilter(suggestionKey);
} catch (NoSuchFieldException | IllegalAccessException e) {
LOG.error("Error while clearing filter: ", e.getMessage());
}
ElasticsearchService.TermResult suggestion = elasticsearchService.termStatisticsAuto(Accession.class, suggestionFilter, 100, suggestionKey);
suggestionRes.put(suggestionKey, suggestion);
}
return suggestionRes;
}
@Override
public Number[][] getGeoBounds(AccessionFilter filter) {
final QAccession accession = QAccession.accession;
......
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