Search in sources :

Example 36 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class NodeContainer method unsetDirty.

/**
 * Called from persistor when node has been saved.
 */
void unsetDirty() {
    final ReferencedFile ncDirectory = getNodeContainerDirectory();
    assert ncDirectory != null : "NC directory must not be null at this point";
    ncDirectory.setDirty(false);
}
Also used : ReferencedFile(org.knime.core.internal.ReferencedFile)

Example 37 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class FileWorkflowPersistor method loadNodeFile.

ReferencedFile loadNodeFile(final NodeSettingsRO settings, final ReferencedFile workflowDirRef) throws InvalidSettingsException {
    String fileString = settings.getString("node_settings_file");
    if (fileString == null) {
        throw new InvalidSettingsException("Unable to read settings " + "file for node " + settings.getKey());
    }
    File workflowDir = workflowDirRef.getFile();
    // fileString is something like "File Reader(#1)/settings.xml", thus
    // it contains two levels of the hierarchy. We leave it here to the
    // java.io.File implementation to resolve these levels
    File fullFile = new File(workflowDir, fileString);
    if (!fullFile.isFile() || !fullFile.canRead()) {
        throw new InvalidSettingsException("Unable to read settings " + "file " + fullFile.getAbsolutePath());
    }
    Stack<String> children = new Stack<String>();
    File workflowDirAbsolute = workflowDir.getAbsoluteFile();
    while (!fullFile.getAbsoluteFile().equals(workflowDirAbsolute)) {
        children.push(fullFile.getName());
        fullFile = fullFile.getParentFile();
    }
    // create a ReferencedFile hierarchy for the settings file
    ReferencedFile result = workflowDirRef;
    while (!children.empty()) {
        result = new ReferencedFile(result, children.pop());
    }
    return result;
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File) ReferencedFile(org.knime.core.internal.ReferencedFile) Stack(java.util.Stack)

Example 38 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class WorkflowManager method load.

/**
 * Implementation of {@link #load(FileWorkflowPersistor, ExecutionMonitor, boolean)}.
 *
 * @noreference This method is not intended to be referenced by clients.
 */
public void load(final TemplateNodeContainerPersistor persistor, final MetaNodeLinkUpdateResult result, final ExecutionMonitor exec, final boolean keepNodeMessages) throws IOException, InvalidSettingsException, CanceledExecutionException, UnsupportedWorkflowVersionException {
    final ReferencedFile refDirectory = persistor.getMetaPersistor().getNodeContainerDirectory();
    exec.setMessage("Loading workflow structure from \"" + refDirectory + "\"");
    exec.checkCanceled();
    LoadVersion version = persistor.getLoadVersion();
    LOGGER.debug("Loading workflow from \"" + refDirectory + "\" (version \"" + version + "\" with loader class \"" + persistor.getClass().getSimpleName() + "\")");
    // data files are loaded using a repository of reference tables;
    Map<Integer, BufferedDataTable> tblRep = new HashMap<Integer, BufferedDataTable>();
    persistor.preLoadNodeContainer(null, null, result);
    NodeContainerTemplate loadedInstance = null;
    boolean isIsolatedProject = persistor.isProject();
    InsertWorkflowPersistor insertPersistor = new InsertWorkflowPersistor(persistor);
    ReentrantLock lock = isIsolatedProject ? new ReentrantLock() : m_workflowLock.getReentrantLock();
    lock.lock();
    try {
        m_loadVersion = persistor.getLoadVersion();
        NodeID[] newIDs = loadContent(insertPersistor, tblRep, null, exec, result, keepNodeMessages).getNodeIDs();
        if (newIDs.length != 1) {
            throw new InvalidSettingsException("Loading workflow failed, " + "couldn't identify child sub flow (typically " + "a project)");
        }
        loadedInstance = (NodeContainerTemplate) getNodeContainer(newIDs[0]);
    } finally {
        lock.unlock();
    }
    exec.setProgress(1.0);
    result.setLoadedInstance(loadedInstance);
    result.setGUIMustReportDataLoadErrors(persistor.mustWarnOnDataLoadError());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ReferencedFile(org.knime.core.internal.ReferencedFile) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) BufferedDataTable(org.knime.core.node.BufferedDataTable) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion)

Example 39 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class WorkflowManager method saveAsTemplate.

/**
 * {@inheritDoc}
 */
