From fb8f581f32bf21254b96786e5c172ecccacf1ffc Mon Sep 17 00:00:00 2001 From: Giancarlo Panichi Date: Tue, 3 Dec 2013 16:55:39 +0000 Subject: [PATCH] Added support for export SDMX git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-gwt-service@86515 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../gwtservice/client/rpc/TDGWTService.java | 24 ++- .../client/rpc/TDGWTServiceAsync.java | 8 +- .../td/gwtservice/server/SessionUtil.java | 43 +++- .../gwtservice/server/TDGWTServiceImpl.java | 195 +++++++++++++++++- .../user/td/gwtservice/shared/Constants.java | 17 +- .../shared/sdmx/SDMXExportMonitor.java | 25 ++- .../shared/sdmx/SDMXExportSession.java | 2 + 7 files changed, 297 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTService.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTService.java index 112fd03..c43c381 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTService.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTService.java @@ -14,6 +14,8 @@ import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVRowError; import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTServiceException; import org.gcube.portlets.user.td.gwtservice.shared.file.FileUploadMonitor; import org.gcube.portlets.user.td.gwtservice.shared.file.HeaderPresence; +import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXExportMonitor; +import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXExportSession; import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXImportMonitor; import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXImportSession; import org.gcube.portlets.user.td.gwtservice.shared.tr.ColumnData; @@ -27,6 +29,7 @@ import org.gcube.portlets.user.td.gwtservice.shared.tr.type.Agencies; import org.gcube.portlets.user.td.gwtservice.shared.tr.type.Codelist; import org.gcube.portlets.user.td.gwtservice.shared.tr.type.Dataset; +import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.rpc.RemoteService; import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; @@ -288,8 +291,27 @@ public interface TDGWTService extends RemoteService { * @throws TDGWTServiceException */ public void startCSVExport(CSVExportSession csvExportSession) throws TDGWTServiceException; + + + + //Export SDMX + /** + * Get Operation Monitor during the SDMX Export operation + * + * @return + * @throws TDGWTServiceException + */ + public SDMXExportMonitor getSDMXExportMonitor() throws TDGWTServiceException; - + + + /** + * Start SDMX Export and invokes the client library + * + * @param sdmxExportSession + * @throws TDGWTServiceException + */ + public void startSDMXExport(SDMXExportSession exportSession) throws TDGWTServiceException; } diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTServiceAsync.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTServiceAsync.java index bc56095..6eaa665 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTServiceAsync.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTServiceAsync.java @@ -13,6 +13,8 @@ import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVImportSession; import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVRowError; import org.gcube.portlets.user.td.gwtservice.shared.file.FileUploadMonitor; import org.gcube.portlets.user.td.gwtservice.shared.file.HeaderPresence; +import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXExportMonitor; +import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXExportSession; import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXImportMonitor; import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXImportSession; import org.gcube.portlets.user.td.gwtservice.shared.tr.ColumnData; @@ -97,5 +99,9 @@ public interface TDGWTServiceAsync { void startCSVExport(CSVExportSession csvExportSession, AsyncCallback callback); - + //Export SDMX + void getSDMXExportMonitor(AsyncCallback callback); + + void startSDMXExport(SDMXExportSession exportSession, AsyncCallback callback); + } diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/SessionUtil.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/SessionUtil.java index 8a0ce1c..eb92081 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/SessionUtil.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/SessionUtil.java @@ -19,6 +19,7 @@ import org.gcube.portlets.user.td.gwtservice.server.trservice.TRTasksManager; import org.gcube.portlets.user.td.gwtservice.shared.Constants; import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVExportSession; import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVImportSession; +import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXExportSession; import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXImportSession; import org.gcube.portlets.user.td.gwtservice.shared.source.SDMXRegistrySource; import org.gcube.portlets.user.td.gwtservice.shared.tr.TRId; @@ -52,17 +53,21 @@ public class SessionUtil { protected static final String SDMX_IMPORT_TABULAR_RESOURCE = "SDMX_IMPORT_TABULAR_RESOURCE"; protected static final String SDMX_IMPORT_TASK="SDMX_IMPORT_TASK"; + protected static final String SDMX_EXPORT_SESSION = "SDMX_EXPORT_SESSION"; + protected static final String SDMX_EXPORT_TASK="SDMX_EXPORT_TASK"; protected static final String CSV_IMPORT_SESSION = "CSV_IMPORT"; protected static final String CSV_IMPORT_FILE_UPLOAD_SESSION = "CSV_IMPORT_FILE_UPLOAD"; protected static final String CSV_IMPORT_TASK="CSV_IMPORT_TASK"; protected static final String CSV_IMPORT_TABULAR_RESOURCE = "CSV_IMPORT_TABULAR_RESOURCE"; - protected static final String CSV_EXPORT_SESSION = "CSV_EXPORT_FILE"; + protected static final String CSV_EXPORT_SESSION = "CSV_EXPORT_SESSION"; protected static final String CSV_EXPORT_TASK="CSV_EXPORT_TASK"; + + protected static Logger logger = LoggerFactory.getLogger(SessionUtil.class); protected static ASLSession getAslSession(HttpSession httpSession) { @@ -272,6 +277,27 @@ public class SessionUtil { return exportSession; } + public static void setSDMXExportSession(HttpSession httpSession, + SDMXExportSession s) { + + SDMXExportSession session = (SDMXExportSession) httpSession + .getAttribute(SDMX_EXPORT_SESSION); + if (session != null) + httpSession.removeAttribute(SDMX_EXPORT_SESSION); + httpSession.setAttribute(SDMX_EXPORT_SESSION, s); + ASLSession aslSession = getAslSession(httpSession); + ScopeProvider.instance.set(aslSession.getScope().toString()); + } + + public static SDMXExportSession getSDMXExportSession(HttpSession httpSession) { + SDMXExportSession exportSession = (SDMXExportSession) httpSession + .getAttribute(SDMX_EXPORT_SESSION); + if (exportSession == null) { + logger.error("SDMXExportSession was not acquired"); + } + return exportSession; + } + public static void setFileUploadSession(HttpSession httpSession, FileUploadSession s) { @@ -410,7 +436,20 @@ public class SessionUtil { httpSession.setAttribute(CSV_EXPORT_TASK, task); } - + public static Task getSDMXExportTask(HttpSession httpSession) { + Task monitor = (Task) httpSession.getAttribute(SDMX_EXPORT_TASK); + if (monitor == null) { + logger.error("SDMX_EXPORT_TASK was not acquired"); + } + return monitor; + } + + public static void setSDMXExportTask(HttpSession httpSession, Task task) { + Task monitor = (Task) httpSession.getAttribute(SDMX_EXPORT_TASK); + if (monitor != null) + httpSession.removeAttribute(SDMX_EXPORT_TASK); + httpSession.setAttribute(SDMX_EXPORT_TASK, task); + } public static TRTasksManager getTRTasksManager(HttpSession httpSession) { TRTasksManager tasksManager = (TRTasksManager) httpSession.getAttribute(TR_TASK_MANAGER); diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/TDGWTServiceImpl.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/TDGWTServiceImpl.java index 4d35be7..385ec5f 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/TDGWTServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/TDGWTServiceImpl.java @@ -77,6 +77,8 @@ import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTServiceExcept import org.gcube.portlets.user.td.gwtservice.shared.file.FileUploadMonitor; import org.gcube.portlets.user.td.gwtservice.shared.file.FileUploadState; import org.gcube.portlets.user.td.gwtservice.shared.file.HeaderPresence; +import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXExportMonitor; +import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXExportSession; import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXImportMonitor; import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXImportSession; import org.gcube.portlets.user.td.gwtservice.shared.task.State; @@ -1048,17 +1050,17 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements String fileUrlOnStorage, FileUploadSession fileUploadSession, CSVImportSession csvImportSession) { Map parameterInstances = new HashMap(); - parameterInstances.put(Constants.URL, fileUrlOnStorage); - parameterInstances.put(Constants.SEPARATOR, String + parameterInstances.put(Constants.PARAMETER_URL, fileUrlOnStorage); + parameterInstances.put(Constants.PARAMETER_SEPARATOR, String .valueOf(fileUploadSession.getParserConfiguration() .getDelimiter()));// ',' - parameterInstances.put(Constants.ENCODING, fileUploadSession + parameterInstances.put(Constants.PARAMETER_ENCODING, fileUploadSession .getParserConfiguration().getCharset().name());// "UTF-8" boolean hasHeader = true; if (fileUploadSession.getParserConfiguration().getHeaderPresence() == HeaderPresence.NONE) { hasHeader = false; } - parameterInstances.put(Constants.HASHEADER, hasHeader);// true + parameterInstances.put(Constants.PARAMETER_HASHEADER, hasHeader);// true return parameterInstances; } @@ -1498,14 +1500,191 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements protected Map csvExportFileParameter( CSVExportSession exportSession) { Map parameterInstances = new HashMap(); - parameterInstances.put(Constants.ENCODING, exportSession.getEncoding()); - parameterInstances.put(Constants.SEPARATOR, + parameterInstances.put(Constants.PARAMETER_ENCODING, exportSession.getEncoding()); + parameterInstances.put(Constants.PARAMETER_SEPARATOR, exportSession.getSeparator()); - parameterInstances.put(Constants.COLUMNS, + parameterInstances.put(Constants.PARAMETER_COLUMNS, exportSession.getColumnsAsArrayOfString()); return parameterInstances; } + + + /** + * + * @param exportSession + * @return + */ + protected Map sdmxExportParameter( + SDMXExportSession exportSession) { + Map parameterInstances = new HashMap(); + parameterInstances.put(Constants.PARAMETER_REGISTRYBASEURL, "http://pc-fortunati.isti.cnr.it:8080/FusionRegistry/ws/rest/"); + parameterInstances.put(Constants.PARAMETER_AGENCY, "SDMX"); + parameterInstances.put(Constants.PARAMETER_ID, "NEW_CL_DIVISION"); + parameterInstances.put(Constants.PARAMETER_VERSION, "2.0"); + return parameterInstances; + } + + + + + + /** + * + * @param exportSession + * @return + */ + @Override + public void startSDMXExport(SDMXExportSession exportSession) + throws TDGWTServiceException { + logger.debug("Start SDMX Export"); + session = this.getThreadLocalRequest().getSession(); + if (session == null) { + throw new TDGWTServiceException( + "Error retrieving the session: null"); + } + logger.info("Session:" + session.getId()); + + aslSession = SessionUtil.getAslSession(session); + if (aslSession == null) { + throw new TDGWTServiceException( + "Error retrieving the asl session: null"); + } + String user = aslSession.getUsername(); + logger.info("Session User:" + user); + + TRId trId = SessionUtil.getTRId(session); + if (trId == null) { + throw new TDGWTServiceException( + "Error no tabular resource in session"); + } + + if (trId.getTableId() == null) { + throw new TDGWTServiceException("Error no table present in session"); + } + + SessionUtil.setSDMXExportSession(session, exportSession); + + Map parameterInstance = sdmxExportParameter(exportSession); + + logger.debug("Tabular Data Service"); + service = TabularDataServiceFactory + .getService(aslSession.getUsername()); + + List capabilities = service.getCapabilities(); + + // Export SDMX Codelist + OperationDescriptor exportSDMXOperation = getOperationDescriptorWithId( + OperationsId.SDMXCodelistExport.toString(), capabilities); + + InvocationCreator ic = InvocationCreator + .getCreator(exportSDMXOperation); + OperationInvocation invocation = ic + .setTargetTable(new TableId(Long.valueOf(trId.getTableId()))) + .setParameters(parameterInstance).create(); + + logger.debug("OperationInvocation: \n" + invocation.toString()); + + Task trTask; + try { + trTask = service.execute(invocation, + new TabularResourceId(Long.valueOf(trId.getId()))); + } catch (Exception e) { + e.printStackTrace(); + throw new TDGWTServiceException( + "Tabular Data Service error exporting TabularResource: " + + e.getLocalizedMessage()); + } + + logger.debug("Start Task on service: TaskId " + trTask.getId()); + SessionUtil.setSDMXExportTask(session, trTask); + } + + + /** + * {@inheritDoc} + */ + @Override + public SDMXExportMonitor getSDMXExportMonitor() throws TDGWTServiceException { + try { + session = this.getThreadLocalRequest().getSession(); + aslSession = SessionUtil.getAslSession(session); + Task task = SessionUtil.getSDMXExportTask(session); + SDMXExportMonitor exportMonitor = new SDMXExportMonitor(); + + if (task == null) { + logger.debug("Task null"); + throw new TDGWTServiceException("Error in exportSDMX task null"); + } else { + TaskStatus status = task.getStatus(); + if (status == null) { + throw new TDGWTServiceException( + "Error in exportSDMX Status null"); + } else { + logger.debug("Status: " + task.getStatus()); + + exportMonitor.setStatus(matchTaskState(task.getStatus())); + switch (exportMonitor.getStatus()) { + case FAILED: + if (task.getResult() != null) { + logger.debug("Task exception:" + + task.getErrorCause()); + exportMonitor.setError(new Throwable(task + .getErrorCause())); + } else { + logger.debug("Task exception: Error In Export"); + exportMonitor.setError(new Throwable( + "Error In Export")); + } + exportMonitor.setProgress(task.getProgress()); + break; + case SUCCEDED: + logger.debug("Task Result:" + task.getResult()); + exportMonitor.setProgress(task.getProgress()); + Table table = task.getResult().getPrimaryTable(); + logger.debug("Table retrived: " + table.toString()); + ExportMetadata exportMetadata = table + .getMetadata(ExportMetadata.class); + logger.debug("ExportMetadata: " + exportMetadata); + + exportMonitor.setTrId(SessionUtil.getTRId(session)); + TabExportMetadata tabExportMetadata = new TabExportMetadata(); + tabExportMetadata.setUrl(exportMetadata.getUri()); + tabExportMetadata.setDestinationType(exportMetadata + .getDestinationType()); + tabExportMetadata.setExportDate(sdf + .format(exportMetadata.getExportDate())); + exportMonitor.setTabExportMetadata(tabExportMetadata); + break; + case IN_PROGRESS: + exportMonitor.setProgress(task.getProgress()); + break; + case ABORTED: + break; + case INITIALIZING: + break; + case WAITING: + break; + default: + break; + } + } + SessionUtil.setSDMXExportTask(session, task); + } + + logger.info("getExportMonitor(): " + exportMonitor); + return exportMonitor; + } catch (Exception e) { + e.printStackTrace(); + throw new TDGWTServiceException( + "Error in exportSDMX SDMXExportMonitor: " + + e.getLocalizedMessage()); + + } + + } + + /** * * {@inheritDoc} @@ -1576,6 +1755,8 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements SessionUtil.setCSVExportTask(session, trTask); } + + /** * {@inheritDoc} */ diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/Constants.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/Constants.java index 2ac9322..c09fae0 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/Constants.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/Constants.java @@ -16,10 +16,17 @@ public class Constants { public final static String DEFAULT_USER = "giancarlo.panichi"; public final static String DEFAULT_SCOPE = "/gcube/devsec"; - public static final String ENCODING = "encoding"; - public static final String HASHEADER = "hasHeader"; - public static final String SEPARATOR = "separator"; - public static final String COLUMNS = "columns"; - public static final String URL = "url"; + public static final String PARAMETER_ENCODING = "encoding"; + public static final String PARAMETER_HASHEADER = "hasHeader"; + public static final String PARAMETER_SEPARATOR = "separator"; + public static final String PARAMETER_COLUMNS = "columns"; + public static final String PARAMETER_URL = "url"; + + public static final String PARAMETER_REGISTRYBASEURL ="registryBaseUrl"; + public static final String PARAMETER_AGENCY ="agency"; + public static final String PARAMETER_ID ="id"; + public static final String PARAMETER_VERSION ="version"; + + } diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/sdmx/SDMXExportMonitor.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/sdmx/SDMXExportMonitor.java index e87b639..6999a8f 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/sdmx/SDMXExportMonitor.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/sdmx/SDMXExportMonitor.java @@ -3,6 +3,8 @@ package org.gcube.portlets.user.td.gwtservice.shared.sdmx; import java.io.Serializable; import org.gcube.portlets.user.td.gwtservice.shared.task.State; +import org.gcube.portlets.user.td.gwtservice.shared.tr.TRId; +import org.gcube.portlets.user.td.gwtservice.shared.tr.table.metadata.TabExportMetadata; /** @@ -20,7 +22,17 @@ public class SDMXExportMonitor implements Serializable { protected String statusDescription; protected Throwable error; protected String url; + protected TabExportMetadata tabExportMetadata; + protected TRId trId; + public TRId getTrId() { + return trId; + } + + public void setTrId(TRId trId) { + this.trId = trId; + } + public float getProgress(){ return progress; }; @@ -65,13 +77,24 @@ public class SDMXExportMonitor implements Serializable { this.url = url; } + public TabExportMetadata getTabExportMetadata() { + return tabExportMetadata; + } + + public void setTabExportMetadata(TabExportMetadata tabExportMetadata) { + this.tabExportMetadata = tabExportMetadata; + } + @Override public String toString() { return "SDMXExportMonitor [progress=" + progress + ", status=" + status + ", statusDescription=" + statusDescription + ", error=" - + error + ", url=" + url + "]"; + + error + ", url=" + url + ", tabExportMetadata=" + + tabExportMetadata + ", trId=" + trId + "]"; } + + } diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/sdmx/SDMXExportSession.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/sdmx/SDMXExportSession.java index 1d2a2bc..de9cae2 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/sdmx/SDMXExportSession.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/sdmx/SDMXExportSession.java @@ -23,6 +23,8 @@ public class SDMXExportSession implements Serializable { protected String id; protected TabResource tabResource; protected Agencies agency; + + public String getId() { return id; }