Search in sources :

Example 6 with WorkflowContext

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

the class ReadContextPropertyConfiguration method extractContextProperty.

private static String extractContextProperty(final String property) {
    WorkflowManager manager = NodeContext.getContext().getWorkflowManager();
    if (CONTEXT_WORKFLOW_NAME.equals(property)) {
        return manager.getName();
    }
    if (CONTEXT_WORKFLOW_PATH.equals(property)) {
        WorkflowContext context = manager.getContext();
        File wfLocation = context.getOriginalLocation() == null ? context.getCurrentLocation() : context.getOriginalLocation();
        File mpLocation = context.getMountpointRoot();
        if (mpLocation == null || wfLocation == null) {
            return "";
        }
        String wfPath = wfLocation.getAbsolutePath();
        String mpPath = mpLocation.getAbsolutePath();
        assert wfPath.startsWith(mpPath);
        String resultPath = wfPath.substring(mpPath.length());
        return resultPath.replace("\\", "/");
    }
    if (CONTEXT_WORKFLOW_ABSOLUTE_PATH.equals(property)) {
        WorkflowContext context = manager.getContext();
        File wfLocation = context.getCurrentLocation();
        if (wfLocation == null) {
            return "";
        }
        return wfLocation.getAbsolutePath().replace("\\", "/");
    }
    if (CONTEXT_SERVER_USER.equals(property)) {
        return manager.getContext().getUserid();
    }
    if (CONTEXT_TEMP_LOCATION.equals(property)) {
        return manager.getContext().getTempLocation().getAbsolutePath();
    }
    AuthorInformation author = manager.getAuthorInformation();
    if (author != null) {
        if (CONTEXT_AUTHOR.equals(property)) {
            return author.getAuthor();
        }
        if (CONTEXT_EDITOR.equals(property)) {
            return author.getLastEditor();
        }
        if (CONTEXT_CREATION_DATE.equals(property)) {
            Date creationDate = author.getAuthoredDate();
            if (creationDate != null) {
                return creationDate.toString();
            }
        }
        if (CONTEXT_LAST_MODIFIED.equals(property)) {
            Date modDate = author.getLastEditDate();
            if (modDate != null) {
                return modDate.toString();
            }
        }
    }
    return null;
}
Also used : WorkflowManager(org.knime.core.node.workflow.WorkflowManager) WorkflowContext(org.knime.core.node.workflow.WorkflowContext) AuthorInformation(org.knime.core.node.workflow.WorkflowManager.AuthorInformation) File(java.io.File) Date(java.util.Date)

Example 7 with WorkflowContext

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

the class NodeLogger method getLogObject.

/**
 * @param message the logging message
 * @return a KNIMELogMessage that not only contains the log message but also the information about the workflow
 * and node that belong to the log message if applicable
 */
private Object getLogObject(final Object message) {
    if (!LOG_NODE_ID && !LOG_IN_WF_DIR && !LOG_WF_DIR) {
        return message;
    }
    final NodeContext context = NodeContext.getContext();
    NodeID nodeID = null;
    String nodeName = null;
    File workflowDir = null;
    if (context != null) {
        if (LOG_NODE_ID) {
            // retrieve and store the node id only if the user has requested to log it
            final NodeContainer nodeContainer = context.getNodeContainer();
            if (nodeContainer != null) {
                nodeID = nodeContainer.getID();
                nodeName = nodeContainer.getName();
            }
        }
        if (LOG_IN_WF_DIR || LOG_WF_DIR) {
            final WorkflowManager workflowManager = context.getWorkflowManager();
            if (workflowManager != null) {
                final WorkflowContext workflowContext = workflowManager.getContext();
                if (workflowContext != null) {
                    workflowDir = workflowContext.getCurrentLocation();
                }
            }
        }
    }
    return new KNIMELogMessage(nodeID, nodeName, workflowDir, message);
}
Also used : NodeContext(org.knime.core.node.workflow.NodeContext) WorkflowManager(org.knime.core.node.workflow.WorkflowManager) WorkflowContext(org.knime.core.node.workflow.WorkflowContext) NodeID(org.knime.core.node.workflow.NodeID) NodeContainer(org.knime.core.node.workflow.NodeContainer) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 8 with WorkflowContext

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

the class FileUtil method getWorkflowTempDir.

/**
 * Reads the temporary directory associated with the current workflow (set in the {@link WorkflowContext}), or - if
 * that is <code>null</code> - the global temp dir (see {@link KNIMEConstants#getKNIMETempPath()}).
 *
 * @return the temp dir of the current {@link WorkflowContext} or the standard temp dir, if no context is set.
 * @since 3.5
 */
public static File getWorkflowTempDir() {
    final File fallbackDir = KNIMEConstants.getKNIMETempPath().toFile();
    NodeContext nodeContext = NodeContext.getContext();
    if (nodeContext == null) {
        return fallbackDir;
    }
    WorkflowManager wfm = nodeContext.getWorkflowManager();
    if (wfm == null) {
        return fallbackDir;
    }
    WorkflowContext workflowContext = wfm.getContext();
    if (workflowContext == null) {
        return fallbackDir;
    }
    if (!workflowContext.getTempLocation().isDirectory()) {
        LOGGER.error("Temp folder \"" + workflowContext.getTempLocation().getAbsolutePath() + "\" does not exist (associated with node context \"" + nodeContext + "\") - using fallback temp folder (\"" + fallbackDir.getAbsolutePath() + "\"");
        return fallbackDir;
    } else {
        return workflowContext.getTempLocation();
    }
}
Also used : NodeContext(org.knime.core.node.workflow.NodeContext) WorkflowManager(org.knime.core.node.workflow.WorkflowManager) WorkflowContext(org.knime.core.node.workflow.WorkflowContext) File(java.io.File)

Aggregations

WorkflowContext (org.knime.core.node.workflow.WorkflowContext)8 File (java.io.File)6 NodeContext (org.knime.core.node.workflow.NodeContext)4 WorkflowManager (org.knime.core.node.workflow.WorkflowManager)4 IOException (java.io.IOException)3 URI (java.net.URI)3 ReferencedFile (org.knime.core.internal.ReferencedFile)3 NodeContainer (org.knime.core.node.workflow.NodeContainer)3 AbstractExplorerFileStore (org.knime.workbench.explorer.filesystem.AbstractExplorerFileStore)3 LocalExplorerFileStore (org.knime.workbench.explorer.filesystem.LocalExplorerFileStore)3 NodeFactory (org.knime.core.node.NodeFactory)2 SubNodeContainer (org.knime.core.node.workflow.SubNodeContainer)2 NodeContainerEditPart (org.knime.workbench.editor2.editparts.NodeContainerEditPart)2 AbstractContentProvider (org.knime.workbench.explorer.view.AbstractContentProvider)2 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1 List (java.util.List)1