Search in sources :

Example 1 with Role

use of org.knime.core.node.workflow.MetaNodeTemplateInformation.Role in project knime-core by knime.

the class FileWorkflowPersistor method saveContent.

/**
 * @param wm The WFM to save.
 * @param preFilledSettings The settings eventually written to workflow.knime (or workflow.knime.encrypted).
 * For workflows it contains the version number, cipher, template infos etc. The name of the setting defines the
 * output file name (so it's important!)
 * @param rawWorkflowDirRef To save to.
 * @param execMon ...
 * @param saveHelper ...
 * @throws IOException ...
 * @throws CanceledExecutionException ...
 * @throws LockFailedException ...
 */
private static void saveContent(final WorkflowManager wm, final NodeSettings preFilledSettings, final ReferencedFile rawWorkflowDirRef, final ExecutionMonitor execMon, final WorkflowSaveHelper saveHelper) throws IOException, CanceledExecutionException, LockFailedException {
    ReferencedFile workflowDirRef = rawWorkflowDirRef;
    Role r = wm.getTemplateInformation().getRole();
    final String fName = preFilledSettings.getKey();
    if (!workflowDirRef.fileLockRootForVM()) {
        throw new LockFailedException("Can't write workflow to \"" + workflowDirRef + "\" because the directory can't be locked");
    }
    try {
        final ReferencedFile nodeContainerDirectory = wm.getNodeContainerDirectory();
        final ReferencedFile autoSaveDirectory = wm.getAutoSaveDirectory();
        if (!saveHelper.isAutoSave() && workflowDirRef.equals(nodeContainerDirectory)) {
            if (!nodeContainerDirectory.isDirty()) {
                return;
            } else {
                // update variable assignment to do changes on member
                workflowDirRef = nodeContainerDirectory;
                // delete "old" node directories if not saving to the working
                // directory -- do this before saving the nodes (dirs newly created)
                WorkflowManager.deleteObsoleteNodeDirs(nodeContainerDirectory.getDeletedNodesFileLocations());
            }
        }
        if (saveHelper.isAutoSave() && workflowDirRef.equals(autoSaveDirectory)) {
            if (!autoSaveDirectory.isDirty()) {
                return;
            } else {
                workflowDirRef = autoSaveDirectory;
                WorkflowManager.deleteObsoleteNodeDirs(autoSaveDirectory.getDeletedNodesFileLocations());
            }
        }
        File workflowDir = workflowDirRef.getFile();
        workflowDir.mkdirs();
        if (!workflowDir.isDirectory()) {
            throw new IOException("Unable to create or write directory \": " + workflowDir + "\"");
        }
        saveWorkflowName(preFilledSettings, wm.getNameField());
        saveAuthorInformation(wm.getAuthorInformation(), preFilledSettings);
        saveWorkflowCipher(preFilledSettings, wm.getWorkflowCipher());
        FileNodeContainerMetaPersistor.save(preFilledSettings, wm, workflowDirRef);
        saveWorkflowVariables(wm, preFilledSettings);
        saveCredentials(wm, preFilledSettings);
        saveWorkflowAnnotations(wm, preFilledSettings);
        NodeSettingsWO nodesSettings = saveSettingsForNodes(preFilledSettings);
        Collection<NodeContainer> nodes = wm.getNodeContainers();
        double progRatio = 1.0 / (nodes.size() + 1);
        for (NodeContainer nextNode : nodes) {
            int id = nextNode.getID().getIndex();
            ExecutionMonitor subExec = execMon.createSubProgress(progRatio);
            execMon.setMessage(nextNode.getNameWithID());
            NodeSettingsWO sub = nodesSettings.addNodeSettings("node_" + id);
            saveNodeContainer(sub, workflowDirRef, nextNode, subExec, saveHelper);
            subExec.setProgress(1.0);
        }
        execMon.setMessage("connection information");
        NodeSettingsWO connSettings = saveSettingsForConnections(preFilledSettings);
        int connectionNumber = 0;
        for (ConnectionContainer cc : wm.getConnectionContainers()) {
            NodeSettingsWO nextConnectionConfig = connSettings.addNodeSettings("connection_" + connectionNumber);
            saveConnection(nextConnectionConfig, cc);
            connectionNumber += 1;
        }
        int inCount = wm.getNrInPorts();
        NodeSettingsWO inPortsSetts = inCount > 0 ? saveInPortsSetting(preFilledSettings) : null;
        NodeSettingsWO inPortsSettsEnum = null;
        if (inPortsSetts != null) {
            // TODO actually not neccessary to save the class name
            saveInportsBarUIInfoClassName(inPortsSetts, wm.getInPortsBarUIInfo());
            saveInportsBarUIInfoSettings(inPortsSetts, wm.getInPortsBarUIInfo());
            inPortsSettsEnum = saveInPortsEnumSetting(inPortsSetts);
        }
        for (int i = 0; i < inCount; i++) {
            NodeSettingsWO sPort = saveInPortSetting(inPortsSettsEnum, i);
            saveInPort(sPort, wm, i);
        }
        int outCount = wm.getNrOutPorts();
        NodeSettingsWO outPortsSetts = outCount > 0 ? saveOutPortsSetting(preFilledSettings) : null;
        NodeSettingsWO outPortsSettsEnum = null;
        if (outPortsSetts != null) {
            saveOutportsBarUIInfoClassName(outPortsSetts, wm.getOutPortsBarUIInfo());
            saveOutportsBarUIInfoSettings(outPortsSetts, wm.getOutPortsBarUIInfo());
            outPortsSettsEnum = saveOutPortsEnumSetting(outPortsSetts);
        }
        for (int i = 0; i < outCount; i++) {
            NodeSettingsWO singlePort = saveOutPortSetting(outPortsSettsEnum, i);
            saveOutPort(singlePort, wm, i);
        }
        saveEditorUIInformation(wm, preFilledSettings);
        File workflowFile = new File(workflowDir, fName);
        String toBeDeletedFileName = Role.Template.equals(r) ? TEMPLATE_FILE : WORKFLOW_FILE;
        new File(workflowDir, toBeDeletedFileName).delete();
        new File(workflowDir, WorkflowCipher.getCipherFileName(toBeDeletedFileName)).delete();
        OutputStream os = new FileOutputStream(workflowFile);
        os = wm.getDirectNCParent().cipherOutput(os);
        preFilledSettings.saveToXML(os);
        if (saveHelper.isSaveData()) {
            File saveWithDataFile = new File(workflowDir, SAVED_WITH_DATA_FILE);
            BufferedWriter o = new BufferedWriter(new FileWriter(saveWithDataFile));
            o.write("Do not delete this file!");
            o.newLine();
            o.write("This file serves to indicate that the workflow was written as part of the usual save " + "routine (not exported).");
            o.newLine();
            o.newLine();
            o.write("Workflow was last saved by user ");
            o.write(System.getProperty("user.name"));
            o.write(" on " + new Date());
            o.close();
        }
        if (saveHelper.isAutoSave() && autoSaveDirectory == null) {
            wm.setAutoSaveDirectory(workflowDirRef);
        }
        if (!saveHelper.isAutoSave() && nodeContainerDirectory == null) {
            wm.setNodeContainerDirectory(workflowDirRef);
        }
        NodeContainerState wmState = wm.getNodeContainerState();
        // non remote executions
        boolean isExecutingLocally = wmState.isExecutionInProgress() && !wmState.isExecutingRemotely();
        if (workflowDirRef.equals(nodeContainerDirectory) && !isExecutingLocally) {
            wm.unsetDirty();
        }
        workflowDirRef.setDirty(isExecutingLocally);
        execMon.setProgress(1.0);
    } finally {
        workflowDirRef.fileUnlockRootForVM();
    }
}
Also used : LockFailedException(org.knime.core.util.LockFailedException) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileWriter(java.io.FileWriter) IOException(java.io.IOException) ReferencedFile(org.knime.core.internal.ReferencedFile) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) Role(org.knime.core.node.workflow.MetaNodeTemplateInformation.Role) FileOutputStream(java.io.FileOutputStream) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 2 with Role

use of org.knime.core.node.workflow.MetaNodeTemplateInformation.Role 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

File (java.io.File)2 IOException (java.io.IOException)2 ReferencedFile (org.knime.core.internal.ReferencedFile)2 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)2 Role (org.knime.core.node.workflow.MetaNodeTemplateInformation.Role)2 BufferedWriter (java.io.BufferedWriter)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Date (java.util.Date)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1 NodeSettingsWO (org.knime.core.node.NodeSettingsWO)1 MetaNodeLinkUpdateResult (org.knime.core.node.workflow.WorkflowPersistor.MetaNodeLinkUpdateResult)1 LockFailedException (org.knime.core.util.LockFailedException)1