From 14ac05ea6c1065348c432b4d3d5cecdba51f755e Mon Sep 17 00:00:00 2001 From: Fabio Sinibaldi Date: Mon, 26 Oct 2020 12:12:43 +0100 Subject: [PATCH] JPA classes manually managed --- .../managers/AbstractRecordManager.java | 253 ++++++++++-------- 1 file changed, 136 insertions(+), 117 deletions(-) diff --git a/src/main/java/org/gcube/application/geoportal/managers/AbstractRecordManager.java b/src/main/java/org/gcube/application/geoportal/managers/AbstractRecordManager.java index 692cae9..3d3a16e 100644 --- a/src/main/java/org/gcube/application/geoportal/managers/AbstractRecordManager.java +++ b/src/main/java/org/gcube/application/geoportal/managers/AbstractRecordManager.java @@ -5,6 +5,7 @@ import java.io.UncheckedIOException; import java.net.URL; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; @@ -43,7 +44,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public abstract class AbstractRecordManager { - + private static EntityManagerFactory emf=null; @Synchronized @@ -52,38 +53,38 @@ public abstract class AbstractRecordManager { try { DatabaseConnection conn=ISUtils.queryForDB("postgresql", "internal-db"); log.debug("Found Internal Database : "+conn); - + emf = new HibernatePersistenceProvider().createContainerEntityManagerFactory( - archiverPersistenceUnitInfo(), - ImmutableMap.builder() - .put(AvailableSettings.JPA_JDBC_DRIVER, "org.postgresql.Driver") - .put(AvailableSettings.JPA_JDBC_URL, conn.getUrl()) - .put(AvailableSettings.DIALECT, org.hibernate.dialect.PostgreSQLDialect.class) - .put(AvailableSettings.HBM2DDL_AUTO, org.hibernate.tool.schema.Action.UPDATE) - .put(AvailableSettings.SHOW_SQL, true) - .put(AvailableSettings.QUERY_STARTUP_CHECKING, false) - .put(AvailableSettings.GENERATE_STATISTICS, false) - .put(AvailableSettings.USE_REFLECTION_OPTIMIZER, false) - .put(AvailableSettings.USE_SECOND_LEVEL_CACHE, false) - .put(AvailableSettings.USE_QUERY_CACHE, false) - .put(AvailableSettings.USE_STRUCTURED_CACHE, false) - .put(AvailableSettings.STATEMENT_BATCH_SIZE, 20) - .put(AvailableSettings.JPA_JDBC_USER, conn.getUser()) - .put(AvailableSettings.JPA_JDBC_PASSWORD, conn.getPwd()) - .build()); - }catch(Throwable t) { - log.warn("Unable to contact DB from IS, trying local..",t); - try{ - emf=Persistence.createEntityManagerFactory(DBConstants.INTERNAL.DB_NAME); - }catch(Throwable t1) { - log.error("NO INTERNAL DATABASE FOUND ",t); - throw new RuntimeException("NO INTERNAL DATABASE",t); - } + archiverPersistenceUnitInfo(), + ImmutableMap.builder() + .put(AvailableSettings.JPA_JDBC_DRIVER, "org.postgresql.Driver") + .put(AvailableSettings.JPA_JDBC_URL, conn.getUrl()) + .put(AvailableSettings.DIALECT, org.hibernate.dialect.PostgreSQLDialect.class) + .put(AvailableSettings.HBM2DDL_AUTO, org.hibernate.tool.schema.Action.UPDATE) + .put(AvailableSettings.SHOW_SQL, true) + .put(AvailableSettings.QUERY_STARTUP_CHECKING, false) + .put(AvailableSettings.GENERATE_STATISTICS, false) + .put(AvailableSettings.USE_REFLECTION_OPTIMIZER, false) + .put(AvailableSettings.USE_SECOND_LEVEL_CACHE, false) + .put(AvailableSettings.USE_QUERY_CACHE, false) + .put(AvailableSettings.USE_STRUCTURED_CACHE, false) + .put(AvailableSettings.STATEMENT_BATCH_SIZE, 20) + .put(AvailableSettings.JPA_JDBC_USER, conn.getUser()) + .put(AvailableSettings.JPA_JDBC_PASSWORD, conn.getPwd()) + .build()); + }catch(Throwable t) { + log.warn("Unable to contact DB from IS, trying local..",t); + try{ + emf=Persistence.createEntityManagerFactory(DBConstants.INTERNAL.DB_NAME); + }catch(Throwable t1) { + log.error("NO INTERNAL DATABASE FOUND ",t); + throw new RuntimeException("NO INTERNAL DATABASE",t); } + } } return emf; } - + public static void shutdown() { getEMF().close(); } @@ -106,10 +107,10 @@ public abstract class AbstractRecordManager { } //Transaction management - + EntityTransaction transaction=null; EntityManager entityManager=null; - + //************************ INSTANCE private T theRecord; @@ -122,7 +123,7 @@ public abstract class AbstractRecordManager { transaction.begin(); this.theRecord=theRecord; storeInfo(); - + this.contentHandler=new ContentHandler(theRecord); } @@ -147,24 +148,24 @@ public abstract class AbstractRecordManager { log.trace("Committing record "+theRecord+" Publish is "+publish); if (publish) validate(); - -// storeInfo(); + + // storeInfo(); log.debug("Record is valid, storing changed content"); contentHandler.storeChanges(publish); -// storeInfo(); + // storeInfo(); if(publish) { log.debug("Registering centroid of "+theRecord); registerCentroid(); -// storeInfo(); + // storeInfo(); } transaction.commit(); return theRecord; } - + @Override protected void finalize() throws Throwable { entityManager.close(); @@ -184,11 +185,11 @@ public abstract class AbstractRecordManager { log.debug("Inserting / updated centroid Row {} ",centroidRow); PreparedStatement ps = db.prepareInsertStatement(centroidsTable, true, true); - - + + log.debug("Deleting centroid if present. ID is "+theRecord.getId()); db.deleteByFieldValue(centroidsTable, new Field(DBConstants.Concessioni.ID,FieldType.TEXT), theRecord.getId()+""); - + centroidsTable.fillCSVPreparedStatament(centroidRow, ps, false); ps.executeUpdate(); db.commit(); @@ -198,8 +199,8 @@ public abstract class AbstractRecordManager { // } - - + + protected abstract Map evaluateCentroid(); protected void publish() { //TODO @@ -214,99 +215,117 @@ public abstract class AbstractRecordManager { //*********** PERSISTENCE - + private static PersistenceUnitInfo archiverPersistenceUnitInfo() { - return new PersistenceUnitInfo() { - @Override - public String getPersistenceUnitName() { - return "ApplicationPersistenceUnit"; - } - @Override - public String getPersistenceProviderClassName() { - return "org.hibernate.jpa.HibernatePersistenceProvider"; - } + final List MANAGED_CLASSES=Arrays.asList(new String[] { + "org.gcube.application.geoportal.model.Record", + "org.gcube.application.geoportal.model.concessioni.Concessione", + "org.gcube.application.geoportal.model.concessioni.LayerConcessione", + "org.gcube.application.geoportal.model.concessioni.RelazioneScavo", - @Override - public PersistenceUnitTransactionType getTransactionType() { - return PersistenceUnitTransactionType.RESOURCE_LOCAL; - } + "org.gcube.application.geoportal.model.content.AssociatedContent", + "org.gcube.application.geoportal.model.content.GeoServerContent", + "org.gcube.application.geoportal.model.content.OtherContent", + "org.gcube.application.geoportal.model.content.PersistedContent", + "org.gcube.application.geoportal.model.content.UploadedImage", + "org.gcube.application.geoportal.model.content.WorkspaceContent", - @Override - public DataSource getJtaDataSource() { - return null; - } + "org.gcube.application.geoportal.model.gis.ShapeFileLayerDescriptor", + "org.gcube.application.geoportal.model.gis.SDILayerDescriptor"}); - @Override - public DataSource getNonJtaDataSource() { - return null; - } - @Override - public List getMappingFileNames() { - return Collections.emptyList(); - } + return new PersistenceUnitInfo() { + @Override + public String getPersistenceUnitName() { + return "ApplicationPersistenceUnit"; + } - @Override - public List getJarFileUrls() { - try { - return Collections.list(this.getClass() - .getClassLoader() - .getResources("")); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } + @Override + public String getPersistenceProviderClassName() { + return "org.hibernate.jpa.HibernatePersistenceProvider"; + } - @Override - public URL getPersistenceUnitRootUrl() { - return null; - } + @Override + public PersistenceUnitTransactionType getTransactionType() { + return PersistenceUnitTransactionType.RESOURCE_LOCAL; + } - @Override - public List getManagedClassNames() { - return Collections.emptyList(); - } + @Override + public DataSource getJtaDataSource() { + return null; + } - @Override - public boolean excludeUnlistedClasses() { - return false; - } + @Override + public DataSource getNonJtaDataSource() { + return null; + } - @Override - public SharedCacheMode getSharedCacheMode() { - return null; - } + @Override + public List getMappingFileNames() { + return Collections.emptyList(); + } - @Override - public ValidationMode getValidationMode() { - return null; - } + @Override + public List getJarFileUrls() { + try { + return Collections.list(this.getClass() + .getClassLoader() + .getResources("")); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } - @Override - public Properties getProperties() { - return new Properties(); - } + @Override + public URL getPersistenceUnitRootUrl() { + return null; + } - @Override - public String getPersistenceXMLSchemaVersion() { - return null; - } + @Override + public List getManagedClassNames() { + return MANAGED_CLASSES; + } - @Override - public ClassLoader getClassLoader() { - return null; - } + @Override + public boolean excludeUnlistedClasses() { + return true; + } - @Override - public void addTransformer(ClassTransformer transformer) { + @Override + public SharedCacheMode getSharedCacheMode() { + return null; + } - } + @Override + public ValidationMode getValidationMode() { + return null; + } - @Override - public ClassLoader getNewTempClassLoader() { - return null; - } - }; + @Override + public Properties getProperties() { + return new Properties(); + } + + @Override + public String getPersistenceXMLSchemaVersion() { + return null; + } + + @Override + public ClassLoader getClassLoader() { + return null; + } + + @Override + public void addTransformer(ClassTransformer transformer) { + + } + + @Override + public ClassLoader getNewTempClassLoader() { + return null; + } + }; } }