@Override
public MetaNodeTemplateInformation saveAsTemplate(final File directory, final ExecutionMonitor exec) throws IOException, CanceledExecutionException, LockFailedException {
    WorkflowManager tempParent = lazyInitTemplateWorkflowRoot();
    WorkflowManager copy = null;
    ReferencedFile workflowDirRef = new ReferencedFile(directory);
    workflowDirRef.lock();
    try {
        WorkflowCopyContent.Builder cntBuilder = WorkflowCopyContent.builder();
        cntBuilder.setNodeIDs(getID());
        WorkflowCopyContent cnt = cntBuilder.build();
        try (WorkflowLock lock = lock()) {
            cnt = tempParent.copyFromAndPasteHere(getParent(), cnt);
        }
        NodeID cID = cnt.getNodeIDs()[0];
        copy = (WorkflowManager) tempParent.getNodeContainer(cID);
        MetaNodeTemplateInformation template = MetaNodeTemplateInformation.createNewTemplate(WorkflowManager.class);
        try (WorkflowLock copyLock = copy.lock()) {
            copy.setTemplateInformation(template);
            copy.setName(null);
            NodeSettings templateSettings = MetaNodeTemplateInformation.createNodeSettingsForTemplate(copy);
            copy.save(directory, new WorkflowSaveHelper(true, false), exec);
            templateSettings.saveToXML(new FileOutputStream(new File(workflowDirRef.getFile(), WorkflowPersistor.TEMPLATE_FILE)));
        }
        return template;
    } finally {
        if (copy != null) {
            tempParent.removeNode(copy.getID());
        }
        workflowDirRef.unlock();
    }
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) FileOutputStream(java.io.FileOutputStream) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 40 with ReferencedFile

use of org.knime.core.internal.ReferencedFile in project knime-core by knime.

the class WorkflowManager method save.

/**
 * @param directory The directory to save in
 * @param exec The execution monitor
 * @param saveHelper ...
 * @throws IOException If an IO error occured
 * @throws CanceledExecutionException If the execution was canceled
 * @throws LockFailedException If locking failed
 * @since 2.10
 */
public void save(final File directory, final WorkflowSaveHelper saveHelper, final ExecutionMonitor exec) throws IOException, CanceledExecutionException, LockFailedException {
    if (this == ROOT) {
        throw new IOException("Can't save root workflow");
    }
    if (m_isWorkflowDirectoryReadonly) {
        throw new IOException("Workflow is read-only, can't save");
    }
    try (WorkflowLock lock = lock()) {
        ReferencedFile directoryReference = new ReferencedFile(directory);
        // if it's the location associated with the workflow we will use the same instance (due to VM lock)
        if (directoryReference.equals(getNodeContainerDirectory())) {
            directoryReference = getNodeContainerDirectory();
        } else if (saveHelper.isAutoSave() && directoryReference.equals(getAutoSaveDirectory())) {
            directoryReference = getAutoSaveDirectory();
        }
        directoryReference.writeLock();
        try {
            final boolean isWorkingDirectory = directoryReference.equals(getNodeContainerDirectory());
            final LoadVersion saveVersion = FileWorkflowPersistor.VERSION_LATEST;
            if (m_loadVersion != null && !m_loadVersion.equals(saveVersion)) {
                LOGGER.info("Workflow was created with another version of KNIME (workflow version " + m_loadVersion + "), converting to current version. This may take some time.");
                setDirtyAll();
            }
            if (isWorkingDirectory) {
                m_loadVersion = saveVersion;
            }
            if (m_authorInformation == null) {
                m_authorInformation = new AuthorInformation();
            } else {
                m_authorInformation = new AuthorInformation(m_authorInformation);
            }
            final File workflowDir = directoryReference.getFile();
            workflowDir.mkdirs();
            boolean isTemplate = getTemplateInformation().getRole().equals(Role.Template);
            if (isTemplate) {
                FileWorkflowPersistor.saveAsTemplate(this, directoryReference, exec, saveHelper);
            } else {
                FileWorkflowPersistor.save(this, directoryReference, exec, saveHelper);
                WorkflowSaveHook.runHooks(this, saveHelper.isSaveData(), workflowDir);
            }
        } finally {
            directoryReference.writeUnlock();
        }
    }
}
Also used : IOException(java.io.IOException) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Aggregations

ReferencedFile (org.knime.core.internal.ReferencedFile)46 File (java.io.File)29 IOException (java.io.IOException)22 BufferedInputStream (java.io.BufferedInputStream)11 FileInputStream (java.io.FileInputStream)11 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)8 PortObject (org.knime.core.node.port.PortObject)8 FlowVariablePortObject (org.knime.core.node.port.flowvariable.FlowVariablePortObject)8 FileStorePortObject (org.knime.core.data.filestore.FileStorePortObject)7 InactiveBranchPortObject (org.knime.core.node.port.inactive.InactiveBranchPortObject)7 HashMap (java.util.HashMap)6 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)6 FileOutputStream (java.io.FileOutputStream)5 Map (java.util.Map)5 ContainerTable (org.knime.core.data.container.ContainerTable)5 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)5 FlowVariablePortObjectSpec (org.knime.core.node.port.flowvariable.FlowVariablePortObjectSpec)5 InactiveBranchPortObjectSpec (org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4