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

Prepared for caching (DataUtil Bean + Dependency + CacheConfiguration)

parent b5156cb7
No related branches found
Tags 2.3.0
No related merge requests found
......@@ -48,6 +48,7 @@
<hibernate-entitymanager.version>4.3.8.Final</hibernate-entitymanager.version>
<spring-data-jpa.version>1.10.2.RELEASE</spring-data-jpa.version>
<spring-boot-starter-actuator.version>1.4.0.RELEASE</spring-boot-starter-actuator.version>
<aspectjweaver.version>1.8.9</aspectjweaver.version>
</properties>
<dependencies>
......@@ -160,6 +161,11 @@
<artifactId>flyway-core</artifactId>
<version>${flyway-core.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
</dependencies>
<pluginRepositories>
......
......@@ -6,10 +6,8 @@ package eu.hbp.mip;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class MIPApplication {
public static void main(String[] args) {
......
package eu.hbp.mip.configuration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
/**
* Created by mirco on 07.11.16.
*/
@Configuration
@EnableCaching
public class CacheConfiguration {
}
package eu.hbp.mip.configuration;
import eu.hbp.mip.utils.DataUtil;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.*;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaVendorAdapter;
......@@ -60,6 +59,9 @@ public class PersistenceConfiguration {
return new JdbcTemplate(scienceDataSource());
}
@Value("#{'${spring.scienceDatasource.main-table:adni_merge}'}")
private String scienceMainTable;
@Bean(name = "entityManagerFactory")
@DependsOn("flyway")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
......@@ -78,4 +80,10 @@ public class PersistenceConfiguration {
return flyway;
}
@Bean(name = "dataUtil")
@Scope("singleton")
public DataUtil dataUtil() {
return new DataUtil(scienceJdbcTemplate(), scienceMainTable);
}
}
......@@ -17,10 +17,8 @@ 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.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
......@@ -55,11 +53,8 @@ public class ModelsApi {
private VariableRepository variableRepository;
@Autowired
@Qualifier("scienceJdbcTemplate")
private JdbcTemplate scienceJdbcTemplate;
@Value("#{'${spring.scienceDatasource.main-table:adni_merge}'}")
private String scienceMainTable;
@Qualifier("dataUtil")
public DataUtil dataUtil;
@ApiOperation(value = "Get models", response = Model.class, responseContainer = "List")
......@@ -290,7 +285,7 @@ public class ModelsApi {
Gson gson = new Gson();
JsonObject jsonModel = gson.fromJson(gson.toJson(model, Model.class), JsonObject.class);
jsonModel.get("dataset").getAsJsonObject()
.add("data", new DataUtil(scienceJdbcTemplate, scienceMainTable).getDataFromVariables(allVars));
.add("data", dataUtil.getDataFromVariables(allVars));
return gson.fromJson(jsonModel, Model.class);
}
......
......@@ -15,9 +15,7 @@ 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.beans.factory.annotation.Value;
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;
......@@ -39,11 +37,9 @@ public class RequestsApi {
private static final Logger LOGGER = Logger.getLogger(RequestsApi.class);
@Autowired
@Qualifier("scienceJdbcTemplate")
private JdbcTemplate scienceJdbcTemplate;
@Qualifier("dataUtil")
public DataUtil dataUtil;
@Value("#{'${spring.scienceDatasource.main-table:adni_merge}'}")
private String scienceMainTable;
@ApiOperation(value = "Post a request", response = Dataset.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success") })
......@@ -103,7 +99,7 @@ public class RequestsApi {
dataset.add("grouping", gson.toJsonTree(groupings));
dataset.add("header", gson.toJsonTree(covariables));
dataset.add("filter", gson.toJsonTree(filters));
dataset.add("data", new DataUtil(scienceJdbcTemplate, scienceMainTable).getDataFromVariables(allVars));
dataset.add("data", dataUtil.getDataFromVariables(allVars));
return ResponseEntity.ok(new Gson().fromJson(dataset, Object.class));
}
......
......@@ -15,9 +15,7 @@ import io.swagger.annotations.ApiResponses;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
......@@ -38,11 +36,9 @@ public class StatsApi {
private ArticleRepository articleRepository;
@Autowired
@Qualifier("scienceJdbcTemplate")
private JdbcTemplate scienceJdbcTemplate;
@Qualifier("dataUtil")
public DataUtil dataUtil;
@Value("#{'${spring.scienceDatasource.main-table:adni_merge}'}")
private String scienceMainTable;
@ApiOperation(value = "Get general statistics", response = GeneralStats.class)
@ApiResponses(value = {@ApiResponse(code = 200, message = "Found"), @ApiResponse(code = 404, message = "Not found") })
......@@ -54,7 +50,7 @@ public class StatsApi {
stats.setUsers(userRepository.count());
stats.setArticles(articleRepository.count());
stats.setVariables(new DataUtil(scienceJdbcTemplate, scienceMainTable).countVariables());
stats.setVariables(dataUtil.countVariables());
return ResponseEntity.ok(stats);
}
......
......@@ -2,7 +2,6 @@ package eu.hbp.mip.utils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
......@@ -25,7 +24,7 @@ public class DataUtil {
this.scienceMainTable = scienceMainTable;
}
@Cacheable("varsdata")
// @Cacheable("varsdata")
public JsonObject getDataFromVariables(List<String> vars)
{
JsonObject data = new JsonObject();
......@@ -52,7 +51,7 @@ public class DataUtil {
return data;
}
@Cacheable("colscount")
// @Cacheable("colscount")
public long countVariables()
{
long count = jdbcTemplate.queryForObject(
......@@ -61,7 +60,7 @@ public class DataUtil {
return count;
}
@Cacheable("rowscount")
// @Cacheable("rowscount")
public long countAdniRows()
{
long count = jdbcTemplate.queryForObject(
......
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