Commit b245a374 authored by Maxym Borodenko's avatar Maxym Borodenko

Improved autocompleter for countries

parent 2ed3df6a
......@@ -286,4 +286,29 @@ public class Country extends BasicModel {
public void setMaxLatitude(final Double maxLatitude) {
this.maxLatitude = maxLatitude;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Country)) return false;
if (!super.equals(o)) return false;
Country country = (Country) o;
if (!code3.equals(country.code3)) return false;
if (!code2.equals(country.code2)) return false;
if (!name.equals(country.name)) return false;
return nameL.equals(country.nameL);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((code3 == null) ? 0 : code3.hashCode());
result = prime * result + ((code2 == null) ? 0 : code2.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((nameL == null) ? 0 : nameL.hashCode());
return result;
}
}
......@@ -434,7 +434,7 @@ public class AccessionServiceImpl implements AccessionService {
LOG.error("Error occurred during search", e);
}
completed.addAll(getCountrySuggestions(termResult, countries));
completed.addAll(getFirst10CountrySuggestions(termResult, countries));
} else if (AppliedFiltersConverter.convertTerm(FilterConstants.INSTITUTE_COUNTRY_ISO3).equalsIgnoreCase(field)) {
final List<Country> countries = geoService.autocomplete(text);
try {
......@@ -443,12 +443,12 @@ public class AccessionServiceImpl implements AccessionService {
LOG.error("Error occurred during search", e);
}
completed.addAll(getCountrySuggestions(termResult, countries));
completed.addAll(getFirst10CountrySuggestions(termResult, countries));
}
return completed;
}
private List<LabelValue<String>> getCountrySuggestions(ElasticsearchService.TermResult termResult, List<Country> countries) {
private List<LabelValue<String>> getFirst10CountrySuggestions(ElasticsearchService.TermResult termResult, List<Country> countries) {
final List<LabelValue<String>> result = new ArrayList<>();
if (termResult != null && !termResult.getTerms().isEmpty()) {
for (Country c : countries) {
......@@ -456,6 +456,9 @@ public class AccessionServiceImpl implements AccessionService {
LabelValue<String> suggestion = getSuggestion(termResult.getTerms(), label, c.getCode3());
if (suggestion != null) {
result.add(suggestion);
if (result.size() == 10) {
break;
}
}
}
}
......
......@@ -16,17 +16,18 @@
package org.genesys2.server.service.impl;
import static org.genesys2.server.model.impl.QCountry.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
......@@ -34,7 +35,7 @@ import java.util.stream.Collectors;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import net.sf.oval.constraint.exclusion.Nullable;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys2.server.exception.NotFoundElement;
......@@ -67,7 +68,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
......@@ -537,7 +537,14 @@ public class GeoServiceImpl implements GeoService {
@Override
public List<Country> autocomplete(String term) {
return countryRepository.autocomplete("%" + term + "%", new PageRequest(0, 10));
Set<Country> countries = new LinkedHashSet<>();
Predicate predicate = country.code3.containsIgnoreCase(term)
.or(country.code2.containsIgnoreCase(term))
.or(country.name.containsIgnoreCase(term));
CollectionUtils.addAll(countries, countryRepository.findAll(predicate).iterator());
CollectionUtils.addAll(countries, countryRepository.findAll(country.nameL.containsIgnoreCase(term)).iterator());
return new ArrayList<>(countries);
}
@Override
......
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