From 3afec34257587c8425f32d6d4a562fc618aff31c Mon Sep 17 00:00:00 2001 From: Giancarlo Panichi Date: Tue, 2 Feb 2016 10:17:20 +0000 Subject: [PATCH] 1452: Implement a GUI for StatMan Algorithms Importer Task-Url: https://support.d4science.org/issues/1452 Fixed InputStream on Annotations git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/statistical-algorithms-importer@122714 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../server/annotation/WPS4RParser.java | 91 +++++++++++++------ .../server/generator/ProjectBuilder.java | 53 +++++++---- 2 files changed, 98 insertions(+), 46 deletions(-) diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/annotation/WPS4RParser.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/annotation/WPS4RParser.java index 5fef0da..4689871 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/annotation/WPS4RParser.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/annotation/WPS4RParser.java @@ -2,6 +2,10 @@ package org.gcube.portlets.user.statisticalalgorithmsimporter.server.annotation; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.List; @@ -18,6 +22,7 @@ import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.workspace.It import org.n52.wps.server.r.R_Config; import org.n52.wps.server.r.metadata.RAnnotationParser; import org.n52.wps.server.r.syntax.RAnnotation; +import org.n52.wps.server.r.syntax.RAnnotationException; import org.n52.wps.server.r.syntax.RAnnotationType; import org.n52.wps.server.r.syntax.RAttribute; import org.slf4j.Logger; @@ -30,11 +35,10 @@ import org.slf4j.LoggerFactory; * */ public class WPS4RParser { - + public static final Logger logger = LoggerFactory .getLogger(WPS4RParser.class); - private Project project; private ASLSession aslSession; @@ -44,56 +48,88 @@ public class WPS4RParser { } public Project parse() throws StatAlgoImporterServiceException { + logger.debug("MainCode: " + project.getMainCode()); ItemDescription mainCode = project.getMainCode().getItemDescription(); FilesStorage fileStorage = new FilesStorage(); InputStream is = fileStorage.retrieveItemOnWorkspace( aslSession.getUsername(), mainCode.getId()); - - R_Config config = R_Config.getInstance(); - - List annotations; + logger.debug("MainCode InputStream: " + is); + + Path tempFile=null; try { - RAnnotationParser parser = new RAnnotationParser(config); - annotations = parser.parseAnnotationsfromScript(is); - } catch (Throwable e) { + tempFile = Files.createTempFile("RCodeToParse", ".R"); + Files.copy(is, tempFile, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { e.printStackTrace(); throw new StatAlgoImporterServiceException(e.getLocalizedMessage()); } finally { try { is.close(); } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } } + logger.debug("" + tempFile); + + R_Config config = R_Config.getInstance(); + logger.debug("R_Config: " + config); + List annotations; + try { + RAnnotationParser parser = new RAnnotationParser(config); + logger.debug("RAnnotations Parser:" + parser); + annotations = parser.parseAnnotationsfromScript(Files.newInputStream(tempFile, StandardOpenOption.READ)); + } catch (RAnnotationException e) { + e.printStackTrace(); + throw new StatAlgoImporterServiceException(e.getLocalizedMessage()); + } catch (Throwable e) { + e.printStackTrace(); + throw new StatAlgoImporterServiceException(e.getLocalizedMessage()); + } + + try { + Files.delete(tempFile); + } catch (IOException e) { + e.printStackTrace(); + } + + logger.debug("RAnnotations: " + annotations); + WPSAlgorithmInfo wpsAlgorithmInfo = mapAnnotations(annotations); - logger.debug("wpsAlgorithmInfo: "+wpsAlgorithmInfo); - if (wpsAlgorithmInfo.getAlgorithmName() == null) + logger.debug("wpsAlgorithmInfo: " + wpsAlgorithmInfo); + if (wpsAlgorithmInfo==null || wpsAlgorithmInfo.getAlgorithmName() == null) return project; else return mapToProject(wpsAlgorithmInfo); } - private Project mapToProject(WPSAlgorithmInfo wpsAlgorithmInfo) throws StatAlgoImporterServiceException { - if(project==null){ + private Project mapToProject(WPSAlgorithmInfo wpsAlgorithmInfo) + throws StatAlgoImporterServiceException { + if (project == null) { throw new StatAlgoImporterServiceException("Open project before!"); } - if(project.getInputData()==null){ - ProjectInfo projectInfo=new ProjectInfo(wpsAlgorithmInfo.getAlgorithmName(), wpsAlgorithmInfo.getDescription(), null); - InputData inputData=new InputData(projectInfo, null, null, wpsAlgorithmInfo.getInputOutputVariables()); + + if (project.getInputData() == null) { + ProjectInfo projectInfo = new ProjectInfo( + wpsAlgorithmInfo.getAlgorithmName(), + wpsAlgorithmInfo.getDescription(), null); + InputData inputData = new InputData(projectInfo, null, null, + wpsAlgorithmInfo.getInputOutputVariables()); project.setInputData(inputData); } else { - InputData inputData=project.getInputData(); - ProjectInfo projectInfo=new ProjectInfo(wpsAlgorithmInfo.getAlgorithmName(), wpsAlgorithmInfo.getDescription(), null); + InputData inputData = project.getInputData(); + ProjectInfo projectInfo = new ProjectInfo( + wpsAlgorithmInfo.getAlgorithmName(), + wpsAlgorithmInfo.getDescription(), null); inputData.setProjectInfo(projectInfo); - inputData.setListInputOutputVariables(wpsAlgorithmInfo.getInputOutputVariables()); + inputData.setListInputOutputVariables(wpsAlgorithmInfo + .getInputOutputVariables()); inputData.setListGlobalVariables(null); } return project; - + } private WPSAlgorithmInfo mapAnnotations(List annotations) @@ -102,15 +138,18 @@ public class WPS4RParser { WPSAlgorithmInfo wpsAlgorithmInfo = new WPSAlgorithmInfo(); ArrayList inputOutputVariables = new ArrayList<>(); - int index=1; + int index = 1; for (RAnnotation rAnnotation : annotations) { + logger.debug("RAnnotation: " + rAnnotation); if (rAnnotation.getType().equals(RAnnotationType.DESCRIPTION)) { wpsAlgorithmInfo.setVersion(rAnnotation .getStringValue(RAttribute.VERSION)); wpsAlgorithmInfo.setDescription(rAnnotation .getStringValue(RAttribute.ABSTRACT)); - wpsAlgorithmInfo.setAlgorithmName(rAnnotation - .getStringValue(RAttribute.TITLE)); + String algorithmName=rAnnotation + .getStringValue(RAttribute.TITLE); + algorithmName=algorithmName.replaceAll("[^A-Za-z0-9]", "_"); + wpsAlgorithmInfo.setAlgorithmName(algorithmName); wpsAlgorithmInfo.setVersion(rAnnotation .getStringValue(RAttribute.VERSION)); } else if (rAnnotation.getType().equals(RAnnotationType.OUTPUT) @@ -144,8 +183,8 @@ public class WPS4RParser { dataType = DataType.ENUMERATED; InputOutputVariables ioVariable = new InputOutputVariables( - index, name, description, defaultValue, dataType, ioType, - ""); + index, name, description, defaultValue, dataType, + ioType, ""); inputOutputVariables.add(ioVariable); index++; } diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/generator/ProjectBuilder.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/generator/ProjectBuilder.java index 41aece8..17f41da 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/generator/ProjectBuilder.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/generator/ProjectBuilder.java @@ -84,6 +84,19 @@ public class ProjectBuilder { createAlgorithm(); createIntegrationInfo(); createProjectJarFile(); + try { + if (algorithmJava != null) + Files.delete(algorithmJava); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + if (infoTXT != null) + Files.delete(infoTXT); + } catch (IOException e) { + e.printStackTrace(); + } return project; @@ -128,24 +141,23 @@ public class ProjectBuilder { throw new StatAlgoImporterServiceException(e.getLocalizedMessage()); } - - filesStorage.saveInputStreamInItem( - aslSession.getUsername(), project.getProjectTarget() - .getProjectDeploy().getPackageProject().getId(), - inputStream); - - - WorkspaceItem packageProject=filesStorage.retrieveItemInfoOnWorkspace(aslSession.getUsername(), project.getProjectTarget() - .getProjectDeploy().getPackageProject().getId()); - + filesStorage.saveInputStreamInItem(aslSession.getUsername(), project + .getProjectTarget().getProjectDeploy().getPackageProject() + .getId(), inputStream); + + WorkspaceItem packageProject = filesStorage + .retrieveItemInfoOnWorkspace(aslSession.getUsername(), project + .getProjectTarget().getProjectDeploy() + .getPackageProject().getId()); + ItemDescription packageProjectItemDescription; try { - packageProjectItemDescription = new ItemDescription(packageProject.getId(), - packageProject.getName(), packageProject - .getOwner().getPortalLogin(), - packageProject.getPath(), packageProject - .getType().name()); - packageProjectItemDescription.setPublicLink(packageProject.getPublicLink(true)); + packageProjectItemDescription = new ItemDescription( + packageProject.getId(), packageProject.getName(), + packageProject.getOwner().getPortalLogin(), + packageProject.getPath(), packageProject.getType().name()); + packageProjectItemDescription.setPublicLink(packageProject + .getPublicLink(true)); } catch (InternalErrorException e) { logger.error(e.getLocalizedMessage()); e.printStackTrace(); @@ -153,11 +165,11 @@ public class ProjectBuilder { } - project.getProjectTarget().getProjectDeploy().setPackageProject(packageProjectItemDescription); + project.getProjectTarget().getProjectDeploy() + .setPackageProject(packageProjectItemDescription); logger.debug("ProjectDeploy: " + project.getProjectTarget().getProjectDeploy()); - - + } private void copyJarInDeploy() throws StatAlgoImporterServiceException { @@ -447,7 +459,8 @@ public class ProjectBuilder { throw new StatAlgoImporterServiceException(e.getLocalizedMessage()); } - project.getProjectTarget().getProjectDeploy().setPackageProject(packageUrl); + project.getProjectTarget().getProjectDeploy() + .setPackageProject(packageUrl); } private void createTargetFolder() throws StatAlgoImporterServiceException {