Commit 9c2de3eb authored by Matija Obreza's avatar Matija Obreza

Extracted TraitServiceImpl from GenesysServiceImpl

Introduced FilterHandler component
parent b2d9a268
......@@ -18,7 +18,6 @@ package org.genesys2.server.service;
import java.util.List;
import org.genesys2.server.model.filters.GenesysFilter;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.service.impl.GenesysFilterServiceImpl.LabelValue;
import org.springframework.data.domain.Page;
......@@ -29,15 +28,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
public interface GenesysFilterService {
String[] DEFAULT_FILTERS = { "crop", "sampStat", "genus", "taxon", "orgCty", "latitude", "longitude", "organization", "instCode", "acceNumb", "available",
"mlsStat", "inTrust", "inSvalbard" };
Page<Accession> listAccessions(ObjectNode jsonTree, Pageable pageable);
List<GenesysFilter> listAvailableFilters();
List<GenesysFilter> selectFilters(String[] selectedFilters);
List<LabelValue<String>> autocomplete(String filter, String ac,ObjectNode jsonTree);
void listGeo(ObjectNode jsonTree, Integer limit, RowCallbackHandler rowHandler);
......
......@@ -88,8 +88,6 @@ public interface GenesysService {
Page<Accession> listMetadataAccessions(long metadataId, Pageable pageable);
List<Method> listMethods(Accession accession);
Map<Long, List<ExperimentTrait>> getAccessionTraitValues(Accession accession);
Map<Long, Map<Long, List<Object>>> getMetadataTraitValues(Metadata metadata, List<Accession> content);
......
......@@ -19,6 +19,7 @@ package org.genesys2.server.service;
import java.util.List;
import java.util.Map;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.genesys.Parameter;
......@@ -141,4 +142,6 @@ public interface TraitService {
Map<Long, List<Method>> mapMethods(Crop crop);
List<Method> listMethods(Accession accession);
}
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.service.impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.filters.AutocompleteFilter;
import org.genesys2.server.model.filters.BasicFilter;
import org.genesys2.server.model.filters.CodedMethodFilter;
import org.genesys2.server.model.filters.GenesysFilter;
import org.genesys2.server.model.filters.GenesysFilter.DataType;
import org.genesys2.server.model.filters.GenesysFilter.FilterType;
import org.genesys2.server.model.filters.I18nListFilter;
import org.genesys2.server.model.filters.MethodFilter;
import org.genesys2.server.model.filters.ValueName;
import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.genesys.TraitCode;
import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.TraitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Converts filter requests (usually JSON) to internal data structures
*
* @author matijaobreza
*
*/
@Component
public class FilterHandler {
private static final Log LOG = LogFactory.getLog(FilterHandler.class);
@Autowired
private TraitService traitService;
@Autowired
private ObjectMapper objectMapper;
private final ArrayList<GenesysFilter> availableFilters;
public FilterHandler() {
this.availableFilters = new ArrayList<GenesysFilter>();
this.availableFilters.add(new BasicFilter(FilterConstants.CROPS, DataType.STRING));
this.availableFilters.add(new I18nListFilter<Integer>(FilterConstants.SAMPSTAT, DataType.NUMERIC).build("accession.sampleStatus", new Integer[] { 100,
110, 120, 130, 200, 300, 400, 410, 411, 412, 413, 414, 415, 416, 420, 421, 422, 423, 500, 600, 999 }));
this.availableFilters.add(new AutocompleteFilter(FilterConstants.TAXONOMY_GENUS, "/explore/ac/genus"));
this.availableFilters.add(new AutocompleteFilter(FilterConstants.TAXONOMY_SPECIES, "/explore/ac/species"));
this.availableFilters.add(new AutocompleteFilter(FilterConstants.TAXONOMY_SCINAME, "/explore/ac/taxonomy"));
this.availableFilters.add(new AutocompleteFilter(FilterConstants.ORGCTY_ISO3, "/explore/ac/country"));
this.availableFilters.add(new BasicFilter(FilterConstants.GEO_LATITUDE, DataType.NUMERIC));
this.availableFilters.add(new BasicFilter(FilterConstants.GEO_LONGITUDE, DataType.NUMERIC));
this.availableFilters.add(new BasicFilter(FilterConstants.GEO_ELEVATION, DataType.NUMERIC));
this.availableFilters.add(new BasicFilter(FilterConstants.ORGANIZATION, DataType.STRING));
this.availableFilters.add(new AutocompleteFilter(FilterConstants.INSTCODE, "/explore/ac/instCode"));
this.availableFilters.add(new BasicFilter(FilterConstants.ACCENUMB, DataType.STRING, FilterType.RANGE));
this.availableFilters.add(new BasicFilter(FilterConstants.ALIAS, DataType.STRING, FilterType.RANGE));
this.availableFilters.add(new BasicFilter(FilterConstants.SGSV, DataType.BOOLEAN));
this.availableFilters.add(new BasicFilter(FilterConstants.MLSSTATUS, DataType.BOOLEAN));
this.availableFilters.add(new BasicFilter(FilterConstants.ART15, DataType.BOOLEAN));
this.availableFilters.add(new BasicFilter(FilterConstants.AVAILABLE, DataType.BOOLEAN));
this.availableFilters.add(new BasicFilter(FilterConstants.COLLMISSID, DataType.STRING));
this.availableFilters.add(new I18nListFilter<Integer>(FilterConstants.STORAGE, DataType.NUMERIC).build("accession.storage", new Integer[] { 10, 11, 12,
13, 20, 30, 40, 50, 99 }));
}
public List<GenesysFilter> listAvailableFilters() {
return Collections.unmodifiableList(this.availableFilters);
}
public List<GenesysFilter> selectFilters(String[] selectedFilters) {
LOG.debug("Loading filter definitions sel=" + ArrayUtils.toString(selectedFilters));
final List<GenesysFilter> filters = new ArrayList<GenesysFilter>();
for (final String selectedFilter : selectedFilters) {
if (selectedFilter.startsWith("gm:")) {
try {
final GenesysFilter methodFilter = getMethodFilter(Long.parseLong(selectedFilter.substring(3)));
if (methodFilter != null) {
filters.add(methodFilter);
}
} catch (NumberFormatException | NullPointerException e) {
LOG.warn(e);
}
} else {
final GenesysFilter coreFilter = CollectionUtils.find(this.availableFilters, new Predicate<GenesysFilter>() {
@Override
public boolean evaluate(GenesysFilter object) {
return object.getKey().equals(selectedFilter);
}
});
if (coreFilter != null) {
filters.add(coreFilter);
}
}
}
return filters;
}
GenesysFilter getMethodFilter(long methodId) {
final Method method = traitService.getMethod(methodId);
return method == null ? null : toFilter(method);
}
private GenesysFilter toFilter(Method method) {
DataType dataType = null;
switch (method.getFieldType()) {
case 1:
case 2:
dataType = DataType.NUMERIC;
break;
case 0:
dataType = DataType.STRING;
}
GenesysFilter filter = null;
if (method.isCoded()) {
final Map<String, Long> stats = traitService.getMethodStatistics(method);
final List<ValueName<?>> options = new ArrayList<ValueName<?>>();
for (final TraitCode traitCode : TraitCode.parseOptions(method.getOptions())) {
options.add(new ValueName<String>(traitCode.getCode(), traitCode.getValue(), stats.get(traitCode.getCode())));
}
filter = new CodedMethodFilter("gm:" + method.getId(), method.getParameter().getTitle(), dataType, options);
} else {
filter = new MethodFilter("gm:" + method.getId(), method.getParameter().getTitle(), dataType);
}
return filter;
}
public List<AppliedFilter> fromJSON(String jsonFilter) throws IOException {
Map<String, List<String>> filters = objectMapper.readValue(jsonFilter, Map.class);
List<AppliedFilter> appliedFilters = new ArrayList<AppliedFilter>();
for (String key : filters.keySet()) {
List<String> filterValues = filters.get(key);
if (filterValues == null || filterValues.isEmpty())
continue;
LOG.info("key=" + key);
}
return appliedFilters;
}
private static class AppliedFilter {
}
}
......@@ -79,12 +79,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
@Transactional(readOnly = true)
public class GenesysFilterServiceImpl implements GenesysFilterService {
private static final Log LOG = LogFactory.getLog(GenesysFilterServiceImpl.class);
private final ArrayList<GenesysFilter> availableFilters;
@Autowired
private TraitValueRepository traitValueRepository;
......@@ -119,101 +115,6 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public GenesysFilterServiceImpl() {
this.availableFilters = new ArrayList<GenesysFilter>();
this.availableFilters.add(new BasicFilter(FilterConstants.CROPS, DataType.STRING));
this.availableFilters.add(new I18nListFilter<Integer>(FilterConstants.SAMPSTAT, DataType.NUMERIC).build("accession.sampleStatus", new Integer[] { 100,
110, 120, 130, 200, 300, 400, 410, 411, 412, 413, 414, 415, 416, 420, 421, 422, 423, 500, 600, 999 }));
this.availableFilters.add(new AutocompleteFilter(FilterConstants.TAXONOMY_GENUS, "/explore/ac/genus"));
this.availableFilters.add(new AutocompleteFilter(FilterConstants.TAXONOMY_SPECIES, "/explore/ac/species"));
this.availableFilters.add(new AutocompleteFilter(FilterConstants.TAXONOMY_SCINAME, "/explore/ac/taxonomy"));
this.availableFilters.add(new AutocompleteFilter(FilterConstants.ORGCTY_ISO3, "/explore/ac/country"));
this.availableFilters.add(new BasicFilter(FilterConstants.GEO_LATITUDE, DataType.NUMERIC));
this.availableFilters.add(new BasicFilter(FilterConstants.GEO_LONGITUDE, DataType.NUMERIC));
this.availableFilters.add(new BasicFilter(FilterConstants.GEO_ELEVATION, DataType.NUMERIC));
this.availableFilters.add(new BasicFilter(FilterConstants.ORGANIZATION, DataType.STRING));
this.availableFilters.add(new AutocompleteFilter(FilterConstants.INSTCODE, "/explore/ac/instCode"));
this.availableFilters.add(new BasicFilter(FilterConstants.ACCENUMB, DataType.STRING, FilterType.RANGE));
this.availableFilters.add(new BasicFilter(FilterConstants.ALIAS, DataType.STRING, FilterType.RANGE));
this.availableFilters.add(new BasicFilter(FilterConstants.SGSV, DataType.BOOLEAN));
this.availableFilters.add(new BasicFilter(FilterConstants.MLSSTATUS, DataType.BOOLEAN));
this.availableFilters.add(new BasicFilter(FilterConstants.ART15, DataType.BOOLEAN));
this.availableFilters.add(new BasicFilter(FilterConstants.AVAILABLE, DataType.BOOLEAN));
this.availableFilters.add(new BasicFilter(FilterConstants.COLLMISSID, DataType.STRING));
this.availableFilters.add(new I18nListFilter<Integer>(FilterConstants.STORAGE, DataType.NUMERIC).build("accession.storage", new Integer[] { 10, 11, 12,
13, 20, 30, 40, 50, 99 }));
}
@Override
public List<GenesysFilter> listAvailableFilters() {
return Collections.unmodifiableList(this.availableFilters);
}
@Override
public List<GenesysFilter> selectFilters(String[] selectedFilters) {
LOG.debug("Loading filter definitions sel=" + ArrayUtils.toString(selectedFilters));
final List<GenesysFilter> filters = new ArrayList<GenesysFilter>();
for (final String selectedFilter : selectedFilters) {
if (selectedFilter.startsWith("gm:")) {
try {
final GenesysFilter methodFilter = getMethodFilter(Long.parseLong(selectedFilter.substring(3)));
if (methodFilter != null) {
filters.add(methodFilter);
}
} catch (NumberFormatException | NullPointerException e) {
LOG.warn(e);
}
} else {
final GenesysFilter coreFilter = CollectionUtils.find(this.availableFilters, new Predicate<GenesysFilter>() {
@Override
public boolean evaluate(GenesysFilter object) {
return object.getKey().equals(selectedFilter);
}
});
if (coreFilter != null) {
filters.add(coreFilter);
}
}
}
return filters;
}
GenesysFilter getMethodFilter(long methodId) {
final Method method = methodRepository.findOne(methodId);
return method == null ? null : toFilter(method);
}
private GenesysFilter toFilter(Method method) {
DataType dataType = null;
switch (method.getFieldType()) {
case 1:
case 2:
dataType = DataType.NUMERIC;
break;
case 0:
dataType = DataType.STRING;
}
GenesysFilter filter = null;
if (method.isCoded()) {
final Map<String, Long> stats = traitService.getMethodStatistics(method);
final List<ValueName<?>> options = new ArrayList<ValueName<?>>();
for (final TraitCode traitCode : TraitCode.parseOptions(method.getOptions())) {
options.add(new ValueName<String>(traitCode.getCode(), traitCode.getValue(), stats.get(traitCode.getCode())));
}
filter = new CodedMethodFilter("gm:" + method.getId(), method.getParameter().getTitle(), dataType, options);
} else {
filter = new MethodFilter("gm:" + method.getId(), method.getParameter().getTitle(), dataType);
}
return filter;
}
@Override
public Page<Accession> listAccessions(ObjectNode jsonTree, Pageable pageable) {
final Iterator<Entry<String, JsonNode>> fields = jsonTree.fields();
......
......@@ -26,6 +26,7 @@ import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.TraitService;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -44,6 +45,9 @@ public class AccessionController extends BaseController {
@Autowired
private GenesysService genesysService;
@Autowired
private TraitService traitService;
@Autowired
private TaxonomyService taxonomyService;
......@@ -70,7 +74,7 @@ public class AccessionController extends BaseController {
model.addAttribute("accessionRemarks", genesysService.listAccessionRemarks(accession));
model.addAttribute("metadatas", genesysService.listMetadata(accession));
model.addAttribute("methods", genesysService.listMethods(accession));
model.addAttribute("methods", traitService.listMethods(accession));
model.addAttribute("methodValues", genesysService.getAccessionTraitValues(accession));
model.addAttribute("crops", cropService.getCrops(accession.getTaxonomy()));
......
......@@ -44,6 +44,7 @@ import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.MappingService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.TraitService;
import org.genesys2.server.service.impl.FilterHandler;
import org.genesys2.server.service.impl.GenesysFilterServiceImpl.LabelValue;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -87,6 +88,9 @@ public class ExplorerController extends BaseController {
@Autowired
private MappingService mappingService;
@Autowired
private FilterHandler filterHandler;
private final ObjectMapper mapper = new ObjectMapper();
......@@ -151,8 +155,8 @@ public class ExplorerController extends BaseController {
_logger.error("Invalid JSON for filters", e);
}
final List<GenesysFilter> currentFilters = filterService.selectFilters(selectedFilters);
final List<GenesysFilter> availableFilters = filterService.listAvailableFilters();
final List<GenesysFilter> currentFilters = filterHandler.selectFilters(selectedFilters);
final List<GenesysFilter> availableFilters = filterHandler.listAvailableFilters();
_logger.debug("Filtering by: " + jsonFilter);
ObjectNode jsonTree = null;
......@@ -191,7 +195,7 @@ public class ExplorerController extends BaseController {
public String getAdditionalFilters(ModelMap model, @RequestParam(value = "filter", required = true, defaultValue = "") String[] selectedFilters)
throws IOException {
final List<GenesysFilter> additionalFilters = filterService.selectFilters(selectedFilters);
final List<GenesysFilter> additionalFilters = filterHandler.selectFilters(selectedFilters);
model.addAttribute("additionalFilters", additionalFilters);
if (ArrayUtils.contains(selectedFilters, "crops")) {
......
......@@ -21,13 +21,12 @@ import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.TraitService;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
......@@ -52,6 +51,10 @@ public class AccessionControllerRdf {
@Autowired
private CropService cropService;
@Autowired
private TraitService traitService;
@RequestMapping(value = "/id/{accessionId}", method = RequestMethod.GET)
public String viewTurtle(ModelMap model, @PathVariable(value = "accessionId") long accessionId) {
......@@ -68,7 +71,7 @@ public class AccessionControllerRdf {
model.addAttribute("accessionGeo", genesysService.listAccessionGeo(accession));
model.addAttribute("metadatas", genesysService.listMetadata(accession));
model.addAttribute("methods", genesysService.listMethods(accession));
model.addAttribute("methods", traitService.listMethods(accession));
model.addAttribute("methodValues", genesysService.getAccessionTraitValues(accession));
model.addAttribute("crops", cropService.getCrops(accession.getTaxonomy()));
......
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