Commit 27155abc authored by Matija Obreza's avatar Matija Obreza

FIX: Duplicate handler for /api/v1/wiews/{code}

parent ef4dfe67
......@@ -16,8 +16,12 @@
package org.genesys2.server.api.v1;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import static org.springframework.context.i18n.LocaleContextHolder.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.service.ShortFilterService;
......@@ -26,13 +30,13 @@ import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.PDCIStatistics;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.filter.InstituteFilter;
import org.genesys2.server.service.*;
import org.genesys2.server.service.StatisticsService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.impl.FilterHandler;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.genesys2.server.service.filter.InstituteFilter;
import org.genesys2.server.service.impl.SearchException;
import org.genesys2.spring.ResourceNotFoundException;
import org.slf4j.Logger;
......@@ -42,15 +46,18 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonView;
import com.google.common.collect.Sets;
import static org.springframework.context.i18n.LocaleContextHolder.getLocale;
import io.swagger.annotations.Api;
/**
* Institute API v1
......@@ -85,19 +92,6 @@ public class InstituteController {
@Autowired
protected ShortFilterService shortFilterService;
/**
* Gets the institute
*
* @param code the code
* @return the institute
*/
@GetMapping(value = "/{code}", produces = { MediaType.APPLICATION_JSON_VALUE })
@JsonView({ JsonViews.Protected.class })
public FaoInstitute getByCode(@PathVariable("code") final String code) {
return instituteService.getInstitute(code);
}
/**
* List institutes by filterCode or filter
*
......@@ -127,8 +121,8 @@ public class InstituteController {
return new FilteredPage<>(filterCode, filter, instituteService.list(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort)));
}
@GetMapping(value ="/{wiewsCode}", produces = MediaType.APPLICATION_JSON_VALUE)
public InstituteDetails details(@PathVariable(value = "wiewsCode") String code) {
@GetMapping(value ="/{code:[A-Z]+[0-9]+}", produces = MediaType.APPLICATION_JSON_VALUE)
public InstituteDetails details(@PathVariable(value = "code") String code) {
FaoInstitute faoInstitute = instituteService.getInstitute(code);
......@@ -136,89 +130,38 @@ public class InstituteController {
throw new ResourceNotFoundException();
}
InstituteFilter instituteFilter = new InstituteFilter();
instituteFilter.code=Sets.newHashSet(faoInstitute.getCode());
AccessionFilter byInstituteFilter=new AccessionFilter();
byInstituteFilter.holder=instituteFilter;
InstituteDetails details = new InstituteDetails();
details.setInstitute(faoInstitute);
details.setBlurp(contentService.getArticle(faoInstitute, "blurp", getLocale()));
details.setPdciStats(statisticsService.statisticsPDCI(faoInstitute));
details.setLastUpdates(genesysService.getLastUpdatedStatistics(faoInstitute));
details.setOverview(getOverviewData(faoInstitute));
details.details = faoInstitute;
details.blurb = contentService.getArticle(faoInstitute, "blurp", getLocale());
details.pdciStats = statisticsService.statisticsPDCI(faoInstitute);
details.lastUpdates = genesysService.getLastUpdatedStatistics(faoInstitute);
details.overview= getOverviewData(byInstituteFilter);
return details;
}
private Map<String, ElasticsearchService.TermResult> getOverviewData(FaoInstitute faoInstitute) {
Map<String, ElasticsearchService.TermResult> stats = new HashMap<>();
AppliedFilter appliedFilter = new AppliedFilter();
appliedFilter.setFilterName(FilterConstants.INSTCODE);
appliedFilter.addFilterValue(new FilterHandler.LiteralValueFilter(faoInstitute.getCode()));
AppliedFilters instituteFilter = new AppliedFilters();
instituteFilter.add(appliedFilter);
private Map<String, ElasticsearchService.TermResult> getOverviewData(AccessionFilter byInstituteFilter) {
String[] terms = new String[] {"crop.shortName", "cropName", "taxonomy.genus", "taxonomy.genusSpecies"};
try {
stats.putAll(elasticsearchService.termStatisticsAuto(Accession.class, AccessionFilter.convert(instituteFilter), 10, terms));
return elasticsearchService.termStatisticsAuto(Accession.class, byInstituteFilter, 10, terms);
} catch (SearchException e) {
LOG.error("Error occurred during search", e);
return null;
}
return stats;
}
public static class InstituteDetails {
private FaoInstitute institute;
private Article blurp;
private PDCIStatistics pdciStats;
private List<Object[]> lastUpdates;
private Map<String, ElasticsearchService.TermResult> overview;
public FaoInstitute getInstitute() {
return institute;
}
public void setInstitute(FaoInstitute institute) {
this.institute = institute;
}
public Article getBlurp() {
return blurp;
}
public void setBlurp(Article blurp) {
this.blurp = blurp;
}
public PDCIStatistics getPdciStats() {
return pdciStats;
}
public void setPdciStats(PDCIStatistics pdciStats) {
this.pdciStats = pdciStats;
}
public List<Object[]> getLastUpdates() {
return lastUpdates;
}
public void setLastUpdates(List<Object[]> lastUpdates) {
this.lastUpdates = lastUpdates;
}
public Map<String, ElasticsearchService.TermResult> getOverview() {
return overview;
}
public void setOverview(Map<String, ElasticsearchService.TermResult> overview) {
this.overview = overview;
}
public FaoInstitute details;
public Article blurb;
public PDCIStatistics pdciStats;
public List<Object[]> lastUpdates;
public Map<String, ElasticsearchService.TermResult> overview;
}
}
......@@ -40,6 +40,7 @@ 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.genesys2.server.service.filter.AccessionFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -208,6 +209,10 @@ public class FilterHandler {
String getType();
}
/**
* This is the original Genesys filters implementation.
* @deprecated Use {@link AccessionFilter} instead.
*/
@JsonSerialize(using = AppliedFilters.Serializer.class)
@JsonDeserialize(using = AppliedFilters.Deserializer.class)
public static class AppliedFilters extends ArrayList<AppliedFilter> {
......
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