Commit 7e1592a1 authored by Alexander Prendetskiy's avatar Alexander Prendetskiy Committed by Matija Obreza

Add a new API controller v1.InstituteController

parent d0f4c654
......@@ -22,9 +22,21 @@ import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys2.server.api.ApiBaseController;
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.InstituteService;
import org.genesys2.server.service.filter.InstituteFilter;
import org.genesys2.server.service.*;
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.impl.SearchException;
import org.genesys2.spring.ResourceNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
......@@ -34,6 +46,12 @@ import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.springframework.context.i18n.LocaleContextHolder.getLocale;
/**
* Institute API v1
*/
......@@ -46,9 +64,23 @@ public class InstituteController {
/** The Constant API_BASE. */
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/wiews";
private static final Logger LOG = LoggerFactory.getLogger(InstituteController.class);
@Autowired
private InstituteService instituteService;
@Autowired
private ElasticsearchService elasticsearchService;
@Autowired
private GenesysService genesysService;
@Autowired
private StatisticsService statisticsService;
@Autowired
private ContentService contentService;
/** The short filter service. */
@Autowired
protected ShortFilterService shortFilterService;
......@@ -95,4 +127,98 @@ 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) {
FaoInstitute faoInstitute = instituteService.getInstitute(code);
if (faoInstitute == null) {
throw new ResourceNotFoundException();
}
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));
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);
String[] terms = new String[] {"crop.shortName", "cropName", "taxonomy.genus", "taxonomy.genusSpecies"};
try {
stats.putAll(elasticsearchService.termStatisticsAuto(Accession.class, AccessionFilter.convert(instituteFilter), 10, terms));
} catch (SearchException e) {
LOG.error("Error occurred during search", e);
}
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;
}
}
}
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