Commit d6e6dcbd authored by Matija Obreza's avatar Matija Obreza
Browse files

WebApiController injects baseUrl, client_id into genesys-webapi.js

parent c4aab051
......@@ -27,6 +27,7 @@ import org.genesys2.server.service.GenesysFilterService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.servlet.controller.rest.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
......@@ -34,36 +35,47 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
@Controller
@RequestMapping(value = { "/webapi/v0/acn" })
@RequestMapping(value = { "/webapi" })
public class WebApiController extends RestController {
private final ObjectMapper mapper = new ObjectMapper();
@Value("${base.url}")
private String baseUrl;
@Autowired
GenesysService genesysService;
@Autowired
private GenesysFilterService filterService;
@RequestMapping(value = "/filter", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(value = "/genesys-webapi.js", method = RequestMethod.GET)
public String getScript(ModelMap model, @RequestParam("client_id") String clientId,
@RequestParam(value = "client_secret", required = false) String clientSecret) {
model.put("baseUrl", baseUrl);
model.put("clientId", clientId);
model.put("clientSecret", clientSecret);
return "/webapi/genesys-webapi";
}
@RequestMapping(value = "/v0/acn/filter", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Page<AccessionJson> filterAccessions(@RequestBody JsonData jsonData) throws IOException {
String filter = "{\"crop\":[\"" + jsonData.crop + "\"]}";
ObjectNode jsonTree = null;
jsonTree = (ObjectNode) mapper.readTree(filter);
ObjectNode jsonTree = (ObjectNode) mapper.readTree(jsonData.filter);
LOG.debug(jsonTree.toString());
LOG.info("filteringAccessions: " + jsonTree.toString());
Pageable pageable = new PageRequest(jsonData.startAt - 1, Math.min(50, jsonData.maxRecords), new Sort("acceNumb"));
Page<Accession> accessions = filterService.listAccessions(jsonTree, pageable);
......@@ -76,9 +88,8 @@ public class WebApiController extends RestController {
}
public static class JsonData {
public String crop;
public String filter;
public Integer startAt;
public Integer maxRecords;
public String otherOptions;
}
}
<%@ page contentType="text/javascript;charset=UTF-8" pageEncoding="UTF-8" language="java" %>/**
* Copyright 2014 Global Crop Diversity Trust
*
* jQuery $.ajax() required.
*/
GenesysPGR = {
defaultOptions: {
startAt: 1,
maxRecords: 50,
success: new Function(),
error: new Function()
},
getUrl: function(apiCall) {
return '${baseUrl}/webapi' + apiCall + '?client_id=${clientId}&client_secret=${clientSecret}';
},
listAccessions: function (filter, opts) {
var o = $.extend({}, GenesysPGR.defaultOptions, opts);
var json={filter: JSON.stringify(filter), startAt: o.startAt, maxRecords: o.maxRecords };
$.ajax(GenesysPGR.getUrl('/v0/acn/filter'), {
dataType: 'json',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(json),
success: function (accessions) {
o.success(accessions);
},
error: function (errorAsync) {
o.error(errorAsync);
}
});
}
}
GenesysPrg = {
Accession: function (data) {
var json={crop: data.crop, startAt: data.startAt, maxRecords: data.maxRecords, other_option: data.other_option};
$.ajax('http://localhost:8080/webapi/v0/acn/filter', {
dataType: 'json',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(json),
success: function (accessions) {
data.success(accessions);
},
error: function (errorAsync) {
data.error(errorAsync);
}
});
}
}
var data = {
crop: this.crop,
startAt: this.startAt,
maxRecords: this.maxRecords,
other_option: this.other_option,
success: new Function(),
error: new Function()
}
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