180 lines
4.7 KiB
Java
180 lines
4.7 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.documentstore.persistence;
|
|
|
|
import java.io.Serializable;
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.Statement;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Calendar;
|
|
import java.util.SortedSet;
|
|
import java.util.TreeSet;
|
|
|
|
import org.gcube.documentstore.records.Record;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class PersistencePostgreSQL extends PersistenceBackend {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(PersistencePostgreSQL.class);
|
|
|
|
public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS Z";
|
|
|
|
public static final String URL_PROPERTY_KEY = "URL";
|
|
public static final String USERNAME_PROPERTY_KEY = "username";
|
|
public static final String PASSWORD_PROPERTY_KEY = "password";
|
|
|
|
private String url;
|
|
private String username;
|
|
private String password;
|
|
|
|
private Connection connection;
|
|
private Statement statement;
|
|
|
|
@Override
|
|
protected void prepareConnection(PersistenceBackendConfiguration configuration) throws Exception {
|
|
logger.trace("prepareConnection()");
|
|
url = configuration.getProperty(URL_PROPERTY_KEY);
|
|
username = configuration.getProperty(USERNAME_PROPERTY_KEY);
|
|
password = configuration.getProperty(PASSWORD_PROPERTY_KEY);
|
|
}
|
|
|
|
@Override
|
|
protected void openConnection() throws Exception {
|
|
logger.trace("openConnection()");
|
|
try {
|
|
Class.forName("org.postgresql.Driver");
|
|
connection = DriverManager.getConnection(url, username, password);
|
|
logger.trace("Opened database successfully");
|
|
connection.setAutoCommit(false);
|
|
statement = connection.createStatement();
|
|
} catch (Exception e) {
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
public void newConnection() throws Exception {
|
|
openConnection();
|
|
}
|
|
|
|
protected void appendString(StringBuffer values, String string) {
|
|
values.append("'");
|
|
values.append(string);
|
|
values.append("'");
|
|
}
|
|
|
|
protected void appendValue(StringBuffer values, Serializable serializable) {
|
|
if(serializable instanceof Integer || serializable instanceof Long) {
|
|
values.append(serializable.toString());
|
|
return;
|
|
}
|
|
|
|
if(serializable instanceof String) {
|
|
appendString(values, serializable.toString());
|
|
return;
|
|
}
|
|
|
|
if(serializable instanceof Calendar) {
|
|
Calendar calendar = (Calendar) serializable;
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATETIME_PATTERN);
|
|
String date = simpleDateFormat.format(calendar.getTime());
|
|
appendString(values, date);
|
|
return;
|
|
}
|
|
|
|
if(serializable instanceof Enum) {
|
|
Enum<?> e = (Enum<?>) serializable;
|
|
appendString(values, e.name());
|
|
return;
|
|
}
|
|
|
|
values.append(serializable.toString());
|
|
}
|
|
|
|
protected void appendKey(StringBuffer sql, String key) {
|
|
|
|
int lenght = key.length();
|
|
for (int i=0; i<lenght; i++) {
|
|
Character ch = key.charAt(i); /*traversing String one by one*/
|
|
if (Character.isUpperCase(ch)) {
|
|
sql.append("_");
|
|
}
|
|
sql.append(Character.toLowerCase(ch));
|
|
}
|
|
}
|
|
|
|
protected String getSQLInsertCommand(Record record) {
|
|
StringBuffer sql = new StringBuffer();
|
|
sql.append("INSERT INTO ");
|
|
sql.append(record.getRecordType().toLowerCase()); // tableNme == RecordType.toLowerCase()
|
|
boolean first = true;
|
|
SortedSet<String> keys = new TreeSet<>(record.getRequiredFields());
|
|
StringBuffer values = new StringBuffer();
|
|
for(String key : keys) {
|
|
if(first) {
|
|
sql.append(" (");
|
|
values.append(" (");
|
|
first = false;
|
|
}else {
|
|
sql.append(",");
|
|
values.append(",");
|
|
}
|
|
appendKey(sql, key);
|
|
switch (key) {
|
|
case "creationTime": case "startTime": case "endTime":
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.setTimeInMillis((long) record.getResourceProperty(key));
|
|
appendValue(values, calendar);
|
|
break;
|
|
|
|
default:
|
|
appendValue(values, record.getResourceProperty(key));
|
|
break;
|
|
}
|
|
}
|
|
sql.append(") VALUES");
|
|
sql.append(values);
|
|
sql.append(");");
|
|
return sql.toString();
|
|
}
|
|
|
|
@Override
|
|
protected void reallyAccount(Record record) throws Exception {
|
|
logger.trace("reallyAccount()");
|
|
String sqlCommand = getSQLInsertCommand(record);
|
|
statement.executeUpdate(sqlCommand);
|
|
}
|
|
|
|
public void insert(Record record) throws Exception {
|
|
reallyAccount(record);
|
|
}
|
|
|
|
@Override
|
|
protected void clean() throws Exception {
|
|
logger.trace("clean()");
|
|
}
|
|
|
|
@Override
|
|
protected void closeConnection() throws Exception {
|
|
logger.trace("closeConnection()");
|
|
statement.close();
|
|
connection.commit();
|
|
connection.close();
|
|
}
|
|
|
|
public void commitAndClose() throws Exception {
|
|
closeConnection();
|
|
}
|
|
|
|
@Override
|
|
public boolean isConnectionActive() throws Exception {
|
|
return connection.isValid(300);
|
|
};
|
|
|
|
}
|