JPA classes manually managed

This commit is contained in:
Fabio Sinibaldi 2020-10-26 12:12:43 +01:00
parent 89ff09e630
commit 14ac05ea6c
1 changed files with 136 additions and 117 deletions

View File

@ -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<T extends Record> {
private static EntityManagerFactory emf=null;
@Synchronized
@ -52,38 +53,38 @@ public abstract class AbstractRecordManager<T extends Record> {
try {
DatabaseConnection conn=ISUtils.queryForDB("postgresql", "internal-db");
log.debug("Found Internal Database : "+conn);
emf = new HibernatePersistenceProvider().createContainerEntityManagerFactory(
archiverPersistenceUnitInfo(),
ImmutableMap.<String, Object>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.<String, Object>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<T extends Record> {
}
//Transaction management
EntityTransaction transaction=null;
EntityManager entityManager=null;
//************************ INSTANCE
private T theRecord;
@ -122,7 +123,7 @@ public abstract class AbstractRecordManager<T extends Record> {
transaction.begin();
this.theRecord=theRecord;
storeInfo();
this.contentHandler=new ContentHandler(theRecord);
}
@ -147,24 +148,24 @@ public abstract class AbstractRecordManager<T extends Record> {
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<T extends Record> {
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<T extends Record> {
//
}
protected abstract Map<String,String> evaluateCentroid();
protected void publish() {
//TODO
@ -214,99 +215,117 @@ public abstract class AbstractRecordManager<T extends Record> {
//*********** 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<String> 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<String> getMappingFileNames() {
return Collections.emptyList();
}
return new PersistenceUnitInfo() {
@Override
public String getPersistenceUnitName() {
return "ApplicationPersistenceUnit";
}
@Override
public List<URL> 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<String> 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<String> getMappingFileNames() {
return Collections.emptyList();
}
@Override
public ValidationMode getValidationMode() {
return null;
}
@Override
public List<URL> 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<String> 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;
}
};
}
}