Commit a4d50529 authored by Maxym Borodenko's avatar Maxym Borodenko

Crop page as JSON

parent 24a68712
......@@ -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);
......
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