2021-03-10 16:28:08 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.documentstore.persistence;
|
|
|
|
|
2021-03-10 17:59:25 +01:00
|
|
|
import java.sql.Statement;
|
2021-03-18 11:24:46 +01:00
|
|
|
import java.util.Map;
|
2021-03-10 17:59:25 +01:00
|
|
|
|
2021-03-18 15:24:36 +01:00
|
|
|
import org.gcube.accounting.utility.postgresql.PostgreSQLQuery;
|
2021-03-18 11:24:46 +01:00
|
|
|
import org.gcube.accounting.utility.postgresql.RecordToDBMapping;
|
|
|
|
import org.gcube.documentstore.records.AggregatedRecord;
|
2021-03-10 16:28:08 +01:00
|
|
|
import org.gcube.documentstore.records.Record;
|
2021-03-18 11:24:46 +01:00
|
|
|
import org.gcube.documentstore.records.RecordUtility;
|
2021-03-10 16:28:08 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class PersistencePostgreSQL extends PersistenceBackend {
|
|
|
|
|
2021-03-18 11:24:46 +01:00
|
|
|
protected static final Logger logger = LoggerFactory.getLogger(PersistencePostgreSQL.class);
|
2021-03-10 16:28:08 +01:00
|
|
|
|
2021-03-18 11:24:46 +01:00
|
|
|
protected StatementMap statementMap;
|
2021-03-10 17:59:25 +01:00
|
|
|
|
2021-03-10 16:28:08 +01:00
|
|
|
@Override
|
|
|
|
protected void prepareConnection(PersistenceBackendConfiguration configuration) throws Exception {
|
2021-03-18 13:17:47 +01:00
|
|
|
statementMap = new StatementMap(configuration);
|
2021-03-18 11:24:46 +01:00
|
|
|
Map<String, Class<? extends AggregatedRecord<?,?>>> aggregatedRecords = RecordUtility.getAggregatedRecordClassesFound();
|
|
|
|
for(String typeName : aggregatedRecords.keySet()) {
|
|
|
|
try {
|
|
|
|
Class<? extends AggregatedRecord<?,?>> clz = aggregatedRecords.get(typeName);
|
2021-03-18 17:05:15 +01:00
|
|
|
RecordToDBMapping.addRecordToDB(clz, configuration);
|
2021-03-18 11:24:46 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 16:28:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void openConnection() throws Exception {
|
2021-03-18 11:24:46 +01:00
|
|
|
|
2021-03-12 11:22:32 +01:00
|
|
|
}
|
2021-03-18 11:24:46 +01:00
|
|
|
|
2021-03-10 16:28:08 +01:00
|
|
|
@Override
|
2021-03-10 17:59:25 +01:00
|
|
|
protected void reallyAccount(Record record) throws Exception {
|
2021-03-18 11:24:46 +01:00
|
|
|
Statement statement = statementMap.getStatement(record);
|
2021-03-18 15:24:36 +01:00
|
|
|
PostgreSQLQuery postgreSQLQuery = new PostgreSQLQuery();
|
|
|
|
String sqlCommand = postgreSQLQuery.getSQLInsertCommand(record);
|
2021-03-10 17:59:25 +01:00
|
|
|
statement.executeUpdate(sqlCommand);
|
2021-03-10 16:28:08 +01:00
|
|
|
}
|
2021-03-12 11:22:32 +01:00
|
|
|
|
|
|
|
public void insert(Record record) throws Exception {
|
|
|
|
reallyAccount(record);
|
|
|
|
}
|
2021-03-10 16:28:08 +01:00
|
|
|
|
|
|
|
@Override
|
2021-03-18 11:24:46 +01:00
|
|
|
protected void clean() throws Exception {
|
|
|
|
statementMap.close();
|
|
|
|
}
|
2021-03-10 16:28:08 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void closeConnection() throws Exception {
|
2021-03-18 11:24:46 +01:00
|
|
|
statementMap.close();
|
2021-03-10 16:28:08 +01:00
|
|
|
}
|
|
|
|
|
2021-03-12 11:22:32 +01:00
|
|
|
public void commitAndClose() throws Exception {
|
2021-03-18 11:24:46 +01:00
|
|
|
statementMap.close();
|
2021-03-12 11:22:32 +01:00
|
|
|
}
|
|
|
|
|
2021-03-10 16:28:08 +01:00
|
|
|
@Override
|
|
|
|
public boolean isConnectionActive() throws Exception {
|
2021-03-18 11:24:46 +01:00
|
|
|
return true;
|
2021-03-10 16:28:08 +01:00
|
|
|
};
|
|
|
|
|
2021-03-15 11:56:13 +01:00
|
|
|
}
|