Creating library

master
Luca Frosini 3 years ago
parent 404642d876
commit ac75159b52

5
.gitignore vendored

@ -23,3 +23,8 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/target/
/.project
/.settings
/.classpath

@ -0,0 +1,8 @@
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
# Changelog for Accounting Postgresql Utilities
## [v1.0.0-SNAPSHOT]
- First Release

@ -1,3 +1,71 @@
# accounting-postgresql-utilities
# Accounting Postgresql Utilities
Accounting Postgresql Utilities
## Built With
* [OpenJDK](https://openjdk.java.net/) - The JDK used
* [Maven](https://maven.apache.org/) - Dependency Management
## Documentation
[Accounting](https://wiki.gcube-system.org/gcube/Accounting)
## Change log
See [Releases](https://code-repo.d4science.org/gCubeSystem/accounting-postgresql-utilities/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 Postgresql Utilities},
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);
This repository contains common utility classes used by accounting PostgreSQL persistences

@ -0,0 +1,61 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.gcube.tools</groupId>
<artifactId>maven-parent</artifactId>
<version>1.1.0</version>
</parent>
<groupId>org.gcube.accounting</groupId>
<artifactId>accounting-postgresql-utilities</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Accounting Postgresql Utilities</name>
<description>Utility classes to persist/query Record in PostgreSQL database</description>
<scm>
<connection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</connection>
<developerConnection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</developerConnection>
<url>https://code-repo.d4science.org/gCubeSystem/${project.artifactId}</url>
</scm>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>2.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.data.publishing</groupId>
<artifactId>document-store-lib</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.19</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -0,0 +1,50 @@
package org.gcube.accounting.utility.postgresql;
import java.sql.Connection;
import java.sql.DriverManager;
import org.gcube.documentstore.persistence.PersistenceBackendConfiguration;
import org.gcube.documentstore.records.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RecordToDBConnection {
private static final Logger logger = LoggerFactory.getLogger(RecordToDBConnection.class);
public static final String URL_PROPERTY_KEY = "url";
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 Record> clz;
protected final String typeName;
protected RecordToDBConnection(String type, Class<? extends Record> clz, PersistenceBackendConfiguration configuration) throws Exception {
this.clz = clz;
this.typeName = type;
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);
}
public 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;
}
}

@ -0,0 +1,76 @@
package org.gcube.accounting.utility.postgresql;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.gcube.documentstore.records.Record;
public class RecordToDBFields {
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 Record> clz;
protected final String typeName;
protected final String tableName;
protected final Map<String, String> tableFieldToRecordField;
protected final Map<String, String> recordFieldToTableField;
public RecordToDBFields(String typeName, Class<? extends Record> clz) throws Exception {
this.clz = clz;
this.typeName = typeName;
this.tableName = typeName.toLowerCase();
this.tableFieldToRecordField = new HashMap<>();
this.recordFieldToTableField = new HashMap<>();
mapFields();
}
protected void mapFields() throws Exception {
Set<String> requiredFields = clz.newInstance().getRequiredFields();
for(String usageRecordField : requiredFields) {
String dbField = getKey(usageRecordField);
tableFieldToRecordField.put(dbField, usageRecordField);
recordFieldToTableField.put(usageRecordField, dbField);
}
}
public String getTableField(String recordField) {
return recordFieldToTableField.get(recordField);
}
public String getRecordField(String tableField) {
String ret = tableFieldToRecordField.get(tableField);
if(ret==null && recordFieldToTableField.keySet().contains(tableField)) {
ret = tableField;
}
return ret;
}
public String getTypeName() {
return typeName;
}
public String getTableName() {
return tableName;
}
}

@ -0,0 +1,49 @@
package org.gcube.accounting.utility.postgresql;
import java.util.HashMap;
import java.util.Map;
import org.gcube.documentstore.persistence.PersistenceBackendConfiguration;
import org.gcube.documentstore.records.AggregatedRecord;
import org.gcube.documentstore.records.Record;
public class RecordToDBMapping {
public static String getRecordTypeByClass(Class<? extends Record> 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 final static Map<String, RecordToDBFields> classToRecordToDBMapper;
protected final static Map<String, RecordToDBConnection> recordToDBInfo;
static {
classToRecordToDBMapper = new HashMap<>();
recordToDBInfo= new HashMap<>();
}
public static synchronized void addRecordToDB(Class<? extends AggregatedRecord<?, ?>> clz, PersistenceBackendConfiguration configuration) throws Exception {
String type = getRecordTypeByClass(clz);
RecordToDBFields recordToDBMapper = new RecordToDBFields(type, clz);
classToRecordToDBMapper.put(type, recordToDBMapper);
RecordToDBConnection recordDBInfo = new RecordToDBConnection(type, clz, configuration);
recordToDBInfo.put(type, recordDBInfo);
}
public static synchronized RecordToDBFields getRecordToDB(Class<? extends Record> clz) throws Exception {
String type = getRecordTypeByClass(clz);
return classToRecordToDBMapper.get(type);
}
public static synchronized RecordToDBConnection getRecordDBInfo(Class<? extends Record> clz) throws Exception {
String type = getRecordTypeByClass(clz);
return recordToDBInfo.get(type);
}
}
Loading…
Cancel
Save