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

variables metadata into meta-db

parent a1d62786
No related branches found
No related tags found
No related merge requests found
......@@ -4,4 +4,3 @@ target/
.DS_Store
.m2
*.jar
src/main/resources/data/
......@@ -47,6 +47,13 @@ public class PersistenceConfiguration {
return new JdbcTemplate(dataSource());
}
@Bean
@Autowired
@Qualifier("jdbcTemplateMeta")
public JdbcTemplate jdbcTemplateMeta() {
return new JdbcTemplate(metaDataSource());
}
@Bean(name = "metaEntityManagerFactory")
@DependsOn("flyway")
public LocalContainerEntityManagerFactoryBean metaEntityManagerFactory() {
......
......@@ -7,22 +7,22 @@ package eu.hbp.mip.controllers;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import eu.hbp.mip.model.Group;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.log4j.Logger;
import org.postgresql.util.PGobject;
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.jdbc.support.rowset.SqlRowSet;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@RestController
......@@ -32,10 +32,12 @@ public class GroupsApi {
private static final Logger LOGGER = Logger.getLogger(GroupsApi.class);
private static final String VARIABLES_FILE = "data/variables.json";
private static String groups;
@Autowired
@Qualifier("jdbcTemplateMeta")
private JdbcTemplate jdbcTemplateMeta;
@ApiOperation(value = "Get the root group (containing all subgroups)", response = Group.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success") })
......@@ -48,20 +50,22 @@ public class GroupsApi {
return ResponseEntity.ok(new Gson().fromJson(groups, Object.class));
}
private static void loadGroups() {
private void loadGroups() {
if(groups == null)
{
InputStream is = VariablesApi.class.getClassLoader().getResourceAsStream(VARIABLES_FILE);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String sqlQuery = "SELECT * FROM meta_variables";
SqlRowSet data = jdbcTemplateMeta.queryForRowSet(sqlQuery);
data.next();
String json = ((PGobject) data.getObject("hierarchy")).getValue();
JsonObject root = new Gson().fromJson(json, JsonObject.class);
JsonObject root = new Gson().fromJson(new JsonReader(br), JsonObject.class);
removeVariablesRecursive(root);
groups = new Gson().toJson(root);
}
}
private static void removeVariablesRecursive(JsonObject element) {
private void removeVariablesRecursive(JsonObject element) {
if (element.has("groups")){
for(JsonElement child : element.getAsJsonArray("groups")) {
removeVariablesRecursive(child.getAsJsonObject());
......
......@@ -9,16 +9,16 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import eu.hbp.mip.model.Variable;
import io.swagger.annotations.*;
import org.apache.log4j.Logger;
import org.postgresql.util.PGobject;
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.jdbc.support.rowset.SqlRowSet;
import org.springframework.web.bind.annotation.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
......@@ -31,10 +31,12 @@ public class VariablesApi {
private static final Logger LOGGER = Logger.getLogger(VariablesApi.class);
private static final String VARIABLES_FILE = "data/variables.json";
private static LinkedList<String> variables;
@Autowired
@Qualifier("jdbcTemplateMeta")
private JdbcTemplate jdbcTemplateMeta;
@ApiOperation(value = "Get variables", response = List.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success") })
......@@ -123,31 +125,33 @@ public class VariablesApi {
) {
LOGGER.info("Get groups and variables hierarchy");
InputStream is = Variable.class.getClassLoader().getResourceAsStream(VARIABLES_FILE);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String sqlQuery = "SELECT * FROM meta_variables";
SqlRowSet data = jdbcTemplateMeta.queryForRowSet(sqlQuery);
data.next();
String json = ((PGobject) data.getObject("hierarchy")).getValue();
Object hierarchy = new Gson().fromJson(new JsonReader(br), Object.class);
Object hierarchy = new Gson().fromJson(json, Object.class);
return ResponseEntity.ok(hierarchy);
}
private static void loadVariables() {
private void loadVariables() {
if(variables == null)
{
InputStream is = VariablesApi.class.getClassLoader().getResourceAsStream(VARIABLES_FILE);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String sqlQuery = "SELECT * FROM meta_variables";
SqlRowSet data = jdbcTemplateMeta.queryForRowSet(sqlQuery);
data.next();
String json = ((PGobject) data.getObject("hierarchy")).getValue();
JsonObject root = new Gson().fromJson(new JsonReader(br), JsonObject.class);
variables = new LinkedList<>();
JsonObject root = new Gson().fromJson(json, JsonObject.class);
variables = new LinkedList<>();
extractVariablesRecursive(root);
}
}
private static void extractVariablesRecursive(JsonObject element) {
private void extractVariablesRecursive(JsonObject element) {
if (element.has("groups")){
for(JsonElement child : element.getAsJsonArray("groups")) {
extractVariablesRecursive(child.getAsJsonObject());
......
This diff is collapsed.
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