commit b9b698b6fc0e6e136e486526bd5d5346d8cc56ce Author: Giancarlo Panichi Date: Wed Feb 1 16:06:09 2017 +0000 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-widgetx-tdx-source@141991 82a268e6-3cf1-43bd-a215-b396298e98cf diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..3e662f9 --- /dev/null +++ b/.classpath @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..1b0542b --- /dev/null +++ b/.project @@ -0,0 +1,36 @@ + + + tabular-data-widgetx-tdx-source + + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/.settings/com.google.gwt.eclipse.core.prefs b/.settings/com.google.gwt.eclipse.core.prefs new file mode 100644 index 0000000..82c36af --- /dev/null +++ b/.settings/com.google.gwt.eclipse.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +filesCopiedToWebInfLib= diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..29abf99 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding//src/test/resources=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..443e085 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000..7111e17 --- /dev/null +++ b/.settings/org.eclipse.wst.common.component @@ -0,0 +1,6 @@ + + + + + + diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..4f92af5 --- /dev/null +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.settings/org.eclipse.wst.validation.prefs b/.settings/org.eclipse.wst.validation.prefs new file mode 100644 index 0000000..04cad8c --- /dev/null +++ b/.settings/org.eclipse.wst.validation.prefs @@ -0,0 +1,2 @@ +disabled=06target +eclipse.preferences.version=1 diff --git a/distro/LICENSE b/distro/LICENSE new file mode 100644 index 0000000..2d9616a --- /dev/null +++ b/distro/LICENSE @@ -0,0 +1 @@ +${gcube.license} \ No newline at end of file diff --git a/distro/README b/distro/README new file mode 100644 index 0000000..739a430 --- /dev/null +++ b/distro/README @@ -0,0 +1,68 @@ +The gCube System - ${name} +-------------------------------------------------- + +${description} + + +${gcube.description} + +${gcube.funding} + + +Version +-------------------------------------------------- + +${version} (${buildDate}) + +Please see the file named "changelog.xml" in this directory for the release notes. + + +Authors +-------------------------------------------------- + +* Giancarlo Panichi (giancarlo.panichi-AT-isti.cnr.it), + Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" CNR, Pisa IT + + +Maintainers +----------- + +* Giancarlo Panichi (giancarlo.panichi-AT-isti.cnr.it), + Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" CNR, Pisa IT + + +Download information +-------------------------------------------------- + +Source code is available from SVN: + ${scm.url} + +Binaries can be downloaded from the gCube website: + ${gcube.website} + + +Installation +-------------------------------------------------- + +Installation documentation is available on-line in the gCube Wiki: + https://gcube.wiki.gcube-system.org/gcube/index.php/Tabular_Data_Manager + + +Documentation +-------------------------------------------------- + +Documentation is available on-line in the gCube Wiki: + https://gcube.wiki.gcube-system.org/gcube/index.php/Tabular_Data_Manager + + +Support +-------------------------------------------------- + +Bugs and support requests can be reported in the gCube issue tracking tool: + ${gcube.issueTracking} + + +Licensing +-------------------------------------------------- + +This software is licensed under the terms you may find in the file named "LICENSE" in this directory. diff --git a/distro/changelog.xml b/distro/changelog.xml new file mode 100644 index 0000000..4961238 --- /dev/null +++ b/distro/changelog.xml @@ -0,0 +1,40 @@ + + + Updated to new PortalContext [ticket #6548] + + + Added fix for support new portal theme + + + Added different colors for columns of different type [issue #251] + + + Updated in line filter to suppurt soundex, text begin and text + end + + + Added column ordering + Updated in line filter + Updated numeric type to double + + + Updated pom dependencies + Updated Geometry type support + + + Updated grid behavior + + + Updated grid, now shows single view column + + + Added Inline Filter + + + Added ColumnDefinitionBuilder + Fixed QueryOrder + + + First Release + + \ No newline at end of file diff --git a/distro/descriptor.xml b/distro/descriptor.xml new file mode 100644 index 0000000..b468e51 --- /dev/null +++ b/distro/descriptor.xml @@ -0,0 +1,32 @@ + + servicearchive + + tar.gz + + / + + + ${distroDirectory} + / + true + + README + LICENSE + changelog.xml + profile.xml + + 755 + true + + + + + target/${build.finalName}.${project.packaging} + /${artifactId} + + + + \ No newline at end of file diff --git a/distro/profile.xml b/distro/profile.xml new file mode 100644 index 0000000..62ae9a7 --- /dev/null +++ b/distro/profile.xml @@ -0,0 +1,29 @@ + + + + Library + + ${project.description} + PortletsUser + ${project.name} + ${version} + + + ${project.description} + ${project.name} + ${version} + + ${project.groupId} + ${project.artifactId} + ${project.version} + + library + + ${project.build.finalName}.${project.packaging} + + + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..31e388d --- /dev/null +++ b/pom.xml @@ -0,0 +1,175 @@ + + 4.0.0 + + maven-parent + org.gcube.tools + 1.0.0 + + + + + org.gcube.portlets.user + tabular-data-widgetx-tdx-source + 1.9.0-SNAPSHOT + + tabular-data-widgetx-tdx-source + tabular-data-widgetx-tdx-source allows access to tabular data service + + + https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-widgetx-tdx-source + + + + + Giancarlo Panichi + g.panichi@isti.cnr.it + CNR Pisa, Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" + + architect + developer + + + + + + ${project.build.directory}/${project.build.finalName} + distro + config + + + 2.5.1 + + ${env.KEYS} + + + UTF-8 + UTF-8 + + + + + localRun + + + org.slf4j + slf4j-api + compile + + + ch.qos.logback + logback-classic + 1.0.1 + runtime + + + + + + + + org.gcube.portlets.user + tabular-data-widgetx + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + + + + + org.gcube.portlets.user + tabular-data-widget-common-event + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + provided + + + + + org.gcube.data.analysis.tabulardata + service-client-impl + [2.0.0-SNAPSHOT,3.0.0-SNAPSHOT) + provided + + + + org.json + json + 20090211 + + + + + org.slf4j + slf4j-api + + + + + + + junit + junit + 4.8.1 + test + + + + + + + ${webappDirectory}/WEB-INF/classes + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.16 + + true + + + + + org.apache.maven.surefire + surefire-junit47 + 2.16 + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 2.2 + + + ${distroDirectory}/descriptor.xml + + + + + servicearchive + install + + single + + + + + + + + + + + + + org.gcube.distribution + maven-portal-bom + LATEST + pom + import + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/Constants.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/Constants.java new file mode 100644 index 0000000..b107d8f --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/Constants.java @@ -0,0 +1,11 @@ +package org.gcube.portlets.user.tdwx.datasource.td; + +/** + * + * @author "Giancarlo Panichi" + * g.panichi@isti.cnr.it + * + */ +public class Constants { + public static final String TDX_DATASOURCE_FACTORY_ID = "TDXDataSourceFactory"; +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/TDXDataSource.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/TDXDataSource.java new file mode 100644 index 0000000..5af1685 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/TDXDataSource.java @@ -0,0 +1,648 @@ +/** + * + */ +package org.gcube.portlets.user.tdwx.datasource.td; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.List; + +import org.gcube.data.analysis.tabulardata.commons.webservice.exception.NoSuchTableException; +import org.gcube.data.analysis.tabulardata.model.column.Column; +import org.gcube.data.analysis.tabulardata.model.metadata.common.TableDescriptorMetadata; +import org.gcube.data.analysis.tabulardata.query.parameters.QueryFilter; +import org.gcube.data.analysis.tabulardata.query.parameters.QueryOrder; +import org.gcube.data.analysis.tabulardata.query.parameters.QueryOrderDirection; +import org.gcube.data.analysis.tabulardata.query.parameters.QueryPage; +import org.gcube.data.analysis.tabulardata.service.TabularDataService; +import org.gcube.data.analysis.tabulardata.service.impl.TabularDataServiceFactory; +import org.gcube.data.analysis.tabulardata.service.tabular.TabularResourceId; +import org.gcube.portlets.user.td.widgetcommonevent.shared.tr.column.RelationshipData; +import org.gcube.portlets.user.tdwx.datasource.td.filters.FiltersBuilder; +import org.gcube.portlets.user.tdwx.datasource.td.map.ColumnDefinitionBuilder; +import org.gcube.portlets.user.tdwx.datasource.td.trservice.TRService; +import org.gcube.portlets.user.tdwx.server.datasource.DataSourceX; +import org.gcube.portlets.user.tdwx.server.datasource.DataSourceXException; +import org.gcube.portlets.user.tdwx.server.datasource.Direction; +import org.gcube.portlets.user.tdwx.server.datasource.util.TableJSonBuilder; +import org.gcube.portlets.user.tdwx.server.util.ServiceCredentials; +import org.gcube.portlets.user.tdwx.shared.ColumnsReorderingConfig; +import org.gcube.portlets.user.tdwx.shared.FilterInformation; +import org.gcube.portlets.user.tdwx.shared.StaticFilterInformation; +import org.gcube.portlets.user.tdwx.shared.model.ColumnDefinition; +import org.gcube.portlets.user.tdwx.shared.model.ColumnType; +import org.gcube.portlets.user.tdwx.shared.model.TableDefinition; +import org.gcube.portlets.user.tdwx.shared.model.TableId; +import org.gcube.portlets.user.tdwx.shared.model.ValueType; +import org.json.JSONArray; +import org.json.JSONException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author "Giancarlo Panichi" g.panichi@isti.cnr.it + * + */ +public class TDXDataSource implements DataSourceX { + + private static final int PAGINGDIMENSION = 300; + private static final String PRIMARY_KEY_COLUMN = "id"; + private static final String JSON_ROWS_FIELD = "ROWS"; + private static final String JSON_TOTAL_LENGTH_FIELD = "total"; + private static final String JSON_OFFSET_FIELD = "offset"; + + private Logger logger = LoggerFactory.getLogger(TDXDataSource.class); + private String dataSourceFactoryId; + private String tableName; + private TableDefinition tableDefinition; + private int tableSize = -1; + private TableJSonBuilder jsonBuilder; + + private TabularDataService service; + private org.gcube.data.analysis.tabulardata.model.table.TableId serviceTableId; + private org.gcube.data.analysis.tabulardata.model.table.Table serviceTable; + private long serviceTabularResourceId; + private TRService trService; + private long tableId; + + /** + * + * @param dataSourceFactoryId + * @param tableName + * @return + */ + public static TDXDataSource createTDDataSource(String dataSourceFactoryId, + ServiceCredentials serviceCredentials, String tableName) + throws DataSourceXException { + TDXDataSource dataSource = new TDXDataSource(dataSourceFactoryId, + serviceCredentials, tableName); + return dataSource; + } + + /** + * + * @param dataSourceFactoryId + * @param tableIdentifier + * @throws DataSourceXException + */ + public TDXDataSource(String dataSourceFactoryId, ServiceCredentials serviceCredentials, + String tableIdentifier) throws DataSourceXException { + if (dataSourceFactoryId == null) { + logger.error("An error occurred, dataSourceFactoryId is null"); + throw new DataSourceXException( + "An error occurred, dataSourceFactoryId is null"); + } + + if (tableIdentifier == null) { + logger.error("An error occurred, tableName is null"); + throw new DataSourceXException( + "An error occurred, tableName is null"); + } + + this.dataSourceFactoryId = dataSourceFactoryId; + this.tableName = tableIdentifier; + + + service = TabularDataServiceFactory.getService(); + + try { + tableId = Long.parseLong(tableIdentifier); + } catch (NumberFormatException e) { + logger.error("An error occurred, tableName is not a long", e); + throw new DataSourceXException( + "An error occurred, no tableName is not a long", e); + } + + serviceTableId = new org.gcube.data.analysis.tabulardata.model.table.TableId( + tableId); + + try { + serviceTable = service.getTable(serviceTableId); + } catch (NoSuchTableException e) { + logger.error("An error occurred, no such table", e); + throw new DataSourceXException("An error occurred, no such table", + e); + } + + // logger.debug("Service Table: " + serviceTable); + + TableDescriptorMetadata tableDesc = null; + + if (serviceTable.contains(TableDescriptorMetadata.class)) { + tableDesc = serviceTable.getMetadata(TableDescriptorMetadata.class); + if (tableDesc.getRefId() == 0) { + logger.error("Error refId=0 for Table:" + serviceTable); + throw new DataSourceXException( + "Error no valid tabular resource associated with the table:" + + serviceTable.getId()); + } else { + logger.debug("Table " + serviceTable.getId() + + " connect to tabular resource: " + + tableDesc.getRefId()); + serviceTabularResourceId = tableDesc.getRefId(); + } + + } else { + logger.debug("No TableDescriptorMetadata found (Supposed Time Table):" + + tableId); + /* + * TIME TABLE hasn't tabular resource connected + */ + /* + * throw new DataSourceXException( + * "Error no valid tabular resource associated with the table:" + + * serviceTable.getId()); + */ + } + + trService = new TRService(); + trService.setService(service); + TabularResourceId tabularResourceId = new TabularResourceId( + serviceTabularResourceId); + trService.setTabularResourceId(tabularResourceId); + } + + @Override + public String getDataSourceFactoryId() { + return dataSourceFactoryId; + } + + @Override + public TableDefinition getTableDefinition() throws DataSourceXException { + logger.debug("Retrieving table definition"); + tableDefinition = extractTableDefinition(); + return tableDefinition; + } + + /** + * + * @return + * @throws DataSourceXException + */ + protected TableDefinition extractTableDefinition() + throws DataSourceXException { + List columns = getColumnDefinitions(); + + logger.debug("Creating tableId..."); + TableId id = new TableId(dataSourceFactoryId, tableName); + + TableDefinition tableDefinition = new TableDefinition(id, tableName, + JSON_ROWS_FIELD, JSON_TOTAL_LENGTH_FIELD, JSON_OFFSET_FIELD, + columns); + + tableDefinition.setModelKeyColumnId(PRIMARY_KEY_COLUMN); + logger.debug("TableDefinition Created"); + return tableDefinition; + + } + + protected List getColumnDefinitions() + throws DataSourceXException { + logger.debug("Creating list of columns definition..."); + List serviceListColumn = serviceTable.getColumns(); + ArrayList columns = new ArrayList(); + ArrayList dimensions = new ArrayList(); + + Column serviceColumn; + for (int i = 0; i < serviceListColumn.size(); i++) { + serviceColumn = serviceListColumn.get(i); + ColumnDefinition column = getColumnDefinition(serviceColumn, i); + columns.add(column); + if (column.getType() == ColumnType.DIMENSION + || column.getType() == ColumnType.TIMEDIMENSION) { + dimensions.add(column); + } + } + + for (int i = 0; i < dimensions.size(); i++) { + ColumnDefinition dim = dimensions.get(i); + RelationshipData rel = dim.getRelationshipData(); + + if (rel != null) { + String cId = rel.getTargetColumnId(); + if (cId != null) { + for (int j = 0; j < columns.size(); j++) { + ColumnDefinition c = columns.get(j); + if (c.getColumnLocalId() != null + && (c.getType() == ColumnType.VIEWCOLUMN_OF_DIMENSION || c + .getType() == ColumnType.VIEWCOLUMN_OF_TIMEDIMENSION) + && c.getColumnLocalId().compareTo(cId) == 0) { + c.setVisible(true); + columns.set(j, c); + break; + } + } + } + } + } + + logger.debug("List of columns definition created"); + return columns; + } + + protected ColumnDefinition getColumnDefinition(Column serviceColumn, + int ordinalPosition) throws DataSourceXException { + ColumnDefinitionBuilder columnDefinitionBuilder = new ColumnDefinitionBuilder( + service, serviceTable, serviceColumn, ordinalPosition); + ColumnDefinition columnDefinition = columnDefinitionBuilder.build(); + + /* logger.debug("Column Definition:" + columnDefinition); */ + + return columnDefinition; + } + + protected ColumnDefinition createPrimaryKeyColumn( + List columns) { + List ids = new ArrayList(columns.size()); + for (ColumnDefinition column : columns) + ids.add(column.getId()); + + String id = PRIMARY_KEY_COLUMN; + for (int i = 0; ids.contains(id); id = PRIMARY_KEY_COLUMN + i++) + ; + + return new ColumnDefinition(id, id, id, ValueType.INTEGER, -1, false, + false, + org.gcube.portlets.user.tdwx.shared.model.ColumnType.SYSTEM); + } + + protected void retrieveTableSize(int start, int limit, + QueryFilter queryFilter) throws DataSourceXException { + tableSize = 0; + try { + tableSize = service.getQueryLenght(serviceTableId, queryFilter); + } catch (NoSuchTableException e) { + logger.error("An error occurred, tableSize is not recovered", e); + throw new DataSourceXException( + "An error occurred, tableSize is not recovered", e); + } + start = Math.max(0, start); + start = Math.min(start, tableSize); + if (start + limit > tableSize) + limit = tableSize - start; + logger.debug("checked bounds start: " + start + " limit: " + limit); + } + + // Request from user + @Override + public String getDataAsJSon(int start, int limit, String sortingColumn, + Direction direction, ArrayList filters, + ArrayList staticFilters) + throws DataSourceXException { + logger.debug("getDataAsJSon start: " + start + " limit: " + limit + + " sortingColumn: " + sortingColumn + " direction: " + + direction + " filters:" + filters.size() + " staticFilters:" + + staticFilters.size()); + + TableDefinition tableDefinition = getTableDefinition(); + logger.debug("Creating queryOrder..."); + QueryOrder queryOrder = null; + if (sortingColumn != null) { + if (tableDefinition.getColumns().get(sortingColumn) == null) { + logger.error("The specified sorting column \"" + sortingColumn + + "\" don't exists"); + + throw new DataSourceXException( + "The specified sorting column \"" + sortingColumn + + "\" don't exists"); + } else { + ColumnDefinition columnDefinition = tableDefinition + .getColumns().get(sortingColumn); + Column column = serviceTable.getColumnByName(columnDefinition + .getId()); + switch (direction) { + case ASC: + queryOrder = new QueryOrder(column.getLocalId(), + QueryOrderDirection.ASCENDING); + break; + case DESC: + queryOrder = new QueryOrder(column.getLocalId(), + QueryOrderDirection.DESCENDING); + break; + default: + break; + } + } + } else { + if (tableDefinition.getColumns().get(PRIMARY_KEY_COLUMN) == null) { + logger.error("The primary key column \"" + PRIMARY_KEY_COLUMN + + "\" don't exists"); + } else { + ColumnDefinition columnDefinition = tableDefinition + .getColumns().get(PRIMARY_KEY_COLUMN); + Column column = serviceTable.getColumnByName(columnDefinition + .getId()); + queryOrder = new QueryOrder(column.getLocalId(), + QueryOrderDirection.ASCENDING); + } + } + + QueryFilter queryFilter = null; + if ((filters != null && filters.size() > 0) + || (staticFilters != null && staticFilters.size() > 0)) { + FiltersBuilder filtersBuilder = new FiltersBuilder(filters, + staticFilters, tableDefinition, serviceTable); + queryFilter = filtersBuilder.createQueryFilter(); + } + + retrieveTableSize(start, limit, queryFilter); + + String json = getJSon(start, queryOrder, queryFilter); + logger.trace("Returning json"); + // logger.debug(json); + return json; + + } + + protected ArrayList sort( + Collection columns) { + + ArrayList lcolumns = new ArrayList(); + for (ColumnDefinition column : columns) { + lcolumns.add(column); + } + + Collections.sort(lcolumns, new Comparator() { + @Override + public int compare(ColumnDefinition cd1, ColumnDefinition cd2) { + int comp = 0; + if (cd1.getPosition() == cd2.getPosition()) { + comp = 0; + } else { + if (cd1.getPosition() > cd2.getPosition()) { + comp = 1; + } else { + comp = -1; + } + } + return comp; + } + }); + + return lcolumns; + + } + + protected String getJSon(int start, QueryOrder queryOrder, + QueryFilter queryFilter) throws DataSourceXException { + logger.debug("Retrieving JSon"); + logger.debug("[" + queryOrder + ", " + queryFilter + "]"); + + TableDefinition tableDefinition = getTableDefinition(); + logger.debug("Retrieved table definition"); + Collection columns = tableDefinition.getColumns() + .values(); + logger.debug("Retrieved Columns"); + ArrayList lcolumns = sort(columns); + + // logger.debug("ColumnDefinition:\n" + lcolumns.toString()); + + QueryPage queryPage = new QueryPage(start, PAGINGDIMENSION); + logger.debug("Created queryPage"); + String serviceJson = null; + try { + if (queryOrder == null && queryFilter == null) { + serviceJson = service.queryAsJson(serviceTableId, queryPage); + } else { + if (queryOrder == null && queryFilter != null) { + serviceJson = service.queryAsJson(serviceTableId, + queryPage, queryFilter); + } else { + if (queryOrder != null && queryFilter == null) { + serviceJson = service.queryAsJson(serviceTableId, + queryPage, queryOrder); + } else { + if (queryOrder != null && queryFilter != null) { + logger.debug("Order & Filter: " + queryOrder + " " + + queryFilter); + serviceJson = service.queryAsJson(serviceTableId, + queryPage, queryFilter, queryOrder); + } else { + logger.debug("No queryAsJson valid"); + } + } + } + + } + } catch (NoSuchTableException e) { + logger.error("An error occurred, no such table", e); + throw new DataSourceXException("An error occurred, no such table", + e); + } catch (Throwable e) { + logger.error("An error occurred", e); + throw new DataSourceXException("An error occurred", e); + } + logger.debug("Created serviceJson"); + // logger.debug(serviceJson); + + return createJson(start, serviceJson, lcolumns); + } + + protected String createJson(int start, String serviceJson, + ArrayList lcolumns) throws DataSourceXException { + TableJSonBuilder json = getBuilder(); + json.startRows(); + int id = start; + + JSONArray currentRow = null; + int i = -1; + int j = -1; + int totalRows = -1; + String s = null; + try { + org.json.JSONObject obj = new org.json.JSONObject(serviceJson); + org.json.JSONArray rows = obj.getJSONArray("rows"); + + totalRows = rows.length(); + logger.debug("Reading rows from json"); + for (i = 0; i < totalRows; i++) { + + json.startRow(); + currentRow = rows.getJSONArray(i); + + j = 0; + + for (ColumnDefinition column : lcolumns) { + + String columnId = column.getId(); + + if (currentRow.isNull(j)) { + json.addValue(columnId, ""); + } else { + + switch (column.getValueType()) { + case DATE: + Long day = currentRow.getLong(j); + Date dd = new Date(); + dd.setTime(day); + json.addValue(columnId, day); + break; + case BOOLEAN: + Boolean b = currentRow.getBoolean(j); + json.addValue(columnId, b); + break; + case DOUBLE: + Double d = currentRow.getDouble(j); + json.addValue(columnId, d); + break; + case INTEGER: + int integ = currentRow.getInt(j); + json.addValue(columnId, integ); + break; + case LONG: + Long l = currentRow.getLong(j); + json.addValue(columnId, l); + break; + case STRING: + s = currentRow.getString(j); + json.addValue(columnId, s); + break; + case GEOMETRY: + s = currentRow.getString(j); + json.addValue(columnId, s); + break; + default: + logger.warn("Unknow value type " + + column.getValueType()); + } + } + j++; + } + + json.endRow(); + } + id += i; + + } catch (JSONException e) { + + logger.error("An error occurred while parsing json document\n" + + "At Row " + i + ",Column " + j + "\nRow Content: " + + currentRow + "\nLenght rows " + totalRows, e); + throw new DataSourceXException( + "An error occurred, while reading json of service", e); + } + + json.endRows(); + + json.setTotalLength(tableSize); + + json.setOffset(start); + + json.close(); + + logger.trace("produced " + (id - start) + " rows"); + + return json.toString(); + + } + + protected TableJSonBuilder getBuilder() throws DataSourceXException { + try { + if (jsonBuilder == null) { + TableDefinition tdef = getTableDefinition(); + logger.debug("Creating jsonBuilder..."); + if (tdef != null) { + jsonBuilder = new TableJSonBuilder(tdef); + } else { + logger.error("table definition is null"); + throw new DataSourceXException("table definition is null"); + } + } else { + jsonBuilder.clean(); + } + return jsonBuilder; + } catch (Exception e) { + logger.debug("Error Creating jsonBuilder: " + e.getMessage()); + throw new DataSourceXException("Error Creating jsonBuilder: " + + e.getMessage()); + } + } + + protected int getTableSize() throws DataSourceXException { + return tableSize; + } + + public void close() { + // The service is stateless there is no need to close + + } + + @Override + public TableDefinition setColumnReordering( + ColumnsReorderingConfig columnsReorderingConfig) + throws DataSourceXException { + logger.debug("SetColumnReordering: " + columnsReorderingConfig); + + trService.startChangeSingleColumnPosition(columnsReorderingConfig); + + updateTableAfterOperation(); + + logger.debug("Retrieving table definition"); + tableDefinition = extractTableDefinition(); + return tableDefinition; + } + + protected void updateTableAfterOperation() throws DataSourceXException { + + serviceTableId = new org.gcube.data.analysis.tabulardata.model.table.TableId( + tableId); + + try { + serviceTable = service.getTable(serviceTableId); + } catch (NoSuchTableException e) { + logger.error("An error occurred, no such table", e); + throw new DataSourceXException("An error occurred, no such table", + e); + } + + // logger.debug("Service Table: " + serviceTable); + + TableDescriptorMetadata tableDesc = null; + + if (serviceTable.contains(TableDescriptorMetadata.class)) { + tableDesc = serviceTable.getMetadata(TableDescriptorMetadata.class); + if (tableDesc.getRefId() == 0) { + logger.debug("Error refId=0 for Table:" + serviceTable); + throw new DataSourceXException( + "Error no valid tabular resource associated with the table:" + + serviceTable.getId()); + } else { + logger.debug("Table " + serviceTable.getId() + + " connect to tabular resource: " + + tableDesc.getRefId()); + serviceTabularResourceId = tableDesc.getRefId(); + } + + } else { + logger.debug("No TableDescriptorMetadata found (Supposed Time Table):" + + tableId); + + /* + * TIME TABLE hasn't tabular resource connected + */ + /* + * throw new DataSourceXException( + * "Error no valid tabular resource associated with the table:" + + * serviceTable.getId()); + */ + } + + trService.setService(service); + TabularResourceId tabularResourceId = new TabularResourceId( + serviceTabularResourceId); + trService.setTabularResourceId(tabularResourceId); + } + + @Override + public String toString() { + return "TDXDataSource [serviceTabularResourceId=" + + serviceTabularResourceId + ", serviceTableId=" + + serviceTableId + "]"; + } + +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/TDXDataSourceFactory.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/TDXDataSourceFactory.java new file mode 100644 index 0000000..0b0e93e --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/TDXDataSourceFactory.java @@ -0,0 +1,54 @@ +/** + * + */ +package org.gcube.portlets.user.tdwx.datasource.td; + + +import org.gcube.portlets.user.tdwx.server.datasource.DataSourceX; +import org.gcube.portlets.user.tdwx.server.datasource.DataSourceXException; +import org.gcube.portlets.user.tdwx.server.datasource.DataSourceXFactory; +import org.gcube.portlets.user.tdwx.server.util.ServiceCredentials; +import org.gcube.portlets.user.tdwx.shared.model.TableId; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + + +/** + * + * @author "Giancarlo Panichi" + * g.panichi@isti.cnr.it + * + */ +public class TDXDataSourceFactory implements DataSourceXFactory { + + private Logger logger = LoggerFactory.getLogger(TDXDataSourceFactory.class); + private static final String ID=Constants.TDX_DATASOURCE_FACTORY_ID; + + /** + * {@inheritDoc} + */ + public String getId() { + return ID; + } + + /** + * {@inheritDoc} + */ + public DataSourceX openDataSource(ServiceCredentials serviceCredentials, TableId tableId) throws DataSourceXException { + logger.debug("openDataSource: "+tableId); + String table = tableId.getTableKey(); + TDXDataSource dataSource = new TDXDataSource(ID,serviceCredentials,table); + return dataSource; + } + + /** + * {@inheritDoc} + */ + public void closeDataSource(ServiceCredentials serviceCredentials, DataSourceX dataSource) throws DataSourceXException { + logger.debug("closeDataSource: "+dataSource); + + ((TDXDataSource)dataSource).close(); + } + +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/exception/OperationException.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/exception/OperationException.java new file mode 100644 index 0000000..481c643 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/exception/OperationException.java @@ -0,0 +1,31 @@ +package org.gcube.portlets.user.tdwx.datasource.td.exception; + +import org.gcube.portlets.user.tdwx.server.datasource.DataSourceXException; + +/** + * + * @author giancarlo email: g.panichi@isti.cnr.it + * + */ +public class OperationException extends DataSourceXException { + + private static final long serialVersionUID = -8593898774262390757L; + + /** + * + * @param message + */ + public OperationException(String message) { + super(message); + } + + /** + * @param message + * @param cause + */ + public OperationException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/filters/FiltersBuilder.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/filters/FiltersBuilder.java new file mode 100644 index 0000000..02e1935 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/filters/FiltersBuilder.java @@ -0,0 +1,490 @@ +package org.gcube.portlets.user.tdwx.datasource.td.filters; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; + +import org.gcube.data.analysis.tabulardata.expression.Expression; +import org.gcube.data.analysis.tabulardata.expression.composite.comparable.Equals; +import org.gcube.data.analysis.tabulardata.expression.composite.comparable.GreaterThan; +import org.gcube.data.analysis.tabulardata.expression.composite.comparable.LessThan; +import org.gcube.data.analysis.tabulardata.expression.composite.text.Soundex; +import org.gcube.data.analysis.tabulardata.expression.composite.text.TextBeginsWith; +import org.gcube.data.analysis.tabulardata.expression.composite.text.TextContains; +import org.gcube.data.analysis.tabulardata.expression.composite.text.TextEndsWith; +import org.gcube.data.analysis.tabulardata.expression.logical.And; +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.ColumnReference; +import org.gcube.data.analysis.tabulardata.model.datatype.DataType; +import org.gcube.data.analysis.tabulardata.model.datatype.value.TDBoolean; +import org.gcube.data.analysis.tabulardata.model.datatype.value.TDDate; +import org.gcube.data.analysis.tabulardata.model.datatype.value.TDInteger; +import org.gcube.data.analysis.tabulardata.model.datatype.value.TDNumeric; +import org.gcube.data.analysis.tabulardata.model.datatype.value.TDText; +import org.gcube.data.analysis.tabulardata.model.table.Table; +import org.gcube.data.analysis.tabulardata.model.table.TableId; +import org.gcube.data.analysis.tabulardata.query.parameters.QueryFilter; +import org.gcube.portlets.user.tdwx.datasource.td.TDXDataSource; +import org.gcube.portlets.user.tdwx.datasource.td.map.DataTypeMap; +import org.gcube.portlets.user.tdwx.server.datasource.DataSourceXException; +import org.gcube.portlets.user.tdwx.shared.FilterInformation; +import org.gcube.portlets.user.tdwx.shared.StaticFilterInformation; +import org.gcube.portlets.user.tdwx.shared.model.ColumnDefinition; +import org.gcube.portlets.user.tdwx.shared.model.TableDefinition; +import org.gcube.portlets.user.tdwx.shared.model.ValueType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author "Giancarlo Panichi" email: g.panichi@isti.cnr.it + * + */ +public class FiltersBuilder { + + protected ArrayList filters; + protected ArrayList staticFilters; + protected TableDefinition tableDefinition; + + protected Table serviceTable; + protected Logger logger = LoggerFactory.getLogger(TDXDataSource.class); + + private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + + /** + * + * @param filters + * @param tableDefinition + */ + public FiltersBuilder(ArrayList filters, + ArrayList staticFilters, + TableDefinition tableDefinition, Table serviceTable) { + this.filters = filters; + this.staticFilters = staticFilters; + this.tableDefinition = tableDefinition; + this.serviceTable = serviceTable; + } + + /** + * + * @return + * @throws DataSourceXException + */ + protected Expression createExpression() throws DataSourceXException { + try { + + Expression exp = null; + + ArrayList andExp = new ArrayList(); + + if (filters != null) { + for (FilterInformation filter : filters) { + switch (filter.getFilterType()) { + case "string": + if (tableDefinition.getColumns().get( + filter.getFilterField()) == null) { + logger.error("The specified filter column \"" + + filter.getFilterField() + + "\" don't exists"); + + throw new DataSourceXException( + "The specified filter column \"" + + filter.getFilterField() + + "\" don't exists"); + } else { + ColumnDefinition columnDefinition = tableDefinition + .getColumns().get(filter.getFilterField()); + Column column = serviceTable + .getColumnByName(columnDefinition.getId()); + Expression ex = getExpressionText(column, filter); + if (ex != null) { + andExp.add(ex); + } + + } + break; + case "numeric": + if (tableDefinition.getColumns().get( + filter.getFilterField()) == null) { + logger.error("The specified filter column \"" + + filter.getFilterField() + + "\" don't exists"); + + throw new DataSourceXException( + "The specified filter column \"" + + filter.getFilterField() + + "\" don't exists"); + } else { + ColumnDefinition columnDefinition = tableDefinition + .getColumns().get(filter.getFilterField()); + Column column = serviceTable + .getColumnByName(columnDefinition.getId()); + + Expression ex = getExpressionNumeric(column, filter); + if (ex != null) { + andExp.add(ex); + } + } + break; + case "boolean": + if (tableDefinition.getColumns().get( + filter.getFilterField()) == null) { + logger.error("The specified filter column \"" + + filter.getFilterField() + + "\" don't exists"); + + throw new DataSourceXException( + "The specified filter column \"" + + filter.getFilterField() + + "\" don't exists"); + } else { + ColumnDefinition columnDefinition = tableDefinition + .getColumns().get(filter.getFilterField()); + Column column = serviceTable + .getColumnByName(columnDefinition.getId()); + + Equals contains = new Equals(new ColumnReference( + serviceTable.getId(), column.getLocalId()), + new TDBoolean(new Boolean(filter + .getFilterValue()))); + andExp.add(contains); + } + + break; + + case "date": + if (tableDefinition.getColumns().get( + filter.getFilterField()) == null) { + logger.error("The specified filter column \"" + + filter.getFilterField() + + "\" don't exists"); + + throw new DataSourceXException( + "The specified filter column \"" + + filter.getFilterField() + + "\" don't exists"); + } else { + ColumnDefinition columnDefinition = tableDefinition + .getColumns().get(filter.getFilterField()); + Column column = serviceTable + .getColumnByName(columnDefinition.getId()); + Expression ex = getExpressionDate(column, filter); + if (ex != null) { + andExp.add(ex); + } + } + + break; + default: + break; + + } + } + } + + if (staticFilters != null) { + for (StaticFilterInformation staticFilter : staticFilters) { + if (tableDefinition.getColumns().get( + staticFilter.getColumnName()) == null) { + logger.error("Static Filter not applicable, the specified column \"" + + staticFilter.getColumnName() + + "\" don't exists"); + + throw new DataSourceXException( + "Static Filter not applicable, the specified column \"" + + staticFilter.getColumnName() + + "\" don't exists"); + } else { + ColumnDefinition columnDefinition = tableDefinition + .getColumns().get(staticFilter.getColumnName()); + Column column = serviceTable + .getColumnById(new ColumnLocalId( + columnDefinition.getColumnLocalId())); + Expression e = createStaticExpression(staticFilter, + serviceTable.getId(), column); + if (e != null) { + andExp.add(e); + } + } + } + } + + if (andExp.size() > 0) { + if (andExp.size() == 1) { + exp = andExp.get(0); + } else { + And andE = new And(andExp); + exp = andE; + } + } + + logger.debug("Filter Expression created: " + exp); + + return exp; + + } catch (Throwable e) { + logger.error("Create expression failed. " + e.getLocalizedMessage()); + e.printStackTrace(); + throw new DataSourceXException("Create expression failed. " + + e.getLocalizedMessage()); + } + + } + + private Expression createStaticExpression( + StaticFilterInformation staticFilter, TableId id, Column column) + throws DataSourceXException { + try { + Expression exp = null; + DataType dataType = column.getDataType(); + ValueType valueType = DataTypeMap.getValueType(dataType); + switch (valueType) { + case BOOLEAN: + TDBoolean bool = new TDBoolean(new Boolean( + staticFilter.getFilterValue())); + exp = new Equals(new ColumnReference(id, column.getLocalId()), + bool); + break; + case DATE: + try { + Date dd = sdf.parse(staticFilter.getFilterValue()); + TDDate tdDate = new TDDate(dd); + exp = new Equals(new ColumnReference(id, + column.getLocalId()), tdDate); + } catch (ParseException e) { + logger.error("Static Filter has not valid value type: " + + staticFilter); + e.printStackTrace(); + } + break; + case DOUBLE: + TDNumeric doublenum = new TDNumeric(new Double( + staticFilter.getFilterValue())); + exp = new Equals(new ColumnReference(id, column.getLocalId()), + doublenum); + break; + case INTEGER: + TDInteger intnum = new TDInteger(new Integer( + staticFilter.getFilterValue())); + exp = new Equals(new ColumnReference(id, column.getLocalId()), + intnum); + break; + case LONG: + TDInteger longnum = new TDInteger(new Integer( + staticFilter.getFilterValue())); + exp = new Equals(new ColumnReference(id, column.getLocalId()), + longnum); + break; + case STRING: + exp = new TextContains(new ColumnReference(id, + column.getLocalId()), new TDText( + staticFilter.getFilterValue())); + break; + case GEOMETRY: + exp = new TextContains(new ColumnReference(id, + column.getLocalId()), new TDText( + staticFilter.getFilterValue())); + break; + default: + break; + + } + + return exp; + } catch (Throwable e) { + logger.error("Create Static Filter expression failed :" + + e.getLocalizedMessage()); + e.printStackTrace(); + throw new DataSourceXException( + "Create Static Filter expression failed :" + + e.getLocalizedMessage()); + } + } + + protected Expression getExpressionDate(Column column, + FilterInformation filter) throws DataSourceXException { + // [{ "filterField":"kqljyp" ,"filterType":"date" + // ,"filterComparison":"on" ,"filterValue": "1399370400000" }] + Expression exp = null; + + Date dd = new Date(); + Long day = null; + + try { + day = new Long(filter.getFilterValue()); + } catch (NumberFormatException e) { + logger.error("No valid filterValue for date: " + + filter.getFilterValue()); + throw new DataSourceXException("No valid filter value"); + } + + dd.setTime(day); + TDDate tdDate = new TDDate(dd); + + switch (filter.getFilterComparison()) { + case "on": + exp = new Equals(new ColumnReference(serviceTable.getId(), + column.getLocalId()), tdDate); + break; + case "after": + exp = new GreaterThan(new ColumnReference(serviceTable.getId(), + column.getLocalId()), tdDate); + break; + case "before": + exp = new LessThan(new ColumnReference(serviceTable.getId(), + column.getLocalId()), tdDate); + break; + default: + break; + + } + return exp; + } + + protected Expression getExpressionText(Column column, + FilterInformation filter) { + Expression exp = null; + + + String comparison=filter.getFilterComparison(); + + if(comparison!=null&& comparison.isEmpty()){ + return exp; + } + + switch(comparison){ + case "contains": + TextContains contains = new TextContains( + new ColumnReference(serviceTable.getId(), + column.getLocalId()), new TDText( + filter.getFilterValue())); + exp=contains; + break; + case "begins": + TextBeginsWith begins = new TextBeginsWith( + new ColumnReference(serviceTable.getId(), + column.getLocalId()), new TDText( + filter.getFilterValue())); + exp=begins; + break; + case "ends": + TextEndsWith ends = new TextEndsWith( + new ColumnReference(serviceTable.getId(), + column.getLocalId()), new TDText( + filter.getFilterValue())); + exp=ends; + break; + case "soundex": + Soundex soundexCol= new Soundex(new ColumnReference(serviceTable.getId(), + column.getLocalId())); + Soundex soundexVal= new Soundex(new TDText( + filter.getFilterValue())); + Equals eq=new Equals(soundexCol, soundexVal); + exp=eq; + default: + break; + + } + + return exp; + } + + protected Expression getExpressionNumeric(Column column, + FilterInformation filter) { + Expression exp = null; + + ValueType vt = DataTypeMap.getValueType(column.getDataType()); + + switch (vt) { + case DOUBLE: + TDNumeric floatnum = new TDNumeric(new Double( + filter.getFilterValue())); + if (filter.getFilterComparison().compareTo("eq") == 0) { + exp = new Equals(new ColumnReference(serviceTable.getId(), + column.getLocalId()), floatnum); + } else { + if (filter.getFilterComparison().compareTo("gt") == 0) { + exp = new GreaterThan(new ColumnReference( + serviceTable.getId(), column.getLocalId()), + floatnum); + } else { + if (filter.getFilterComparison().compareTo("lt") == 0) { + exp = new LessThan(new ColumnReference( + serviceTable.getId(), column.getLocalId()), + floatnum); + } else { + + } + + } + + } + break; + case INTEGER: + TDInteger intnum = new TDInteger(new Integer( + filter.getFilterValue())); + if (filter.getFilterComparison().compareTo("eq") == 0) { + exp = new Equals(new ColumnReference(serviceTable.getId(), + column.getLocalId()), intnum); + } else { + if (filter.getFilterComparison().compareTo("gt") == 0) { + exp = new GreaterThan(new ColumnReference( + serviceTable.getId(), column.getLocalId()), intnum); + } else { + if (filter.getFilterComparison().compareTo("lt") == 0) { + exp = new LessThan(new ColumnReference( + serviceTable.getId(), column.getLocalId()), + intnum); + } else { + + } + } + } + break; + case LONG: + TDInteger longnum = new TDInteger(new Integer( + filter.getFilterValue())); + if (filter.getFilterComparison().compareTo("eq") == 0) { + exp = new Equals(new ColumnReference(serviceTable.getId(), + column.getLocalId()), longnum); + } else { + if (filter.getFilterComparison().compareTo("gt") == 0) { + exp = new GreaterThan(new ColumnReference( + serviceTable.getId(), column.getLocalId()), longnum); + } else { + if (filter.getFilterComparison().compareTo("lt") == 0) { + exp = new LessThan(new ColumnReference( + serviceTable.getId(), column.getLocalId()), + longnum); + } else { + + } + } + } + break; + default: + break; + + } + return exp; + } + + /** + * + * @return + * @throws DataSourceXException + */ + public QueryFilter createQueryFilter() throws DataSourceXException { + QueryFilter queryFilter = null; + Expression exp = createExpression(); + if (exp != null) { + queryFilter = new QueryFilter(exp); + } + logger.debug("QueryFilter: " + queryFilter); + return queryFilter; + } + +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/map/ColumnDefinitionBuilder.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/map/ColumnDefinitionBuilder.java new file mode 100644 index 0000000..cf0ba0c --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/map/ColumnDefinitionBuilder.java @@ -0,0 +1,336 @@ +package org.gcube.portlets.user.tdwx.datasource.td.map; + +import java.util.List; + +import org.gcube.data.analysis.tabulardata.metadata.NoSuchMetadataException; +import org.gcube.data.analysis.tabulardata.model.column.Column; +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.DataLocaleMetadata; +import org.gcube.data.analysis.tabulardata.model.metadata.column.PeriodTypeMetadata; +import org.gcube.data.analysis.tabulardata.model.metadata.column.ValidationReferencesMetadata; +import org.gcube.data.analysis.tabulardata.model.metadata.column.ViewColumnMetadata; +import org.gcube.data.analysis.tabulardata.model.metadata.common.LocalizedText; +import org.gcube.data.analysis.tabulardata.model.metadata.common.NamesMetadata; +import org.gcube.data.analysis.tabulardata.model.metadata.common.Validation; +import org.gcube.data.analysis.tabulardata.model.metadata.common.ValidationsMetadata; +import org.gcube.data.analysis.tabulardata.model.relationship.ColumnRelationship; +import org.gcube.data.analysis.tabulardata.model.table.Table; +import org.gcube.data.analysis.tabulardata.model.time.PeriodType; +import org.gcube.data.analysis.tabulardata.service.TabularDataService; +import org.gcube.portlets.user.td.widgetcommonevent.shared.tr.column.ColumnTypeCode; +import org.gcube.portlets.user.td.widgetcommonevent.shared.tr.column.RelationshipData; +import org.gcube.portlets.user.tdwx.server.datasource.DataSourceXException; +import org.gcube.portlets.user.tdwx.shared.model.ColumnDefinition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ColumnDefinitionBuilder { + private static final String PRIMARY_KEY_COLUMN = "id"; + + private Logger logger = LoggerFactory + .getLogger(ColumnDefinitionBuilder.class); + + private TabularDataService service; + private Table serviceTable; + private Column serviceColumn; + private String tooltipMessage; + private String columnName; + private String columnLocalId; + private boolean visible; + private boolean editable; + private org.gcube.portlets.user.tdwx.shared.model.ColumnType type; + private String columnLabel; + private String locale; + private int ordinalPosition; + + private PeriodTypeMetadata periodTypeMetadata; + protected PeriodType periodType; + + private RelationshipData relationshipData; + + private String sourceTableDimensionColumnId; + private String targetTableColumnId; + private long targetTableId; + private boolean viewColumn; + + public ColumnDefinitionBuilder(TabularDataService service, + Table serviceTable, Column serviceColumn, int ordinalPosition) + throws DataSourceXException { + + this.service = service; + this.serviceTable = serviceTable; + this.serviceColumn = serviceColumn; + this.ordinalPosition = ordinalPosition; + + visible = true; + type = org.gcube.portlets.user.tdwx.shared.model.ColumnType.USER; + editable = true; + ColumnType ct = serviceColumn.getColumnType(); + if (ColumnTypeMap.isIdColumnType(ct)) { + columnLabel = PRIMARY_KEY_COLUMN; + visible = false; + editable = false; + type = org.gcube.portlets.user.tdwx.shared.model.ColumnType.COLUMNID; + } else { + periodTypeMetadata = null; + if (serviceColumn.contains(PeriodTypeMetadata.class)) { + periodTypeMetadata = serviceColumn + .getMetadata(PeriodTypeMetadata.class); + periodType = periodTypeMetadata.getType(); + } + + if (ColumnTypeMap.isValidationColumnType(ct)) { + type = org.gcube.portlets.user.tdwx.shared.model.ColumnType.VALIDATION; + editable = false; + } else { + if (ColumnTypeMap.isTimeDimensionColumnType(ct)) { + type = org.gcube.portlets.user.tdwx.shared.model.ColumnType.TIMEDIMENSION; + visible = false; + retrieveRelationship(); + } else { + if (ColumnTypeMap.isDimensionColumnType(ct)) { + type = org.gcube.portlets.user.tdwx.shared.model.ColumnType.DIMENSION; + visible = false; + retrieveRelationship(); + } else { + if (ColumnTypeMap.isMeasureColumnType(ct)) { + type = org.gcube.portlets.user.tdwx.shared.model.ColumnType.MEASURE; + } else { + if (ColumnTypeMap.isCodeColumnType(ct)) { + type = org.gcube.portlets.user.tdwx.shared.model.ColumnType.CODE; + } + + } + } + } + } + + if (serviceColumn.contains(ViewColumnMetadata.class)) { + + retrieveViewColumnMetadata(); + + visible = false; + viewColumn = true; + } else { + viewColumn = false; + } + + NamesMetadata labelsMetadata = null; + try { + labelsMetadata = serviceColumn.getMetadata(NamesMetadata.class); + } catch (NoSuchMetadataException e) { + logger.debug("labelMetadata: NoSuchMetadataException " + + e.getLocalizedMessage()); + } + + if (labelsMetadata == null) { + columnLabel = "nolabel"; + } else { + LocalizedText cl = null; + cl = labelsMetadata.getTextWithLocale("en"); + if (cl == null) { + columnLabel = "nolabel"; + logger.debug("ColumnLabel no label in en"); + } else { + columnLabel = cl.getValue(); + if (columnLabel == null || columnLabel.isEmpty()) { + columnLabel = "nolabel"; + } + } + } + + DataLocaleMetadata dataLocaleMetadata = null; + try { + dataLocaleMetadata = serviceColumn + .getMetadata(DataLocaleMetadata.class); + } catch (NoSuchMetadataException e) { + logger.debug("DataLocaleMetadata: NoSuchMetadataException " + + e.getLocalizedMessage()); + } + + if (dataLocaleMetadata == null) { + logger.debug("No DataLocaleMetadata"); + locale = ""; + } else { + locale = dataLocaleMetadata.getLocale(); + } + + } + + } + + private void retrieveViewColumnMetadata() throws DataSourceXException { + ViewColumnMetadata viewMetadata = serviceColumn + .getMetadata(ViewColumnMetadata.class); + // logger.debug("ViewColumnMetadata: " + viewMetadata.toString()); + + Column sourceColumn = serviceTable.getColumnById(viewMetadata + .getSourceTableDimensionColumnId()); + if (sourceColumn.getColumnType().getCode() + .compareTo(ColumnTypeCode.TIMEDIMENSION.toString()) == 0) { + type = org.gcube.portlets.user.tdwx.shared.model.ColumnType.VIEWCOLUMN_OF_TIMEDIMENSION; + PeriodTypeMetadata periodTypeMetadataSourceColumn = null; + if (sourceColumn.contains(PeriodTypeMetadata.class)) { + periodTypeMetadataSourceColumn = sourceColumn + .getMetadata(PeriodTypeMetadata.class); + + Table timeTable = service + .getTimeTable(periodTypeMetadataSourceColumn.getType()); + + if (timeTable == null || timeTable.getId() == null) { + throw new DataSourceXException( + "Error retrieving Time Table: " + timeTable); + } + // logger.debug("Time Table Id: " + timeTable.getId()); + + Column timeColumn = timeTable + .getColumnByName(periodTypeMetadataSourceColumn + .getType().getName()); + + sourceTableDimensionColumnId = viewMetadata + .getSourceTableDimensionColumnId().getValue(); + + targetTableColumnId = timeColumn.getLocalId().getValue(); + + targetTableId = timeTable.getId().getValue(); + + } else { + logger.error("Error retrieving Time Table for view column:" + + serviceColumn + + " , source column do not have a PeriodTypeMetadata: " + + sourceColumn); + throw new DataSourceXException( + "Error retrieving Time Table, source column do not have a PeriodTypeMetadata"); + } + + } else { + type = org.gcube.portlets.user.tdwx.shared.model.ColumnType.VIEWCOLUMN_OF_DIMENSION; + sourceTableDimensionColumnId = viewMetadata + .getSourceTableDimensionColumnId().getValue(); + targetTableColumnId = viewMetadata.getTargetTableColumnId() + .getValue(); + targetTableId = viewMetadata.getTargetTableId().getValue(); + + } + + } + + private void retrieveRelationship() throws DataSourceXException { + if (serviceColumn.getColumnType().getCode() + .compareTo(ColumnTypeCode.TIMEDIMENSION.toString()) == 0) { + Table timeTable = service + .getTimeTable(periodTypeMetadata.getType()); + if (timeTable == null || timeTable.getId() == null) { + throw new DataSourceXException("Error retrieving Time Table: " + + timeTable); + } + // logger.debug("Time Table Id: " + timeTable.getId()); + Column timeColumn = timeTable.getColumnByName(periodTypeMetadata + .getType().getName()); + relationshipData = new RelationshipData(timeTable.getId() + .getValue(), timeColumn.getLocalId().getValue()); + + } else { + ColumnRelationship rel = serviceColumn.getRelationship(); + if (rel != null) { + relationshipData = new RelationshipData(rel.getTargetTableId() + .getValue(), rel.getTargetColumnId().getValue()); + } else { + logger.error("No valid relationship for column: " + + serviceColumn.toString()); + throw new DataSourceXException( + "Column has not valid relationship"); + } + + } + + } + + public boolean isVisible() { + return visible; + } + + public org.gcube.portlets.user.tdwx.shared.model.ColumnType getType() { + return type; + } + + public String getColumnLabel() { + return columnLabel; + } + + public String getColumnName() { + columnName = serviceColumn.getName(); + return columnName; + } + + public String getColumnLocalId() { + columnLocalId = serviceColumn.getLocalId().getValue(); + return columnLocalId; + } + + public ValidationReferencesMetadata getValidationReferencesMetadata() { + ValidationReferencesMetadata refs = null; + try { + refs = serviceColumn + .getMetadata(ValidationReferencesMetadata.class); + } catch (NoSuchMetadataException e) { + logger.debug("ValidationReferencesMetadata: NoSuchMetadataException " + + e.getLocalizedMessage()); + } + + return refs; + } + + public String getTooltipMessage() { + String valids = new String(); + ValidationsMetadata val = null; + try { + val = serviceColumn.getMetadata(ValidationsMetadata.class); + } catch (NoSuchMetadataException e) { + /* + * logger.debug("No validatationsMetadata present: " + + * e.getLocalizedMessage()); + */ + } + + if (val != null) { + List listValidationsMetadata = ((ValidationsMetadata) val) + .getValidations(); + for (Validation validation : listValidationsMetadata) { + valids.concat(validation.getDescription() + "\n"); + } + } + tooltipMessage = valids; + + return tooltipMessage; + } + + public ColumnDefinition build() { + ColumnDefinition columnDefinition = new ColumnDefinition( + getColumnName(), getColumnLocalId(), getColumnLabel()); + columnDefinition.setLocale(locale); + + columnDefinition.setRelationshipData(relationshipData); + columnDefinition.setViewColumn(viewColumn); + columnDefinition.setTargetTableId(targetTableId); + columnDefinition.setTargetTableColumnId(targetTableColumnId); + columnDefinition + .setSourceTableDimensionColumnId(sourceTableDimensionColumnId); + + DataType dataType = serviceColumn.getDataType(); + columnDefinition.setVisible(visible); + columnDefinition.setEditable(editable); + columnDefinition.setValueType(DataTypeMap.getValueType(dataType)); + columnDefinition.setType(getType()); + columnDefinition.setPosition(ordinalPosition); + columnDefinition.setTooltipMessage(getTooltipMessage()); + columnDefinition.setColumnTypeName(serviceColumn.getColumnType() + .getName()); + columnDefinition.setColumnDataType(serviceColumn.getDataType() + .getName()); + columnDefinition.setWidth(100); + return columnDefinition; + } + +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/map/ColumnTypeMap.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/map/ColumnTypeMap.java new file mode 100644 index 0000000..71b0175 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/map/ColumnTypeMap.java @@ -0,0 +1,85 @@ +package org.gcube.portlets.user.tdwx.datasource.td.map; + +import org.gcube.data.analysis.tabulardata.model.column.ColumnType; +import org.gcube.data.analysis.tabulardata.model.column.type.CodeColumnType; +import org.gcube.data.analysis.tabulardata.model.column.type.DimensionColumnType; +import org.gcube.data.analysis.tabulardata.model.column.type.IdColumnType; +import org.gcube.data.analysis.tabulardata.model.column.type.MeasureColumnType; +import org.gcube.data.analysis.tabulardata.model.column.type.TimeDimensionColumnType; +import org.gcube.data.analysis.tabulardata.model.column.type.ValidationColumnType; + +/** + * + * @author giancarlo email: g.panichi@isti.cnr.it + * + */ +public class ColumnTypeMap { + + /* + * public static org.gcube.portlets.user.tdwx.shared.model.ColumnType + * map(ColumnType columnType){ if(columnType==null){ return null; } + * + * if(columnType instanceof AnnotationColumnType){ + * + * } + * + * if(columnType instanceof AttributeColumnType){ + * + * } } + */ + + public static boolean isIdColumnType(ColumnType columnType) { + if (columnType instanceof IdColumnType) { + return true; + } else { + return false; + } + } + + public static boolean isMeasureColumnType(ColumnType columnType) { + if (columnType instanceof MeasureColumnType) { + return true; + } else { + return false; + } + + } + + public static boolean isCodeColumnType(ColumnType columnType) { + if (columnType instanceof CodeColumnType) { + return true; + } else { + return false; + } + + } + + public static boolean isValidationColumnType(ColumnType columnType) { + if (columnType instanceof ValidationColumnType) { + return true; + } else { + return false; + } + + } + + public static boolean isDimensionColumnType(ColumnType columnType) { + if (columnType instanceof DimensionColumnType) { + return true; + } else { + return false; + } + + } + + public static boolean isTimeDimensionColumnType(ColumnType columnType) { + if (columnType instanceof TimeDimensionColumnType) { + return true; + } else { + return false; + } + + } + +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/map/DataTypeMap.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/map/DataTypeMap.java new file mode 100644 index 0000000..a3b0d42 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/map/DataTypeMap.java @@ -0,0 +1,40 @@ +package org.gcube.portlets.user.tdwx.datasource.td.map; + +import org.gcube.data.analysis.tabulardata.model.datatype.BooleanType; +import org.gcube.data.analysis.tabulardata.model.datatype.DataType; +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.portlets.user.tdwx.shared.model.ValueType; + +/** + * + * @author "Giancarlo Panichi" + * g.panichi@isti.cnr.it + * + */ +public class DataTypeMap { + + public static ValueType getValueType(DataType dataType){ + if( dataType instanceof BooleanType){ + return ValueType.BOOLEAN; + } + if( dataType instanceof DateType){ + return ValueType.DATE; + } + if( dataType instanceof IntegerType){ + return ValueType.INTEGER; + } + if( dataType instanceof GeometryType){ + return ValueType.GEOMETRY; + } + if( dataType instanceof NumericType){ + return ValueType.DOUBLE; + } + + return ValueType.STRING; + }; + +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpCostants.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpCostants.java new file mode 100644 index 0000000..f7f14d1 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpCostants.java @@ -0,0 +1,14 @@ +package org.gcube.portlets.user.tdwx.datasource.td.opexecution; + +/** + * Constants Parameter Name + * + * @author giancarlo email: g.panichi@isti.cnr.it + * + */ +public class OpCostants { + + public static final String PARAMETER_CHANGE_COLUMN_POSITION_POSITION = "position"; + +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpExecution4ChangeSingleColumnPosition.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpExecution4ChangeSingleColumnPosition.java new file mode 100644 index 0000000..f015ab4 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpExecution4ChangeSingleColumnPosition.java @@ -0,0 +1,55 @@ +package org.gcube.portlets.user.tdwx.datasource.td.opexecution; + +import org.gcube.data.analysis.tabulardata.commons.webservice.types.operations.OperationExecution; +import org.gcube.data.analysis.tabulardata.service.TabularDataService; +import org.gcube.portlets.user.tdwx.datasource.td.exception.OperationException; +import org.gcube.portlets.user.tdwx.shared.ColumnsReorderingConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Operation Execution for change single column on grid. + * This operation is not supported on TDM beacause ordering of columns on view table + * is not allowed. + * + * @author "Giancarlo Panichi" email: g.panichi@isti.cnr.it + * + */ +public class OpExecution4ChangeSingleColumnPosition extends OpExecutionBuilder { + private static Logger logger = LoggerFactory + .getLogger(OpExecution4ChangeSingleColumnPosition.class); + + //private TabularDataService service; + private ColumnsReorderingConfig columnsReorderingConfig; + + public OpExecution4ChangeSingleColumnPosition(TabularDataService service, + ColumnsReorderingConfig columnsReorderingConfig) { + //this.service = service; + this.columnsReorderingConfig = columnsReorderingConfig; + } + + @Override + public void buildOpEx() throws OperationException { + logger.debug("ColumnsReorderingConfig :" + columnsReorderingConfig); + + + OperationExecution invocation = null; + /* + + OperationDefinition operationDefinition; + + operationDefinition = OperationDefinitionMap.map( + OperationsId.ChangeSingleColumnPosition.toString(), service); + + Map map = new HashMap(); + + map.put(OpCostants.PARAMETER_CHANGE_COLUMN_POSITION_POSITION, new Integer(columnsReorderingConfig.getColumnIndex())); + + invocation = new OperationExecution(columnsReorderingConfig.getColumnDefinition().getColumnLocalId(),operationDefinition.getOperationId(), map); + */ + operationExecutionSpec.setOp(invocation); + + } + +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpExecutionBuilder.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpExecutionBuilder.java new file mode 100644 index 0000000..17aa28e --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpExecutionBuilder.java @@ -0,0 +1,26 @@ +package org.gcube.portlets.user.tdwx.datasource.td.opexecution; + +import org.gcube.portlets.user.tdwx.datasource.td.exception.OperationException; + +/** + * Abstract class for build Operation Execution + * + * @author "Giancarlo Panichi" + * email: g.panichi@isti.cnr.it + * + */ +abstract class OpExecutionBuilder { + protected OpExecutionSpec operationExecutionSpec; + + public OpExecutionSpec getOperationExecutionSpec(){ + return operationExecutionSpec; + } + public void createSpec(){ + operationExecutionSpec=new OpExecutionSpec(); + + } + + public abstract void buildOpEx() throws OperationException; + + +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpExecutionDirector.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpExecutionDirector.java new file mode 100644 index 0000000..12b4d47 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpExecutionDirector.java @@ -0,0 +1,39 @@ +package org.gcube.portlets.user.tdwx.datasource.td.opexecution; + +import java.util.ArrayList; + +import org.gcube.data.analysis.tabulardata.commons.webservice.types.operations.OperationExecution; +import org.gcube.portlets.user.tdwx.datasource.td.exception.OperationException; + + +/** + * Operation Execution Director + * + * @author "Giancarlo Panichi" + * email: g.panichi@isti.cnr.it + * + */ +public class OpExecutionDirector { + OpExecutionBuilder operationExecutionBuilder; + + public void setOperationExecutionBuilder( + OpExecutionBuilder operationExecutionBuilder) { + this.operationExecutionBuilder = operationExecutionBuilder; + } + + public OperationExecution getOperationExecution() { + return operationExecutionBuilder.getOperationExecutionSpec().getOp(); + + } + + public ArrayList getListOperationExecution() { + return operationExecutionBuilder.getOperationExecutionSpec().getOps(); + + } + + public void constructOperationExecution() throws OperationException { + operationExecutionBuilder.createSpec(); + operationExecutionBuilder.buildOpEx(); + + } +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpExecutionSpec.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpExecutionSpec.java new file mode 100644 index 0000000..9f856e9 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/opexecution/OpExecutionSpec.java @@ -0,0 +1,33 @@ +package org.gcube.portlets.user.tdwx.datasource.td.opexecution; + +import java.util.ArrayList; + +import org.gcube.data.analysis.tabulardata.commons.webservice.types.operations.OperationExecution; + +/** + * + * @author "Giancarlo Panichi" + * email: g.panichi@isti.cnr.it + * + */ +public class OpExecutionSpec { + private OperationExecution op; + private ArrayList ops; + + public OperationExecution getOp() { + return op; + } + + public void setOp(OperationExecution op) { + this.op = op; + } + + public ArrayList getOps() { + return ops; + } + + public void setOps(ArrayList ops) { + this.ops = ops; + } + +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/trservice/OperationDefinitionMap.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/trservice/OperationDefinitionMap.java new file mode 100644 index 0000000..42fdfa1 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/trservice/OperationDefinitionMap.java @@ -0,0 +1,47 @@ +package org.gcube.portlets.user.tdwx.datasource.td.trservice; + +import org.gcube.data.analysis.tabulardata.commons.webservice.exception.NoSuchOperationException; +import org.gcube.data.analysis.tabulardata.commons.webservice.types.operations.OperationDefinition; +import org.gcube.data.analysis.tabulardata.service.TabularDataService; +import org.gcube.portlets.user.tdwx.datasource.td.exception.OperationException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author "Giancarlo Panichi" g.panichi@isti.cnr.it + * + */ +public class OperationDefinitionMap { + protected static Logger logger = LoggerFactory + .getLogger(OperationDefinitionMap.class); + + public static OperationDefinition map(String op, TabularDataService service) + throws OperationException { + OperationDefinition operationDefinition = null; + logger.debug("Retrieve Capability from Service"); + try { + operationDefinition = service.getCapability(Long.valueOf(op)); + } catch (NumberFormatException e) { + logger.error("No valid operation type: " + op); + e.printStackTrace(); + throw new OperationException( + "No valid operation type: " + op); + } catch (NoSuchOperationException e) { + logger.error("NoSuchOperationException: " + e.getLocalizedMessage()); + e.printStackTrace(); + throw new OperationException( + "NoSuchOperationException: " + e.getLocalizedMessage()); + } catch(Throwable e) { + logger.error("Error Retrieving Service Capability: " + e.getLocalizedMessage()); + e.printStackTrace(); + throw new OperationException( + "Error Retrieving Service Capability: " + e.getLocalizedMessage()); + } + + + return operationDefinition; + + } +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/trservice/TRService.java b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/trservice/TRService.java new file mode 100644 index 0000000..4a49f05 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/datasource/td/trservice/TRService.java @@ -0,0 +1,91 @@ +package org.gcube.portlets.user.tdwx.datasource.td.trservice; + +import org.gcube.data.analysis.tabulardata.commons.webservice.types.operations.OperationExecution; +import org.gcube.data.analysis.tabulardata.service.TabularDataService; +import org.gcube.data.analysis.tabulardata.service.tabular.TabularResource; +import org.gcube.data.analysis.tabulardata.service.tabular.TabularResourceId; +import org.gcube.portlets.user.tdwx.datasource.td.exception.OperationException; +import org.gcube.portlets.user.tdwx.datasource.td.opexecution.OpExecution4ChangeSingleColumnPosition; +import org.gcube.portlets.user.tdwx.datasource.td.opexecution.OpExecutionDirector; +import org.gcube.portlets.user.tdwx.shared.ColumnsReorderingConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Call operations on service + * + * @author giancarlo email: g.panichi@isti.cnr.it + * + */ +public class TRService { + // private static final String TABULAR_RESOURCE_IS_FINAL = + // "Tabular Resource Is Final"; + private static final String TABULAR_RESOURCE_IS_LOCKED = "Tabular Resource Is Locked"; + private static final String SECURITY_EXCEPTION_RIGHTS = "Security exception, you don't have the required rights!"; + + private static final Logger logger = LoggerFactory + .getLogger(TRService.class); + + private TabularDataService service; + private TabularResourceId tabularResourceId; + + public TRService() { + + } + + public TabularDataService getService() { + return service; + } + + public void setService(TabularDataService service) { + this.service = service; + } + + public TabularResourceId getTabularResourceId() { + return tabularResourceId; + } + + public void setTabularResourceId(TabularResourceId tabularResourceId) { + this.tabularResourceId = tabularResourceId; + } + + public void startChangeSingleColumnPosition( + ColumnsReorderingConfig columnsReorderingConfig) + throws OperationException { + try { + + TabularResource tabularResource = service + .getTabularResource(tabularResourceId); + + if (tabularResource.isLocked()) { + logger.error(TABULAR_RESOURCE_IS_LOCKED); + throw new OperationException(TABULAR_RESOURCE_IS_LOCKED); + } + + OpExecution4ChangeSingleColumnPosition opEx = new OpExecution4ChangeSingleColumnPosition( + service, columnsReorderingConfig); + OpExecutionDirector director = new OpExecutionDirector(); + director.setOperationExecutionBuilder(opEx); + director.constructOperationExecution(); + OperationExecution invocation = director.getOperationExecution(); + + logger.debug("OperationInvocation: \n" + invocation); + + service.executeSynchMetadataOperation(invocation, tabularResourceId); + + return; + + } catch (OperationException e) { + throw e; + } catch (SecurityException e) { + e.printStackTrace(); + throw new OperationException(SECURITY_EXCEPTION_RIGHTS); + } catch (Throwable e) { + e.printStackTrace(); + throw new OperationException("Error Changing The Column Position: " + + e.getLocalizedMessage()); + } + + } +}