Search in sources :

Example 1 with MetaNodeLinkUpdateResult

use of org.knime.core.node.workflow.WorkflowPersistor.MetaNodeLinkUpdateResult in project knime-core by knime.

the class LoadMetaNodeTemplateRunnable method run.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("null")
@Override
public void run(final IProgressMonitor pm) {
    try {
        // create progress monitor
        ProgressHandler progressHandler = new ProgressHandler(pm, 101, "Loading metanode template...");
        final CheckCancelNodeProgressMonitor progressMonitor = new CheckCancelNodeProgressMonitor(pm);
        progressMonitor.addProgressListener(progressHandler);
        URI sourceURI = m_templateKNIMEFolder.toURI();
        File parentFile = ResolverUtil.resolveURItoLocalOrTempFile(sourceURI, pm);
        if (pm.isCanceled()) {
            throw new InterruptedException();
        }
        Display d = Display.getDefault();
        GUIWorkflowLoadHelper loadHelper = new GUIWorkflowLoadHelper(d, parentFile.getName(), sourceURI, parentFile, null, true);
        TemplateNodeContainerPersistor loadPersistor = loadHelper.createTemplateLoadPersistor(parentFile, sourceURI);
        MetaNodeLinkUpdateResult loadResult = new MetaNodeLinkUpdateResult("Template from \"" + sourceURI + "\"");
        m_parentWFM.load(loadPersistor, loadResult, new ExecutionMonitor(progressMonitor), false);
        m_result = loadResult;
        if (pm.isCanceled()) {
            throw new InterruptedException();
        }
        pm.subTask("Finished.");
        pm.done();
        final IStatus status = createStatus(m_result, !m_result.getGUIMustReportDataLoadErrors());
        final String message;
        switch(status.getSeverity()) {
            case IStatus.OK:
                message = "No problems during load.";
                break;
            case IStatus.WARNING:
                message = "Warnings during load";
                logPreseveLineBreaks("Warnings during load: " + m_result.getFilteredError("", LoadResultEntryType.Warning), false);
                break;
            default:
                message = "Errors during load";
                logPreseveLineBreaks("Errors during load: " + m_result.getFilteredError("", LoadResultEntryType.Warning), true);
        }
        Display.getDefault().asyncExec(new Runnable() {

            @Override
            public void run() {
                // will not open if status is OK.
                ErrorDialog.openError(Display.getDefault().getActiveShell(), "Workflow Load", message, status);
            }
        });
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        // IMPORTANT: Remove the reference to the file and the
        // editor!!! Otherwise the memory cannot be freed later
        m_parentWFM = null;
        m_templateKNIMEFolder = null;
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) URI(java.net.URI) TemplateNodeContainerPersistor(org.knime.core.node.workflow.TemplateNodeContainerPersistor) MetaNodeLinkUpdateResult(org.knime.core.node.workflow.WorkflowPersistor.MetaNodeLinkUpdateResult) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) File(java.io.File) Display(org.eclipse.swt.widgets.Display)

Example 2 with MetaNodeLinkUpdateResult

use of org.knime.core.node.workflow.WorkflowPersistor.MetaNodeLinkUpdateResult in project knime-core by knime.

the class CreateMetaNodeTemplateCommand method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute() {
    // Add node to workflow and get the container
    LoadMetaNodeTemplateRunnable loadRunnable = null;
    try {
        IWorkbench wb = PlatformUI.getWorkbench();
        IProgressService ps = wb.getProgressService();
        // this one sets the workflow manager in the editor
        loadRunnable = new LoadMetaNodeTemplateRunnable(getHostWFM(), m_templateKNIMEFolder);
        ps.run(false, true, loadRunnable);
        MetaNodeLinkUpdateResult result = loadRunnable.getLoadResult();
        m_container = (NodeContainer) result.getLoadedInstance();
        if (m_container == null) {
            throw new RuntimeException("No template returned by load routine, see log for details");
        }
        // create extra info and set it
        NodeUIInformation info = NodeUIInformation.builder().setNodeLocation(m_location.x, m_location.y, -1, -1).setHasAbsoluteCoordinates(false).setSnapToGrid(m_snapToGrid).setIsDropLocation(true).build();
        m_container.setUIInformation(info);
    } catch (Throwable t) {
        Throwable cause = t;
        while ((cause.getCause() != null) && (cause.getCause() != cause)) {
            cause = cause.getCause();
        }
        String error = "The selected node could not be created";
        if (cause instanceof FileNotFoundException) {
            error += " because a file could not be found: " + cause.getMessage();
            MessageDialog.openError(Display.getDefault().getActiveShell(), "Node cannot be created.", error);
        } else if (cause instanceof IOException) {
            error += " because of an I/O error: " + cause.getMessage();
            MessageDialog.openError(Display.getDefault().getActiveShell(), "Node cannot be created.", error);
        } else if (cause instanceof InvalidSettingsException) {
            error += " because the metanode contains invalid settings: " + cause.getMessage();
            MessageDialog.openError(Display.getDefault().getActiveShell(), "Node cannot be created.", error);
        } else if (cause instanceof UnsupportedWorkflowVersionException) {
            error += " because the metanode version is incompatible: " + cause.getMessage();
            MessageDialog.openError(Display.getDefault().getActiveShell(), "Node cannot be created.", error);
        } else if ((cause instanceof CanceledExecutionException) || (cause instanceof InterruptedException)) {
            LOGGER.info("Metanode loading was canceled by the user", cause);
        } else {
            LOGGER.error(String.format("Metanode loading failed with %s: %s", cause.getClass().getSimpleName(), cause.getMessage()), cause);
            error += ": " + cause.getMessage();
            MessageDialog.openError(Display.getDefault().getActiveShell(), "Node cannot be created.", error);
        }
    }
}
Also used : UnsupportedWorkflowVersionException(org.knime.core.node.workflow.UnsupportedWorkflowVersionException) LoadMetaNodeTemplateRunnable(org.knime.workbench.editor2.LoadMetaNodeTemplateRunnable) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) IWorkbench(org.eclipse.ui.IWorkbench) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IProgressService(org.eclipse.ui.progress.IProgressService) NodeUIInformation(org.knime.core.node.workflow.NodeUIInformation) MetaNodeLinkUpdateResult(org.knime.core.node.workflow.WorkflowPersistor.MetaNodeLinkUpdateResult)

