Search in sources :

Example 1 with AuthorInformation

use of org.knime.core.util.workflowalizer.AuthorInformation in project knime-core by knime.

the class FileWorkflowPersistor method loadAuthorInformation.

private AuthorInformation loadAuthorInformation(final NodeSettingsRO settings, final LoadResult loadResult) throws InvalidSettingsException {
    if (getLoadVersion().ordinal() >= LoadVersion.V280.ordinal() && settings.containsKey(CFG_AUTHOR_INFORMATION)) {
        final NodeSettingsRO sub = settings.getNodeSettings(CFG_AUTHOR_INFORMATION);
        final String author = sub.getString("authored-by");
        final String authorDateS = sub.getString("authored-when");
        OffsetDateTime authorDate;
        if (authorDateS == null) {
            authorDate = null;
        } else {
            try {
                authorDate = parseDate(authorDateS);
            } catch (DateTimeParseException e) {
                authorDate = OffsetDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC);
                loadResult.addWarning(String.format("Can't parse authored-when-date \"%s\". Replaced with \"%s\".", authorDateS, authorDate.toString()));
            }
        }
        final String editor = sub.getString("lastEdited-by");
        final String editDateS = sub.getString("lastEdited-when");
        OffsetDateTime editDate;
        if (editDateS == null) {
            editDate = null;
        } else {
            try {
                editDate = parseDate(editDateS);
            } catch (DateTimeParseException e) {
                editDate = OffsetDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC);
                loadResult.addWarning(String.format("Can't parse lastEdit-when-date \"%s\". Replaced with \"%s\".", editDateS, editDate.toString()));
            }
        }
        return new AuthorInformation(author, authorDate, editor, editDate);
    } else {
        return AuthorInformation.UNKNOWN;
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) OffsetDateTime(java.time.OffsetDateTime) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) AuthorInformation(org.knime.core.util.workflowalizer.AuthorInformation)

Example 2 with AuthorInformation

use of org.knime.core.util.workflowalizer.AuthorInformation 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.util.LoadVersion) AuthorInformation(org.knime.core.util.workflowalizer.AuthorInformation) ReferencedFile(org.knime.core.internal.ReferencedFile) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 3 with AuthorInformation

use of org.knime.core.util.workflowalizer.AuthorInformation in project knime-core by knime.

the class ContextProperties method extractContextProperty.

/**
 * Extracts the value of a context property.
 *
 * @param property the name of the context property.
 * @return the non-{@code null}, but possibly empty, value of the context property.
 * @throws IllegalArgumentException of {@code property} is not the name of a context property.
 */
public static String extractContextProperty(final String property) {
    final WorkflowManager manager = NodeContext.getContext().getWorkflowManager();
    if (CONTEXT_PROPERTY_WORKFLOW_NAME.equals(property)) {
        return manager.getName();
    }
    if (CONTEXT_PROPERTY_JOB_ID.equals(property)) {
        final WorkflowContext context = manager.getContext();
        return context == null ? null : context.getJobId().map(UUID::toString).orElse(null);
    }
    if (CONTEXT_PROPERTY_WORKFLOW_PATH.equals(property)) {
        final WorkflowContext context = manager.getContext();
        if (context.getRelativeRemotePath().isPresent()) {
            return context.getRelativeRemotePath().get();
        }
        final File wfLocation = context.getOriginalLocation() == null ? context.getCurrentLocation() : context.getOriginalLocation();
        final File mpLocation = context.getMountpointRoot();
        if (mpLocation == null || wfLocation == null) {
            return "";
        }
        final String wfPath = wfLocation.getAbsolutePath();
        final String mpPath = mpLocation.getAbsolutePath();
        assert wfPath.startsWith(mpPath);
        final String resultPath = wfPath.substring(mpPath.length());
        return resultPath.replace("\\", "/");
    }
    if (CONTEXT_PROPERTY_WORKFLOW_ABSOLUTE_PATH.equals(property)) {
        final WorkflowContext context = manager.getContext();
        final File wfLocation = context.getCurrentLocation();
        if (wfLocation == null) {
            return "";
        }
        return wfLocation.getAbsolutePath().replace("\\", "/");
    }
    if (CONTEXT_PROPERTY_SERVER_USER.equals(property)) {
        return manager.getContext().getUserid();
    }
    if (CONTEXT_PROPERTY_TEMP_LOCATION.equals(property)) {
        return manager.getContext().getTempLocation().getAbsolutePath();
    }
    final AuthorInformation authInfo = manager.getAuthorInformation();
    if (authInfo != null) {
        final String author = Optional.ofNullable(authInfo.getAuthor()).orElse("");
        final String dateCreated = Optional.ofNullable(authInfo.getAuthoredDate()).map(Object::toString).orElse("");
        if (CONTEXT_PROPERTY_AUTHOR.equals(property)) {
            return author;
        }
        if (CONTEXT_PROPERTY_EDITOR.equals(property)) {
            /**
             * If there is no known last editor (e.g., since the workflow has just been created), the original
             * author is returned as last editor.
             */
            return authInfo.getLastEditor().orElse(author);
        }
        if (CONTEXT_PROPERTY_CREATION_DATE.equals(property)) {
            return dateCreated;
        }
        if (CONTEXT_PROPERTY_LAST_MODIFIED.equals(property)) {
            /**
             * If there is no known last edit date (e.g., since the workflow has just been created), the created
             * date is returned as last edit date.
             */
            return authInfo.getLastEditDate().map(Object::toString).orElse(dateCreated);
        }
    } else if (CONTEXT_PROPERTY_AUTHOR.equals(property) || CONTEXT_PROPERTY_EDITOR.equals(property) || CONTEXT_PROPERTY_CREATION_DATE.equals(property) || CONTEXT_PROPERTY_LAST_MODIFIED.equals(property)) {
        return "";
    }
    throw new IllegalArgumentException("Not a context property : \"" + property + '"');
}
Also used : WorkflowManager(org.knime.core.node.workflow.WorkflowManager) WorkflowContext(org.knime.core.node.workflow.WorkflowContext) UUID(java.util.UUID) AuthorInformation(org.knime.core.util.workflowalizer.AuthorInformation) File(java.io.File)

Aggregations

AuthorInformation (org.knime.core.util.workflowalizer.AuthorInformation)3 File (java.io.File)2 IOException (java.io.IOException)1 OffsetDateTime (java.time.OffsetDateTime)1 DateTimeParseException (java.time.format.DateTimeParseException)1 UUID (java.util.UUID)1 ReferencedFile (org.knime.core.internal.ReferencedFile)1 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)1 WorkflowContext (org.knime.core.node.workflow.WorkflowContext)1 WorkflowManager (org.knime.core.node.workflow.WorkflowManager)1 LoadVersion (org.knime.core.util.LoadVersion)1