Commit 29e31b83 authored by igoshin's avatar igoshin Committed by Matija Obreza

Filters with context

parent 8a047a50
......@@ -19,6 +19,7 @@ package org.genesys2.server.persistence.domain;
import java.util.List;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Crop;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
......@@ -28,9 +29,18 @@ public interface Taxonomy2Repository extends JpaRepository<Taxonomy2, Long> {
@Query("select distinct t.genus from Taxonomy2 t where t.genus like ?1")
List<String> autocompleteGenus(String term, Pageable page);
@Query("select distinct t.genus from Taxonomy2 t join t.cropTaxonomies ct where t.genus like ?1 and ct.crop = ?2")
List<String> autocompleteGenusByCrop(String term, Crop crop, Pageable page);
@Query("select distinct t.species from Taxonomy2 t where t.species like ?1")
List<String> autocompleteSpecies(String term, Pageable page);
@Query("select distinct t.species from Taxonomy2 t join t.cropTaxonomies ct where t.species like ?1 and ct.crop = ?2")
List<String> autocompleteSpeciesByCrop(String term, Crop crop, Pageable page);
@Query("select distinct t.species from Taxonomy2 t where t.species like ?1 and t.genus in (?2)")
List<String> autocompleteSpeciesByGenus(String term, List<String> genus, Pageable page);
@Query("select distinct t.taxonName from Taxonomy2 t where t.taxonName like ?1")
List<String> autocompleteTaxonomy(String term, Pageable page);
......
......@@ -50,7 +50,7 @@ public interface GenesysFilterService {
List<GenesysFilter> selectFilters(String[] selectedFilters);
List<LabelValue<String>> autocomplete(String filter, String ac);
List<LabelValue<String>> autocomplete(String filter, String ac,ObjectNode jsonTree);
void listGeo(ObjectNode jsonTree, Integer limit, RowCallbackHandler rowHandler);
......
......@@ -19,12 +19,13 @@ package org.genesys2.server.service;
import java.util.List;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Crop;
public interface TaxonomyService {
List<String> autocompleteGenus(String term);
List<String> autocompleteGenus(String term,Crop crop);
List<String> autocompleteSpecies(String term);
List<String> autocompleteSpecies(String term, Crop crop, List<String> genus);
List<String> autocompleteTaxonomy(String term);
......
......@@ -38,10 +38,12 @@ import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.genesys.TraitCode;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.domain.AccessionRepository;
import org.genesys2.server.persistence.domain.MethodRepository;
import org.genesys2.server.persistence.domain.TraitValueRepository;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.GenesysFilterService;
import org.genesys2.server.service.GenesysFilterService.GenesysFilter.DataType;
import org.genesys2.server.service.GenesysFilterService.GenesysFilter.FilterType;
......@@ -63,6 +65,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
@Service
......@@ -99,7 +102,10 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
@Autowired
private TaxonomyService taxonomyService;
@Autowired
@Autowired
private CropService cropService;
@Autowired
public void setDataSource(final DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
......@@ -237,10 +243,18 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
* Filtering autocompleter
*/
@Override
public List<LabelValue<String>> autocomplete(String filter, String ac) {
public List<LabelValue<String>> autocomplete(String filter, String ac,ObjectNode jsonTree) {
if (LOG.isDebugEnabled()) {
LOG.debug("Autocomplete " + filter + " ac=" + ac);
}
String shortName = null;
try {
shortName = jsonTree.get("crops").get(0).asText();
} catch (NullPointerException e) {
LOG.info("Crop not selected");
}
Crop crop = cropService.getCrop(shortName);
final List<LabelValue<String>> completed = new ArrayList<LabelValue<String>>();
if ("instCode".equalsIgnoreCase(filter)) {
......@@ -254,12 +268,24 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
completed.add(new LabelValue<String>(c.getCode3(), c.getCode3() + ", " + c.getName()));
}
} else if ("genus".equalsIgnoreCase(filter)) {
final List<String> genera = taxonomyService.autocompleteGenus(ac);
final List<String> genera = taxonomyService.autocompleteGenus(ac,crop);
for (final String value : genera) {
completed.add(new LabelValue<String>(value, value));
}
} else if ("species".equalsIgnoreCase(filter)) {
final List<String> species = taxonomyService.autocompleteSpecies(ac);
List<String> genus= new ArrayList<>();
ArrayNode arrayRoot = (ArrayNode) jsonTree.get("taxonomy.genus");
if (arrayRoot!=null){
Iterator<JsonNode> elements = arrayRoot.elements();
JsonNode element;
while(elements.hasNext()){
element = elements.next();
genus.add(element.asText());
}
}
final List<String> species = taxonomyService.autocompleteSpecies(ac,crop, genus);
for (final String value : species) {
completed.add(new LabelValue<String>(value, value));
}
......
......@@ -23,6 +23,7 @@ import org.apache.commons.lang3.text.WordUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.persistence.domain.Taxonomy2Repository;
import org.genesys2.server.service.TaxonomyService;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -44,15 +45,33 @@ public class TaxonomyServiceImpl implements TaxonomyService {
return taxonomy2Repository.findOne(id);
}
@Override
public List<String> autocompleteGenus(String term) {
return taxonomy2Repository.autocompleteGenus(term + "%", new PageRequest(0, 10));
}
@Override
public List<String> autocompleteSpecies(String term) {
return taxonomy2Repository.autocompleteSpecies(term + "%", new PageRequest(0, 10));
}
@Override
public List<String> autocompleteGenus(String term, Crop crop) {
List<String> strings;
if (crop != null) {
strings = taxonomy2Repository.autocompleteGenusByCrop(term + "%", crop, new PageRequest(0, 10));
} else {
strings = taxonomy2Repository.autocompleteGenus(term + "%", new PageRequest(0, 10));
}
return strings;
}
@Override
public List<String> autocompleteSpecies(String term, Crop crop, List<String> genus) {
List<String> strings;
if (!genus.isEmpty()) {
strings = taxonomy2Repository.autocompleteSpeciesByGenus(term + "%", genus, new PageRequest(0, 10));
} else if (crop != null) {
strings = taxonomy2Repository.autocompleteSpeciesByCrop(term + "%", crop, new PageRequest(0, 10));
} else {
strings = taxonomy2Repository.autocompleteSpecies(term + "%", new PageRequest(0, 10));
}
return strings;
}
@Override
public List<String> autocompleteTaxonomy(String term) {
......
......@@ -167,7 +167,7 @@ public class ExplorerController extends BaseController {
}
// TODO FIXME Remove unsupported keys
model.addAttribute("jsonFilter", jsonTree.toString());
} catch (final IOException e) {
......@@ -227,8 +227,15 @@ public class ExplorerController extends BaseController {
@RequestMapping(value = "/explore/ac/{field}", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<LabelValue<String>> autocomplete(@PathVariable("field") String filter, @RequestParam(value = "term", required = true) String ac) {
return filterService.autocomplete(filter, ac);
public List<LabelValue<String>> autocomplete(@PathVariable("field") String filter, @RequestParam(value = "term", required = true) String ac,
@RequestParam(value = "jsonFilter", required = false, defaultValue = "{}") String jsonFilter) {
ObjectNode jsonTree = null;
try {
jsonTree = (ObjectNode) mapper.readTree(jsonFilter);
} catch (final IOException e) {
_logger.error(e.getMessage(), e);
}
return filterService.autocomplete(filter, ac, jsonTree);
}
@RequestMapping(value = "/explore/map", method = RequestMethod.GET)
......@@ -363,20 +370,18 @@ public class ExplorerController extends BaseController {
if (_logger.isDebugEnabled())
_logger.debug("Changing color to " + color);
try {
final Color newColor = Color.decode(color);
if (newColor.equals(Color.yellow)) {
return imageBytes;
}
Color origColor = new Color(Color.yellow.getRed(), Color.yellow.getGreen(), Color.yellow.getBlue(), 170) ;
Color origColor = new Color(Color.yellow.getRed(), Color.yellow.getGreen(), Color.yellow.getBlue(), 170);
final int originalColor = origColor.getRGB();
Color alphaColor = new Color(newColor.getRed(), newColor.getGreen(), newColor.getBlue(), 170);
final int updatedColor = alphaColor.getRGB();
final MapColorsFilter mcf = new MapColorsFilter(originalColor, updatedColor);
final ByteArrayInputStream bios = new ByteArrayInputStream(imageBytes);
......
......@@ -414,7 +414,7 @@
$('input[i-key=' + normKey + value + ']').prop('checked', false);
});
GenesysFilterUtil.registerAutocomplete("#allfilters");
GenesysFilterUtil.registerAutocomplete("#allfilters",jsonData);
});
</script>
</content>
......
......@@ -351,12 +351,20 @@ GenesysFilter = {
GenesysFilterUtil = {
// Enable autocompleters
registerAutocomplete: function (container) {
registerAutocomplete: function (container,jsonData) {
$(container).find(".autocomplete-filter").each(function () {
var t = $(this);
console.log("Autocomplete " + t);
t.autocomplete({ delay: 200, minLength: 3, source: t.attr('x-source'), messages: { noResults: '', results: function () {
} } });
t.autocomplete({
delay: 200,
minLength: 3,
messages: { noResults: '', results: function () {
} } ,
source: function (request, response) {
$.get(t.attr('x-source'), { term: request.term ,jsonFilter:JSON.stringify(jsonData)}, function (data) {
response(data);
});
}});
});
},
//replace chars
......@@ -458,7 +466,7 @@ GenesysFilterUtil = {
success: function (data) {
jsonData[filter] = [];
$("#allfilters").append(data);
GenesysFilterUtil.registerAutocomplete("#allfilters");
GenesysFilterUtil.registerAutocomplete("#allfilters",jsonData);
GenesysFilterUtil.showFilter(filterId);
},
error: function (error) {
......
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