@ -18,13 +18,15 @@ import org.gcube.portlets.user.speciesdiscovery.server.persistence.dao.TaxonRowP
import org.gcube.portlets.user.speciesdiscovery.server.persistence.dao.TaxonomyJobPersistence ;
import org.gcube.portlets.user.speciesdiscovery.server.persistence.dao.TaxonomyRowPersistence ;
import org.gcube.portlets.user.speciesdiscovery.shared.DatabaseServiceException ;
import org.gcube.portlets.user.speciesdiscovery.shared.SessionExpired ;
/ * *
* The Class DaoSession .
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
* Feb 9 , 2017
* Mar 17 , 2017
* /
public class DaoSession {
@ -35,14 +37,48 @@ public class DaoSession {
private static final String JDBCDRIVER = "jdbc:h2:" ;
/ * *
* Check asl session .
*
* @param session the session
* @return true , if is valid
* @throws SessionExpired the session expired
* /
public static boolean checkASLSession ( ASLSession session ) throws SessionExpired {
if ( session = = null ) {
String error = "ASL session is null!!!!" ;
logger . error ( error ) ;
throw new SessionExpired ( error ) ;
}
String username = session . getUsername ( ) ;
String scope = session . getScopeName ( ) ;
if ( username = = null | | username . isEmpty ( ) ) {
String error = "Username is null or empty, throwing session expired exception" ;
logger . error ( error ) ;
throw new SessionExpired ( error ) ;
}
if ( scope = = null | | scope . isEmpty ( ) ) {
String error = "Scope is null or empty, throwing session expired exception" ;
logger . error ( error ) ;
throw new SessionExpired ( error ) ;
}
logger . trace ( "Session is valid: [username: " + username + ", scope: " + scope + "]" ) ;
return true ;
}
/ * *
* Gets the username scope value .
*
* @param username the username
* @param scope the scope
* @return the username scope value
* @throws SessionExpired the session expired
* /
public static String getUsernameScopeValue ( String username , String scope ) {
public static String getUsernameScopeValue ( String username , String scope ) throws SessionExpired {
scope = scope . replaceAll ( "/" , "" ) ;
username = username . replaceAll ( "\\." , "" ) ;
@ -51,8 +87,6 @@ public class DaoSession {
}
/ * *
* Gets the tomcat folder .
*
@ -75,12 +109,12 @@ public class DaoSession {
*
* @param session the session
* @return the connection url
* @throws SessionExpired the session expired
* /
public static String getConnectionUrl ( ASLSession session ) {
public static String getConnectionUrl ( ASLSession session ) throws SessionExpired {
String username = session . getUsername ( ) ;
String scope = session . getScopeName ( ) ;
return JDBCDRIVER + getTomcatFolder ( ) + PERSISTENCE_H2DBSPECIES_H2 + getUsernameScopeValue ( username , scope ) + ";create=true" ;
checkASLSession ( session ) ;
return JDBCDRIVER + getTomcatFolder ( ) + PERSISTENCE_H2DBSPECIES_H2 + getUsernameScopeValue ( session . getUsername ( ) , session . getScopeName ( ) ) + ";create=true" ;
}
@ -112,8 +146,10 @@ public class DaoSession {
*
* @param session the session
* @return the entity manager
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
public static EntityManager newEntityManager ( ASLSession session ) {
public static EntityManager newEntityManager ( ASLSession session ) throws SessionExpired , Exception {
return getEntityManagerFactory ( session ) . createEntityManager ( ) ;
}
@ -123,8 +159,10 @@ public class DaoSession {
*
* @param session the session
* @return the entity manager factory
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
public static EntityManagerFactory getEntityManagerFactory ( ASLSession session ) {
public static EntityManagerFactory getEntityManagerFactory ( ASLSession session ) throws SessionExpired , Exception {
EntityManagerFactory factory = SessionUtil . getEntityManagerFactory ( session ) ;
@ -142,11 +180,12 @@ public class DaoSession {
*
* @param session the session
* @return the occurrences job dao
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
public static OccurrenceJobPersistence getOccurrencesJobDAO ( ASLSession session ) throws Exception
{
public static OccurrenceJobPersistence getOccurrencesJobDAO ( ASLSession session ) throws SessionExpired , Exception {
checkASLSession ( session ) ;
OccurrenceJobPersistence occurrencesJobDao = SessionUtil . getCurrentDaoOccurrencesJob ( session ) ;
if ( occurrencesJobDao = = null ) {
@ -164,10 +203,12 @@ public class DaoSession {
*
* @param session the session
* @return the gis layers job dao
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
public static GisLayerJobPersistence getGisLayersJobDAO ( ASLSession session ) throws Exception {
public static GisLayerJobPersistence getGisLayersJobDAO ( ASLSession session ) throws SessionExpired, Exception{
checkASLSession ( session ) ;
GisLayerJobPersistence gisLayerJobDao = SessionUtil . getCurrentGisLayersJob ( session ) ;
if ( gisLayerJobDao = = null ) {
@ -184,8 +225,10 @@ public class DaoSession {
*
* @param session the session
* @return the gis layer job persistence
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
private static GisLayerJobPersistence initGisLayerJobsDao ( ASLSession session ) {
private static GisLayerJobPersistence initGisLayerJobsDao ( ASLSession session ) throws SessionExpired , Exception {
GisLayerJobPersistence gisLayerJobP = new GisLayerJobPersistence ( getEntityManagerFactory ( session ) ) ;
SessionUtil . setCurrentEJBGisLayerJob ( session , gisLayerJobP ) ;
return gisLayerJobP ;
@ -196,11 +239,11 @@ public class DaoSession {
*
* @param session the session
* @return the taxonomy job dao
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
public static TaxonomyJobPersistence getTaxonomyJobDAO ( ASLSession session ) throws Exception
{
public static TaxonomyJobPersistence getTaxonomyJobDAO ( ASLSession session ) throws SessionExpired , Exception {
checkASLSession ( session ) ;
TaxonomyJobPersistence taxonomyJobPeristence = SessionUtil . getCurrentDAOTaxonomyJob ( session ) ;
if ( taxonomyJobPeristence = = null ) {
@ -216,9 +259,12 @@ public class DaoSession {
*
* @param session the session
* @return the entity manager factory
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
public static EntityManagerFactory createEntityManagerFactory ( ASLSession session ) {
public static EntityManagerFactory createEntityManagerFactory ( ASLSession session ) throws SessionExpired , Exception {
checkASLSession ( session ) ;
Map < String , String > properties = new HashMap < String , String > ( ) ;
// properties.put("javax.persistence.jdbc.driver", jdbcDriverH2);
try {
@ -226,9 +272,8 @@ public class DaoSession {
}
catch ( Exception e ) {
logger . error ( "error on get connection url " + e , e ) ;
String username = session . getUsername ( ) ;
String scope = session . getScopeName ( ) ;
properties . put ( "javax.persistence.jdbc.url" , JDBCDRIVER + getTomcatFolder ( ) + "/temp/h2dbspecies/h2" + getUsernameScopeValue ( username , scope ) + ";create=true" ) ;
checkASLSession ( session ) ;
properties . put ( "javax.persistence.jdbc.url" , JDBCDRIVER + getTomcatFolder ( ) + "/temp/h2dbspecies/h2" + getUsernameScopeValue ( session . getUsername ( ) , session . getScopeName ( ) ) + ";create=true" ) ;
}
//emf = Persistence.createEntityManagerFactory("jpablogPUnit");
return Persistence . createEntityManagerFactory ( "SPD_PERSISTENCE_FACTORY" , properties ) ;
@ -241,14 +286,11 @@ public class DaoSession {
*
* @param session the session
* @return the occurrence job persistence
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
public static OccurrenceJobPersistence initOccurrencesJobsDao ( ASLSession session ) throws Exception
{
String username = session . getUsername ( ) ;
String scope = session . getScopeName ( ) ;
return createOccurrencesJobDao ( username , scope , session ) ;
public static OccurrenceJobPersistence initOccurrencesJobsDao ( ASLSession session ) throws SessionExpired , Exception {
return createOccurrencesJobDao ( session . getUsername ( ) , session . getScopeName ( ) , session ) ;
}
@ -274,9 +316,10 @@ public class DaoSession {
* @param scope the scope
* @param session the session
* @return the occurrence job persistence
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
public static OccurrenceJobPersistence createOccurrencesJobDao ( String username , String scope , ASLSession session ) throws Exception{
public static OccurrenceJobPersistence createOccurrencesJobDao ( String username , String scope , ASLSession session ) throws SessionExpired, Exception{
OccurrenceJobPersistence occurrenceJobPersistence = new OccurrenceJobPersistence ( getEntityManagerFactory ( session ) ) ;
SessionUtil . setCurrentEJBOccurrencesJob ( session , occurrenceJobPersistence ) ;
@ -310,11 +353,11 @@ public class DaoSession {
*
* @param session the session
* @return the occurrence dao
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
public static OccurrenceRowPersistence getOccurrenceDAO ( ASLSession session ) throws Exception
{
public static OccurrenceRowPersistence getOccurrenceDAO ( ASLSession session ) throws SessionExpired , Exception {
checkASLSession ( session ) ;
OccurrenceRowPersistence occurrenceEJB = SessionUtil . getCurrentEJBOccurrence ( session ) ;
if ( occurrenceEJB = = null ) {
@ -330,10 +373,10 @@ public class DaoSession {
*
* @param session the session
* @return the taxon row persistence
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
public static TaxonRowPersistence initTaxonDao ( ASLSession session ) throws Exception
{
public static TaxonRowPersistence initTaxonDao ( ASLSession session ) throws SessionExpired , Exception {
TaxonRowPersistence taxonRowPersistence = new TaxonRowPersistence ( getEntityManagerFactory ( session ) ) ;
SessionUtil . setCurrentEJBTaxonRow ( session , taxonRowPersistence ) ;
@ -367,11 +410,11 @@ public class DaoSession {
*
* @param session the session
* @return the taxon dao
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
public static TaxonRowPersistence getTaxonDAO ( ASLSession session ) throws Exception
{
public static TaxonRowPersistence getTaxonDAO ( ASLSession session ) throws SessionExpired , Exception {
checkASLSession ( session ) ;
TaxonRowPersistence taxonDao = SessionUtil . getCurrentEJBTaxonRow ( session ) ;
if ( taxonDao = = null ) {
@ -390,10 +433,11 @@ public class DaoSession {
*
* @param session the session
* @return the taxonomy dao
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
public static TaxonomyRowPersistence getTaxonomyDAO ( ASLSession session ) throws Exception
{
public static TaxonomyRowPersistence getTaxonomyDAO ( ASLSession session ) throws SessionExpired, Exception{
checkASLSession ( session ) ;
logger . trace ( "In getTaxonomyDAO..." ) ;
TaxonomyRowPersistence taxonItemDAO = SessionUtil . getCurrentEJBTaxonomyItem ( session ) ;
@ -430,27 +474,19 @@ public class DaoSession {
*
* @param session the session
* @return the result row dao
* @throws SessionExpired the session expired
* @throws Exception the exception
* /
public static ResultRowPersistence getResultRowDAO ( ASLSession session ) throws Exception
{
public static ResultRowPersistence getResultRowDAO ( ASLSession session ) throws SessionExpired , Exception {
checkASLSession ( session ) ;
logger . trace ( "In getResultRowDAO..." ) ;
ResultRowPersistence resultRowEJB = SessionUtil . getCurrentEJBResultRow ( session ) ;
if ( resultRowEJB = = null ) {
resultRowEJB = initResultRowDao ( session ) ;
SessionUtil . setCurrentEJBResultRow ( session , resultRowEJB ) ;
}
// logger.trace("Return result row Dao : " + resultRowEJB + " table name: " + resultRowDAO.getTableConfig().getTableName());
// logger.trace("session id: " + session.getExternalSessionID() + " sessione username: " +session.getUsername() + " session scope: "+session.getScopeName());
return resultRowEJB ;
}
}