Refactoring library
parent
dc27ed4af4
commit
41fb6aa88c
@ -0,0 +1,8 @@
|
||||
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
# Changelog for Accounting Analytics Backend Connector for PostgreSQL
|
||||
|
||||
|
||||
## [v1.0.0-SNAPSHOT]
|
||||
|
||||
- First Release
|
@ -1,3 +1,71 @@
|
||||
# accounting-analytics-persistence-postgresql
|
||||
# Accounting Analytics Backend Connector for PostgreSQL
|
||||
|
||||
Accounting Analytics Backend Connector for PostgreSQL
|
||||
|
||||
## Built With
|
||||
|
||||
* [OpenJDK](https://openjdk.java.net/) - The JDK used
|
||||
* [Maven](https://maven.apache.org/) - Dependency Management
|
||||
|
||||
## Documentation
|
||||
|
||||
[Accounting Analytics Backend Connector for PostgreSQL](https://wiki.gcube-system.org/gcube/Accounting_Analytics)
|
||||
|
||||
## Change log
|
||||
|
||||
See [Releases](https://code-repo.d4science.org/gCubeSystem/accounting-analytics-persistence-postgresql/releases).
|
||||
|
||||
## Authors
|
||||
|
||||
* **Luca Frosini** ([ORCID](https://orcid.org/0000-0003-3183-2291)) - [ISTI-CNR Infrascience Group](http://nemis.isti.cnr.it/groups/infrascience)
|
||||
|
||||
## How to Cite this Software
|
||||
|
||||
Tell people how to cite this software.
|
||||
* Cite an associated paper?
|
||||
* Use a specific BibTeX entry for the software?
|
||||
|
||||
|
||||
@Manual{,
|
||||
title = {Accounting Analytics Backend Connector for PostgreSQL},
|
||||
author = {{Frosini, Luca}},
|
||||
organization = {ISTI - CNR},
|
||||
address = {Pisa, Italy},
|
||||
year = 2021,
|
||||
url = {http://www.gcube-system.org/}
|
||||
}
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the EUPL V.1.2 License - see the [LICENSE.md](LICENSE.md) file for details.
|
||||
|
||||
|
||||
## About the gCube Framework
|
||||
This software is part of the [gCubeFramework](https://www.gcube-system.org/ "gCubeFramework"): an
|
||||
open-source software toolkit used for building and operating Hybrid Data
|
||||
Infrastructures enabling the dynamic deployment of Virtual Research Environments
|
||||
by favouring the realisation of reuse oriented policies.
|
||||
|
||||
The projects leading to this software have received funding from a series of European Union programmes including:
|
||||
|
||||
- the Sixth Framework Programme for Research and Technological Development
|
||||
- DILIGENT (grant no. 004260).
|
||||
- the Seventh Framework Programme for research, technological development and demonstration
|
||||
- D4Science (grant no. 212488);
|
||||
- D4Science-II (grant no.239019);
|
||||
- ENVRI (grant no. 283465);
|
||||
- iMarine(grant no. 283644);
|
||||
- EUBrazilOpenBio (grant no. 288754).
|
||||
- the H2020 research and innovation programme
|
||||
- SoBigData (grant no. 654024);
|
||||
- PARTHENOS (grant no. 654119);
|
||||
- EGIEngage (grant no. 654142);
|
||||
- ENVRIplus (grant no. 654182);
|
||||
- BlueBRIDGE (grant no. 675680);
|
||||
- PerformFish (grant no. 727610);
|
||||
- AGINFRAplus (grant no. 731001);
|
||||
- DESIRA (grant no. 818194);
|
||||
- ARIADNEplus (grant no. 823914);
|
||||
- RISIS2 (grant no. 824091);
|
||||
|
||||
|
||||
Accounting Analytics Backend Connector for PostgreSQL
|
@ -1,98 +0,0 @@
|
||||
package org.gcube.accounting.analytics.persistence.postgresql;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.gcube.documentstore.records.AggregatedRecord;
|
||||
import org.gcube.documentstore.records.Record;
|
||||
|
||||
public class RecordToDBMapper {
|
||||
|
||||
protected static String getRecordTypeByClass(Class<? extends AggregatedRecord<?, ?>> clz) {
|
||||
try {
|
||||
Record r = clz.newInstance();
|
||||
return r.getRecordType();
|
||||
}catch (Exception e) {
|
||||
String type = clz.getSimpleName();
|
||||
type = type.replace("Abstract", "");
|
||||
type = type.replace("Aggregated", "");
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
protected static String getKey(String fieldName) {
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
int lenght = fieldName.length();
|
||||
boolean lastLowerCase = true;
|
||||
for (int i=0; i<lenght; i++) {
|
||||
Character ch = fieldName.charAt(i); /*traversing String one by one*/
|
||||
if (Character.isUpperCase(ch)) {
|
||||
if(lastLowerCase) {
|
||||
stringBuffer.append("_");
|
||||
}
|
||||
lastLowerCase = false;
|
||||
}else {
|
||||
lastLowerCase = true;
|
||||
}
|
||||
stringBuffer.append(Character.toLowerCase(ch));
|
||||
}
|
||||
return stringBuffer.toString();
|
||||
}
|
||||
|
||||
protected final Class<? extends AggregatedRecord<?, ?>> clz;
|
||||
|
||||
protected final String typeName;
|
||||
protected final String tableName;
|
||||
|
||||
protected final Map<String, String> tableFieldToUsageRecordField;
|
||||
protected final Map<String, String> usageRecordFieldToTableField;
|
||||
|
||||
public RecordToDBMapper(Class<? extends AggregatedRecord<?, ?>> clz) throws Exception {
|
||||
this.clz = clz;
|
||||
this.typeName = getRecordTypeByClass(clz);
|
||||
this.tableName = typeName.toLowerCase();
|
||||
this.tableFieldToUsageRecordField = new HashMap<>();
|
||||
this.usageRecordFieldToTableField = new HashMap<>();
|
||||
mapFields();
|
||||
}
|
||||
|
||||
public RecordToDBMapper(String typeName, Class<? extends AggregatedRecord<?, ?>> clz) throws Exception {
|
||||
this.clz = clz;
|
||||
this.typeName = typeName;
|
||||
this.tableName = typeName.toLowerCase();
|
||||
this.tableFieldToUsageRecordField = new HashMap<>();
|
||||
this.usageRecordFieldToTableField = new HashMap<>();
|
||||
mapFields();
|
||||
}
|
||||
|
||||
protected void mapFields() throws Exception {
|
||||
Set<String> requiredFields = clz.newInstance().getRequiredFields();
|
||||
for(String usageRecordField : requiredFields) {
|
||||
String dbField = getKey(usageRecordField);
|
||||
tableFieldToUsageRecordField.put(dbField, usageRecordField);
|
||||
usageRecordFieldToTableField.put(usageRecordField, dbField);
|
||||
}
|
||||
}
|
||||
|
||||
public String getTableField(String usageRecordField) {
|
||||
return usageRecordFieldToTableField.get(usageRecordField);
|
||||
}
|
||||
|
||||
public String getUsageRecordField(String tableField) {
|
||||
String ret = tableFieldToUsageRecordField.get(tableField);
|
||||
if(ret==null && usageRecordFieldToTableField.keySet().contains(tableField)) {
|
||||
ret = tableField;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public String getTypeName() {
|
||||
return typeName;
|
||||
}
|
||||
|
||||
public String getTableName() {
|
||||
return tableName;
|
||||
}
|
||||
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
package org.gcube.accounting.analytics.persistence.postgresql;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
|
||||
import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQueryConfiguration;
|
||||
import org.gcube.accounting.persistence.AccountingPersistenceConfiguration;
|
||||
import org.gcube.documentstore.records.AggregatedRecord;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class UsageRecordDBInfo {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(UsageRecordDBInfo.class);
|
||||
|
||||
public static final String URL_PROPERTY_KEY = AccountingPersistenceConfiguration.URL_PROPERTY_KEY;
|
||||
public static final String DB_SUFFIX = "-db";
|
||||
public static final String USERNAME_SUFFIX = "-username";
|
||||
public static final String PASSWORD_SUFFIX = "-password";
|
||||
|
||||
private final String baseURL;
|
||||
private final String dbName;
|
||||
private final String url;
|
||||
private final String username;
|
||||
private final String password;
|
||||
|
||||
protected final Class<? extends AggregatedRecord<?, ?>> clz;
|
||||
protected final String typeName;
|
||||
|
||||
public UsageRecordDBInfo(Class<? extends AggregatedRecord<?, ?>> clz, AccountingPersistenceBackendQueryConfiguration configuration) throws Exception {
|
||||
this.clz = clz;
|
||||
this.typeName = UsageRecordToDBMapping.getRecordToDB(clz).getTypeName();
|
||||
|
||||
this.baseURL = configuration.getProperty(URL_PROPERTY_KEY);
|
||||
this.dbName = configuration.getProperty(typeName+DB_SUFFIX);
|
||||
this.url = baseURL + "/" + dbName;
|
||||
|
||||
this.username = configuration.getProperty(typeName+USERNAME_SUFFIX);
|
||||
this.password = configuration.getProperty(typeName+PASSWORD_SUFFIX);
|
||||
|
||||
}
|
||||
|
||||
protected Connection getConnection() throws Exception {
|
||||
Class.forName("org.postgresql.Driver");
|
||||
Connection connection = DriverManager.getConnection(url, username, password);
|
||||
logger.trace("Database {} opened successfully", url);
|
||||
connection.setAutoCommit(false);
|
||||
return connection;
|
||||
}
|
||||
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package org.gcube.accounting.analytics.persistence.postgresql;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.documentstore.records.AggregatedRecord;
|
||||
|
||||
public class UsageRecordToDBMapping {
|
||||
|
||||
protected final static Map<Class<? extends AggregatedRecord<?, ?>>, RecordToDBMapper> classToRecordToDBMapper;
|
||||
|
||||
static {
|
||||
classToRecordToDBMapper = new HashMap<>();
|
||||
}
|
||||
|
||||
public static synchronized RecordToDBMapper getRecordToDB(Class<? extends AggregatedRecord<?, ?>> clz) throws Exception {
|
||||
RecordToDBMapper recordToDBMapper = classToRecordToDBMapper.get(clz);
|
||||
if(recordToDBMapper==null) {
|
||||
recordToDBMapper = new RecordToDBMapper(clz);
|
||||
classToRecordToDBMapper.put(clz, recordToDBMapper);
|
||||
}
|
||||
return recordToDBMapper;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue