Refactoring lib
This commit is contained in:
parent
261b0b9d8d
commit
eddcb0b451
10
pom.xml
10
pom.xml
|
@ -58,6 +58,11 @@
|
||||||
<version>42.2.19</version>
|
<version>42.2.19</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- Test Dependencies -->
|
<!-- Test Dependencies -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.gcube.accounting</groupId>
|
||||||
|
<artifactId>accounting-lib</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
|
@ -69,11 +74,6 @@
|
||||||
<artifactId>logback-classic</artifactId>
|
<artifactId>logback-classic</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.gcube.accounting</groupId>
|
|
||||||
<artifactId>accounting-lib</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
|
@ -3,14 +3,10 @@
|
||||||
*/
|
*/
|
||||||
package org.gcube.documentstore.persistence;
|
package org.gcube.documentstore.persistence;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.SortedSet;
|
|
||||||
import java.util.TreeSet;
|
|
||||||
|
|
||||||
|
import org.gcube.accounting.utility.postgresql.PostgreSQLQuery;
|
||||||
import org.gcube.accounting.utility.postgresql.RecordToDBMapping;
|
import org.gcube.accounting.utility.postgresql.RecordToDBMapping;
|
||||||
import org.gcube.documentstore.records.AggregatedRecord;
|
import org.gcube.documentstore.records.AggregatedRecord;
|
||||||
import org.gcube.documentstore.records.Record;
|
import org.gcube.documentstore.records.Record;
|
||||||
|
@ -25,12 +21,6 @@ public class PersistencePostgreSQL extends PersistenceBackend {
|
||||||
|
|
||||||
protected static final Logger logger = LoggerFactory.getLogger(PersistencePostgreSQL.class);
|
protected 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";
|
|
||||||
|
|
||||||
protected StatementMap statementMap;
|
protected StatementMap statementMap;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -54,92 +44,11 @@ public class PersistencePostgreSQL extends PersistenceBackend {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void appendString(StringBuffer values, String string) {
|
|
||||||
values.append("'");
|
|
||||||
values.append(string);
|
|
||||||
values.append("'");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void appendValue(StringBuffer values, Serializable serializable) {
|
|
||||||
if(serializable instanceof Number) {
|
|
||||||
values.append(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// String, URI etc
|
|
||||||
appendString(values, serializable.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void appendKey(StringBuffer sql, String key) {
|
|
||||||
int lenght = key.length();
|
|
||||||
boolean lastLowerCase = true;
|
|
||||||
for (int i=0; i<lenght; i++) {
|
|
||||||
Character ch = key.charAt(i); /*traversing String one by one*/
|
|
||||||
if (Character.isUpperCase(ch)) {
|
|
||||||
if(lastLowerCase) {
|
|
||||||
sql.append("_");
|
|
||||||
}
|
|
||||||
lastLowerCase = false;
|
|
||||||
}else {
|
|
||||||
lastLowerCase = true;
|
|
||||||
}
|
|
||||||
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
|
@Override
|
||||||
protected void reallyAccount(Record record) throws Exception {
|
protected void reallyAccount(Record record) throws Exception {
|
||||||
Statement statement = statementMap.getStatement(record);
|
Statement statement = statementMap.getStatement(record);
|
||||||
String sqlCommand = getSQLInsertCommand(record);
|
PostgreSQLQuery postgreSQLQuery = new PostgreSQLQuery();
|
||||||
|
String sqlCommand = postgreSQLQuery.getSQLInsertCommand(record);
|
||||||
statement.executeUpdate(sqlCommand);
|
statement.executeUpdate(sqlCommand);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ import org.gcube.accounting.datamodel.usagerecords.PortletUsageRecord;
|
||||||
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
|
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
|
||||||
import org.gcube.accounting.datamodel.usagerecords.StorageStatusRecord;
|
import org.gcube.accounting.datamodel.usagerecords.StorageStatusRecord;
|
||||||
import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord;
|
import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord;
|
||||||
|
import org.gcube.accounting.utility.postgresql.PostgreSQLQuery;
|
||||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -201,26 +202,26 @@ public class PersistencePostgreSQLTest extends ContextTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSQLStatementString() throws Exception {
|
public void testSQLStatementString() throws Exception {
|
||||||
PersistencePostgreSQL persistencePostgreSQL = new PersistencePostgreSQL();
|
|
||||||
|
|
||||||
UsageRecord usageRecord = getTestAggregatedJobUsageRecord();
|
UsageRecord usageRecord = getTestAggregatedJobUsageRecord();
|
||||||
String sql = persistencePostgreSQL.getSQLInsertCommand(usageRecord);
|
PostgreSQLQuery postgreSQLQuery = new PostgreSQLQuery();
|
||||||
|
String sql = postgreSQLQuery.getSQLInsertCommand(usageRecord);
|
||||||
logger.debug(sql);
|
logger.debug(sql);
|
||||||
|
|
||||||
usageRecord = getTestAggregatedPortletUsageRecord();
|
usageRecord = getTestAggregatedPortletUsageRecord();
|
||||||
sql = persistencePostgreSQL.getSQLInsertCommand(usageRecord);
|
sql = postgreSQLQuery.getSQLInsertCommand(usageRecord);
|
||||||
logger.debug(sql);
|
logger.debug(sql);
|
||||||
|
|
||||||
usageRecord = getTestAggregatedServiceUsageRecord();
|
usageRecord = getTestAggregatedServiceUsageRecord();
|
||||||
sql = persistencePostgreSQL.getSQLInsertCommand(usageRecord);
|
sql = postgreSQLQuery.getSQLInsertCommand(usageRecord);
|
||||||
logger.debug(sql);
|
logger.debug(sql);
|
||||||
|
|
||||||
usageRecord = getTestAggregatedStorageStatusRecord();
|
usageRecord = getTestAggregatedStorageStatusRecord();
|
||||||
sql = persistencePostgreSQL.getSQLInsertCommand(usageRecord);
|
sql = postgreSQLQuery.getSQLInsertCommand(usageRecord);
|
||||||
logger.debug(sql);
|
logger.debug(sql);
|
||||||
|
|
||||||
usageRecord = getTestAggregatedStorageUsageRecord();
|
usageRecord = getTestAggregatedStorageUsageRecord();
|
||||||
sql = persistencePostgreSQL.getSQLInsertCommand(usageRecord);
|
sql = postgreSQLQuery.getSQLInsertCommand(usageRecord);
|
||||||
logger.debug(sql);
|
logger.debug(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue