Skip to content

Commit

Permalink
Merge branch 'aiccra-staging' into AICCRA
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjitm committed Aug 6, 2024
2 parents c9d863f + 2265736 commit 083782c
Show file tree
Hide file tree
Showing 43 changed files with 508 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,33 @@

package org.cgiar.ccafs.marlo;

import java.util.Properties;

import javax.sql.DataSource;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Environment;
import org.hibernate.cache.ehcache.internal.EhcacheRegionFactory;
import org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory;
import org.hibernate.cfg.AvailableSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;


/**
* Configuration for Database and Flyway beans.
*/
@Configuration
@EnableTransactionManagement
public class MarloDatabaseConfiguration {

/**
Expand All @@ -56,6 +64,11 @@ public class MarloDatabaseConfiguration {

private final Logger log = LoggerFactory.getLogger(MarloDatabaseConfiguration.class);

@Bean(name = "ehcacheRegionFactory")
public EhcacheRegionFactory ehcacheRegionFactory() {
return new SingletonEhcacheRegionFactory();
}

@Bean(name = "dataSource", destroyMethod = "close")
public DataSource getDataSource() {

Expand Down Expand Up @@ -87,6 +100,38 @@ public DataSource getDataSource() {
return dataSource;
}

private Properties hibernateProperties() {
Properties props = new Properties();
props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL8Dialect");
if (Boolean.TRUE.equals(showSql)) {
props.setProperty(AvailableSettings.SHOW_SQL, "true");
}
props.setProperty("hibernate.cache.use_second_level_cache", Boolean.TRUE.toString());
props.setProperty("hibernate.cache.use_query_cache", Boolean.TRUE.toString());
return props;
}

@Bean(name = "transactionManager")
public PlatformTransactionManager hibernateTransactionManager() {
final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(this.sessionFactory());
return transactionManager;
}

@Bean(name = "localSessionFactoryBean")
public LocalSessionFactoryBean localSessionFactory() {
log.info("Setting LocalSessionFactory");
MarloLocalSessionFactoryBean sessionFactory = new MarloLocalSessionFactoryBean();
sessionFactory.setDataSource(this.getDataSource());
sessionFactory.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
sessionFactory.setHibernateProperties(this.hibernateProperties());

// Enable second level cache with ehcache
sessionFactory.setRegionFactory(this.ehcacheRegionFactory());

return sessionFactory;
}

/**
* Leave commented out for now.
*
Expand All @@ -98,32 +143,9 @@ public DataSource getDataSource() {
// }

@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(final DataSource dataSource) {
public SessionFactory sessionFactory() {
log.info("Setting SessionFactory");
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
sessionBuilder.configure("hibernate.cfg.xml");
sessionBuilder.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
if (showSql) {
sessionBuilder.setProperty(Environment.SHOW_SQL, "true");
}

// Enable second level cache with ehcache
sessionBuilder.setProperty("hibernate.cache.use_second_level_cache", Boolean.TRUE.toString());
sessionBuilder.setProperty("hibernate.cache.region.factory_class",
"org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");

// // Enable query cache
sessionBuilder.setProperty("hibernate.cache.use_query_cache", Boolean.TRUE.toString());

SessionFactory sessionFactory = sessionBuilder.buildSessionFactory();
return sessionFactory;
}


@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(final SessionFactory sessionFactory) {
final HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
return transactionManager;
return this.localSessionFactory().getObject();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
*
*/
package org.cgiar.ccafs.marlo;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cache.spi.RegionFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;

/**
* Custom session factory bean extended from {@link LocalSessionFactoryBean}. This implementation overrides the method
* buildSessionFactory to provide Hibernate with own region factory.
*
* @author Alexander Filigrana - Premize S.A.S
* @since 4.4.0
* @see {@link RegionFactory}
*/
public class MarloLocalSessionFactoryBean extends LocalSessionFactoryBean {

private RegionFactory regionFactory;

@Override
protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {

StandardServiceRegistryBuilder serviceRegistryBuilder = sfb.getStandardServiceRegistryBuilder();
serviceRegistryBuilder.addService(RegionFactory.class, regionFactory);

return sfb.buildSessionFactory();
}

@Required
public void setRegionFactory(RegionFactory regionFactory) {
this.regionFactory = regionFactory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
import org.cgiar.ccafs.marlo.data.AuditColumnHibernateListener;
import org.cgiar.ccafs.marlo.data.HibernateAuditLogListener;

import org.hibernate.cfg.Configuration;
import org.hibernate.boot.Metadata;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.metamodel.source.MetadataImplementor;
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -38,7 +37,7 @@ public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactor
}

@Override
public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory,
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);

Expand All @@ -59,12 +58,5 @@ public void integrate(Configuration configuration, SessionFactoryImplementor ses

}

@Override
public void integrate(MetadataImplementor metadata, SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
return;

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,28 @@ List<Deliverable> getDeliverablesByPhaseAndUrlAndDoiAndHandel(long phase, String
List<DeliverableDTO> getDeliverablesLeadByUserAndProjectWithSimpleConditions(long userId, long phaseId,
long projectId);

/**
* Get listing to validate duplicate information (dissemination_URL,DIO, handle)
*
* @author IBD
* @param phase phase of the project
* @return deliverable list with the data to validate duplicates (dissemination_URL,DIO, handle)
*/
List<String> getDuplicatesDeliverablesByPhase(long phase);

/**
* Get listing to validate duplicate information (dissemination_URL,DIO, handle)
*
* @author IBD
* @param phase phase of the project
* @param DOI DOI
* @param handle
* @param disseminationURL url of dissemination
* @return deliverable list with the data to validate duplicates (dissemination_URL,DIO, handle)
*/
List<String> getDuplicatesDeliverablesByPhaseWithDissemination(long phase, String DOI, String handle,
String disseminationURL);

public List<Deliverable> getPublicationsByPhase(long phase);

/**
Expand Down
Loading

0 comments on commit 083782c

Please sign in to comment.