Improved persistence
This commit is contained in:
parent
7b8734bf47
commit
e77a2791c6
|
@ -3,9 +3,12 @@
|
|||
*/
|
||||
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;
|
||||
|
||||
|
@ -20,6 +23,8 @@ 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";
|
||||
|
@ -53,10 +58,60 @@ public class PersistencePostgreSQL extends PersistenceBackend {
|
|||
}
|
||||
}
|
||||
|
||||
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()); // tableNme == RecordType
|
||||
sql.append(record.getRecordType().toLowerCase()); // tableNme == RecordType.toLowerCase()
|
||||
boolean first = true;
|
||||
SortedSet<String> keys = new TreeSet<>(record.getRequiredFields());
|
||||
StringBuffer values = new StringBuffer();
|
||||
|
@ -69,9 +124,18 @@ public class PersistencePostgreSQL extends PersistenceBackend {
|
|||
sql.append(",");
|
||||
values.append(",");
|
||||
}
|
||||
sql.append(key);
|
||||
values.append(record.getResourceProperty(key));
|
||||
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);
|
||||
|
@ -86,6 +150,10 @@ public class PersistencePostgreSQL extends PersistenceBackend {
|
|||
statement.executeUpdate(sqlCommand);
|
||||
}
|
||||
|
||||
public void insert(Record record) throws Exception {
|
||||
reallyAccount(record);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void clean() throws Exception {
|
||||
logger.trace("clean()");
|
||||
|
@ -99,6 +167,10 @@ public class PersistencePostgreSQL extends PersistenceBackend {
|
|||
connection.close();
|
||||
}
|
||||
|
||||
public void commitAndClose() throws Exception {
|
||||
closeConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConnectionActive() throws Exception {
|
||||
return connection.isValid(300);
|
||||
|
|
Loading…
Reference in New Issue