Skip to content
Snippets Groups Projects
Commit 8cb1d332 authored by Mirco Nasuti's avatar Mirco Nasuti
Browse files

add new version of request API (without data for now)

parent 19616828
No related branches found
No related tags found
No related merge requests found
......@@ -4,16 +4,27 @@
package eu.hbp.mip.controllers;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import eu.hbp.mip.model.Dataset;
import eu.hbp.mip.model.Query;
import io.swagger.annotations.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
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.RestController;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@RestController
......@@ -23,16 +34,90 @@ public class RequestsApi {
private static final Logger LOGGER = Logger.getLogger(RequestsApi.class);
@Autowired
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@ApiOperation(value = "Post a request", response = Dataset.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success") })
@RequestMapping(method = RequestMethod.POST)
@Deprecated
public ResponseEntity<Dataset> postRequests(
public ResponseEntity<Object> postRequests(
@RequestBody @ApiParam(value = "Query to process", required = true) Query query
) {
LOGGER.info("Post a request");
return ResponseEntity.ok(null); // TODO: Get data from second datasource
JsonObject dataset = new JsonObject();
String code = generateDSCode(query);
Date date = new Date();
List<String> variables = new LinkedList<>();
List<String> groupings = new LinkedList<>();
List<String> covariables = new LinkedList<>();
Map<String, LinkedList<Object>> data = new HashMap<>();
Gson gson = new Gson();
JsonObject q = gson.fromJson(gson.toJson(query, Query.class), JsonObject.class);
JsonArray queryVars = q.getAsJsonArray("variables");
JsonArray queryGrps = q.getAsJsonArray("grouping");
JsonArray queryCoVars = q.getAsJsonArray("covariables");
List<String> allVars = new LinkedList<>();
for (JsonElement var : queryVars) {
String varCode = var.getAsJsonObject().get("code").getAsString();
variables.add(varCode);
allVars.add(varCode);
}
for (JsonElement var : queryGrps) {
String varCode = var.getAsJsonObject().get("code").getAsString();
groupings.add(varCode);
allVars.add(varCode);
}
for (JsonElement var : queryCoVars) {
String varCode = var.getAsJsonObject().get("code").getAsString();
covariables.add(varCode);
allVars.add(varCode);
}
for(String varCode : allVars)
{
String sqlQuery = "SELECT " + varCode + " FROM adni_merge";
for (Map resultMap : jdbcTemplate.queryForList(sqlQuery))
{
resultMap.get(varCode);
}
}
dataset.addProperty("code", code);
dataset.addProperty("date", date.getTime());
dataset.add("variable", gson.toJsonTree(variables));
dataset.add("grouping", gson.toJsonTree(groupings));
dataset.add("header", gson.toJsonTree(covariables));
dataset.add("data", new Gson().toJsonTree(data));
return ResponseEntity.ok(new Gson().fromJson(dataset, Object.class));
}
private String generateDSCode(Query query) {
String prefix = "DS";
String queryStr = Integer.toString(query.hashCode());
Pattern p = Pattern.compile("@(\\w+)");
Matcher m = p.matcher(queryStr);
String memId;
if (m.find()) {
memId = m.group(1);
} else {
memId = Long.toString(new Date().getTime()); // "This should never happen"
}
return prefix + memId;
}
}
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