branch for release 3.5

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/data-analysis/tabulardata/cube-manager-parent/3.5@101332 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Lucio Lelii 2014-10-31 17:12:51 +00:00
commit e65f9da00c
158 changed files with 6209 additions and 0 deletions

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource>
<ID></ID>
<Type>Service</Type>
<Profile>
<Description>Module that declares the interface of the cube manager</Description>
<Class>DataAnalysis</Class>
<Name>cube-manager-api</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Description>Module that declares the interface of the cube manager</Description>
<Name>cube-manager-api</Name>
<Version>3.5.0-SNAPSHOT</Version>
<MavenCoordinates>
<groupId>org.gcube.data.analysis.tabulardata</groupId>
<artifactId>cube-manager-api</artifactId>
<version>3.5.0-SNAPSHOT</version>
</MavenCoordinates>
<Type>library</Type>
<Files>
<File>cube-manager-api-3.5.0-SNAPSHOT.jar</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

View File

View File

@ -0,0 +1,6 @@
gCube System - License
------------------------------------------------------------
The gCube/gCore software is licensed as Free Open Source software conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
The software and documentation is provided by its authors/distributors "as is" and no expressed or
implied warranty is given for its use, quality or fitness for a particular case.

View File

@ -0,0 +1,2 @@
Luigi Fortunati (luigi.fortunati@isti.cnr.it), CNR Pisa,
Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo".

View File

@ -0,0 +1,39 @@
The gCube System - Cube Manager API
------------------------------------------------------------
This work has been supported by the following European projects: iMarine (FP7-INFRASTRUCTURES-2011-2)
Authors
-------
Luigi Fortunati (luigi.fortunati@isti.cnr.it), CNR Pisa,
Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo".
Version and Release Date
------------------------
v. 3.5.0-SNAPSHOT (2013-10-21)
Description
-----------
Module that declares the interface of the cube manager
Download information
--------------------
Source code URL: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/tabulardata/cube-manager-parent/cube-manager-api
Documentation
-------------
Module that declares the interface of the cube manager
Licensing
---------
This software is licensed under the terms you may find in the file named "LICENSE" in this directory.

View File

@ -0,0 +1,5 @@
<ReleaseNotes>
<Changeset component="org.gcube.data.analysis.tabulardata.cube-manager-api.2.0.0" date="2013-10-21">
<Change>First version</Change>
</Changeset>
</ReleaseNotes>

View File

@ -0,0 +1 @@
https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/tabulardata/cube-manager-parent/cube-manager-api

48
cube-manager-api/pom.xml Normal file
View File

@ -0,0 +1,48 @@
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.gcube.data.analysis.tabulardata</groupId>
<artifactId>cube-manager-parent</artifactId>
<version>3.5.0-SNAPSHOT</version>
</parent>
<artifactId>cube-manager-api</artifactId>
<name>Cube Manager API</name>
<description>Module that declares the interface of the cube manager</description>
<scm>
<url>https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/tabulardata/cube-manager-parent/cube-manager-api</url>
</scm>
<dependencies>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>database-resource-api</artifactId>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>servicearchive</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,55 @@
package org.gcube.data.analysis.tabulardata.cube;
import java.util.Collection;
import org.gcube.data.analysis.tabulardata.cube.exceptions.NoSuchTableException;
import org.gcube.data.analysis.tabulardata.cube.tablemanagers.TableCreator;
import org.gcube.data.analysis.tabulardata.cube.tablemanagers.TableMetaCreator;
import org.gcube.data.analysis.tabulardata.model.column.Column;
import org.gcube.data.analysis.tabulardata.model.column.ColumnLocalId;
import org.gcube.data.analysis.tabulardata.model.metadata.common.ValidationsMetadata;
import org.gcube.data.analysis.tabulardata.model.table.Table;
import org.gcube.data.analysis.tabulardata.model.table.TableId;
import org.gcube.data.analysis.tabulardata.model.table.TableType;
import org.gcube.data.analysis.tabulardata.model.time.PeriodType;
/**
* The cube manager holds
* information on statistical data (datasets, codelists, hierarchical codelists, transformation mappings)
* and allows to perform several actions on database resources:
* <ul>
* <li>create empty tables, given a structure.</li>
* <li>clone a table (and their relationship if's a dataset)</li>
* <li>obtain information about tables, their structure (columns and indexes),
* relationships between tables</li>
* <li>add validation information on tables</li>
* </ul>
*
* @author "Luigi Fortunati"
*
*/
public interface CubeManager {
public TableCreator createTable(TableType type);
public TableMetaCreator modifyTableMeta(TableId tableId) throws NoSuchTableException;
public Collection<Table> getTables();
public Collection<Table> getTables(TableType tableType);
public Table getTable(TableId id) throws NoSuchTableException;
public void removeTable(TableId id) throws NoSuchTableException;
public Table createTimeCodelist(PeriodType periodType);
public Table removeValidations(TableId id) throws NoSuchTableException;
public Table removeColumn(TableId id, ColumnLocalId localId) throws NoSuchTableException;
public Table addValidations(TableId id, ValidationsMetadata tableValidationMetadata, Column ... validationColumns ) throws NoSuchTableException;
public Table addValidations(TableId id, Column ... validationColumns ) throws NoSuchTableException;
}

View File

@ -0,0 +1,17 @@
package org.gcube.data.analysis.tabulardata.cube.data.connection;
import java.sql.Connection;
import java.sql.SQLException;
import org.gcube.common.database.endpoint.DatabaseEndpoint;
import org.postgresql.PGConnection;
public interface DatabaseConnectionProvider {
public abstract Connection getConnection() throws SQLException;
public abstract PGConnection getPostgreSQLConnection() throws SQLException;
public abstract DatabaseEndpoint getDatabaseEndpoint();
}

View File

@ -0,0 +1,26 @@
package org.gcube.data.analysis.tabulardata.cube.exceptions;
public class CubeManagerException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = -18059704949428002L;
public CubeManagerException() {
super();
}
public CubeManagerException(String message, Throwable cause) {
super(message, cause);
}
public CubeManagerException(String message) {
super(message);
}
public CubeManagerException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,36 @@
package org.gcube.data.analysis.tabulardata.cube.exceptions;
import org.gcube.data.analysis.tabulardata.model.table.TableId;
import org.gcube.data.analysis.tabulardata.model.table.TableType;
public class NoSuchTableException extends CubeManagerException {
/**
*
*/
private static final long serialVersionUID = -3218208012859148967L;
TableId tableId;
TableType type;
public NoSuchTableException(TableId id){
super(String.format("Unable to retrieve table with id: '%1$s'.",id));
this.tableId = id;
}
public NoSuchTableException(TableId id, TableType type) {
super("Unable to retrieve table of type " + type + " and id=" + id);
this.tableId = id;
this.type = type;
}
public TableId getId() {
return tableId;
}
public TableType getType() {
return type;
}
}

View File

