Commit de940276 authored by Matija Obreza's avatar Matija Obreza

Merge branch '231-crop-page-as-json' into 'master'

Crop page as JSON

Closes #231

See merge request genesys-pgr/genesys-server!133
parents 24a68712 349bcbb0
......@@ -48,6 +48,12 @@ public interface AccessionRepository extends JpaRepository<Accession, Long>, Acc
@Query("select count(a) from Accession a where a.institute = ?1 and a.historic = false")
long countByInstitute(FaoInstitute institute);
/**
* Return the number of active ({@link Accession#historic} == false) accession records held at the institute with DOI
*/
@Query("select count(a) from Accession a where a.institute = ?1 and a.historic = false and a.doi is not null")
long countByInstituteWithDoi(FaoInstitute institute);
/**
* Return the number of active ({@link Accession#historic} == false) accession records originating from specified country
*/
......@@ -147,6 +153,9 @@ public interface AccessionRepository extends JpaRepository<Accession, Long>, Acc
@Query("select year(a.lastModifiedDate), month(a.lastModifiedDate), day(a.lastModifiedDate), count(a) from Accession a where a.institute = ?1 group by year(a.lastModifiedDate), month(a.lastModifiedDate), day(a.lastModifiedDate) order by year(a.lastModifiedDate) desc, month(a.lastModifiedDate) desc, day(a.lastModifiedDate) desc")
public List<Object[]> lastUpdatedStatistics(FaoInstitute faoInstitute);
@Query("select year(a.lastModifiedDate), count(a) from Accession a where a.institute = ?1 and a.historic = ?2 group by year(a.lastModifiedDate) order by year(a.lastModifiedDate) desc")
public List<Object[]> lastUpdatedStatistics(FaoInstitute faoInstitute, boolean historic);
public Accession findByDoi(String doi);
@Query("update Accession accession set accession.doi = null where accession.doi is not null")
......
......@@ -65,6 +65,12 @@ public interface GenesysService {
*/
long countByInstitute(FaoInstitute institute);
/**
* Return the number of active ({@link Accession#historic} == false)
* accession records with DOI
*/
long countByInstituteWithDoi(FaoInstitute institute);
/**
* Return the number of active ({@link Accession#historic} == false)
* accession records
......@@ -289,6 +295,8 @@ public interface GenesysService {
List<Object[]> getLastUpdatedStatistics();
List<Object[]> getLastUpdatedStatistics(FaoInstitute faoInstitute);
List<Object[]> getLastUpdatedStatistics(FaoInstitute faoInstitute, boolean historic);
Page<AccessionDetails> listAccessionsDetails(Collection<Long> accessionIds, Pageable page);
......
......@@ -258,6 +258,11 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
return accessionRepository.countByInstitute(institute);
}
@Override
public long countByInstituteWithDoi(final FaoInstitute institute) {
return accessionRepository.countByInstituteWithDoi(institute);
}
@Override
public long countByOrigin(Country country) {
return accessionRepository.countByOrigin(country);
......@@ -1903,5 +1908,11 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
public List<Object[]> getLastUpdatedStatistics(FaoInstitute faoInstitute) {
return accessionRepository.lastUpdatedStatistics(faoInstitute);
}
@Override
@Cacheable(value = "statistics", unless = "#result == null", key = "'stats.' + #root.methodName + '-' + #faoInstitute.code + '-' + #historic")
public List<Object[]> getLastUpdatedStatistics(FaoInstitute faoInstitute, boolean historic) {
return accessionRepository.lastUpdatedStatistics(faoInstitute, historic);
}
}
......@@ -23,6 +23,8 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.elasticsearch.common.lang3.StringUtils;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
......@@ -42,9 +44,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
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.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
......@@ -55,6 +59,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/c")
......@@ -91,6 +96,27 @@ public class CropController extends BaseController {
return "/crop/list";
}
@GetMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ObjectNode statistics() {
LOG.debug("Fetching statistics of crops.");
final ObjectMapper mapper = new ObjectMapper();
final ObjectNode mainNode = mapper.createObjectNode();
final ArrayNode arrayNode = mapper.createArrayNode();
final List<Crop> cropList = cropService.list(getLocale());
for (final Crop crop : cropList) {
final ObjectNode objectNode = mapper.createObjectNode();
objectNode.put("crop", crop.getShortName());
objectNode.put("count", genesysService.countByCrop(crop));
arrayNode.add(objectNode);
}
mainNode.set("crops", arrayNode);
mainNode.put("numberOfCountries", statisticsService.numberOfCountries());
mainNode.put("numberOfInstitutes", statisticsService.numberOfInstitutes());
mainNode.put("numberOfActiveAccessions", statisticsService.numberOfActiveAccessions());
return mainNode;
}
@RequestMapping("/{shortName}")
public String view(ModelMap model, @PathVariable(value = "shortName") String shortName) {
LOG.debug("Viewing crop {}", shortName);
......
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2017 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.
......@@ -12,7 +12,7 @@
* 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.servlet.controller;
......@@ -33,6 +33,9 @@ import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
......@@ -40,6 +43,7 @@ import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.model.genesys.PDCIStatistics;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.CRMException;
......@@ -195,6 +199,37 @@ public class WiewsController extends BaseController {
return "/wiews/details";
}
@GetMapping(value = "/{instCode}", produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody ObjectNode statistics(@PathVariable("instCode") String instCode) throws Exception {
LOG.info("Statistics information for {}", instCode);
final FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
throw new Exception("Institute with instCode=" + instCode + " doesn't exist.");
}
final PDCIStatistics pdciStatistics = statisticsService.statisticsPDCI(institute);
final List<Object[]> updatedStats = genesysService.getLastUpdatedStatistics(institute, false);
final Long activeAccessions = institute.getAccessionCount();
final Long activeAccessionsWithDoi = genesysService.countByInstituteWithDoi(institute);
final ObjectMapper mapper = new ObjectMapper();
final ObjectNode lastUpdatedNode = mapper.createObjectNode();
for (final Object[] object : updatedStats) {
final String year = object[0] != null ? object[0].toString() : "never";
final Long updates = (Long) object[1];
lastUpdatedNode.put(year, updates);
}
final ObjectNode objectNode = mapper.createObjectNode();
objectNode.put("instCode", instCode);
objectNode.put("active", true);
objectNode.put("count", activeAccessions);
objectNode.set("lastUpdated", lastUpdatedNode);
objectNode.put("averagePdci", pdciStatistics.getAvg());
objectNode.put("withDoi", activeAccessionsWithDoi);
return objectNode;
}
@RequestMapping(value = "/{wiewsCode}/stat-cropName", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
......
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