Example 3 with MetaNodeLinkUpdateResult

use of org.knime.core.node.workflow.WorkflowPersistor.MetaNodeLinkUpdateResult in project knime-core by knime.

the class WorkflowManager method loadMetaNodeTemplate.

/**
 * Reads the template info from the metanode argument and then resolves that URI and returns a workflow manager that
 * lives as child of {@link #templateWorkflowRoot}. Used to avoid duplicate loading from a remote location. The
 * returned instance is then copied to the final destination.
 */
private NodeContainerTemplate loadMetaNodeTemplate(final NodeContainerTemplate meta, final WorkflowLoadHelper loadHelper, final LoadResult loadResult) throws IOException, UnsupportedWorkflowVersionException, CanceledExecutionException {
    MetaNodeTemplateInformation linkInfo = meta.getTemplateInformation();
    URI sourceURI = linkInfo.getSourceURI();
    WorkflowManager tempParent = lazyInitTemplateWorkflowRoot();
    MetaNodeLinkUpdateResult loadResultChild;
    NodeContext.pushContext((NodeContainer) meta);
    try {
        if (m_workflowContext != null && m_workflowContext.getMountpointURI().isPresent() && sourceURI.getHost().startsWith("knime.") && (ResolverUtil.resolveURItoLocalFile(m_workflowContext.getMountpointURI().get()) == null)) {
            // a workflow relative template URI but the workflow itself is not local
            // => the template is also not local and must be resolved using the workflow's original location
            URI origWfUri = m_workflowContext.getMountpointURI().get();
            String combinedPath = origWfUri.getPath() + sourceURI.getPath();
            sourceURI = new URI(origWfUri.getScheme(), origWfUri.getUserInfo(), origWfUri.getHost(), origWfUri.getPort(), combinedPath, origWfUri.getQuery(), origWfUri.getFragment()).normalize();
        }
        File localDir = ResolverUtil.resolveURItoLocalOrTempFile(sourceURI);
        if (localDir.isFile()) {
            // looks like a zipped metanode downloaded from a 4.4+ server
            File unzipped = FileUtil.createTempDir("metanode-template");
            FileUtil.unzip(localDir, unzipped);
            localDir = unzipped.listFiles()[0];
        }
        TemplateNodeContainerPersistor loadPersistor = loadHelper.createTemplateLoadPersistor(localDir, sourceURI);
        loadResultChild = new MetaNodeLinkUpdateResult("Template from " + sourceURI.toString());
        tempParent.load(loadPersistor, loadResultChild, new ExecutionMonitor(), false);
    } catch (InvalidSettingsException | URISyntaxException e) {
        throw new IOException("Unable to read template metanode: " + e.getMessage(), e);
    } finally {
        NodeContext.removeLastContext();
    }
    NodeContainerTemplate linkResult = loadResultChild.getLoadedInstance();
    MetaNodeTemplateInformation templInfo = linkResult.getTemplateInformation();
    Role sourceRole = templInfo.getRole();
    switch(sourceRole) {
        case Link:
            // (this is due to the template information link uri set above)
            break;
        default:
            throw new IOException("The source of the linked instance does " + "not represent a template but is of role " + sourceRole);
    }
    loadResult.addChildError(loadResultChild);
    return linkResult;
}
Also used : URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) Role(org.knime.core.node.workflow.MetaNodeTemplateInformation.Role) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) MetaNodeLinkUpdateResult(org.knime.core.node.workflow.WorkflowPersistor.MetaNodeLinkUpdateResult) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Aggregations

MetaNodeLinkUpdateResult (org.knime.core.node.workflow.WorkflowPersistor.MetaNodeLinkUpdateResult)3 File (java.io.File)2 IOException (java.io.IOException)2 URI (java.net.URI)2 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 FileNotFoundException (java.io.FileNotFoundException)1 URISyntaxException (java.net.URISyntaxException)1 IStatus (org.eclipse.core.runtime.IStatus)1 Display (org.eclipse.swt.widgets.Display)1 IWorkbench (org.eclipse.ui.IWorkbench)1 IProgressService (org.eclipse.ui.progress.IProgressService)1 ReferencedFile (org.knime.core.internal.ReferencedFile)1 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)1 Role (org.knime.core.node.workflow.MetaNodeTemplateInformation.Role)1 NodeUIInformation (org.knime.core.node.workflow.NodeUIInformation)1 TemplateNodeContainerPersistor (org.knime.core.node.workflow.TemplateNodeContainerPersistor)1 UnsupportedWorkflowVersionException (org.knime.core.node.workflow.UnsupportedWorkflowVersionException)1 LoadMetaNodeTemplateRunnable (org.knime.workbench.editor2.LoadMetaNodeTemplateRunnable)1