@ -0,0 +1,44 @@
package org.gcube.data.analysis.tabulardata.cube.exceptions;
import org.gcube.data.analysis.tabulardata.model.table.TableType;
public class TableCreationException extends CubeManagerException {
/**
*
*/
private static final long serialVersionUID = -7539479218035847331L;
Long id;
TableType type;
String reason;
public TableCreationException(String reason) {
super(reason);
}
public TableCreationException(long id, TableType type,String reason) {
super("Unable to create table of type " + type + " and id=" + id + ", Reason: " + reason);
this.id = id;
this.type = type;
}
public long getId() {
return id;
}
public TableType getType() {
return type;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
}

View File

@ -0,0 +1,40 @@
package org.gcube.data.analysis.tabulardata.cube.tablemanagers;
import java.util.List;
import org.gcube.data.analysis.tabulardata.cube.exceptions.TableCreationException;
import org.gcube.data.analysis.tabulardata.model.column.Column;
import org.gcube.data.analysis.tabulardata.model.datatype.DataType;
import org.gcube.data.analysis.tabulardata.model.table.Table;
public interface TableCreator {
public TableCreator addColumn(Column column);
public TableCreator addColumns(Column... columns);
public TableCreator like(Table table, boolean copyData);
public TableCreator like(Table table, boolean copyData, List<Column> columnsToRemove);
public Table create() throws TableCreationException;
public Table create(String tableName) throws TableCreationException;
TableCreator addColumnAfter(Column columnToAdd, Column columnBefore);
TableCreator addColumnBefore(Column columnToAdd, Column columnAfter);
TableCreator addColumnsAfter(Column columnBefore, Column ... columns);
TableCreator addColumnsBefore(Column columnAfter, Column ... columns);
TableCreator removeColumn(Column columnToRemove);
TableCreator changeColumnType(Column column, DataType newType);
TableCreator addColumnFirst(Column columnToAdd);
//TableCreator addColumnAt(Column columnToAdd, int index);
}

View File

@ -0,0 +1,28 @@
package org.gcube.data.analysis.tabulardata.cube.tablemanagers;
import org.gcube.data.analysis.tabulardata.cube.exceptions.TableCreationException;
import org.gcube.data.analysis.tabulardata.model.column.ColumnLocalId;
import org.gcube.data.analysis.tabulardata.model.metadata.column.ColumnMetadata;
import org.gcube.data.analysis.tabulardata.model.metadata.table.TableMetadata;
import org.gcube.data.analysis.tabulardata.model.table.Table;
import org.gcube.data.analysis.tabulardata.model.table.TableType;
public interface TableMetaCreator {
public TableMetaCreator setTableMetadata(TableMetadata... metadata);
public TableMetaCreator setTableType(TableType tableType);
public TableMetaCreator removeTableMetadata(Class<? extends TableMetadata> metadataType);
public TableMetaCreator removeAllTableMetadata();
public TableMetaCreator setColumnMetadata(ColumnLocalId columnId, ColumnMetadata... metadata);
public TableMetaCreator removeColumnMetadata(ColumnLocalId columnId, Class<? extends ColumnMetadata> metadataType);
public TableMetaCreator removeAllColumnMetadata(ColumnLocalId columnId);
public Table create() throws TableCreationException;
}

View File

View File

@ -0,0 +1,6 @@
gCube System - License
------------------------------------------------------------
The gCube/gCore software is licensed as Free Open Source software conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
The software and documentation is provided by its authors/distributors "as is" and no expressed or
implied warranty is given for its use, quality or fitness for a particular case.

View File

@ -0,0 +1,2 @@
Luigi Fortunati (luigi.fortunati@isti.cnr.it), CNR Pisa,
Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo".

View File

@ -0,0 +1,39 @@
The gCube System - ${name}
------------------------------------------------------------
This work has been supported by the following European projects: iMarine (FP7-INFRASTRUCTURES-2011-2)
Authors
-------
Luigi Fortunati (luigi.fortunati@isti.cnr.it), CNR Pisa,
Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo".
Version and Release Date
------------------------
v. ${project.version} (${release.date})
Description
-----------
${project.description}
Download information
--------------------
Source code URL: ${scm.url}
Documentation
-------------
${project.description}
Licensing
---------
This software is licensed under the terms you may find in the file named "LICENSE" in this directory.

View File

@ -0,0 +1,5 @@
<ReleaseNotes>
<Changeset component="${groupId}.${artifactId}.2.0.0" date="2013-10-21">
<Change>First version</Change>
</Changeset>
</ReleaseNotes>

View File

@ -0,0 +1,39 @@
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>servicearchive</id>
<formats>
<format>tar.gz</format>
</formats>
<baseDirectory>/</baseDirectory>
<fileSets>
<fileSet>
<directory>${distroDirectory}</directory>
<outputDirectory>/</outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>README</include>
<include>LICENSE</include>
<include>INSTALL</include>
<include>MAINTAINERS</include>
<include>changelog.xml</include>
</includes>
<fileMode>755</fileMode>
</fileSet>
</fileSets>
<files>
<file>
<source>${distroDirectory}/svnpath.txt</source>
<outputDirectory>/${artifactId}</outputDirectory>
</file>
<file>
<source>${configDirectory}/profile.xml</source>
<outputDirectory>/</outputDirectory>
</file>
<file>
<source>target/${build.finalName}.${packaging}</source>
<outputDirectory>/${artifactId}</outputDirectory>
</file>
</files>
</assembly>

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource>
<ID></ID>
<Type>Service</Type>
<Profile>
<Description>${project.description}</Description>
<Class>DataAnalysis</Class>
<Name>${project.artifactId}</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Description>${project.description}</Description>
<Name>${project.artifactId}</Name>
<Version>${project.version}</Version>
<MavenCoordinates>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</MavenCoordinates>
<Type>library</Type>
<Files>
<File>${project.build.finalName}.${project.packaging}</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

View File

@ -0,0 +1 @@
${scm.url}

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource>
<ID></ID>
<Type>Service</Type>
<Profile>
<Description>Cube Manager module that handles management of TD tables on a PostgreSQL DB backend.</Description>
<Class>DataAnalysis</Class>
<Name>cube-manager-data</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Description>Cube Manager module that handles management of TD tables on a PostgreSQL DB backend.</Description>
<Name>cube-manager-data</Name>
<Version>3.5.0-SNAPSHOT</Version>
<MavenCoordinates>
<groupId>org.gcube.data.analysis.tabulardata</groupId>
<artifactId>cube-manager-data</artifactId>
<version>3.5.0-SNAPSHOT</version>
</MavenCoordinates>
<Type>library</Type>
<Files>
<File>cube-manager-data-3.5.0-SNAPSHOT.jar</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

View File

View File

@ -0,0 +1,6 @@
gCube System - License
------------------------------------------------------------
The gCube/gCore software is licensed as Free Open Source software conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
The software and documentation is provided by its authors/distributors "as is" and no expressed or
implied warranty is given for its use, quality or fitness for a particular case.

View File

@ -0,0 +1,2 @@
Luigi Fortunati (luigi.fortunati@isti.cnr.it), CNR Pisa,
Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo".

View File

@ -0,0 +1,39 @@
The gCube System - Cube Manager Data
------------------------------------------------------------
This work has been supported by the following European projects: iMarine (FP7-INFRASTRUCTURES-2011-2)
Authors
-------
Luigi Fortunati (luigi.fortunati@isti.cnr.it), CNR Pisa,
Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo".
Version and Release Date
------------------------
v. 3.5.0-SNAPSHOT (2013-10-21)
Description
-----------
Cube Manager module that handles management of TD tables on a PostgreSQL DB backend.
Download information
--------------------
Source code URL: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/tabulardata/cube-manager-parent/cube-manager-data/
Documentation
-------------
Cube Manager module that handles management of TD tables on a PostgreSQL DB backend.
Licensing
---------
This software is licensed under the terms you may find in the file named "LICENSE" in this directory.

View File

@ -0,0 +1,8 @@
<ReleaseNotes>
<Changeset component="org.gcube.data.analysis.tabulardata.cube-manager-data.2.0.0" date="2013-10-21">
<Change>Implemented lazy mechanism for db connection initialization</Change>
</Changeset>
<Changeset component="org.gcube.data.analysis.tabulardata.cube-manager-data.1.0.0" date="2013-07-16">
<Change>First version</Change>
</Changeset>
</ReleaseNotes>

View File

@ -0,0 +1 @@
https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/tabulardata/cube-manager-parent/cube-manager-data/

78
cube-manager-data/pom.xml Normal file
View File

@ -0,0 +1,78 @@
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.gcube.data.analysis.tabulardata</groupId>
<artifactId>cube-manager-parent</artifactId>
<version>3.5.0-SNAPSHOT</version>
</parent>
<artifactId>cube-manager-data</artifactId>
<name>Cube Manager Data</name>
<description>Cube Manager module that handles management of TD tables on a PostgreSQL DB backend.</description>
<scm>
<url>https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/tabulardata/cube-manager-parent/cube-manager-data/</url>
</scm>
<dependencies>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>database-resource</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>7.0.41</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>20030203.000129</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.gcube.data.analysis.tabulardata</groupId>
<artifactId>tabular-model-postgresql</artifactId>
<version>[3.0.0-SNAPSHOT,4.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<artifactId>cube-manager-api</artifactId>
<groupId>org.gcube.data.analysis.tabulardata</groupId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>servicearchive</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,6 @@
package org.gcube.data.analysis.tabulardata.cube.data;
public enum Condition {
UPDATE, DELETE, INSERT
}

View File

@ -0,0 +1,66 @@
package org.gcube.data.analysis.tabulardata.cube.data;
import java.util.List;
import org.gcube.data.analysis.tabulardata.model.datatype.DataType;
import org.gcube.data.analysis.tabulardata.model.datatype.value.TDTypeValue;
/**
* Database Wrangler offers methods for managing relational tables on a DB:
* - Creation of tables
* - Removal of tables
*
* @author "Luigi Fortunati"
*
*/
public interface DatabaseWrangler {
/**
*
* @return the name of the created table
*/
public String createTable();
/**
*
* @return
*/
public void createTable(String name);
/**
*
* @param unsafe a boolean telling if the table can should be unsafe or not (tradeoff performance/reliability)
* @return the name of the created table
*/
public String createTable(boolean unsafe);
public void removeTable(String tableName);
public boolean exists(String tableName);
/**
*
* @param tableName
* @param withData
* @param unsafe
* @return the name of the created table
*/
public String cloneTable(String tableName, boolean withData, boolean unsafe);
public void addColumn(String tableName, String columnName, DataType type);
public void addColumn(String tableName, String columnName, DataType type, TDTypeValue defaultValue);
public void removeColumn(String tableName, String columnName);
public void alterColumnType(String tableName, String columnName, DataType newType);
public void createIndex(String tableName, String columnName);
public void setNullable(String tableName, String columnName, boolean nullable);
public void createUniqueIndex(String tableName, String columnName);
public void createTriggerOnTable(String triggerName, List<Condition> condition, HTime htime, String targetTableName, String procedureName);
}

View File

@ -0,0 +1,7 @@
package org.gcube.data.analysis.tabulardata.cube.data;
public enum HTime {
AFTER, BEFORE
}

View File

@ -0,0 +1,13 @@
package org.gcube.data.analysis.tabulardata.cube.data;
import java.io.InputStream;
import java.util.Collection;
import java.util.regex.Pattern;
public interface ResourceFinder {
Collection<String> getResourcesPath(Pattern patter);
InputStream getStream(String file);
}

View File

@ -0,0 +1,115 @@
package org.gcube.data.analysis.tabulardata.cube.data;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import javax.enterprise.inject.Default;
/**
* list resources available from the classpath @ *
*/
@Default
public class ResourceList implements ResourceFinder{
/* (non-Javadoc)
* @see org.gcube.data.analysis.tabulardata.cube.data.ResourceFinder#getStream(java.lang.String)
*/
@Override
public InputStream getStream(String file) {
return ClassLoader.getSystemResourceAsStream(file);
}
/**
* for all elements of java.class.path get a Collection of resources Pattern
* pattern = Pattern.compile(".*"); gets all resources
*
* @param pattern
* the pattern to match
* @return the resources in the order they are found
*/
public Collection<String> getResourcesPath(
final Pattern pattern){
final ArrayList<String> retval = new ArrayList<String>();
final String classPath = System.getProperty("java.class.path");
final String[] classPathElements = classPath.split(":");
for(final String element : classPathElements){
retval.addAll(getResources(element, pattern));
}
return retval;
}
protected Collection<String> getResources(
final String element,
final Pattern pattern){
final ArrayList<String> retval = new ArrayList<String>();
final File file = new File(element);
if(file.isDirectory()){
retval.addAll(getResourcesFromDirectory(file, pattern));
} else{
retval.addAll(getResourcesFromJarFile(file, pattern));
}
return retval;
}
@SuppressWarnings("rawtypes")
protected Collection<String> getResourcesFromJarFile(
final File file,
final Pattern pattern){
final ArrayList<String> retval = new ArrayList<String>();
ZipFile zf;
try{
zf = new ZipFile(file);
} catch(final ZipException e){
throw new Error(e);
} catch(final IOException e){
throw new Error(e);
}
final Enumeration e = zf.entries();
while(e.hasMoreElements()){
final ZipEntry ze = (ZipEntry) e.nextElement();
final String fileName = ze.getName();
final boolean accept = pattern.matcher(fileName).matches();
if(accept){
retval.add(fileName);
}
}
try{
zf.close();
} catch(final IOException e1){
throw new Error(e1);
}
return retval;
}
protected Collection<String> getResourcesFromDirectory(
final File directory,
final Pattern pattern){
final ArrayList<String> retval = new ArrayList<String>();
final File[] fileList = directory.listFiles();
for(final File file : fileList){
if(file.isDirectory()){
retval.addAll(getResourcesFromDirectory(file, pattern));
} else{
final String fileName = file.getName();
final boolean accept = pattern.matcher(fileName).matches();
if(accept){
retval.add(fileName);
}
}
}
return retval;
}
}

View File

@ -0,0 +1,340 @@
package org.gcube.data.analysis.tabulardata.cube.data;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.regex.Pattern;
import javax.annotation.PostConstruct;
import javax.enterprise.inject.Default;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.gcube.data.analysis.tabulardata.cube.data.connection.DatabaseConnectionProvider;
import org.gcube.data.analysis.tabulardata.cube.data.connection.admin.Admin;
import org.gcube.data.analysis.tabulardata.cube.data.connection.unprivileged.Unprivileged;
import org.gcube.data.analysis.tabulardata.model.datatype.DataType;
import org.gcube.data.analysis.tabulardata.model.datatype.value.TDTypeValue;
import org.gcube.data.analysis.tabulardata.model.mapping.SQLModelMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Default
@Singleton
public class SQLDatabaseWrangler implements DatabaseWrangler {
private final String DEFAULT_SCHEMA_NAME = "public";
private static Logger log = LoggerFactory.getLogger(SQLDatabaseWrangler.class);
private DatabaseConnectionProvider adminConnectionProvider;
private DatabaseConnectionProvider unprivilegedConnectionProvider;
private SQLModelMapper sqlModelMapper;
private ResourceFinder resourceFinder;
@Inject
public SQLDatabaseWrangler(@Admin DatabaseConnectionProvider adminConnectionProvider,
@Unprivileged DatabaseConnectionProvider unprivilegedConnectionProvider, SQLModelMapper sqlModelMapper, @Default ResourceFinder resourceFinder) {
super();
this.adminConnectionProvider = adminConnectionProvider;
this.unprivilegedConnectionProvider = unprivilegedConnectionProvider;
this.sqlModelMapper = sqlModelMapper;
this.resourceFinder = resourceFinder;
}
@PostConstruct
private void initializeSql() {
for (String file :resourceFinder.getResourcesPath(Pattern.compile(".*\\.sql"))){
BufferedReader reader =null;
if (!file.contains("org/gcube/data/analysis/tabulardata/sql/"))
file = "org/gcube/data/analysis/tabulardata/sql/"+file;
InputStream is = resourceFinder.getStream(file);
if (is==null)
continue;
try{
reader = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
while( ( line = reader.readLine() ) != null )
stringBuilder.append( line );
executeQuery(stringBuilder.toString());
}catch(Exception e){
throw new RuntimeException("error initializing sql",e);
}finally{
if (reader!=null )
try {
reader.close();
} catch (IOException e) {
}
}
}
}
@Override
public String createTable() {
return createTable(false);
}
@Override
public void createTable(String name){
createInternal(false, name);
}
@Override
public String createTable(boolean unsafe) {
String tableName = generateTableName();
createInternal(unsafe, tableName);
return tableName;
}
private void createInternal(boolean unsafe, String tableName){
String query = generateCreateTableQuery(tableName, unsafe);
query += generateUserAccountGrantQuery(tableName);
query += generateUserAccountGrantQuery(tableName + "_id_seq");
executeQuery(query);
}
private String generateCreateTableQuery(String tableName, boolean unlogged) {
if (unlogged)
return String.format("CREATE UNLOGGED TABLE %1$s ( id serial primary key);", tableName);
return String.format("CREATE TABLE %1$s ( id serial primary key);", tableName);
}
@Override
public void removeTable(String tableName) {
String query = generateDropTableQuery(tableName);
executeQuery(query);
}
private String generateDropTableQuery(String tableName) {
return String.format("DROP TABLE %1$s;", tableName);
}
@Override
public String cloneTable(String tableName, boolean withData, boolean unsafe) {
String newTableName = generateTableName();
String query = generateCloneTableQuery(newTableName, tableName, withData, unsafe);
query += generateUserAccountGrantQuery(newTableName);
query += generateUserAccountGrantQuery(newTableName + "_id_seq");
executeQuery(query);
return newTableName;
}
private String generateCloneTableQuery(String newTableName, String tableToCloneName, boolean withData,
boolean unsafe) {
StringBuilder sb = new StringBuilder();
String unlogged = "";
if (unsafe)
unlogged = "UNLOGGED";
String data = "";
if (withData)
data = "WITH DATA";
else
data = "WITH NO DATA";
sb.append(String.format("CREATE %1$s TABLE %2$s AS TABLE %3$s %4$s;", unlogged, newTableName, tableToCloneName,
data));
sb.append(String.format("CREATE SEQUENCE %1$s_id_seq OWNED BY %1$s.id;", newTableName));
sb.append(String.format("SELECT setval('%1$s_id_seq', max(id) ) FROM %2$s;", newTableName, tableToCloneName));
sb.append(String.format("ALTER TABLE %1$s ALTER id SET NOT NULL;", newTableName));
sb.append(String.format("ALTER TABLE %1$s ALTER id SET DEFAULT nextval('%1$s_id_seq');", newTableName));
return sb.toString();
}
@Override
public boolean exists(String tableName) {
return executeCount(String.format("SELECT count(*) FROM pg_tables WHERE tablename='%1$s'", tableName.toLowerCase()))>0;
}
@Override
public void addColumn(String tableName, String columnName, DataType type) {
String query = generateAddColumnQuery(tableName, columnName, type, null);
executeQuery(query);
}
@Override
public void addColumn(String tableName, String columnName, DataType type, TDTypeValue defaultValue) {
String query = generateAddColumnQuery(tableName, columnName, type, defaultValue);
executeQuery(query);
}
private String generateAddColumnQuery(String tableName, String columnName, DataType type,TDTypeValue defaultValue) {
return String.format("ALTER TABLE %1$s ADD COLUMN %2$s %3$s %4$s", tableName, columnName, getColumnSQLType(type), getDefaultValueSQL(defaultValue));
}
private String getColumnSQLType(DataType type) {
return sqlModelMapper.translateDataTypeToSQL(type);
}
private String getDefaultValueSQL(TDTypeValue defaultValue) {
if (defaultValue==null) return "";
else return String.format("DEFAULT %s", sqlModelMapper.translateModelValueToSQL(defaultValue));
}
@Override
public void removeColumn(String tableName, String columnName) {
String query = generateDropColumnQuery(tableName, columnName);
executeQuery(query);
}
private String generateDropColumnQuery(String tableName, String columnName) {
return String.format("ALTER TABLE %1$s DROP COLUMN %2$s;", tableName, columnName);
}
@Override
public void alterColumnType(String tableName, String columnName,
DataType newType) {
String query = generateAlterTypeQuery(tableName, columnName, newType);
executeQuery(query);
}
private String generateAlterTypeQuery(String tableName, String columnName, DataType type) {
return String.format("ALTER TABLE %1$s ALTER COLUMN %2$s TYPE %3$s;", tableName, columnName, getColumnSQLType(type));
}
private String generateTableName() {
String tableName = null;
int count = 0;
do {
tableName = RandomStringUtils.random(32, true, false).toLowerCase();
log.debug("Generated table name: " + tableName);
Connection connection = null;
Statement statement = null;
try {
connection = adminConnectionProvider.getConnection();
statement = connection.createStatement();
statement.execute(String.format(
"SELECT * FROM pg_tables WHERE schemaname='%1$s' AND tablename='%2$s';", DEFAULT_SCHEMA_NAME,
tableName));
count = statement.getFetchSize();
log.debug(String.format("Table with name '%1$s' found %2$s times.", tableName, count));
} catch (SQLException e) {
log.error("Error occurred while verifying generated table name.", e);
throw new RuntimeException("Unable to generate a table name", e);
} finally {
DbUtils.closeQuietly(connection);
DbUtils.closeQuietly(statement);
}
} while (count > 0);
return tableName;
}
public void executeQuery(String query) {
log.debug("Executing SQL query: " + query);
Connection connection = null;
Statement statement = null;
try {
connection = adminConnectionProvider.getConnection();
statement = connection.createStatement();
statement.execute(query + ";");
connection.close();
} catch (SQLException e) {
log.error("Unable to execute query: " + query, e);
throw new RuntimeException("Error encountered while executing database query: " + query,e);
} finally {
DbUtils.closeQuietly(connection);
DbUtils.closeQuietly(statement);
}
}
private int executeCount(String query) {
log.debug("Executing SQL query: " + query);
Connection connection = null;
Statement statement = null;
try {
connection = adminConnectionProvider.getConnection();
statement = connection.createStatement();
ResultSet ret = statement.executeQuery(query + ";");
int toReturn = 0;
if (ret.next())
toReturn = ret.getInt(1);
connection.close();
return toReturn;
} catch (SQLException e) {
log.error("Unable to execute query: " + query, e);
throw new RuntimeException("Error encountered while executing database query: " + query,e);
} finally {
DbUtils.closeQuietly(connection);
DbUtils.closeQuietly(statement);
}
}
private String generateUserAccountGrantQuery(String tableName) {
String unprivilegedUser = unprivilegedConnectionProvider.getDatabaseEndpoint().getCredentials().getUsername();
return String.format("GRANT SELECT,UPDATE,INSERT ON TABLE %1$s TO %2$s;", tableName, unprivilegedUser);
}
@Override
public void createIndex(String tableName, String columnName) {
Thread t = new Thread(new IndexCreator(tableName, columnName));
t.start();
}
private class IndexCreator implements Runnable {
private String tableName;
private String columnName;
public IndexCreator(String tableName, String columnName) {
this.tableName = tableName;
this.columnName = columnName;
}
@Override
public void run() {
String query = String.format("CREATE INDEX ON %1$s ( %2$s );", tableName, columnName);
executeQuery(query);
}
}
@Override
public void setNullable(String tableName, String columnName, boolean nullable) {
String notNullSnippet = "SET NOT NULL";
String nullableSnippet = "DROP NOT NULL";
executeQuery(String.format("ALTER TABLE %s ALTER COLUMN %s %s;", tableName, columnName,
nullable ? nullableSnippet : notNullSnippet));
}
@Override
public void createTriggerOnTable(String triggerName, List<Condition> conditions, HTime htime, String targetTableName, String procedure) {
if (conditions.isEmpty()) throw new IllegalArgumentException("at least a condition has to be set");
StringBuilder sBuilder = new StringBuilder();
for (Condition cond : conditions)
sBuilder.append(cond.name()).append(" OR ");
String conds = sBuilder.delete(sBuilder.length()-4, sBuilder.length()).toString();
executeQuery(String.format(
"CREATE TRIGGER %s %s %s ON %s FOR EACH ROW EXECUTE PROCEDURE %s;", triggerName, htime.name(), conds,
targetTableName, procedure));
}
@Override
public void createUniqueIndex(String tableName, String columnName) {
executeQuery(String.format("CREATE UNIQUE INDEX ON %s ( %s );", tableName, columnName));
}
}

View File

@ -0,0 +1,80 @@
package org.gcube.data.analysis.tabulardata.cube.data.connection;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.gcube.common.database.DatabaseEndpointIdentifier;
import org.gcube.common.database.DatabaseProvider;
import org.gcube.common.database.endpoint.DatabaseEndpoint;
import org.postgresql.PGConnection;
public class Tomcat7DatabaseConnectionProvider implements DatabaseConnectionProvider {
private final static String DRIVER = "org.postgresql.Driver";
private DataSource dataSource = null;
private DatabaseProvider databaseProvider;
private DatabaseEndpointIdentifier endpointIdentifier;
boolean initialized = false;
public Tomcat7DatabaseConnectionProvider(DatabaseProvider databaseProvider, DatabaseEndpointIdentifier endpointIdentifier){
this.databaseProvider = databaseProvider;
this.endpointIdentifier = endpointIdentifier;
}
private void initializeDatasource(){
PoolProperties poolConfiguration = buildPoolConfiguration();
dataSource = new DataSource();
dataSource.setPoolProperties(poolConfiguration);
initialized = true;
}
private PoolProperties buildPoolConfiguration(){
PoolProperties poolProperties = new PoolProperties();
poolProperties.setUrl(getConnectionString());
poolProperties.setDriverClassName(DRIVER);
poolProperties.setUsername(getDBUsername());
poolProperties.setPassword(getDBPassword());
return poolProperties;
}
@Override
public Connection getConnection() throws SQLException {
initializeDatasourceIfNotInitialized();
return dataSource.getConnection();
}
private void initializeDatasourceIfNotInitialized() {
if (!initialized) initializeDatasource();
}
@Override
public PGConnection getPostgreSQLConnection() throws SQLException {
initializeDatasourceIfNotInitialized();
return dataSource.getConnection().unwrap(PGConnection.class);
}
@Override
public DatabaseEndpoint getDatabaseEndpoint(){
return databaseProvider.get(endpointIdentifier);
}
private String getConnectionString(){
return getDatabaseEndpoint().getConnectionString();
}
private String getDBUsername(){
return getDatabaseEndpoint().getCredentials().getUsername();
}
private String getDBPassword(){
return getDatabaseEndpoint().getCredentials().getPassword();
}
}

View File

@ -0,0 +1,17 @@
package org.gcube.data.analysis.tabulardata.cube.data.connection.admin;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface Admin {}

View File

@ -0,0 +1,44 @@
package org.gcube.data.analysis.tabulardata.cube.data.connection.admin;
import java.sql.Connection;
import java.sql.SQLException;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.gcube.common.database.DatabaseProvider;
import org.gcube.common.database.endpoint.DatabaseEndpoint;
import org.gcube.data.analysis.tabulardata.cube.data.connection.DatabaseConnectionProvider;
import org.gcube.data.analysis.tabulardata.cube.data.connection.Tomcat7DatabaseConnectionProvider;
import org.gcube.data.analysis.tabulardata.cube.data.connection.config.DatabaseEndpointIdentifierConfiguration;
import org.postgresql.PGConnection;
@Admin
@Singleton
public class AdminDatabaseConnectionProvider implements DatabaseConnectionProvider {
DatabaseConnectionProvider delegate;
DatabaseEndpointIdentifierConfiguration databaseEndpointIdentifierProvider = new DatabaseEndpointIdentifierConfiguration();
@Inject
public AdminDatabaseConnectionProvider(DatabaseProvider databaseProvider) {
this.delegate = new Tomcat7DatabaseConnectionProvider(databaseProvider, databaseEndpointIdentifierProvider.getDataAdmin() );
}
@Override
public Connection getConnection() throws SQLException {
return delegate.getConnection();
}
@Override
public PGConnection getPostgreSQLConnection() throws SQLException {
return delegate.getPostgreSQLConnection();
}
@Override
public DatabaseEndpoint getDatabaseEndpoint() {
return delegate.getDatabaseEndpoint();
}
}

View File

@ -0,0 +1,29 @@
package org.gcube.data.analysis.tabulardata.cube.data.connection.config;
import javax.enterprise.inject.Produces;
import javax.inject.Named;
import javax.inject.Singleton;
import org.gcube.common.database.DatabaseEndpointIdentifier;
@Singleton
public class DatabaseEndpointIdentifierConfiguration {
private static final DatabaseEndpointIdentifier dataAdmin = new DatabaseEndpointIdentifier("TabularData Database",
"Data-Admin");
private static final DatabaseEndpointIdentifier dataClient = new DatabaseEndpointIdentifier("TabularData Database",
"Data-User");
@Produces
@Named("Data-Admin")
public DatabaseEndpointIdentifier getDataAdmin() {
return dataAdmin;
}
@Produces
@Named("Data-User")
public DatabaseEndpointIdentifier getDataUser() {
return dataClient;
}
}

View File

@ -0,0 +1,16 @@
package org.gcube.data.analysis.tabulardata.cube.data.connection.unprivileged;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface Unprivileged {}

View File

@ -0,0 +1,46 @@
package org.gcube.data.analysis.tabulardata.cube.data.connection.unprivileged;
import java.sql.Connection;
import java.sql.SQLException;
import javax.enterprise.inject.Default;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.gcube.common.database.DatabaseProvider;
import org.gcube.common.database.endpoint.DatabaseEndpoint;
import org.gcube.data.analysis.tabulardata.cube.data.connection.DatabaseConnectionProvider;
import org.gcube.data.analysis.tabulardata.cube.data.connection.Tomcat7DatabaseConnectionProvider;
import org.gcube.data.analysis.tabulardata.cube.data.connection.config.DatabaseEndpointIdentifierConfiguration;
import org.postgresql.PGConnection;
@Unprivileged
@Default
@Singleton
public class UnprivilegedDatabaseConnectionProvider implements DatabaseConnectionProvider {
DatabaseConnectionProvider delegate;
DatabaseEndpointIdentifierConfiguration databaseEndpointIdentifierProvider = new DatabaseEndpointIdentifierConfiguration();
@Inject
public UnprivilegedDatabaseConnectionProvider(DatabaseProvider databaseProvider) {
this.delegate = new Tomcat7DatabaseConnectionProvider(databaseProvider, databaseEndpointIdentifierProvider.getDataUser());
}
@Override
public Connection getConnection() throws SQLException {
return delegate.getConnection();
}
@Override
public PGConnection getPostgreSQLConnection() throws SQLException {
return delegate.getPostgreSQLConnection();
}
@Override
public DatabaseEndpoint getDatabaseEndpoint() {
return delegate.getDatabaseEndpoint();
}
}

View File

@ -0,0 +1,18 @@
package org.gcube.data.analysis.tabulardata.cube.data.exceptions;
public class TableAlreadyExistsException extends Exception {
/**
*
*/
private static final long serialVersionUID = 5871964853743672022L;
public TableAlreadyExistsException(String name){
super(String.format("The table with name '%1$s' already exists in the db.",name));
}
}

View File

@ -0,0 +1,7 @@
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

View File

@ -0,0 +1,3 @@
CREATE OR REPLACE FUNCTION get_day_id(timestamp with time zone) RETURNS integer as $$
SELECT to_char($1, 'YYYYMMDD')::integer;
$$ LANGUAGE SQL;

View File

@ -0,0 +1,3 @@
CREATE OR REPLACE FUNCTION get_month_id(timestamp with time zone) RETURNS integer as $$
SELECT to_char($1, 'YYYYMM')::integer;
$$ LANGUAGE SQL;

View File

@ -0,0 +1,3 @@
CREATE OR REPLACE FUNCTION get_quarter_of_year_id(timestamp with time zone) RETURNS integer as $$
SELECT to_char($1, 'YYYYQ')::integer;
$$ LANGUAGE SQL;

View File

@ -0,0 +1,3 @@
CREATE OR REPLACE FUNCTION get_year_id(timestamp with time zone) RETURNS integer as $$
SELECT to_char($1, 'YYYY')::integer;
$$ LANGUAGE SQL;

View File

@ -0,0 +1,14 @@
CREATE OR REPLACE FUNCTION is_valid_date(date_value anyelement) RETURNS boolean AS $$
DECLARE
tmp date;
BEGIN
IF date_value IS NULL THEN return false;
END IF;
tmp := CAST(date_value AS date);
return true;
EXCEPTION
WHEN OTHERS THEN RETURN false;
END;
$$ LANGUAGE plpgsql;

View File

@ -0,0 +1,4 @@
CREATE OR REPLACE FUNCTION is_valid_month(anyelement) RETURNS boolean AS $$
select $1::text~E'^\\d{4}-(0?[1-9]|1[012])$'
$$ LANGUAGE SQL

View File

@ -0,0 +1,5 @@
CREATE OR REPLACE FUNCTION is_valid_quarter(anyelement) RETURNS boolean AS $$
select $1::text~E'^[0-9]{1,4}-Q[1-4]$'
$$ LANGUAGE SQL

View File

@ -0,0 +1,3 @@
CREATE OR REPLACE FUNCTION is_valid_year(anyelement) RETURNS boolean AS $$
select $1::text~E'^\\d{4}$'
$$ LANGUAGE SQL

View File

@ -0,0 +1,3 @@
CREATE OR REPLACE FUNCTION normalize_day(timestamp with time zone) RETURNS text as $$
SELECT to_char($1, 'YYYY-MM-DD');
$$ LANGUAGE SQL;

View File

@ -0,0 +1,3 @@
CREATE OR REPLACE FUNCTION normalize_month(timestamp with time zone) RETURNS text as $$
SELECT to_char($1, 'YYYY-MM');
$$ LANGUAGE SQL;

View File

@ -0,0 +1,3 @@
CREATE OR REPLACE FUNCTION normalize_quarter_of_year(timestamp with time zone) RETURNS text as $$
SELECT to_char($1, 'YYYY-"Q"Q');
$$ LANGUAGE SQL;

View File

@ -0,0 +1,3 @@
CREATE OR REPLACE FUNCTION normalize_year(timestamp with time zone) RETURNS text as $$
SELECT to_char($1, 'YYYY');
$$ LANGUAGE SQL;

View File

@ -0,0 +1,3 @@
CREATE OR REPLACE FUNCTION to_iso_day(text) RETURNS timestamp with time zone as $$
SELECT $1::timestamp with time zone;
$$ LANGUAGE SQL;

View File

@ -0,0 +1,3 @@
CREATE OR REPLACE FUNCTION to_iso_month(text) RETURNS timestamp with time zone as $$
SELECT ($1::text || '-01')::timestamp with time zone;
$$ LANGUAGE SQL;

View File

@ -0,0 +1,3 @@
CREATE OR REPLACE FUNCTION to_iso_quarter_of_year(text) RETURNS timestamp with time zone as $$
SELECT (substring($1, '(\d{4})') || '-' || ((substring($1, '.*-Q(.)')::int)*3)-2 || '-01')::timestamp with time zone;
$$ LANGUAGE SQL;

View File

@ -0,0 +1,3 @@
CREATE OR REPLACE FUNCTION to_iso_year(anyelement) RETURNS timestamp with time zone as $$
SELECT ($1::text || '-01-01')::timestamp with time zone;
$$ LANGUAGE SQL;

View File

@ -0,0 +1,21 @@
CREATE OR REPLACE FUNCTION update_gregorian_day_dimension() RETURNS trigger as $$
DECLARE
tmp_day date;
BEGIN
IF is_valid_date(NEW.day_code) THEN
NEW.day_code := normalize_day(NEW.day_code);
tmp_day := DATE(NEW.day_code);
NEW.the_day = tmp_day;
NEW.week_of_iso_year := EXTRACT(WEEK FROM tmp_day);
NEW.month_of_year := EXTRACT(MONTH FROM tmp_day);
NEW.quarter_of_year := EXTRACT(QUARTER FROM tmp_day);
NEW.year := EXTRACT(YEAR FROM tmp_day);
NEW.iso_year := EXTRACT(ISOYEAR FROM tmp_day);
NEW.decade := EXTRACT(DECADE FROM tmp_day);
RETURN NEW;
ELSE
RAISE EXCEPTION '% is not a valid ISO8601 day code', quote_literal(tmp_day);
END IF;
END
$$ LANGUAGE plpgsql;

View File

@ -0,0 +1,18 @@
CREATE OR REPLACE FUNCTION update_gregorian_month_dimension() RETURNS trigger as $$
DECLARE
tmp_day date;
BEGIN
IF (is_valid_month(NEW.month_code)) THEN
NEW.month_code := normalize_month(NEW.month_code);
tmp_day := DATE(NEW.month_code || '-01');
NEW.month_of_year := EXTRACT(MONTH FROM tmp_day);
NEW.quarter_of_year := EXTRACT(QUARTER FROM tmp_day);
NEW.year := EXTRACT(YEAR FROM tmp_day);
NEW.decade := EXTRACT(DECADE FROM tmp_day);
RETURN NEW;
ELSE
RAISE EXCEPTION '% is not a valid ISO8601 month code', quote_literal(month_code);
END IF;
END
$$ LANGUAGE plpgsql;

View File

@ -0,0 +1,19 @@
CREATE OR REPLACE FUNCTION update_gregorian_quarter_dimension() RETURNS trigger as $$
DECLARE
tmp_date date;
BEGIN
IF is_valid_quarter(NEW.quarter_code) THEN
NEW.quarter_code := normalize_quarter(NEW.quarter_code);
NEW.quarter_of_year := SUBSTRING(SUBSTRING(NEW.quarter_code FROM 'Q[0-9]') FROM '[1-4]$');
RAISE NOTICE 'Evaluated Quearter of year: %',NEW.quarter_of_year;
NEW.year := SUBSTRING(NEW.quarter_code FROM 'Q([1-4])$') AS integer;
RAISE NOTICE 'Evaluated year: %',NEW.year;
tmp_date := NEW.year || '-01-01';
NEW.decade := EXTRACT(DECADE FROM tmp_date);
RETURN NEW;
ELSE
RAISE EXCEPTION '% is not a valid quarter code', quote_literal(NEW.quarter_code);
END IF;
END
$$ LANGUAGE plpgsql;

View File

@ -0,0 +1,16 @@
CREATE OR REPLACE FUNCTION update_gregorian_year_dimension() RETURNS trigger as $$
DECLARE
tmp_date date;
BEGIN
IF is_valid_year(NEW.year_code) THEN
NEW.year_code := normalize_year(NEW.year_code);
tmp_date := NEW.year_code || '-01-01';
NEW.year := EXTRACT(YEAR FROM tmp_date);
NEW.decade := EXTRACT(DECADE FROM tmp_date);
RETURN NEW;
ELSE
RAISE EXCEPTION '%s is not a valid ISO8601 year code', quote_literal(year_code);
END IF;
END
$$ LANGUAGE plpgsql;

View File

@ -0,0 +1,117 @@
package org.gcube.data.analysis.tabulardata.cube.data;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
import javax.inject.Inject;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data.analysis.tabulardata.model.datatype.BooleanType;
import org.gcube.data.analysis.tabulardata.model.datatype.DateType;
import org.gcube.data.analysis.tabulardata.model.datatype.GeometryType;
import org.gcube.data.analysis.tabulardata.model.datatype.IntegerType;
import org.gcube.data.analysis.tabulardata.model.datatype.NumericType;
import org.gcube.data.analysis.tabulardata.model.datatype.TextType;
import org.gcube.data.analysis.tabulardata.model.datatype.geometry.GeometryShape;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.googlecode.jeeunit.JeeunitRunner;
@RunWith(JeeunitRunner.class)
public class DatabaseWranglerTest {
@Inject
private DatabaseWrangler dw;
@BeforeClass
public static void setup() {
ScopeProvider.instance.set("/gcube/devsec");
}
@Inject
ResourceFinder finder;
@Test
public void testAll(){
System.out.println("Creating table");
String tableName = dw.createTable();
Assert.assertNotNull(tableName);
Assert.assertFalse(tableName.isEmpty());
System.out.println("Adding columns");
dw.addColumn(tableName, "field0", new BooleanType());
dw.addColumn(tableName, "field1", new DateType());
dw.addColumn(tableName, "field2", new GeometryType(GeometryShape.LINESTRING, 4));
dw.addColumn(tableName, "field3", new GeometryType(GeometryShape.POLYGON, 2));
dw.addColumn(tableName, "field4", new IntegerType());
dw.addColumn(tableName, "field5", new NumericType(3,1));
dw.addColumn(tableName, "field6", new TextType(10));
dw.addColumn(tableName, "field7", new GeometryType(2));
dw.createIndex(tableName, "field2");
dw.createIndex(tableName, "field3");
dw.createIndex(tableName, "field4");
String tmp;
System.out.println("Cloning table with data");
tmp = dw.cloneTable(tableName, true, false);
Assert.assertNotNull(tmp);
Assert.assertFalse(tmp.isEmpty());
dw.removeTable(tmp);
System.out.println("Cloning table without data");
tmp = dw.cloneTable(tableName, false, false);
Assert.assertNotNull(tmp);
Assert.assertFalse(tmp.isEmpty());
dw.removeTable(tmp);
System.out.println("Removing columns");
dw.removeColumn(tableName,"field6");
dw.removeColumn(tableName,"field7");
}
@Test
public void testUnloggedTable(){
ScopeProvider.instance.set("/gcube/devsec");
String tableName = dw.createTable(true);
Assert.assertNotNull(tableName);
Assert.assertFalse(tableName.isEmpty());
}
@Test
public void retrieveSqlFunctions() {
for (String file :finder.getResourcesPath(Pattern.compile(".*\\.sql"))){
BufferedReader reader =null;
InputStream is = ClassLoader.getSystemResourceAsStream("org/gcube/data/analysis/tabulardata/sql/"+file);
if (is==null)
continue;
try{
reader = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
while( ( line = reader.readLine() ) != null )
stringBuilder.append( line );
System.out.println(stringBuilder.toString());
}catch(Exception e){
throw new RuntimeException("error initializing sql",e);
}finally{
if (reader!=null )
try {
reader.close();
} catch (IOException e) {
}
}
}
}
}

View File

@ -0,0 +1,36 @@
package org.gcube.data.analysis.tabulardata.cube.data;
import javax.inject.Inject;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data.analysis.tabulardata.cube.data.connection.DatabaseConnectionProvider;
import org.gcube.data.analysis.tabulardata.cube.data.connection.admin.Admin;
import org.gcube.data.analysis.tabulardata.cube.data.connection.unprivileged.Unprivileged;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.googlecode.jeeunit.JeeunitRunner;
@RunWith(JeeunitRunner.class)
public class InjectionTest {
@Inject @Admin
DatabaseConnectionProvider adminDBConnProvider;
@Inject @Unprivileged
DatabaseConnectionProvider unprivilegedDBConnProvider;
@BeforeClass
public static void beforeClass(){
ScopeProvider.instance.set("/gcube/devsec");
}
@Test
public void testDatabaseConnectionProviderInjection(){
Assert.assertNotNull(adminDBConnProvider);
Assert.assertNotNull(unprivilegedDBConnProvider);
}
}

View File

@ -0,0 +1,73 @@
package org.gcube.data.analysis.tabulardata.cube.data.connection;
import java.sql.Connection;
import java.sql.SQLException;
import javax.inject.Inject;
import org.gcube.common.database.endpoint.DatabaseEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data.analysis.tabulardata.cube.data.connection.admin.Admin;
import org.gcube.data.analysis.tabulardata.cube.data.connection.admin.AdminDatabaseConnectionProvider;
import org.gcube.data.analysis.tabulardata.cube.data.connection.unprivileged.Unprivileged;
import org.gcube.data.analysis.tabulardata.cube.data.connection.unprivileged.UnprivilegedDatabaseConnectionProvider;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.postgresql.PGConnection;
import org.postgresql.copy.CopyManager;
import com.googlecode.jeeunit.JeeunitRunner;
@RunWith(JeeunitRunner.class)
public class DatabaseConnectionProviderTest {
@Inject
@Unprivileged
UnprivilegedDatabaseConnectionProvider unprivilegedDatabaseConnectionProvider;
@Inject
@Admin
AdminDatabaseConnectionProvider adminDatabaseConnectionProvider;
@Inject
DatabaseConnectionProvider defaultDatabaseConnectionProvider;
@BeforeClass
public static void beforeClass() {
ScopeProvider.instance.set("/gcube/devsec");
}
@Test
public void testUnprivileged() throws SQLException {
testDatabaseConnectionProvider(unprivilegedDatabaseConnectionProvider);
}
@Test
public void testPrivileged() throws SQLException {
testDatabaseConnectionProvider(adminDatabaseConnectionProvider);
}
public void testDatabaseConnectionProvider(DatabaseConnectionProvider databaseConnectionProvider)
throws SQLException {
Connection connection = databaseConnectionProvider.getConnection();
Assert.assertNotNull(connection);
PGConnection pgConnection = databaseConnectionProvider.getPostgreSQLConnection();
Assert.assertNotNull(pgConnection);
CopyManager copyManager = pgConnection.getCopyAPI();
Assert.assertNotNull(copyManager);
DatabaseEndpoint dbEndpoint = databaseConnectionProvider.getDatabaseEndpoint();
Assert.assertNotNull(dbEndpoint);
}
@Test
public void testDefaultDatabaseConnectionProvider() throws SQLException {
Assert.assertNotNull(defaultDatabaseConnectionProvider);
Assert.assertEquals(unprivilegedDatabaseConnectionProvider, defaultDatabaseConnectionProvider);
Connection connection = defaultDatabaseConnectionProvider.getConnection();
Assert.assertNotNull(connection);
}
}

View File

@ -0,0 +1 @@
< ャ@Qaj、F÷gク<67>

View File

View File

@ -0,0 +1,6 @@
gCube System - License
------------------------------------------------------------
The gCube/gCore software is licensed as Free Open Source software conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
The software and documentation is provided by its authors/distributors "as is" and no expressed or
implied warranty is given for its use, quality or fitness for a particular case.

View File

@ -0,0 +1,2 @@
Luigi Fortunati (luigi.fortunati@isti.cnr.it), CNR Pisa,
Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo".

View File

@ -0,0 +1,39 @@
The gCube System - ${name}
------------------------------------------------------------
This work has been supported by the following European projects: iMarine (FP7-INFRASTRUCTURES-2011-2)
Authors
-------
Luigi Fortunati (luigi.fortunati@isti.cnr.it), CNR Pisa,
Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo".
Version and Release Date
------------------------
v. ${project.version} (${release.date})
Description
-----------
${project.description}
Download information
--------------------
Source code URL: ${scm.url}
Documentation
-------------
${project.description}
Licensing
---------
This software is licensed under the terms you may find in the file named "LICENSE" in this directory.

View File

@ -0,0 +1,8 @@
<ReleaseNotes>
<Changeset component="${groupId}.${artifactId}.2.0.0" date="2013-10-21">
<Change>Implemented lazy mechanism for db connection initialization</Change>
</Changeset>
<Changeset component="${groupId}.${artifactId}.1.0.0" date="2013-07-16">
<Change>First version</Change>
</Changeset>
</ReleaseNotes>

View File

@ -0,0 +1,39 @@
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>servicearchive</id>
<formats>
<format>tar.gz</format>
</formats>
<baseDirectory>/</baseDirectory>
<fileSets>
<fileSet>
<directory>${distroDirectory}</directory>
<outputDirectory>/</outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>README</include>
<include>LICENSE</include>
<include>INSTALL</include>
<include>MAINTAINERS</include>
<include>changelog.xml</include>
</includes>
<fileMode>755</fileMode>
</fileSet>
</fileSets>
<files>
<file>
<source>${distroDirectory}/svnpath.txt</source>
<outputDirectory>/${artifactId}</outputDirectory>
</file>
<file>
<source>${configDirectory}/profile.xml</source>
<outputDirectory>/</outputDirectory>
</file>
<file>
<source>target/${build.finalName}.${packaging}</source>
<outputDirectory>/${artifactId}</outputDirectory>
</file>
</files>
</assembly>

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource>
<ID></ID>
<Type>Service</Type>
<Profile>
<Description>${project.description}</Description>
<Class>DataAnalysis</Class>
<Name>${project.artifactId}</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Description>${project.description}</Description>
<Name>${project.artifactId}</Name>
<Version>${project.version}</Version>
<MavenCoordinates>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</MavenCoordinates>
<Type>library</Type>
<Files>
<File>${project.build.finalName}.${project.packaging}</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

View File

@ -0,0 +1 @@
${scm.url}

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource>
<ID></ID>
<Type>Service</Type>
<Profile>
<Description>Cube Manager module that manages the table and column metadata and their persistence on a backend.</Description>
<Class>DataAnalysis</Class>
<Name>cube-manager-metadata</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Description>Cube Manager module that manages the table and column metadata and their persistence on a backend.</Description>
<Name>cube-manager-metadata</Name>
<Version>3.5.0-SNAPSHOT</Version>
<MavenCoordinates>
<groupId>org.gcube.data.analysis.tabulardata</groupId>
<artifactId>cube-manager-metadata</artifactId>
<version>3.5.0-SNAPSHOT</version>
</MavenCoordinates>
<Type>library</Type>
<Files>
<File>cube-manager-metadata-3.5.0-SNAPSHOT.jar</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

View File

View File

@ -0,0 +1,6 @@
gCube System - License
------------------------------------------------------------
The gCube/gCore software is licensed as Free Open Source software conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
The software and documentation is provided by its authors/distributors "as is" and no expressed or
implied warranty is given for its use, quality or fitness for a particular case.

View File

@ -0,0 +1,2 @@
Luigi Fortunati (luigi.fortunati@isti.cnr.it), CNR Pisa,
Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo".

View File

@ -0,0 +1,39 @@
The gCube System - Cube Manager Metadata
------------------------------------------------------------
This work has been supported by the following European projects: iMarine (FP7-INFRASTRUCTURES-2011-2)
Authors
-------
Luigi Fortunati (luigi.fortunati@isti.cnr.it), CNR Pisa,
Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo".
Version and Release Date
------------------------
v. 3.5.0-SNAPSHOT (2013-10-21)
Description
-----------
Cube Manager module that manages the table and column metadata and their persistence on a backend.
Download information
--------------------
Source code URL: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/tabulardata/cube-manager-parent/cube-manager-metadata/
Documentation
-------------
Cube Manager module that manages the table and column metadata and their persistence on a backend.
Licensing
---------
This software is licensed under the terms you may find in the file named "LICENSE" in this directory.

View File

@ -0,0 +1,8 @@
<ReleaseNotes>
<Changeset component="org.gcube.data.analysis.tabulardata.cube-manager-metadata.2.0.0" date="2013-10-21">
<Change>Lazy mechanism for DB connection init</Change>
</Changeset>
<Changeset component="org.gcube.data.analysis.tabulardata.cube-manager-metadata.1.0.0" date="2013-07-16">
<Change>First version</Change>
</Changeset>
</ReleaseNotes>

View File

@ -0,0 +1 @@
https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/tabulardata/cube-manager-parent/cube-manager-metadata/

View File

@ -0,0 +1,75 @@
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.gcube.data.analysis.tabulardata</groupId>
<artifactId>cube-manager-parent</artifactId>
<version>3.5.0-SNAPSHOT</version>
</parent>
<artifactId>cube-manager-metadata</artifactId>
<name>Cube Manager Metadata</name>
<description>Cube Manager module that manages the table and column metadata and their persistence on a backend.</description>
<scm>
<url>https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/tabulardata/cube-manager-parent/cube-manager-metadata/</url>
</scm>
<repositories>
<repository>
<id>eclipselink</id>
<url>http://maven.research-infrastructures.eu:8081/nexus/content/repositories/eclipselink/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>database-resource</artifactId>
</dependency>
<dependency>
<artifactId>eclipselink</artifactId>
<groupId>org.eclipse.persistence</groupId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<!-- test deps -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.166</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.data.analysis.tabulardata</groupId>
<artifactId>tabular-model-util</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>servicearchive</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,26 @@
package org.gcube.data.analysis.tabulardata.cube.metadata;
import java.util.List;
import org.gcube.data.analysis.tabulardata.cube.metadata.exceptions.NoSuchTableException;
import org.gcube.data.analysis.tabulardata.model.table.Table;
import org.gcube.data.analysis.tabulardata.model.table.TableId;
import org.gcube.data.analysis.tabulardata.model.table.TableType;
public interface CubeMetadataWrangler {
public Table get(TableId id) throws NoSuchTableException;
public Table getTableByName(String name) throws NoSuchTableException;
public List<Table> getAll();
public List<Table> getAll(TableType tableType);
// public Table update(Table table);
public Table save(Table table, boolean overwrite);
public void remove(TableId id) throws NoSuchTableException;
}

View File

@ -0,0 +1,81 @@
package org.gcube.data.analysis.tabulardata.cube.metadata;
import java.util.Map;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.gcube.common.database.DatabaseEndpointIdentifier;
import org.gcube.common.database.DatabaseProvider;
import org.gcube.common.database.endpoint.DatabaseEndpoint;
import org.gcube.common.database.endpoint.DatabaseProperty;
import com.google.common.collect.Maps;
@Singleton
public class ISEntityManagerProvider {
private DatabaseEndpointIdentifier metaDBEndpointId;
private DatabaseProvider dbProvider;
private EntityManager em;
public ISEntityManagerProvider(DatabaseEndpointIdentifier metaDBEndpointId,
DatabaseProvider dbProvider) {
this.metaDBEndpointId = metaDBEndpointId;
this.dbProvider = dbProvider;
}
public EntityManager get() {
if (em == null) {
DatabaseEndpoint dbDescriptor = dbProvider.get(metaDBEndpointId);
// Retrieve driver from properties
String driver = null;
String eclipseLinkTargetDb = null;
String ddlGenerationStrategy = null;
for (DatabaseProperty p : dbDescriptor.getProperties()) {
if (p.getKey().equals("driver"))
driver = p.getValue();
if (p.getKey().equals("eclipselink.target-database"))
eclipseLinkTargetDb = p.getValue();
if (p.getKey().equals("eclipselink.ddl-generation"))
ddlGenerationStrategy = p.getValue();
}
if (driver == null)
throw new RuntimeException("Unable to find the right driver for the connection to the DB: "
+ dbDescriptor);
Map<String, String> properties = Maps.newHashMap();
properties.put("javax.persistence.jdbc.user", dbDescriptor.getCredentials().getUsername());
properties.put("javax.persistence.jdbc.password", dbDescriptor.getCredentials().getPassword());
properties.put("javax.persistence.jdbc.driver", driver);
properties.put("javax.persistence.jdbc.url", dbDescriptor.getConnectionString());
properties.put("eclipselink.target-database", eclipseLinkTargetDb);
if (ddlGenerationStrategy != null)
properties.put("eclipselink.ddl-generation", ddlGenerationStrategy);
else
properties.put("eclipselink.ddl-generation", "create-tables");
// properties.put("eclipselink.ddl-generation","create-or-extend-tables");
// properties.put("eclipselink.ddl-generation","drop-and-create-tables");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("default", properties);
em = emf.createEntityManager();
}
return em;
}
public void close(){
em.flush();
em.close();
em = null;
}
public boolean isInitialized(){
return em!=null;
}
}

View File

@ -0,0 +1,174 @@
package org.gcube.data.analysis.tabulardata.cube.metadata;
import java.util.List;
import javax.enterprise.inject.Default;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import org.gcube.data.analysis.tabulardata.cube.metadata.exceptions.NoSuchTableException;
import org.gcube.data.analysis.tabulardata.cube.metadata.model.JPATable;
import org.gcube.data.analysis.tabulardata.cube.metadata.model.JPATableFactory;
import org.gcube.data.analysis.tabulardata.cube.metadata.model.TableFactory;
import org.gcube.data.analysis.tabulardata.model.table.Table;
import org.gcube.data.analysis.tabulardata.model.table.TableId;
import org.gcube.data.analysis.tabulardata.model.table.TableType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
@Default
@Singleton
public class JPACubeMetadataWrangler implements CubeMetadataWrangler {
Logger log = LoggerFactory.getLogger(JPACubeMetadataWrangler.class);
EntityManager em = null;
ISEntityManagerProvider emp;
@Inject
public JPACubeMetadataWrangler(ISEntityManagerProvider emp) {
super();
this.emp = emp;
}
// public Table update(Table table){
// TableConsistencyChecker.checkTableConsistency(table);
// JPATable jpaTable = JPATableFactory.createJPATable(table);
// mergeEntity(jpaTable);
// return TableFactory.createTable(jpaTable);
// }
@Override
public synchronized Table save(Table table, boolean overwrite) {
TableConsistencyChecker.checkTableConsistency(table);
JPATable jpaTable = null;
if (overwrite)
try {
jpaTable = JPATableFactory.updateJPATable(getJPATableById(table.getId().getValue()), table);
} catch (NoSuchTableException e) {
log.warn("the table with id {} is not persisted, cannot overwrite ",table.getId().getValue());
}
if (jpaTable==null)
jpaTable = JPATableFactory.createJPATable(table);
persistEntity(jpaTable);
return TableFactory.createTable(jpaTable);
}
private void initializeIfNot() {
if (em == null)
em = emp.get();
}
@Override
public synchronized Table get(TableId id) throws NoSuchTableException {
return TableFactory.createTable(getJPATableById(id.getValue()));
}
@Override
public Table getTableByName(String name) throws NoSuchTableException {
return TableFactory.createTable(getJPATableByTableName(name));
}
@Override
public synchronized List<Table> getAll() {
return Lists.transform(getAllJPATables(), new Function<JPATable, Table>() {
@Override
public Table apply(JPATable input) {
return TableFactory.createTable(input);
}
});
}
@Override
public synchronized List<Table> getAll(TableType tableType) {
return Lists.transform(getAllJPATablesByType(tableType), new Function<JPATable, Table>() {
@Override
public Table apply(JPATable input) {
return TableFactory.createTable(input);
}
});
}
@Override
public synchronized void remove(TableId id) throws NoSuchTableException {
removeEntity(getJPATableById(id.getValue()));
}
private JPATable getJPATableById(long id) throws NoSuchTableException {
JPATable table = getEntityManager().find(JPATable.class, id);
if (table==null)throw new NoSuchTableException(id);
return table;
}
private JPATable getJPATableByTableName(String name) throws NoSuchTableException {
TypedQuery<JPATable> query = getEntityManager().createNamedQuery("Table.findByName", JPATable.class);
query.setParameter("Name", name);
try{
return query.getSingleResult();
}catch(NoResultException e ){
throw new NoSuchTableException(name);
}
}
private List<JPATable> getAllJPATables() {
TypedQuery<JPATable> query = getEntityManager().createNamedQuery("Table.findAll", JPATable.class);
return query.getResultList();
}
private List<JPATable> getAllJPATablesByType(TableType tableType) {
TypedQuery<JPATable> query = getEntityManager().createNamedQuery("Table.findAllByType", JPATable.class);
query.setParameter("TableType", tableType);
return query.getResultList();
}
private void persistEntity(JPATable entity) {
try{
getEntityManager().getTransaction().begin();
if (!em.contains(entity))
getEntityManager().persist(entity);
else getEntityManager().merge(entity);
getEntityManager().flush();
getEntityManager().getTransaction().commit();
}catch(Throwable t){
log.warn("DB error",t);
em.getTransaction().rollback();
}
//log.debug("Saved entity: " + entity);
}
// private void mergeEntity(Object entity){
// log.debug("Updating entity: " + entity);
// getEntityManager().getTransaction().begin();
// getEntityManager().merge(entity);
// getEntityManager().getTransaction().commit();
// log.debug("Updated entity: " + entity);
// }
private void removeEntity(Object entity) {
getEntityManager().getTransaction().begin();
getEntityManager().remove(entity);
getEntityManager().flush();
getEntityManager().getTransaction().commit();
}
private EntityManager getEntityManager() {
initializeIfNot();
return em;
}
}

View File

@ -0,0 +1,37 @@
package org.gcube.data.analysis.tabulardata.cube.metadata;
import java.util.List;
import org.gcube.data.analysis.tabulardata.model.column.Column;
import org.gcube.data.analysis.tabulardata.model.table.Table;
public class TableConsistencyChecker {
public static void checkTableConsistency(Table table) {
if (!table.hasName())
throw new RuntimeException("Table metadata does not point to a relational table:\n" + table);
if (table.getTableType() == null)
throw new RuntimeException("Table metadata does not present a type:\n" + table);
checkColumnsConsistency(table);
checkColumnsIdOrdering(table.getColumns());
}
private static void checkColumnsIdOrdering(List<Column> columns) {
for (Column column : columns) {
ColumnConsistencyChecker.checkColumnConsistency(column);
}
}
private static void checkColumnsConsistency(Table table) {
}
private static class ColumnConsistencyChecker {
private static void checkColumnConsistency(Column column) {
if (!column.hasName())
throw new RuntimeException("Column metadata does not point to a relational table column:\n" + column);
}
}
}

View File

@ -0,0 +1,31 @@
package org.gcube.data.analysis.tabulardata.cube.metadata.config;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Named;
import org.gcube.common.database.DatabaseEndpointIdentifier;
import org.gcube.common.database.DatabaseProvider;
import org.gcube.data.analysis.tabulardata.cube.metadata.ISEntityManagerProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CDIUtil {
private Logger logger = LoggerFactory.getLogger(CDIUtil.class);
@Produces
public ISEntityManagerProvider createEntityManager(@Named("Metadata-Admin") DatabaseEndpointIdentifier metaDBEndpointId,
DatabaseProvider dbProvider, InjectionPoint injectionPoint){
logger.info("ISEntityManagerProvider produced");
return new ISEntityManagerProvider(metaDBEndpointId, dbProvider);
}
public void disposeEntityManagerProvider(@Disposes ISEntityManagerProvider emp){
emp.close();
logger.info("ISEntityManagerProvider disposed");
}
}

View File

@ -0,0 +1,21 @@
package org.gcube.data.analysis.tabulardata.cube.metadata.config;
import javax.enterprise.inject.Produces;
import javax.inject.Named;
import javax.inject.Singleton;
import org.gcube.common.database.DatabaseEndpointIdentifier;
@Singleton
public class DatabaseEndpointIdentifierConfiguration {
private static final DatabaseEndpointIdentifier metaAdmin = new DatabaseEndpointIdentifier("TabularData Database",
"Metadata-Admin");
@Produces
@Named("Metadata-Admin")
public DatabaseEndpointIdentifier getMetaAdmin() {
return metaAdmin;
}
}

View File

@ -0,0 +1,39 @@
package org.gcube.data.analysis.tabulardata.cube.metadata.exceptions;
import org.gcube.data.analysis.tabulardata.model.table.TableType;
public class NoSuchTableException extends Exception {
/**
*
*/
private static final long serialVersionUID = -3218208012859148967L;
long id;
TableType type;
public NoSuchTableException(long id){
super(String.format("Unable to retrieve table with id '%1$s'.",id));
this.id = id;
}
public NoSuchTableException(String name){
super(String.format("Unable to retrieve table with name '%1$s'.",name));
}
public NoSuchTableException(long id, TableType type) {
super("Unable to retrieve table of type " + type + " and id=" + id);
this.id = id;
this.type = type;
}
public long getId() {
return id;
}
public TableType getType() {
return type;
}
}

View File

@ -0,0 +1,153 @@
package org.gcube.data.analysis.tabulardata.cube.metadata.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.gcube.data.analysis.tabulardata.model.column.ColumnType;
import org.gcube.data.analysis.tabulardata.model.datatype.DataType;
import org.gcube.data.analysis.tabulardata.model.metadata.column.ColumnMetadata;
import org.gcube.data.analysis.tabulardata.model.relationship.ColumnRelationship;
import com.google.common.collect.Lists;
@Entity(name = "TDColumn")
@SequenceGenerator(name = "tdcolumn_seq", sequenceName = "tdcolumn_seq", allocationSize = 1)
public class JPAColumn {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tdcolumn_seq")
private long id;
private String localId;
private String name;
private ColumnType type;
private DataType dataType;
private int position;
private ColumnRelationship relationship = null;
private ArrayList<ColumnMetadata> metadata = Lists.newArrayList();
@Column(insertable = true, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
public Date createdTimestamp = new Date();
@Column(insertable = true, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
public Date updatedTimestamp = new Date();
public JPAColumn() {
}
public void setColumnId(long id) {
this.id = id;
}
public long getColumnId() {
return id;
}
public String getLocalId() {
return localId;
}
public void setLocalId(String localId) {
this.localId = localId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ColumnType getType() {
return type;
}
public void setType(ColumnType type) {
this.type = type;
}
public DataType getDataType() {
return dataType;
}
public void setDataType(DataType dataType) {
this.dataType = dataType;
}
public ColumnRelationship getRelationship() {
return relationship;
}
public void setRelationship(ColumnRelationship relationship) {
this.relationship = relationship;
}
public Collection<ColumnMetadata> getMetadata() {
return metadata;
}
public void setMetadata(Collection<ColumnMetadata> metadata) {
this.metadata = Lists.newArrayList(metadata);
}
/**
* @return the position
*/
public int getPosition() {
return position;
}
/**
* @param position the position to set
*/
public void setPosition(int position) {
this.position = position;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("JPAColumn [\n\tid=");
builder.append(id);
builder.append(",\n\tposition=");
builder.append(position);
builder.append(",\n\tlocalId=");
builder.append(localId);
builder.append(",\n\tname=");
builder.append(name);
builder.append(",\n\ttype=");
builder.append(type);
builder.append(",\n\tdataType=");
builder.append(dataType);
builder.append(",\n\trelationship=");
builder.append(relationship);
builder.append(",\n\tmetadata=");
builder.append(metadata);
builder.append(",\n\tcreatedTimestamp=");
builder.append(createdTimestamp);
builder.append(",\n\tupdatedTimestamp=");
builder.append(updatedTimestamp);
builder.append("\n]");
return builder.toString();
}
}

View File

@ -0,0 +1,136 @@
package org.gcube.data.analysis.tabulardata.cube.metadata.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.gcube.data.analysis.tabulardata.model.column.ColumnLocalId;
import org.gcube.data.analysis.tabulardata.model.metadata.table.TableMetadata;
import org.gcube.data.analysis.tabulardata.model.table.TableType;
import com.google.common.collect.Lists;
@Entity(name = "TDTable")
@NamedQueries({ @NamedQuery(name = "Table.findAll", query = "SELECT t FROM TDTable AS t"),
@NamedQuery(name = "Table.findAllByType", query = "SELECT t FROM TDTable AS t WHERE t.tableType = :TableType"),
@NamedQuery(name = "Table.findById", query = "SELECT t FROM TDTable AS t WHERE t.id = :Id") ,
@NamedQuery(name = "Table.findByName", query = "SELECT t FROM TDTable AS t WHERE t.name = :Name") })
@SequenceGenerator(name = "tdtable_seq", sequenceName = "tdtable_seq", allocationSize = 1)
public class JPATable {
@Id
@Column(name = "ID", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tdtable_seq")
private long id;
private String name;
private TableType tableType;
@OneToMany(cascade = CascadeType.ALL)
@OrderBy(value="position")
private Collection<JPAColumn> columns = Lists.newArrayList();
private ArrayList<TableMetadata> metadata = Lists.newArrayList();
@Column(insertable = true, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
public Date createdTimestamp = new Date();
@Column(insertable = true, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
public Date updatedTimestamp = new Date();
public JPATable() {
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public TableType getTableType() {
return tableType;
}
public void setTableType(TableType tableType) {
this.tableType = tableType;
}
public Collection<JPAColumn> getColumns() {
return columns;
}
public void setColumns(Collection<JPAColumn> columns) {
this.columns = columns;
}
public Collection<TableMetadata> getMetadata() {
return metadata;
}
public void setMetadata(Collection<TableMetadata> metadata) {
this.metadata = Lists.newArrayList(metadata);
}
public Date getCreatedTimestamp() {
return createdTimestamp;
}
public Date getUpdatedTimestamp() {
return updatedTimestamp;
}
public JPAColumn getColumn(ColumnLocalId localId){
for (JPAColumn col : columns)
if (col.getLocalId()== localId.getValue())
return col;
return null;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("JPATable [\n\tid=");
builder.append(id);
builder.append(",\n\tname=");
builder.append(name);
builder.append(",\n\ttableType=");
builder.append(tableType);
builder.append(",\n\tcolumns=");
builder.append(columns);
builder.append(",\n\tmetadata=");
builder.append(metadata);
builder.append(",\n\tcreatedTimestamp=");
builder.append(createdTimestamp);
builder.append(",\n\tupdatedTimestamp=");
builder.append(updatedTimestamp);
builder.append("\n]");
return builder.toString();
}
}

View File

@ -0,0 +1,73 @@
package org.gcube.data.analysis.tabulardata.cube.metadata.model;
import java.util.ArrayList;
import java.util.List;
import org.gcube.data.analysis.tabulardata.model.column.Column;
import org.gcube.data.analysis.tabulardata.model.table.Table;
public class JPATableFactory {
public static JPATable createJPATable(Table table){
JPATable result = new JPATable();
result.setName(table.getName());
result.setTableType(table.getTableType());
int pos=0;
for (Column column : table.getColumns())
result.getColumns().add(JPAColumnFactory.createJPAColumn(column, pos++));
result.getMetadata().addAll(table.getAllMetadata());
return result;
}
public static JPATable updateJPATable(JPATable jpaTable, Table table){
jpaTable.setName(table.getName());
jpaTable.setTableType(table.getTableType());
List<JPAColumn> columns= new ArrayList<JPAColumn>();
int pos=0;
for (Column column : table.getColumns()) {
JPAColumn jpaColumn = null;
if((jpaColumn=jpaTable.getColumn(column.getLocalId()))!=null)
columns.add(JPAColumnFactory.updateJPAColumn(jpaColumn, column, pos++));
else
columns.add(JPAColumnFactory.createJPAColumn(column, pos++));
}
jpaTable.setColumns(columns);
jpaTable.getMetadata().addAll(table.getAllMetadata());
return jpaTable;
}
private static class JPAColumnFactory {
public static JPAColumn createJPAColumn(Column column, int position){
JPAColumn result = new JPAColumn();
result.setLocalId(column.getLocalId().getValue());
result.setName(column.getName());
result.setType(column.getColumnType());
result.setDataType(column.getDataType());
result.setRelationship(column.getRelationship());
result.setMetadata(column.getAllMetadata());
result.setPosition(position);
return result;
}
public static JPAColumn updateJPAColumn(JPAColumn jpaColumn, Column column, int position){
JPAColumn result = new JPAColumn();
result.setColumnId(jpaColumn.getColumnId());
result.setLocalId(column.getLocalId().getValue());
result.setName(column.getName());
result.setType(column.getColumnType());
result.setDataType(column.getDataType());
result.setRelationship(column.getRelationship());
result.setMetadata(column.getAllMetadata());
result.setPosition(position);
return result;
}
}
}

View File

@ -0,0 +1,47 @@
package org.gcube.data.analysis.tabulardata.cube.metadata.model;
import java.util.Collection;
import java.util.List;
import org.gcube.data.analysis.tabulardata.model.column.Column;
import org.gcube.data.analysis.tabulardata.model.column.ColumnLocalId;
import org.gcube.data.analysis.tabulardata.model.table.Table;
import org.gcube.data.analysis.tabulardata.model.table.TableId;
import com.google.common.collect.Lists;
public class TableFactory {
public static Table createTable(JPATable jpaTable) {
List<Column> columns = ColumnFactory.createColumns(jpaTable.getColumns());
Table resultTable = new Table(jpaTable.getTableType());
resultTable.setId(new TableId(jpaTable.getId()));
resultTable.setColumns(columns);
resultTable.setName(jpaTable.getName());
resultTable.setAllMetadata(jpaTable.getMetadata());
return resultTable;
}
private static class ColumnFactory {
public static List<Column> createColumns(Collection<JPAColumn> columns) {
List<Column> result = Lists.newArrayList();
for (JPAColumn jpaColumn : columns) {
Column newcolumn = createColumn(jpaColumn);
result.add(newcolumn);
}
return result;
}
private static Column createColumn(JPAColumn jpaColumn) {
Column result = null;
result = new Column(new ColumnLocalId(jpaColumn.getLocalId()), jpaColumn.getDataType(), jpaColumn.getType());
result.setName(jpaColumn.getName());
result.setRelationship(jpaColumn.getRelationship());
result.setAllMetadata(jpaColumn.getMetadata());
return result;
}
}
}

View File

@ -0,0 +1,7 @@
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!-- Second model -->
<class>org.gcube.data.analysis.tabulardata.cube.metadata.model.JPATable</class>
<class>org.gcube.data.analysis.tabulardata.cube.metadata.model.JPAColumn</class>
<properties>
<!-- <property name="javax.persistence.jdbc.user" value="sa" /> -->
<!-- <property name="javax.persistence.jdbc.password" value="sa" /> -->
<!-- <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"
/> -->
<!-- <property name="javax.persistence.jdbc.url" value="jdbc:h2:repository/meta-data"
/> -->
<!-- <property name="eclipselink.target-database" -->
<!-- value="org.eclipse.persistence.platform.database.H2Platform" /> -->
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<!-- <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> -->
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<!-- <property name="eclipselink.logging.level" value="OFF" /> -->
<property name="eclipselink.logging.level" value="WARNING" />
<!-- <property name="eclipselink.logging.level" value="INFO" /> -->
<!-- <property name="eclipselink.logging.level" value="FINER" /> -->
</properties>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,132 @@
package org.gcube.data.analysis.tabulardata.cube.metadata;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.inject.Inject;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data.analysis.tabulardata.model.column.Column;
import org.gcube.data.analysis.tabulardata.model.column.ColumnLocalId;
import org.gcube.data.analysis.tabulardata.model.column.factories.AnnotationColumnFactory;
import org.gcube.data.analysis.tabulardata.model.column.factories.CodeColumnFactory;
import org.gcube.data.analysis.tabulardata.model.column.factories.CodeNameColumnFactory;
import org.gcube.data.analysis.tabulardata.model.column.type.IdColumnType;
import org.gcube.data.analysis.tabulardata.model.datatype.IntegerType;
import org.gcube.data.analysis.tabulardata.model.metadata.common.ImmutableLocalizedText;
import org.gcube.data.analysis.tabulardata.model.metadata.table.ImportMetadata;
import org.gcube.data.analysis.tabulardata.model.table.Table;
import org.gcube.data.analysis.tabulardata.model.table.type.GenericTableType;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.googlecode.jeeunit.JeeunitRunner;
@RunWith(JeeunitRunner.class)
public class CubeMetadataWranglerTest {
@Inject
private CubeMetadataWrangler cmw;
private static Table startingTable;
@BeforeClass
public static void beforeClass() {
ScopeProvider.instance.set("/gcube/devsec");
}
@BeforeClass
public static void setUp() {
List<Column> columns = new ArrayList<Column>();
Column idColumn = new Column(new ColumnLocalId("test"), new IntegerType(), new IdColumnType());
idColumn.setName(UUID.randomUUID().toString());
columns.add(idColumn);
Column codeColumn = new CodeColumnFactory().createDefault();
codeColumn.setName(UUID.randomUUID().toString());
columns.add(codeColumn);
Column codeNameColumn = new CodeNameColumnFactory().create("en");
codeNameColumn.setName(UUID.randomUUID().toString());
columns.add(codeNameColumn);
Column annotationColumn = new AnnotationColumnFactory().create(new ImmutableLocalizedText("La mia annotazione", "it"));
annotationColumn.setName(UUID.randomUUID().toString());
columns.add(annotationColumn);
startingTable = new Table(new GenericTableType());
startingTable.setName(UUID.randomUUID().toString());
startingTable.setColumns(columns);
startingTable.setMetadata(new ImportMetadata("SMDX", "http://localhost:8080/myservice", new Date()));
}
private Table saveTable(Table table) {
return cmw.save(table, false);
}
@Test
public void testSave() {
System.out.println("Save Test");
Table savedTable = saveTable(startingTable);
System.out.println("Starting Table:\n" + startingTable);
System.out.println("Saved Table:\n" + savedTable);
Assert.assertNotNull(savedTable);
Assert.assertTrue(startingTable.sameStructureAs(savedTable));
//Assert.assertFalse(startingTable.equals(savedTable));
//Assert.assertFalse(savedTable.equals(startingTable));
}
@Test
public void testGet() throws Exception {
System.out.println("Get test (cmw is null? "+(cmw==null)+" )");
Table savedTable = saveTable(startingTable);
Table loadedTable1 = cmw.get(savedTable.getId());
Table loadedTable2 = cmw.get(savedTable.getId());
System.out.println("Saved Table:\n" + savedTable);
System.out.println("First loaded Table:\n" + loadedTable1);
System.out.println("Second loaded Table:\n" + loadedTable2);
Assert.assertEquals(savedTable, loadedTable1);
Assert.assertEquals(loadedTable1, loadedTable2);
Assert.assertEquals(savedTable, loadedTable2);
Assert.assertTrue(savedTable.sameStructureAs(loadedTable1));
Assert.assertTrue(savedTable.sameStructureAs(loadedTable2));
}
@Test
public void testGetAll() {
Table created = saveTable(startingTable);
List<Table> tables = cmw.getAll();
System.out.println("All tables: " + tables);
Assert.assertTrue(tables.contains(created));
}
@Test
public void testGetAllByType() {
Table created = saveTable(startingTable);
List<Table> tables = cmw.getAll(created.getTableType());
System.out.println("All tables of type " + startingTable.getTableType() + ": " + tables);
Assert.assertTrue(tables.contains(created));
}
@Test
public void testSaveTwice() {
System.out.println("Save-Twice test");
Table savedTable1 = saveTable(startingTable);
Table savedTable2 = saveTable(savedTable1);
System.out.println("First saved table:\n" + savedTable1);
System.out.println("Second saved table:\n" + savedTable2);
Assert.assertTrue(savedTable1.sameStructureAs(savedTable2));
Assert.assertFalse(savedTable1.equals(savedTable2));
Assert.assertTrue(savedTable2.getId().getValue() > savedTable1.getId().getValue());
}
}

View File

@ -0,0 +1,22 @@
package org.gcube.data.analysis.tabulardata.cube.metadata;
import javax.inject.Inject;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.googlecode.jeeunit.JeeunitRunner;
@RunWith(JeeunitRunner.class)
public class InjectionTest {
@Inject
JPACubeMetadataWrangler bean;
@Test
public void test() {
Assert.assertNotNull(bean);
}
}

Some files were not shown because too many files have changed in this diff Show More