76 lines
2.0 KiB
Java
76 lines
2.0 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.documentstore.persistence;
|
|
|
|
import java.sql.Statement;
|
|
import java.util.Map;
|
|
|
|
import org.gcube.accounting.utility.postgresql.PostgreSQLQuery;
|
|
import org.gcube.accounting.utility.postgresql.RecordToDBMapping;
|
|
import org.gcube.documentstore.records.AggregatedRecord;
|
|
import org.gcube.documentstore.records.Record;
|
|
import org.gcube.documentstore.records.RecordUtility;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class PersistencePostgreSQL extends PersistenceBackend {
|
|
|
|
protected static final Logger logger = LoggerFactory.getLogger(PersistencePostgreSQL.class);
|
|
|
|
protected StatementMap statementMap;
|
|
|
|
@Override
|
|
protected void prepareConnection(PersistenceBackendConfiguration configuration) throws Exception {
|
|
statementMap = new StatementMap(configuration);
|
|
Map<String, Class<? extends AggregatedRecord<?,?>>> aggregatedRecords = RecordUtility.getAggregatedRecordClassesFound();
|
|
for(String typeName : aggregatedRecords.keySet()) {
|
|
try {
|
|
Class<? extends AggregatedRecord<?,?>> clz = aggregatedRecords.get(typeName);
|
|
RecordToDBMapping.addRecordToDB(clz, configuration);
|
|
} catch (Exception e) {
|
|
new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void openConnection() throws Exception {
|
|
|
|
}
|
|
|
|
@Override
|
|
protected void reallyAccount(Record record) throws Exception {
|
|
Statement statement = statementMap.getStatement(record);
|
|
PostgreSQLQuery postgreSQLQuery = new PostgreSQLQuery();
|
|
String sqlCommand = postgreSQLQuery.getSQLInsertCommand(record);
|
|
statement.executeUpdate(sqlCommand);
|
|
}
|
|
|
|
public void insert(Record record) throws Exception {
|
|
reallyAccount(record);
|
|
}
|
|
|
|
@Override
|
|
protected void clean() throws Exception {
|
|
statementMap.close();
|
|
}
|
|
|
|
@Override
|
|
protected void closeConnection() throws Exception {
|
|
statementMap.close();
|
|
}
|
|
|
|
public void commitAndClose() throws Exception {
|
|
statementMap.close();
|
|
}
|
|
|
|
@Override
|
|
public boolean isConnectionActive() throws Exception {
|
|
return true;
|
|
};
|
|
|
|
} |