Improved persistence
This commit is contained in:
parent
7b8734bf47
commit
e77a2791c6
|
@ -3,9 +3,12 @@
|
||||||
*/
|
*/
|
||||||
package org.gcube.documentstore.persistence;
|
package org.gcube.documentstore.persistence;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
@ -20,6 +23,8 @@ public class PersistencePostgreSQL extends PersistenceBackend {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(PersistencePostgreSQL.class);
|
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 URL_PROPERTY_KEY = "URL";
|
||||||
public static final String USERNAME_PROPERTY_KEY = "username";
|
public static final String USERNAME_PROPERTY_KEY = "username";
|
||||||
public static final String PASSWORD_PROPERTY_KEY = "password";
|
public static final String PASSWORD_PROPERTY_KEY = "password";
|
||||||
|
@ -52,11 +57,61 @@ public class PersistencePostgreSQL extends PersistenceBackend {
|
||||||
throw 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) {
|
protected String getSQLInsertCommand(Record record) {
|
||||||
StringBuffer sql = new StringBuffer();
|
StringBuffer sql = new StringBuffer();
|
||||||
sql.append("INSERT INTO ");
|
sql.append("INSERT INTO ");
|
||||||
sql.append(record.getRecordType()); // tableNme == RecordType
|
sql.append(record.getRecordType().toLowerCase()); // tableNme == RecordType.toLowerCase()
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
SortedSet<String> keys = new TreeSet<>(record.getRequiredFields());
|
SortedSet<String> keys = new TreeSet<>(record.getRequiredFields());
|
||||||
StringBuffer values = new StringBuffer();
|
StringBuffer values = new StringBuffer();
|
||||||
|
@ -69,9 +124,18 @@ public class PersistencePostgreSQL extends PersistenceBackend {
|
||||||
sql.append(",");
|
sql.append(",");
|
||||||
values.append(",");
|
values.append(",");
|
||||||
}
|
}
|
||||||
sql.append(key);
|
appendKey(sql, key);
|
||||||
values.append(record.getResourceProperty(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(values);
|
sql.append(values);
|
||||||
|
@ -85,6 +149,10 @@ public class PersistencePostgreSQL extends PersistenceBackend {
|
||||||
String sqlCommand = getSQLInsertCommand(record);
|
String sqlCommand = getSQLInsertCommand(record);
|
||||||
statement.executeUpdate(sqlCommand);
|
statement.executeUpdate(sqlCommand);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void insert(Record record) throws Exception {
|
||||||
|
reallyAccount(record);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void clean() throws Exception {
|
protected void clean() throws Exception {
|
||||||
|
@ -99,6 +167,10 @@ public class PersistencePostgreSQL extends PersistenceBackend {
|
||||||
connection.close();
|
connection.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void commitAndClose() throws Exception {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isConnectionActive() throws Exception {
|
public boolean isConnectionActive() throws Exception {
|
||||||
return connection.isValid(300);
|
return connection.isValid(300);
|
||||||
|
|
Loading…
Reference in New Issue