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;
}
}
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();
}
}
}
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 + '"');
}
Aggregations