Commit e4d7f563 authored by Matija Obreza's avatar Matija Obreza

Merge branch 'ui-264-api-for-autocomplete-filter'

* ui-264-api-for-autocomplete-filter:
  Implemented API endpoint and service method for AutocompleteFilter component
parents ae831885 27e16722
......@@ -51,6 +51,7 @@ import org.genesys2.server.service.ElasticsearchService.TermResult;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.filter.AccessionGeoFilter;
import org.genesys2.server.service.impl.GenesysFilterServiceImpl.LabelValue;
import org.genesys2.server.service.impl.SearchException;
import org.genesys2.server.service.worker.AccessionProcessor;
import org.slf4j.Logger;
......@@ -374,6 +375,20 @@ public class AccessionController {
return objectMapper.readValue(objectMapper.writeValueAsString(filter), AccessionFilter.class);
}
@GetMapping(value = "/autocomplete/{field:.+}", produces = MediaType.APPLICATION_JSON_VALUE)
public List<LabelValue<String>> autocomplete(@PathVariable("field") String field,
@RequestParam(value = "term", required = true) String term,
@RequestParam(name = "f", required = false) String filterCode, @RequestBody(required = false) AccessionFilter filter) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, AccessionFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
FilterInfo<AccessionFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, AccessionFilter.class);
return accessionService.autocomplete(filterInfo.filter, field, term);
}
@RequestMapping(value = "/downloadKml", produces = "application/vnd.google-earth.kml+xml", method = RequestMethod.POST)
public void downloadKml(@RequestParam(value = "f", required = false, defaultValue = "") String filterCode, @RequestParam(value="filter", required = false) AccessionFilter filter, HttpServletResponse response) throws IOException {
......
......@@ -31,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.GenesysFilterServiceImpl.LabelValue;
import org.genesys2.server.service.impl.SearchException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
......@@ -77,6 +78,15 @@ public interface AccessionService {
*/
Page<Accession> list(AccessionFilter filter, Pageable page);
/**
* Filtering autocompleter with suggestions
*
* @param filter currently applied accession filters
* @param field autocompleting field
* @param text typed text
* @return list of suggestions
*/
List<LabelValue<String>> autocomplete(AccessionFilter filter, String field, String text) throws IOException;
Map<String, ElasticsearchService.TermResult> getSuggestions(AccessionFilter filter) throws SearchException, IOException;
......
......@@ -55,8 +55,6 @@ public class AppliedFiltersConverter {
// These are renamed
case FilterConstants.CROPS:
return "crop.shortName";
case FilterConstants.INSTCODE:
return "institute.code";
case FilterConstants.INSTITUTE_COUNTRY_ISO2:
return "institute.country.code2";
case FilterConstants.INSTITUTE_COUNTRY_ISO3:
......@@ -69,7 +67,6 @@ public class AppliedFiltersConverter {
return "countryOfOrigin.region.isoCode";
case FilterConstants.TAXONOMY_SCINAME:
return "taxonomy.taxonName";
case FilterConstants.SGSV:
case FilterConstants.IN_SGSV:
return ACCEFILT_SGSV;
case FilterConstants.ART15:
......@@ -84,10 +81,12 @@ public class AppliedFiltersConverter {
case FilterConstants.DUPLSITE:
case FilterConstants.HISTORIC:
case FilterConstants.ID:
case FilterConstants.INSTCODE:
case FilterConstants.MLSSTATUS:
case FilterConstants.SAMPSTAT:
case FilterConstants.STORAGE:
case FilterConstants.SEQUENTIAL_NUMBER:
case FilterConstants.SGSV:
case FilterConstants.UUID:
case FilterConstants.GEO_ELEVATION:
case FilterConstants.GEO_LATITUDE:
......
......@@ -42,16 +42,20 @@ import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.SubsetService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.impl.GenesysFilterServiceImpl.LabelValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
......@@ -100,6 +104,12 @@ public class AccessionServiceImpl implements AccessionService {
@Autowired
private AuditTrailService auditService;
@Autowired
private InstituteService instituteService;
@Autowired
private TaxonomyService taxonomyService;
private static final int LOAD_CHUNK_SIZE = 200;
private <T extends AccessionData> T lazyLoad(T accession) {
......@@ -325,6 +335,60 @@ public class AccessionServiceImpl implements AccessionService {
return new PageImpl<>(content, page, total);
}
@Override
public List<LabelValue<String>> autocomplete(AccessionFilter filter, String field, String text) throws IOException {
AccessionFilter suggestionFilter = filter.copy(AccessionFilter.class);
try {
suggestionFilter.clearFilter(field);
} catch (NoSuchFieldException | IllegalAccessException e) {
LOG.error("Error while clearing filter: ", e.getMessage());
}
final List<LabelValue<String>> completed = new ArrayList<>();
if (FilterConstants.INSTCODE.equalsIgnoreCase(field)) {
final List<FaoInstitute> faoInst = instituteService.autocomplete(text);
ElasticsearchService.TermResult termResult = null;
try {
termResult = elasticsearchService.termStatisticsAuto(Accession.class, suggestionFilter, 20000, FilterConstants.INSTCODE);
} catch (SearchException e) {
LOG.error("Error occurred during search", e);
}
if (termResult != null && !termResult.getTerms().isEmpty()) {
for (FaoInstitute inst : faoInst) {
String label = inst.getCode() + ", " + inst.getFullName();
for (ElasticsearchService.Term term : termResult.getTerms()) {
if (term.getTerm().equalsIgnoreCase(inst.getCode()) && term.getCount() > 0) {
label = label.concat(" (" + term.getCount() + ")");
completed.add(new LabelValue<>(inst.getCode(), label));
break;
}
}
}
}
} else if (FilterConstants.TAXONOMY_GENUS.equalsIgnoreCase(field)) {
final List<String> genusList = taxonomyService.autocompleteGenus(text, null);
ElasticsearchService.TermResult termResult = null;
try {
termResult = elasticsearchService.termStatisticsAuto(Accession.class, suggestionFilter, 20000, FilterConstants.TAXONOMY_GENUS);
} catch (SearchException e) {
LOG.error("Error occurred during search", e);
}
if (termResult != null && !termResult.getTerms().isEmpty()) {
for (String genus : genusList) {
for (ElasticsearchService.Term term : termResult.getTerms()) {
if (term.getTerm().equalsIgnoreCase(genus) && term.getCount() > 0) {
completed.add(new LabelValue<>(genus, genus.concat(" (" + term.getCount() + ")")));
break;
}
}
}
}
}
return completed;
}
@Override
public Map<String, ElasticsearchService.TermResult> getSuggestions(AccessionFilter filter) throws SearchException, IOException {
Map<String, ElasticsearchService.TermResult> suggestionRes = new HashMap<>(suggestions.size());
......
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