Search in sources :

Example 6 with LoadVersion

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

the class WorkflowLoadHelper method createTemplateLoadPersistor.

/**
 * Create persistor for a workflow or template.
 * @noreference Clients should only be required to load projects using
 * {@link WorkflowManager#loadProject(File, ExecutionMonitor, WorkflowLoadHelper)}
 * @param directory The directory to load from
 * @param templateSourceURI URI of the link to the template (will load a template and when the template is
 * instantiated and put into the workflow a link is created)
 * @return The persistor
 * @throws IOException If an IO error occured
 * @throws UnsupportedWorkflowVersionException If the workflow is of an unsupported version
 */
public final TemplateNodeContainerPersistor createTemplateLoadPersistor(final File directory, final URI templateSourceURI) throws IOException, UnsupportedWorkflowVersionException {
    if (directory == null) {
        throw new NullPointerException("Arguments must not be null.");
    }
    String fileName = getDotKNIMEFileName();
    if (!directory.isDirectory() || !directory.canRead()) {
        throw new IOException("Can't read metanode/template directory " + directory);
    }
    // template.knime or workflow.knime
    ReferencedFile dotKNIMERef = new ReferencedFile(new ReferencedFile(directory), fileName);
    File dotKNIME = dotKNIMERef.getFile();
    if (!dotKNIME.isFile()) {
        throw new IOException("No \"" + fileName + "\" file in directory \"" + directory.getAbsolutePath() + "\"");
    }
    NodeSettingsRO settings = NodeSettings.loadFromXML(new BufferedInputStream(new FileInputStream(dotKNIME)));
    // CeBIT 2006 version did not contain a version string.
    String versionString;
    if (settings.containsKey(WorkflowLoadHelper.CFG_VERSION)) {
        try {
            versionString = settings.getString(WorkflowLoadHelper.CFG_VERSION);
        } catch (InvalidSettingsException e) {
            throw new IOException("Can't read version number from \"" + dotKNIME.getAbsolutePath() + "\"", e);
        }
    } else {
        versionString = "0.9.0";
    }
    // might also be FUTURE
    LoadVersion version = FileWorkflowPersistor.parseVersion(versionString);
    boolean isSetDirtyAfterLoad = false;
    StringBuilder versionDetails = new StringBuilder(versionString);
    String createdBy = settings.getString(WorkflowLoadHelper.CFG_CREATED_BY, null);
    Version createdByVersion = null;
    if (createdBy != null) {
        versionDetails.append(" (created by KNIME ").append(createdBy).append(")");
        try {
            createdByVersion = new Version(createdBy);
        } catch (IllegalArgumentException e) {
            // ideally this goes into the 'LoadResult' but it's not instantiated yet
            LOGGER.warn(String.format("Unable to parse version string \"%s\" (file \"%s\"): %s", createdBy, dotKNIME.getAbsolutePath(), e.getMessage()), e);
        }
    }
    // added 3.5.0
    boolean isNightly = settings.getBoolean(CFG_NIGHTLY, false);
    boolean isRunningNightly = KNIMEConstants.isNightlyBuild();
    boolean isFutureWorkflow = createdByVersion != null && !new Version(KNIMEConstants.VERSION).isSameOrNewer(createdByVersion);
    if (version == LoadVersion.FUTURE || isFutureWorkflow || (!isRunningNightly && isNightly)) {
        switch(getUnknownKNIMEVersionLoadPolicy(version, createdByVersion, isNightly)) {
            case Abort:
                StringBuilder e = new StringBuilder("Unable to load ");
                e.append(isTemplateFlow() ? "template, " : "workflow, ");
                if (version == LoadVersion.FUTURE || isFutureWorkflow) {
                    e.append("it was created with a future version of KNIME (").append(createdBy).append("). ");
                    e.append("You are running ").append(KNIMEConstants.VERSION).append(".");
                } else {
                    e.append("it was created with a nightly build of KNIME (version ").append(createdBy).append("). ");
                    e.append("You are running ").append(KNIMEConstants.VERSION).append(".");
                }
                throw new UnsupportedWorkflowVersionException(e.toString());
            default:
                isSetDirtyAfterLoad = true;
        }
    } else if (version.isOlderThan(LoadVersion.V200)) {
        LOGGER.warn("The current KNIME version (" + KNIMEConstants.VERSION + ") is different from the one that " + "created the workflow (" + version + ") you are trying to load. In some rare cases, it  " + "might not be possible to load all data or some nodes can't be configured. " + "Please re-configure and/or re-execute these nodes.");
    }
    final MetaNodeTemplateInformation templateInfo;
    if (isTemplateFlow() && templateSourceURI != null) {
        try {
            templateInfo = MetaNodeTemplateInformation.load(settings, version);
            CheckUtils.checkSetting(Role.Template.equals(templateInfo.getRole()), "Role is not '%s' but '%s'", Role.Template, templateInfo.getRole());
        } catch (InvalidSettingsException e) {
            throw new IOException(String.format("Attempting to load template from \"%s\" but can't locate template information: %s", dotKNIME.getAbsolutePath(), e.getMessage()), e);
        }
    } else if (isTemplateFlow()) {
        // LOGGER.coding("Supposed to instantiate a template but the link URI is not set");
        // metanode template from node repository
        templateInfo = null;
    } else {
        templateInfo = null;
    }
    final TemplateNodeContainerPersistor persistor;
    // TODO only create new hash map if workflow is a project?
    HashMap<Integer, ContainerTable> tableRep = new GlobalTableRepository();
    WorkflowFileStoreHandlerRepository fileStoreHandlerRepository = new WorkflowFileStoreHandlerRepository();
    // ordinary workflow is loaded
    if (templateInfo == null) {
        persistor = new FileWorkflowPersistor(tableRep, fileStoreHandlerRepository, dotKNIMERef, this, version, !isTemplateFlow());
    } else {
        // some template is loaded
        switch(templateInfo.getNodeContainerTemplateType()) {
            case MetaNode:
                final ReferencedFile workflowDotKNIME;
                if (version.isOlderThan(LoadVersion.V2100)) {
                    // before 2.10 everything was stored in template.knime
                    workflowDotKNIME = dotKNIMERef;
                } else {
                    workflowDotKNIME = new ReferencedFile(dotKNIMERef.getParent(), WorkflowPersistor.WORKFLOW_FILE);
                }
                persistor = new FileWorkflowPersistor(tableRep, fileStoreHandlerRepository, workflowDotKNIME, this, version, !isTemplateFlow());
                break;
            case SubNode:
                final ReferencedFile settingsDotXML = new ReferencedFile(dotKNIMERef.getParent(), SingleNodeContainerPersistor.SETTINGS_FILE_NAME);
                persistor = new FileSubNodeContainerPersistor(settingsDotXML, this, version, tableRep, fileStoreHandlerRepository, true);
                break;
            default:
                throw new IllegalStateException("Unsupported template type");
        }
    }
    if (templateInfo != null) {
        persistor.setOverwriteTemplateInformation(templateInfo.createLink(templateSourceURI));
        persistor.setNameOverwrite(directory.getName());
    }
    if (isSetDirtyAfterLoad) {
        persistor.setDirtyAfterLoad();
    }
    return persistor;
}
Also used : WorkflowFileStoreHandlerRepository(org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository) ReferencedFile(org.knime.core.internal.ReferencedFile) ContainerTable(org.knime.core.data.container.ContainerTable) BufferedInputStream(java.io.BufferedInputStream) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) Version(org.knime.core.util.Version) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Aggregations

LoadVersion (org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion)6 File (java.io.File)4 ReferencedFile (org.knime.core.internal.ReferencedFile)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 Version (org.knime.core.util.Version)3 IOException (java.io.IOException)2 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)2 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)2 WorkflowLoadResult (org.knime.core.node.workflow.WorkflowPersistor.WorkflowLoadResult)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 AssertionFailedError (junit.framework.AssertionFailedError)1 Test (org.junit.Test)1 ContainerTable (org.knime.core.data.container.ContainerTable)1 WorkflowFileStoreHandlerRepository (org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository)1 BufferedDataTable (org.knime.core.node.BufferedDataTable)1 PortObject (org.knime.core.node.port.PortObject)1