From 339785116518be164c7a75b87f6cd46ba22f4bde Mon Sep 17 00:00:00 2001
From: Mirco Nasuti <mirco.nasuti@chuv.ch>
Date: Tue, 24 Oct 2017 17:36:55 +0200
Subject: [PATCH] use the features main table name to get the right metadata
 when multiple datasets are available

---
 src/main/java/eu/hbp/mip/controllers/GroupsApi.java    | 6 +++++-
 src/main/java/eu/hbp/mip/controllers/VariablesApi.java | 8 ++++++--
 tests/meta-db/sql/create.sql                           | 8 +++++---
 3 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/main/java/eu/hbp/mip/controllers/GroupsApi.java b/src/main/java/eu/hbp/mip/controllers/GroupsApi.java
index bb325947c..289624981 100644
--- a/src/main/java/eu/hbp/mip/controllers/GroupsApi.java
+++ b/src/main/java/eu/hbp/mip/controllers/GroupsApi.java
@@ -15,6 +15,7 @@ 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.beans.factory.annotation.Value;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.http.ResponseEntity;
 import org.springframework.jdbc.core.JdbcTemplate;
@@ -38,6 +39,9 @@ public class GroupsApi {
     @Qualifier("metaJdbcTemplate")
     private JdbcTemplate metaJdbcTemplate;
 
+    @Value("#{'${spring.featuresDatasource.main-table:features}'}")
+    private String featuresMainTable;
+
 
     @ApiOperation(value = "Get the root group (containing all subgroups)", response = Group.class)
     @Cacheable("groups")
@@ -49,7 +53,7 @@ public class GroupsApi {
     }
 
     private String loadGroups() {
-        String sqlQuery = "SELECT * FROM meta_variables";
+        String sqlQuery = String.format("SELECT * FROM meta_variables where target_table='%s'", featuresMainTable);
         SqlRowSet data = metaJdbcTemplate.queryForRowSet(sqlQuery);
         data.next();
         String json = ((PGobject) data.getObject("hierarchy")).getValue();
diff --git a/src/main/java/eu/hbp/mip/controllers/VariablesApi.java b/src/main/java/eu/hbp/mip/controllers/VariablesApi.java
index 74acd1edb..e08e0d19f 100644
--- a/src/main/java/eu/hbp/mip/controllers/VariablesApi.java
+++ b/src/main/java/eu/hbp/mip/controllers/VariablesApi.java
@@ -14,6 +14,7 @@ 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.beans.factory.annotation.Value;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.http.ResponseEntity;
 import org.springframework.jdbc.core.JdbcTemplate;
@@ -38,6 +39,9 @@ public class VariablesApi {
     @Qualifier("metaJdbcTemplate")
     private JdbcTemplate metaJdbcTemplate;
 
+    @Value("#{'${spring.featuresDatasource.main-table:features}'}")
+    private String featuresMainTable;
+
 
     @ApiOperation(value = "Get variables", response = List.class, responseContainer = "List")
     @Cacheable("variables")
@@ -120,7 +124,7 @@ public class VariablesApi {
     )  {
         LOGGER.info("Get groups and variables hierarchy");
 
-        String sqlQuery = "SELECT * FROM meta_variables";
+        String sqlQuery = String.format("SELECT * FROM meta_variables where target_table='%s'", featuresMainTable);
         SqlRowSet data = metaJdbcTemplate.queryForRowSet(sqlQuery);
         data.next();
         String json = ((PGobject) data.getObject("hierarchy")).getValue();
@@ -132,7 +136,7 @@ public class VariablesApi {
 
 
     private List<String> loadVariables() {
-        String sqlQuery = "SELECT * FROM meta_variables";
+        String sqlQuery = String.format("SELECT * FROM meta_variables where target_table='%s'", featuresMainTable);
         SqlRowSet data = metaJdbcTemplate.queryForRowSet(sqlQuery);
         data.next();
         String json = ((PGobject) data.getObject("hierarchy")).getValue();
diff --git a/tests/meta-db/sql/create.sql b/tests/meta-db/sql/create.sql
index eb69c50a0..0e8a94438 100644
--- a/tests/meta-db/sql/create.sql
+++ b/tests/meta-db/sql/create.sql
@@ -3,9 +3,10 @@ SET datestyle to 'European';
 CREATE TABLE IF NOT EXISTS meta_variables (
   ID serial NOT NULL PRIMARY KEY,
   source varchar(256) UNIQUE NOT NULL,
-  hierarchy json NOT NULL
+  hierarchy json NOT NULL,
+  target_table varchar(256) UNIQUE NOT NULL
 );
-INSERT INTO meta_variables (source, hierarchy) VALUES (
+INSERT INTO meta_variables (source, hierarchy, target_table) VALUES (
   'features',
   '
   {
@@ -40,5 +41,6 @@ INSERT INTO meta_variables (source, hierarchy) VALUES (
         }]
     }]
   }
-  '
+  ',
+  'features'
 );
-- 
GitLab