diff --git a/src/main/java/org/hbp/mip/MIPApplication.java b/src/main/java/org/hbp/mip/MIPApplication.java index d5f62247e97375b5b6eada30c3b1c99193516242..b2230f583b030a3d717a62fdc2639c37e149c0e4 100644 --- a/src/main/java/org/hbp/mip/MIPApplication.java +++ b/src/main/java/org/hbp/mip/MIPApplication.java @@ -26,8 +26,8 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiParam; import org.hbp.mip.model.User; import org.hbp.mip.utils.CORSFilter; +import org.hbp.mip.utils.HibernateUtil; import org.hibernate.Session; -import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -95,9 +95,6 @@ public class MIPApplication extends WebSecurityConfigurerAdapter { @Autowired OAuth2ClientContext oauth2ClientContext; - @Autowired - SessionFactory sessionFactoryBean; - @Autowired HttpSessionCsrfTokenRepository httpSessionCsrfTokenRepository; @@ -128,7 +125,7 @@ public static void main(String[] args) { * @return */ public synchronized User getUser(Principal principal) { - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); User user = (User) session .createQuery("from User where username= :username") @@ -188,7 +185,7 @@ public static void main(String[] args) { public ResponseEntity<Void> postUser(Principal principal, HttpServletResponse response, @ApiParam(value = "Has the user agreed on the NDA") @RequestParam(value = "agreeNDA", required = true) Boolean agreeNDA) { ObjectMapper mapper = new ObjectMapper(); - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); User user = (User) session .createQuery("from User where username= :username") diff --git a/src/main/java/org/hbp/mip/controllers/ArticlesApi.java b/src/main/java/org/hbp/mip/controllers/ArticlesApi.java index 53ba7f8d06c4e9dc47d42d80088860baf21da694..cbfddbb67f951e5a73728b884d2e6f18ee0b47e9 100644 --- a/src/main/java/org/hbp/mip/controllers/ArticlesApi.java +++ b/src/main/java/org/hbp/mip/controllers/ArticlesApi.java @@ -9,11 +9,10 @@ import io.swagger.annotations.*; import org.hbp.mip.MIPApplication; import org.hbp.mip.model.Article; import org.hbp.mip.model.User; +import org.hbp.mip.utils.HibernateUtil; import org.hibernate.Query; import org.hibernate.Session; -import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -33,9 +32,6 @@ public class ArticlesApi { @Autowired MIPApplication mipApplication; - @Autowired - SessionFactory sessionFactoryBean; - @ApiOperation(value = "Get articles", response = Article.class, responseContainer = "List") @ApiResponses(value = { @ApiResponse(code = 200, message = "Success") }) @RequestMapping(method = RequestMethod.GET) @@ -68,7 +64,7 @@ public class ArticlesApi { } } - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); List<Article> articles = new LinkedList<>(); // Query DB try{ @@ -123,7 +119,7 @@ public class ArticlesApi { article.setCreatedBy(user); // Save article into DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try{ session.beginTransaction(); session.save(article); @@ -147,7 +143,7 @@ public class ArticlesApi { ) { // Query DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); Article article = null; try{ session.beginTransaction(); @@ -179,7 +175,7 @@ public class ArticlesApi { User user = mipApplication.getUser(principal); // Query DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try{ session.beginTransaction(); session.update(article); diff --git a/src/main/java/org/hbp/mip/controllers/DatasetsApi.java b/src/main/java/org/hbp/mip/controllers/DatasetsApi.java index 481d43c8cc71ea02236ba52b3c39f7892bf66712..1d03600cf7969d219da68cf2b4e91cb0a463b687 100644 --- a/src/main/java/org/hbp/mip/controllers/DatasetsApi.java +++ b/src/main/java/org/hbp/mip/controllers/DatasetsApi.java @@ -7,10 +7,9 @@ package org.hbp.mip.controllers; import io.swagger.annotations.*; import org.hbp.mip.model.Dataset; +import org.hbp.mip.utils.HibernateUtil; import org.hibernate.Query; import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; @@ -24,9 +23,6 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @RequestMapping(value = "/datasets", produces = {APPLICATION_JSON_VALUE}) @Api(value = "/datasets", description = "the datasets API") public class DatasetsApi { - - @Autowired - SessionFactory sessionFactoryBean; @ApiOperation(value = "Get a dataset", response = Dataset.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "Success") }) @@ -36,7 +32,7 @@ public class DatasetsApi { ) { // Query DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); Dataset dataset = null; try{ session.beginTransaction(); diff --git a/src/main/java/org/hbp/mip/controllers/GroupsApi.java b/src/main/java/org/hbp/mip/controllers/GroupsApi.java index 063ba59926d0fe16f4f9d6e52c3d8cfb38c0fab4..59681d143ded7134c01862d5af1cbc61442143d3 100644 --- a/src/main/java/org/hbp/mip/controllers/GroupsApi.java +++ b/src/main/java/org/hbp/mip/controllers/GroupsApi.java @@ -6,9 +6,8 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import org.hbp.mip.model.Group; +import org.hbp.mip.utils.HibernateUtil; import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; @@ -22,10 +21,6 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @Api(value = "/groups", description = "the groups API") public class GroupsApi { - - @Autowired - SessionFactory sessionFactoryBean; - @ApiOperation(value = "Get the root group (containing all subgroups)", response = Group.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "Success") }) @RequestMapping(method = RequestMethod.GET) @@ -35,7 +30,7 @@ public class GroupsApi { String rootCode = "root"; // Query DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); Group group = null; try{ session.beginTransaction(); diff --git a/src/main/java/org/hbp/mip/controllers/ModelsApi.java b/src/main/java/org/hbp/mip/controllers/ModelsApi.java index 59275a8798a9fec304a88d765216cf2b13005147..3dc76cbdb3e6e12cefd0a2a61543d09997a188ed 100644 --- a/src/main/java/org/hbp/mip/controllers/ModelsApi.java +++ b/src/main/java/org/hbp/mip/controllers/ModelsApi.java @@ -9,9 +9,9 @@ import io.swagger.annotations.*; import org.hbp.mip.MIPApplication; import org.hbp.mip.model.*; import org.hbp.mip.utils.CSVUtil; +import org.hbp.mip.utils.HibernateUtil; import org.hibernate.Query; import org.hibernate.Session; -import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -34,9 +34,6 @@ public class ModelsApi { @Autowired MIPApplication mipApplication; - @Autowired - SessionFactory sessionFactoryBean; - private static final String DATA_FILE = "data/values.csv"; @ApiOperation(value = "Get models", response = Model.class, responseContainer = "List") @@ -67,7 +64,7 @@ public class ModelsApi { } // Query DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); List<Model> models = new LinkedList<>(); try{ session.beginTransaction(); @@ -120,7 +117,7 @@ public class ModelsApi { model.setCreatedAt(new Date()); // Save model into DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try{ session.beginTransaction(); session.save(model); @@ -143,7 +140,7 @@ public class ModelsApi { ) { // Query DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); org.hibernate.Query query = session.createQuery("from Model where slug= :slug"); query.setString("slug", slug); @@ -151,7 +148,7 @@ public class ModelsApi { session.getTransaction().commit(); if(model != null) { - session = sessionFactoryBean.getCurrentSession(); + session = HibernateUtil.getSessionFactory().getCurrentSession(); try{ session.beginTransaction(); query = session.createQuery("from Query where id= :id"); @@ -226,7 +223,7 @@ public class ModelsApi { User user = mipApplication.getUser(principal); // Query DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try{ session.beginTransaction(); session.update(model); @@ -261,7 +258,7 @@ public class ModelsApi { model.setSlug(copySlug); // Save model into DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); try{ session.beginTransaction(); session.save(model); diff --git a/src/main/java/org/hbp/mip/controllers/StatsApi.java b/src/main/java/org/hbp/mip/controllers/StatsApi.java index b468260d605626fbe67688a6d90bc70914819daa..e19a548a9b0ea300c8c7872a41f403a806282785 100644 --- a/src/main/java/org/hbp/mip/controllers/StatsApi.java +++ b/src/main/java/org/hbp/mip/controllers/StatsApi.java @@ -3,10 +3,9 @@ package org.hbp.mip.controllers; import io.swagger.annotations.*; import org.hbp.mip.model.GeneralStats; import org.hbp.mip.model.Statistics; +import org.hbp.mip.utils.HibernateUtil; import org.hibernate.Query; import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; @@ -23,9 +22,6 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @Api(value = "/stats", description = "the stats API") public class StatsApi { - @Autowired - SessionFactory sessionFactoryBean; - @ApiOperation(value = "Get general statistics", response = GeneralStats.class) @ApiResponses(value = {@ApiResponse(code = 200, message = "Found"), @ApiResponse(code = 404, message = "Not found") }) @RequestMapping(method = RequestMethod.GET) @@ -33,7 +29,7 @@ public class StatsApi { GeneralStats stats = new GeneralStats(); - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); Long nbUsers = 0L; Long nbArticles = 0L; Long nbVariables = 0L; diff --git a/src/main/java/org/hbp/mip/controllers/UsersApi.java b/src/main/java/org/hbp/mip/controllers/UsersApi.java index ddc02821124da96664c5a9d47a64dc3f86d72283..e302ebeff007ca7a0adbe10de1e6a664a2bbc752 100644 --- a/src/main/java/org/hbp/mip/controllers/UsersApi.java +++ b/src/main/java/org/hbp/mip/controllers/UsersApi.java @@ -2,9 +2,8 @@ package org.hbp.mip.controllers; import io.swagger.annotations.*; import org.hbp.mip.model.User; +import org.hbp.mip.utils.HibernateUtil; import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; @@ -23,8 +22,6 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @Api(value = "/users", description = "the users API") public class UsersApi { - @Autowired - SessionFactory sessionFactoryBean; @ApiOperation(value = "Get a user", response = User.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "Found"), @ApiResponse(code = 404, message = "Not found") }) @@ -34,7 +31,7 @@ public class UsersApi { ) { // Query DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); User user = null; try{ session.beginTransaction(); diff --git a/src/main/java/org/hbp/mip/controllers/VariablesApi.java b/src/main/java/org/hbp/mip/controllers/VariablesApi.java index ee01a9dc53d9efa780add3646a4ac963c4cc7f9e..a7f1e873dc6b3204ab7214fcd30d1f186e637d49 100644 --- a/src/main/java/org/hbp/mip/controllers/VariablesApi.java +++ b/src/main/java/org/hbp/mip/controllers/VariablesApi.java @@ -8,9 +8,8 @@ package org.hbp.mip.controllers; import io.swagger.annotations.*; import org.hbp.mip.model.Value; import org.hbp.mip.model.Variable; +import org.hbp.mip.utils.HibernateUtil; import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -25,9 +24,6 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @Api(value = "/variables", description = "the variables API") public class VariablesApi { - @Autowired - SessionFactory sessionFactoryBean; - @ApiOperation(value = "Get variables", response = Variable.class, responseContainer = "List") @ApiResponses(value = { @ApiResponse(code = 200, message = "Success") }) @RequestMapping(method = RequestMethod.GET) @@ -41,7 +37,7 @@ public class VariablesApi { ) { // Get variables from DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); List<Variable> variables = new LinkedList<>(); try{ session.beginTransaction(); @@ -66,7 +62,7 @@ public class VariablesApi { ) { // Query DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); Variable variable = null; try{ session.beginTransaction(); @@ -95,7 +91,7 @@ public class VariablesApi { ) { // Query DB - Session session = sessionFactoryBean.getCurrentSession(); + Session session = HibernateUtil.getSessionFactory().getCurrentSession(); List<Value> values = new LinkedList<>(); try{ session.beginTransaction(); diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index 7fcb9a7d3e9164620627ba8bb0c955331efa35d4..6e00ac17ffe569d105d5e47e2b94ff1cc017454c 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -17,5 +17,19 @@ <property name="hibernate.show_sql">false</property> <property name="hibernate.hbm2ddl.auto">update</property> + <mapping class="org.hbp.mip.model.Article"/> + <mapping class="org.hbp.mip.model.Chart"/> + <mapping class="org.hbp.mip.model.ChartConfigSet"/> + <mapping class="org.hbp.mip.model.Dataset"/> + <mapping class="org.hbp.mip.model.Model"/> + <mapping class="org.hbp.mip.model.Query"/> + <mapping class="org.hbp.mip.model.Tag"/> + <mapping class="org.hbp.mip.model.User"/> + <mapping class="org.hbp.mip.model.Value"/> + <mapping class="org.hbp.mip.model.Variable"/> + <mapping class="org.hbp.mip.model.Filter"/> + <mapping class="org.hbp.mip.model.Group"/> + <mapping class="org.hbp.mip.model.Config"/> + </session-factory> </hibernate-configuration> diff --git a/src/main/resources/spring/application-context.xml b/src/main/resources/spring/application-context.xml index fb31f6f722664505a6675ce43fac00767e8babd4..7eef8445fce729c7e75e97d930fe7e139c43b0d5 100644 --- a/src/main/resources/spring/application-context.xml +++ b/src/main/resources/spring/application-context.xml @@ -2,21 +2,11 @@ <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" - xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:component-scan base-package="org.hbp.mip" /> - <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> - <property name="packagesToScan"> - <list> - <value>org.hbp.mip.model</value> - </list> - </property> - - <property name="configLocation" value="classpath:hibernate.cfg.xml" /> - </bean> - <bean id="httpSessionCsrfTokenRepository" class="org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository"> <property name="headerName" value="X-XSRF-TOKEN" /> </bean> diff --git a/src/test/java/org/hbp/mip/MIPApplicationTests.java b/src/test/java/org/hbp/mip/MIPApplicationTests.java index b6f5a5a80fc6a6362eb4ee18863e629e09eb5a75..bfe9832163384494b33694d93d0223c35cba6c12 100644 --- a/src/test/java/org/hbp/mip/MIPApplicationTests.java +++ b/src/test/java/org/hbp/mip/MIPApplicationTests.java @@ -15,30 +15,13 @@ */ package org.hbp.mip; -import static org.junit.Assert.*; -import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.orm.hibernate4.LocalSessionFactoryBean; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @SpringApplicationConfiguration(classes=MIPApplication.class, locations={"classpath:spring/application-context.xml"}) public class MIPApplicationTests { - - @Autowired - LocalSessionFactoryBean sessionFactoryBean; - - @Test - public void contextLoads() { - } - - @Test - public void testMainApp() { - assertNotNull(sessionFactoryBean); - } - }