From b308f1c32b7f03d23d9bd0045a454122b19d9e7a Mon Sep 17 00:00:00 2001 From: Giancarlo Panichi Date: Mon, 1 Feb 2016 18:07:11 +0000 Subject: [PATCH] 1452: Implement a GUI for StatMan Algorithms Importer Task-Url: https://support.d4science.org/issues/1452 Added Annotation Parser git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/statistical-algorithms-importer@122702 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 33 ++++ .../client/project/ProjectManager.java | 13 +- .../client/rpc/StatAlgoImporterService.java | 4 +- .../rpc/StatAlgoImporterServiceAsync.java | 6 +- .../tools/input/GlobalVariablesPanel.java | 2 +- .../input/InputOutputVariablesPanel.java | 2 +- .../tools/input/InputVariableTabPanel.java | 7 +- .../tools/input/InterpreterInfoPanel.java | 4 + .../server/StatAlgoImporterServiceImpl.java | 19 +- .../server/annotation/WPS4RParser.java | 174 ++++++++++++++++++ .../server/annotation/WPSAlgorithmInfo.java | 77 ++++++++ 11 files changed, 320 insertions(+), 21 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/annotation/WPS4RParser.java create mode 100644 src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/annotation/WPSAlgorithmInfo.java diff --git a/pom.xml b/pom.xml index ba5d197..c0c8268 100644 --- a/pom.xml +++ b/pom.xml @@ -302,6 +302,25 @@ [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + + + org.n52.wps + 52n-wps-client-lib + 3.2.0 + + + gt-opengis + org.geotools + + + + + org.n52.wps + 52n-wps-r + (0.0.1,5.0.0) + + + @@ -515,7 +534,21 @@ + + + n52-releases + 52n Releases + http://52north.org/maven/repo/releases/ + + true + + + true + + + + diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/project/ProjectManager.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/project/ProjectManager.java index 7e7977b..c97d714 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/project/ProjectManager.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/project/ProjectManager.java @@ -14,7 +14,6 @@ import org.gcube.portlets.user.statisticalalgorithmsimporter.client.type.Session import org.gcube.portlets.user.statisticalalgorithmsimporter.client.utils.UtilsGXT3; import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterSessionExpiredException; import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.InputData; -import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.MainCode; import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.Project; import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.ProjectFolder; import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.workspace.ItemDescription; @@ -403,7 +402,7 @@ public class ProjectManager { Log.debug("Set Main Code: " + itemDescription); StatAlgoImporterServiceAsync.INSTANCE.setMainCode(itemDescription, - new AsyncCallback() { + new AsyncCallback() { @Override public void onFailure(Throwable caught) { @@ -421,8 +420,8 @@ public class ProjectManager { } @Override - public void onSuccess(Void result) { - project.setMainCode(new MainCode(itemDescription)); + public void onSuccess(Project result) { + project=result; fireProjectStatusMainCodeSetEvent(); } }); @@ -433,7 +432,7 @@ public class ProjectManager { final StatAlgoImporterMonitor monitor) { StatAlgoImporterServiceAsync.INSTANCE.setNewMainCode( newMainCodeEvent.getFile(), newMainCodeEvent.getCode(), - new AsyncCallback() { + new AsyncCallback() { @Override public void onFailure(Throwable caught) { @@ -452,8 +451,8 @@ public class ProjectManager { } @Override - public void onSuccess(ItemDescription itemDescription) { - project.setMainCode(new MainCode(itemDescription)); + public void onSuccess(Project result) { + project=result; fireProjectStatusMainCodeSetEvent(); monitor.hide(); } diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterService.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterService.java index 0f7ed73..1ba35fa 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterService.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterService.java @@ -47,7 +47,7 @@ public interface StatAlgoImporterService extends RemoteService { throws StatAlgoImporterServiceException; // - public void setMainCode(ItemDescription itemDescription) + public Project setMainCode(ItemDescription itemDescription) throws StatAlgoImporterServiceException; public void addResourceToProject(ItemDescription itemDescription) @@ -73,7 +73,7 @@ public interface StatAlgoImporterService extends RemoteService { public Project restoreUISession(String value) throws StatAlgoImporterServiceException; - public ItemDescription setNewMainCode(ItemDescription itemDescription, + public Project setNewMainCode(ItemDescription itemDescription, String code) throws StatAlgoImporterServiceException; public void publishSoftware() throws StatAlgoImporterServiceException; diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterServiceAsync.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterServiceAsync.java index 97bed22..4af2e2a 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterServiceAsync.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/rpc/StatAlgoImporterServiceAsync.java @@ -39,7 +39,8 @@ public interface StatAlgoImporterServiceAsync { void createProjectOnWorkspace(ItemDescription itemDescription, AsyncCallback callback); - void setMainCode(ItemDescription itemDescription, AsyncCallback callback); + void setMainCode(ItemDescription itemDescription, + AsyncCallback callback); void addResourceToProject(ItemDescription itemDescription, AsyncCallback asyncCallback); @@ -55,7 +56,8 @@ public interface StatAlgoImporterServiceAsync { void saveCode(String code, AsyncCallback asyncCallback); - void setNewMainCode(ItemDescription itemDescription, String code, AsyncCallback asyncCallback); + void setNewMainCode(ItemDescription itemDescription, String code, + AsyncCallback asyncCallback); void createSoftware(InputData inputData, AsyncCallback callback); diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/GlobalVariablesPanel.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/GlobalVariablesPanel.java index cb692ae..29d0a5c 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/GlobalVariablesPanel.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/GlobalVariablesPanel.java @@ -429,7 +429,7 @@ public class GlobalVariablesPanel extends ContentPanel { return listGlobalVarialbles; } - public void setMainCode(Project project) { + public void clearVariables(Project project) { storeGlobalVariable.clear(); storeGlobalVariable.commitChanges(); seq = 0; diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/InputOutputVariablesPanel.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/InputOutputVariablesPanel.java index b9eb11f..5743646 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/InputOutputVariablesPanel.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/InputOutputVariablesPanel.java @@ -485,7 +485,7 @@ public class InputOutputVariablesPanel extends ContentPanel { return listInputOutputVarialbles; } - public void setMainCode(Project project) { + public void clearVariables(Project project) { storeInputOutputVariables.clear(); storeInputOutputVariables.commitChanges(); seq = 0; diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/InputVariableTabPanel.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/InputVariableTabPanel.java index 852e6fe..8b419c3 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/InputVariableTabPanel.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/InputVariableTabPanel.java @@ -77,9 +77,10 @@ public class InputVariableTabPanel extends TabPanel { // public void setMainCode(Project project) { try { - globalVariablesPanel.setMainCode(project); - inputOutputVariablesPanel.setMainCode(project); - + globalVariablesPanel.update(project); + inputOutputVariablesPanel.update(project); + projectInfoPanel.update(project); + interpreterInfoPanel.update(project); forceLayout(); } catch (Throwable e) { Log.error("Error in InputVariableTabPanel: " diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/InterpreterInfoPanel.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/InterpreterInfoPanel.java index ea7069c..b691840 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/InterpreterInfoPanel.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/tools/input/InterpreterInfoPanel.java @@ -391,10 +391,14 @@ public class InterpreterInfoPanel extends ContentPanel { seq = project.getInputData().getInterpreterInfo() .getInterpreterPackagesInfo().size(); } else { + storeInterpreterPackageInfo.clear(); + storeInterpreterPackageInfo.commitChanges(); seq = 0; } } else { interpreterVersion.clear(); + storeInterpreterPackageInfo.clear(); + storeInterpreterPackageInfo.commitChanges(); seq = 0; } diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/StatAlgoImporterServiceImpl.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/StatAlgoImporterServiceImpl.java index 4c44abb..9a4fb84 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/StatAlgoImporterServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/StatAlgoImporterServiceImpl.java @@ -11,6 +11,7 @@ import javax.servlet.http.HttpSession; import org.apache.commons.io.IOUtils; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.portlets.user.statisticalalgorithmsimporter.client.rpc.StatAlgoImporterService; +import org.gcube.portlets.user.statisticalalgorithmsimporter.server.annotation.WPS4RParser; import org.gcube.portlets.user.statisticalalgorithmsimporter.server.file.CodeReader; import org.gcube.portlets.user.statisticalalgorithmsimporter.server.generator.ProjectBuilder; import org.gcube.portlets.user.statisticalalgorithmsimporter.server.social.AlgorithmNotification; @@ -260,21 +261,25 @@ public class StatAlgoImporterServiceImpl extends RemoteServiceServlet implements } @Override - public void setMainCode(ItemDescription itemDescription) + public Project setMainCode(ItemDescription itemDescription) throws StatAlgoImporterServiceException { try { HttpSession session = this.getThreadLocalRequest().getSession(); - SessionUtil.getAslSession(session); + ASLSession aslSession=SessionUtil.getAslSession(session); logger.debug("SetMainCode()"); Project project = SessionUtil.getProjectSession(session); if (project != null) { project.setMainCode(new MainCode(itemDescription)); + project.setInputData(null); + project.setProjectTarget(null); + WPS4RParser wps4Parser=new WPS4RParser(project, aslSession); + project=wps4Parser.parse(); SessionUtil.setProjectSession(session, project); } else { throw new StatAlgoImporterServiceException("No project open!"); } - return; + return project; } catch (StatAlgoImporterServiceException e) { e.printStackTrace(); throw e; @@ -498,7 +503,7 @@ public class StatAlgoImporterServiceImpl extends RemoteServiceServlet implements } @Override - public ItemDescription setNewMainCode(ItemDescription fileDescription, + public Project setNewMainCode(ItemDescription fileDescription, String code) throws StatAlgoImporterServiceException { try { HttpSession session = this.getThreadLocalRequest().getSession(); @@ -513,8 +518,12 @@ public class StatAlgoImporterServiceImpl extends RemoteServiceServlet implements aslSession, fileDescription, code, project); MainCode mainCode = new MainCode(mainCodeItemDescription); project.setMainCode(mainCode); + project.setInputData(null); + project.setProjectTarget(null); + WPS4RParser wps4Parser=new WPS4RParser(project, aslSession); + project=wps4Parser.parse(); SessionUtil.setProjectSession(session, project); - return mainCodeItemDescription; + return project; } else { throw new StatAlgoImporterServiceException("No project open!"); } 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 new file mode 100644 index 0000000..5fef0da --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/annotation/WPS4RParser.java @@ -0,0 +1,174 @@ +package org.gcube.portlets.user.statisticalalgorithmsimporter.server.annotation; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import org.gcube.application.framework.core.session.ASLSession; +import org.gcube.portlets.user.statisticalalgorithmsimporter.server.storage.FilesStorage; +import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterServiceException; +import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.DataType; +import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.IOType; +import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.InputOutputVariables; +import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.ProjectInfo; +import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.InputData; +import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.Project; +import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.workspace.ItemDescription; +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.RAnnotationType; +import org.n52.wps.server.r.syntax.RAttribute; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Giancarlo Panichi email: g.panichi@isti.cnr.it + * + */ +public class WPS4RParser { + + public static final Logger logger = LoggerFactory + .getLogger(WPS4RParser.class); + + + private Project project; + private ASLSession aslSession; + + public WPS4RParser(Project project, ASLSession aslSession) { + this.project = project; + this.aslSession = aslSession; + } + + public Project parse() throws StatAlgoImporterServiceException { + 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; + try { + RAnnotationParser parser = new RAnnotationParser(config); + annotations = parser.parseAnnotationsfromScript(is); + } catch (Throwable e) { + e.printStackTrace(); + throw new StatAlgoImporterServiceException(e.getLocalizedMessage()); + } finally { + try { + is.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + WPSAlgorithmInfo wpsAlgorithmInfo = mapAnnotations(annotations); + logger.debug("wpsAlgorithmInfo: "+wpsAlgorithmInfo); + if (wpsAlgorithmInfo.getAlgorithmName() == null) + return project; + else + return mapToProject(wpsAlgorithmInfo); + + } + + 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()); + project.setInputData(inputData); + } else { + InputData inputData=project.getInputData(); + ProjectInfo projectInfo=new ProjectInfo(wpsAlgorithmInfo.getAlgorithmName(), wpsAlgorithmInfo.getDescription(), null); + inputData.setProjectInfo(projectInfo); + inputData.setListInputOutputVariables(wpsAlgorithmInfo.getInputOutputVariables()); + inputData.setListGlobalVariables(null); + } + return project; + + } + + private WPSAlgorithmInfo mapAnnotations(List annotations) + throws StatAlgoImporterServiceException { + try { + + WPSAlgorithmInfo wpsAlgorithmInfo = new WPSAlgorithmInfo(); + ArrayList inputOutputVariables = new ArrayList<>(); + int index=1; + for (RAnnotation rAnnotation : annotations) { + if (rAnnotation.getType().equals(RAnnotationType.DESCRIPTION)) { + wpsAlgorithmInfo.setVersion(rAnnotation + .getStringValue(RAttribute.VERSION)); + wpsAlgorithmInfo.setDescription(rAnnotation + .getStringValue(RAttribute.ABSTRACT)); + wpsAlgorithmInfo.setAlgorithmName(rAnnotation + .getStringValue(RAttribute.TITLE)); + wpsAlgorithmInfo.setVersion(rAnnotation + .getStringValue(RAttribute.VERSION)); + } else if (rAnnotation.getType().equals(RAnnotationType.OUTPUT) + || rAnnotation.getType().equals(RAnnotationType.INPUT)) { + // output, text, Random number list, + + String type = rAnnotation.getStringValue(RAttribute.TYPE); + String name = rAnnotation + .getStringValue(RAttribute.IDENTIFIER); + String description = rAnnotation + .getStringValue(RAttribute.TITLE); + String defaultValue = rAnnotation + .getStringValue(RAttribute.DEFAULT_VALUE); + if (type == null) + type = "string"; + if (name == null) + name = ""; + if (description == null) + description = ""; + if (defaultValue == null) + defaultValue = ""; + + IOType ioType = IOType.INPUT; + + if (rAnnotation.getType().equals(RAnnotationType.OUTPUT)) + ioType = IOType.OUTPUT; + + DataType dataType = WPStype2DataType(type); + if (defaultValue != null && defaultValue.contains("|") + && dataType == DataType.STRING) + dataType = DataType.ENUMERATED; + + InputOutputVariables ioVariable = new InputOutputVariables( + index, name, description, defaultValue, dataType, ioType, + ""); + inputOutputVariables.add(ioVariable); + index++; + } + } + wpsAlgorithmInfo.setInputOutputVariables(inputOutputVariables); + return wpsAlgorithmInfo; + + } catch (Throwable e) { + e.printStackTrace(); + throw new StatAlgoImporterServiceException(e.getLocalizedMessage()); + } + } + + private static DataType WPStype2DataType(String type) { + if (type.equalsIgnoreCase("double")) + return DataType.DOUBLE; + else if (type.equalsIgnoreCase("integer")) + return DataType.INTEGER; + else if (type.equalsIgnoreCase("string")) + return DataType.STRING; + else if (type.equalsIgnoreCase("boolean")) + return DataType.BOOLEAN; + else + return DataType.FILE; + } +} diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/annotation/WPSAlgorithmInfo.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/annotation/WPSAlgorithmInfo.java new file mode 100644 index 0000000..6725ea3 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/annotation/WPSAlgorithmInfo.java @@ -0,0 +1,77 @@ +package org.gcube.portlets.user.statisticalalgorithmsimporter.server.annotation; + +import java.io.Serializable; +import java.util.ArrayList; + +import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.InputOutputVariables; + +/** + * + * @author Giancarlo Panichi email: g.panichi@isti.cnr.it + * + */ +public class WPSAlgorithmInfo implements Serializable { + + private static final long serialVersionUID = 6333710590825979561L; + private String description; + private String algorithmName; + private String version; + private ArrayList inputOutputVariables; + + public WPSAlgorithmInfo() { + super(); + } + + public WPSAlgorithmInfo(String description, String algorithmName, + String version, ArrayList inputOutputVariables) { + super(); + this.description = description; + this.algorithmName = algorithmName; + this.version = version; + this.inputOutputVariables = inputOutputVariables; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getAlgorithmName() { + return algorithmName; + } + + public void setAlgorithmName(String algorithmName) { + this.algorithmName = algorithmName; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public ArrayList getInputOutputVariables() { + return inputOutputVariables; + } + + public void setInputOutputVariables( + ArrayList inputOutputVariables) { + this.inputOutputVariables = inputOutputVariables; + } + + @Override + public String toString() { + return "WPSAlgorithmInfo [description=" + description + + ", algorithmName=" + algorithmName + ", version=" + version + + ", inputOutputVariables=" + inputOutputVariables + "]"; + } + + + +}