From 5d1603580bf948eabe79cf91e1f0b906f847ad67 Mon Sep 17 00:00:00 2001 From: Giancarlo Panichi Date: Fri, 29 May 2015 10:41:05 +0000 Subject: [PATCH] 180: TDM - Portlets must support Storage Id in communications with service Task-Url: https://support.d4science.org/issues/180 Fixed CSVImport git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-gwt-service@115139 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../gwtservice/server/TDGWTServiceImpl.java | 262 ++++++------------ .../opexecution/OpExecution4CSVImport.java | 121 ++++++++ 2 files changed, 204 insertions(+), 179 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/user/td/gwtservice/server/opexecution/OpExecution4CSVImport.java 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 a8ae2e9..dc1dd3c 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 @@ -117,6 +117,7 @@ import org.gcube.portlets.user.td.gwtservice.server.file.CodelistMappingFileUplo import org.gcube.portlets.user.td.gwtservice.server.file.FileUtil; import org.gcube.portlets.user.td.gwtservice.server.opexecution.OpExecution4AddColumn; import org.gcube.portlets.user.td.gwtservice.server.opexecution.OpExecution4CSVExport; +import org.gcube.portlets.user.td.gwtservice.server.opexecution.OpExecution4CSVImport; import org.gcube.portlets.user.td.gwtservice.server.opexecution.OpExecution4ChangeColumnType; import org.gcube.portlets.user.td.gwtservice.server.opexecution.OpExecution4ChangeColumnsPosition; import org.gcube.portlets.user.td.gwtservice.server.opexecution.OpExecution4ChangeTableType; @@ -2806,7 +2807,7 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements } } - // TODO + /** * * @@ -4006,38 +4007,67 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements @Override public String startCSVImport(CSVImportSession csvImportSession) throws TDGWTServiceException { + logger.debug("StartCSVImport: " + csvImportSession); CSVFileUploadSession fileUploadSession = null; - + try { - HttpSession session = this.getThreadLocalRequest().getSession(); if (session == null) { throw new TDGWTServiceException( "Error retrieving the session: null"); } logger.debug("Session:" + session.getId()); - - ASLSession aslSession = SessionUtil.getAslSession(session); - String user = aslSession.getUsername(); - logger.debug("Session User:" + user); - - logger.debug("StartCSVImport: " + csvImportSession.toString()); - + fileUploadSession = SessionUtil.getCSVFileUploadSession(session); if (fileUploadSession == null) { throw new TDGWTServiceException( "Error retrieving the fileUploadSession: null"); } - String fileUrlOnStorage = loadCSVFileOnStorage(user, - fileUploadSession, csvImportSession); + ASLSession aslSession = SessionUtil.getAslSession(session); - fileUploadSession.getCsvFile().delete(); + AuthorizationProvider.instance.set(new AuthorizationToken( + aslSession.getUsername(), aslSession.getScope())); + TabularDataService service = TabularDataServiceFactory.getService(); - String taskId = importCSVFileOnService(session, aslSession, user, - fileUrlOnStorage, fileUploadSession, csvImportSession); + OpExecution4CSVImport opEx = new OpExecution4CSVImport(session, + aslSession, service, csvImportSession, fileUploadSession); + OpExecutionDirector director = new OpExecutionDirector(); + director.setOperationExecutionBuilder(opEx); + director.constructOperationExecution(); + OperationExecution invocation = director.getOperationExecution(); - return taskId; + if (invocation == null) { + throw new TDGWTServiceException( + "Error in invocation: Operation not supported"); + } + + logger.debug("OperationInvocation: \n" + invocation.toString()); + TabularResource tabularResource = service.createTabularResource(); + TabResource csvImportTabResource = csvImportSession + .getTabResource(); + syncTRMetaData(tabularResource, csvImportTabResource); + TRId trId = new TRId(String.valueOf(tabularResource.getId() + .getValue())); + csvImportTabResource.setTrId(trId); + logger.debug(csvImportTabResource.toString()); + SessionUtil.setCSVImportTabResource(session, csvImportTabResource); + Task trTask; + try { + trTask = service.execute(invocation, tabularResource.getId()); + } catch (Throwable e) { + e.printStackTrace(); + throw new TDGWTServiceException( + "Tabular Data Service error creating TabularResource: " + + e.getLocalizedMessage()); + } + + logger.debug("Start Task on service: TaskId " + trTask.getId()); + TaskWrapper taskWrapper = new TaskWrapper(trTask, + UIOperationsId.CSVImport, trId); + SessionUtil.setStartedTask(session, taskWrapper); + + return trTask.getId().getValue(); } catch (TDGWTServiceException e) { try { @@ -4073,134 +4103,13 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements logger.error(e1.getLocalizedMessage()); } e.printStackTrace(); - throw new TDGWTServiceException("Error in CSVImport: " + throw new TDGWTServiceException("Error in CSV Import: " + e.getLocalizedMessage()); } + } - /** - * - * @param fileUrlOnStorage - * @param fileUploadSession - * @param csvImportSession - * @return - */ - private Map csvImportFileParameter(String fileUrlOnStorage, - CSVFileUploadSession fileUploadSession, - CSVImportSession csvImportSession) { - Map parameterInstances = new HashMap(); - parameterInstances.put(Constants.PARAMETER_URL, fileUrlOnStorage); - parameterInstances.put(Constants.PARAMETER_SEPARATOR, String - .valueOf(fileUploadSession.getParserConfiguration() - .getDelimiter()));// ',' - 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.PARAMETER_HASHEADER, hasHeader);// true - parameterInstances.put(Constants.PARAMETER_FIELDMASK, - csvImportSession.getColumnToImportMask());// Column Mask - parameterInstances.put(Constants.PARAMETER_SKIPERROR, - csvImportSession.isSkipInvalidLines()); - return parameterInstances; - } - - /** - * - * @param user - * @param fileUploadSession - * @param csvImportSession - * @throws TDGWTServiceException - */ - private String loadCSVFileOnStorage(String user, - CSVFileUploadSession fileUploadSession, - CSVImportSession csvImportSession) throws TDGWTServiceException { - String fileUrlOnStorage = null; - - logger.debug("File Storage Access"); - - logger.debug("CSVImportSession skip:" - + csvImportSession.isSkipInvalidLines()); - /* - * if (csvImportSession.isSkipInvalidLines()) { try { - * fileUploadSession.setCsvFile(CSVFileUtil.skipError( - * fileUploadSession.getCsvFile(), - * fileUploadSession.getParserConfiguration())); } catch (ParseException - * e) { logger.debug("Error parsing file for skip:" + e.getMessage()); - * throw new TDGWTServiceException("Error parsing file for skip: " + - * e.getMessage()); - * - * } catch (IOException e) { - * logger.debug("Error IOException skipping file error: " + - * e.getMessage()); throw new TDGWTServiceException( - * "Error IOException skipping file error: " + e.getMessage()); - * - * } } - */ - FilesStorage filesStorage = new FilesStorage(); - - fileUrlOnStorage = filesStorage.storageCSVTempFile(user, - fileUploadSession.getCsvFile()); - logger.debug("File Url On Storage:" + fileUrlOnStorage); - - if (fileUrlOnStorage == null || fileUrlOnStorage.isEmpty()) { - throw new TDGWTServiceException( - "Tabular Data Service error loading file on storage"); - } - - return fileUrlOnStorage; - } - - private String importCSVFileOnService(HttpSession session, - ASLSession aslSession, String user, String fileUrlOnStorage, - CSVFileUploadSession fileUploadSession, - CSVImportSession csvImportSession) throws TDGWTServiceException { - - Map parameterInstance = csvImportFileParameter( - fileUrlOnStorage, fileUploadSession, csvImportSession); - - logger.debug("Tabular Data Service"); - - AuthorizationProvider.instance.set(new AuthorizationToken(aslSession - .getUsername(), aslSession.getScope())); - TabularDataService service = TabularDataServiceFactory.getService(); - - // Import CSV file - - OperationDefinition importCSVFileOperation = OperationDefinitionMap - .map(OperationsId.CSVImport.toString(), service); - - OperationExecution invocation = new OperationExecution( - importCSVFileOperation.getOperationId(), parameterInstance); - - logger.debug("OperationInvocation: \n" + invocation.toString()); - TabularResource tabularResource = service.createTabularResource(); - TabResource csvImportTabResource = csvImportSession.getTabResource(); - syncTRMetaData(tabularResource, csvImportTabResource); - TRId trId = new TRId(String.valueOf(tabularResource.getId().getValue())); - csvImportTabResource.setTrId(trId); - logger.debug(csvImportTabResource.toString()); - SessionUtil.setCSVImportTabResource(session, csvImportTabResource); - Task trTask; - try { - trTask = service.execute(invocation, tabularResource.getId()); - } catch (Throwable e) { - e.printStackTrace(); - throw new TDGWTServiceException( - "Tabular Data Service error creating TabularResource: " - + e.getLocalizedMessage()); - } - - logger.debug("Start Task on service: TaskId " + trTask.getId()); - TaskWrapper taskWrapper = new TaskWrapper(trTask, - UIOperationsId.CSVImport, trId); - SessionUtil.setStartedTask(session, taskWrapper); - - return trTask.getId().getValue(); - } + /** * @@ -5424,8 +5333,7 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements checkTRId(filterColumnSession.getTrId()); TabularResourceId tabularResourceId = new TabularResourceId( - Long.valueOf(filterColumnSession.getTrId() - .getId())); + Long.valueOf(filterColumnSession.getTrId().getId())); TabularResource tabularResource = service .getTabularResource(tabularResourceId); @@ -6565,11 +6473,11 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements SessionUtil.setStartedTask(session, taskWrapper); return trTask.getId().getValue(); - - } catch (TemplateNotCompatibleException e){ + + } catch (TemplateNotCompatibleException e) { logger.debug(TEMPLATE_NOT_COMPATIBLE_EXCEPTION); e.printStackTrace(); - throw new TDGWTServiceException(TEMPLATE_NOT_COMPATIBLE_EXCEPTION); + throw new TDGWTServiceException(TEMPLATE_NOT_COMPATIBLE_EXCEPTION); } catch (TDGWTServiceException e) { throw e; } catch (SecurityException e) { @@ -9031,10 +8939,10 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getAslSession(session); logger.debug("Get uri from resolver: " + uriResolverSession); - - UriResolverTDClient uriResolverTDClient=new UriResolverTDClient(); - link=uriResolverTDClient.resolve(uriResolverSession, aslSession); - + + UriResolverTDClient uriResolverTDClient = new UriResolverTDClient(); + link = uriResolverTDClient.resolve(uriResolverSession, aslSession); + return link; } catch (TDGWTServiceException e) { @@ -9197,7 +9105,7 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements internalURITD.getUrl(), applicationType, resourceTDDescriptor.getName(), internalURITD.getMimeType()); - mimeType=internalURITD.getMimeType(); + mimeType = internalURITD.getMimeType(); } else { if (resource instanceof TableResourceTD) { throw new TDGWTServiceException( @@ -9209,10 +9117,10 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements } } } - - UriResolverTDClient uriResolverTDClient=new UriResolverTDClient(); - String link=uriResolverTDClient.resolve(uriResolverSession, aslSession); - + + UriResolverTDClient uriResolverTDClient = new UriResolverTDClient(); + String link = uriResolverTDClient.resolve(uriResolverSession, + aslSession); if (link == null || link.isEmpty()) { throw new TDGWTServiceException( @@ -9561,9 +9469,7 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements } } - - - + /** * * @param applyTableRuleSession @@ -9572,14 +9478,14 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements * @throws TDGWTServiceException */ public String startApplyTableRule( - ApplyTableRuleSession applyTableRuleSession, - HttpSession session) throws TDGWTServiceException { + ApplyTableRuleSession applyTableRuleSession, HttpSession session) + throws TDGWTServiceException { try { // HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getAslSession(session); - SessionUtil.setApplyTableRuleSession(session, - applyTableRuleSession); + SessionUtil + .setApplyTableRuleSession(session, applyTableRuleSession); if (applyTableRuleSession == null) { logger.error("Apply Table Rule Session is null"); @@ -9594,27 +9500,26 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements checkTRId(applyTableRuleSession.getTrId()); TabularResourceId tabularResourceId = new TabularResourceId( - Long.valueOf(applyTableRuleSession.getTrId() - .getId())); + Long.valueOf(applyTableRuleSession.getTrId().getId())); TabularResource tabularResource = service .getTabularResource(tabularResourceId); checkTabularResourceIsFlow(tabularResource); checkTabularResourceLocked(tabularResource); checkTabularResourceIsFinal(tabularResource); - - //TODO + + // TODO Task trTask = null; - //service.applyTableRule(tabularResourceId,....); - //logger.debug("Rules On Table Apply: TaskId " + trTask.getId()); + // service.applyTableRule(tabularResourceId,....); + // logger.debug("Rules On Table Apply: TaskId " + trTask.getId()); TaskWrapper taskWrapper = new TaskWrapper(trTask, - UIOperationsId.RuleOnColumnApply, - applyTableRuleSession.getTrId()); + UIOperationsId.RuleOnColumnApply, + applyTableRuleSession.getTrId()); SessionUtil.setStartedTask(session, taskWrapper); return null; - //return trTask.getId().getValue(); - + // return trTask.getId().getValue(); + } catch (TDGWTServiceException e) { throw e; } catch (SecurityException e) { @@ -9701,8 +9606,7 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements } } - - + /** * * @param detachTableRulesSession @@ -9711,8 +9615,8 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements * @throws TDGWTServiceException */ public void setDetachTableRules( - DetachTableRulesSession detachTableRulesSession, - HttpSession session) throws TDGWTServiceException { + DetachTableRulesSession detachTableRulesSession, HttpSession session) + throws TDGWTServiceException { try { // HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getAslSession(session); @@ -9741,7 +9645,6 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements checkTabularResourceLocked(tabularResource); checkTabularResourceIsFinal(tabularResource); - ArrayList rules = detachTableRulesSession .getRules(); ArrayList ruleIds = new ArrayList(); @@ -9750,9 +9653,10 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements RuleId ruleId = new RuleId(r.getId()); ruleIds.add(ruleId); } - //TODO - /*service.detachTableRules(tabularResourceId, - ruleIds);*/ + // TODO + /* + * service.detachTableRules(tabularResourceId, ruleIds); + */ } return; diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/opexecution/OpExecution4CSVImport.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/opexecution/OpExecution4CSVImport.java new file mode 100644 index 0000000..e5f90cf --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/opexecution/OpExecution4CSVImport.java @@ -0,0 +1,121 @@ +package org.gcube.portlets.user.td.gwtservice.server.opexecution; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpSession; + +import org.gcube.application.framework.core.session.ASLSession; +import org.gcube.data.analysis.tabulardata.commons.webservice.types.operations.OperationDefinition; +import org.gcube.data.analysis.tabulardata.commons.webservice.types.operations.OperationExecution; +import org.gcube.data.analysis.tabulardata.service.TabularDataService; +import org.gcube.portlets.user.td.gwtservice.server.file.CSVFileUploadSession; +import org.gcube.portlets.user.td.gwtservice.server.storage.FilesStorage; +import org.gcube.portlets.user.td.gwtservice.server.trservice.OperationDefinitionMap; +import org.gcube.portlets.user.td.gwtservice.shared.Constants; +import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVImportSession; +import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTServiceException; +import org.gcube.portlets.user.td.gwtservice.shared.file.HeaderPresence; +import org.gcube.portlets.user.td.widgetcommonevent.shared.operations.OperationsId; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Operation Execution for CSV Import + * + * @author "Giancarlo Panichi" email: g.panichi@isti.cnr.it + * + */ +public class OpExecution4CSVImport extends OpExecutionBuilder { + protected static Logger logger = LoggerFactory + .getLogger(OpExecution4CSVImport.class); + + private TabularDataService service; + private CSVImportSession csvImportSession; + private ASLSession aslSession; + private CSVFileUploadSession fileUploadSession; + + public OpExecution4CSVImport(HttpSession session, ASLSession aslSession, + TabularDataService service, CSVImportSession csvImportSession, + CSVFileUploadSession fileUploadSession) { + this.service = service; + this.csvImportSession = csvImportSession; + this.aslSession = aslSession; + this.fileUploadSession = fileUploadSession; + } + + @Override + public void buildOpEx() throws TDGWTServiceException { + logger.debug("CSV Import: " + csvImportSession); + + String user = aslSession.getUsername(); + logger.debug("Session User:" + user); + + String fileUrlOnStorage = loadCSVFileOnStorage(user, + fileUploadSession, csvImportSession); + + fileUploadSession.getCsvFile().delete(); + + OperationDefinition operationDefinition = OperationDefinitionMap + .map(OperationsId.CSVImport.toString(), service); + + Map map = new HashMap(); + map.put(Constants.PARAMETER_URL, fileUrlOnStorage); + map.put(Constants.PARAMETER_SEPARATOR, String + .valueOf(fileUploadSession.getParserConfiguration() + .getDelimiter()));// ',' + map.put(Constants.PARAMETER_ENCODING, + fileUploadSession.getParserConfiguration().getCharset() + .name());// "UTF-8" + boolean hasHeader = true; + if (fileUploadSession.getParserConfiguration().getHeaderPresence() == HeaderPresence.NONE) { + hasHeader = false; + } + + map.put(Constants.PARAMETER_HASHEADER, hasHeader);// true + map.put(Constants.PARAMETER_FIELDMASK, + csvImportSession.getColumnToImportMask());// Column Mask + map.put(Constants.PARAMETER_SKIPERROR, + csvImportSession.isSkipInvalidLines()); + + OperationExecution invocation = new OperationExecution( + operationDefinition.getOperationId(), map); + + operationExecutionSpec.setOp(invocation); + + + } + + /** + * + * @param user + * @param fileUploadSession + * @param csvImportSession + * @throws TDGWTServiceException + */ + private String loadCSVFileOnStorage(String user, + CSVFileUploadSession fileUploadSession, + CSVImportSession csvImportSession) throws TDGWTServiceException { + String fileUrlOnStorage = null; + + logger.debug("File Storage Access"); + + logger.debug("CSVImportSession skip:" + + csvImportSession.isSkipInvalidLines()); + + FilesStorage filesStorage = new FilesStorage(); + + fileUrlOnStorage = filesStorage.storageCSVTempFile(user, + fileUploadSession.getCsvFile()); + logger.debug("File Url On Storage:" + fileUrlOnStorage); + + if (fileUrlOnStorage == null || fileUrlOnStorage.isEmpty()) { + throw new TDGWTServiceException( + "Tabular Data Service error loading file on storage"); + } + + return fileUrlOnStorage; + } + +}