Search in sources :

Example 1 with WizardPageContribution

use of org.knime.core.node.wizard.page.WizardPageContribution in project knime-core by knime.

the class WebResourceController method validateViewValuesInternal.

/**
 * Validates a given set of serialized view values for a given subnode.
 *
 * @param viewValues the values to validate
 * @param subnodeID the id of the subnode containing the appropriate view nodes
 * @param wizardNodeSet the set of view nodes that the view values correspond to.
 * @return an empty map if validation succeeds, map of errors otherwise
 * @throws IllegalArgumentException if the provided subnode id is <code>null</code>
 * @throws IllegalStateException if there are no nodes with the provided id prefixes in the page or the provided
 *             wizard-node-set doesn't contain the required wizard nodes
 */
@SuppressWarnings({ "rawtypes" })
protected Map<String, ValidationError> validateViewValuesInternal(final Map<String, String> viewValues, final NodeID subnodeID, final Map<NodeID, NativeNodeContainer> wizardNodeSet) {
    if (subnodeID == null) {
        throw new IllegalArgumentException("No node ID supplied for validating view values of wizard page");
    }
    WorkflowManager manager = m_manager;
    assert manager.isLockedByCurrentThread();
    Map<String, ValidationError> resultMap = new LinkedHashMap<>();
    for (Map.Entry<String, String> entry : viewValues.entrySet()) {
        NodeID.NodeIDSuffix suffix = NodeID.NodeIDSuffix.fromString(entry.getKey());
        NodeID id = suffix.prependParent(manager.getProjectWFM().getID());
        CheckUtils.checkState(id.hasPrefix(subnodeID), "The wizard page content for ID %s (suffix %s) does not belong to the current Component (ID %s)", id, entry.getKey(), subnodeID);
        NativeNodeContainer wizardNode = wizardNodeSet.get(id);
        CheckUtils.checkState(wizardNode != null, "No wizard node with ID %s in Component, valid IDs are: " + "%s", id, ConvenienceMethods.getShortStringFrom(wizardNodeSet.entrySet(), 10));
        ValidationError validationError = null;
        try {
            if (wizardNode.getNodeModel() instanceof WizardNode) {
                validationError = validateViewValueForWizardNode((WizardNode) wizardNode.getNodeModel(), entry.getValue());
            } else if (wizardNode.getNode().getFactory() instanceof WizardPageContribution) {
                validationError = ((WizardPageContribution) wizardNode.getNode().getFactory()).validateViewValue(wizardNode, entry.getValue()).map(ValidationError::new).orElse(null);
            }
        } catch (Exception e) {
            // NOSONAR
            validationError = new ValidationError("An unexpected error occurred while validating the view value: " + entry.getValue() + ": \n" + e.getMessage());
        }
        if (validationError != null) {
            resultMap.put(entry.getKey(), validationError);
        }
    }
    if (!resultMap.isEmpty()) {
        return resultMap;
    }
    return Collections.emptyMap();
}
Also used : WizardNode(org.knime.core.node.wizard.WizardNode) NodeIDSuffix(org.knime.core.node.workflow.NodeID.NodeIDSuffix) WizardPageContribution(org.knime.core.node.wizard.page.WizardPageContribution) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) ViewRequestHandlingException(org.knime.core.node.interactive.ViewRequestHandlingException) LinkedHashMap(java.util.LinkedHashMap) ValidationError(org.knime.core.node.web.ValidationError) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with WizardPageContribution

use of org.knime.core.node.wizard.page.WizardPageContribution in project knime-core by knime.

the class WebResourceController method loadViewValues.

@SuppressWarnings("rawtypes")
private static void loadViewValues(final Map<String, String> filteredViewContentMap, final Map<NodeID, NativeNodeContainer> wizardNodeSet, final WorkflowManager manager, final boolean useAsDefault) {
    for (Map.Entry<String, String> entry : filteredViewContentMap.entrySet()) {
        NodeID.NodeIDSuffix suffix = NodeID.NodeIDSuffix.fromString(entry.getKey());
        NodeID id = suffix.prependParent(manager.getProjectWFM().getID());
        NativeNodeContainer wizardNode = wizardNodeSet.get(id);
        try {
            if (wizardNode.getNodeModel() instanceof WizardNode) {
                loadViewValueForWizardNode(wizardNode, (WizardNode) wizardNode.getNodeModel(), entry.getValue(), useAsDefault);
            } else if (wizardNode.getNode().getFactory() instanceof WizardPageContribution) {
                ((WizardPageContribution) wizardNode.getNode().getFactory()).loadViewValue(wizardNode, entry.getValue());
            } else {
                throw new IllegalStateException("The node '" + wizardNode.getNameWithID() + "' doesn't contribute to a wizard page. View values can't be loaded.");
            }
        } catch (Exception e) {
            LOGGER.error("Failed to load view value into node \"" + wizardNode.getID() + "\" although validation succeeded", e);
        }
    }
}
Also used : WizardNode(org.knime.core.node.wizard.WizardNode) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) NodeIDSuffix(org.knime.core.node.workflow.NodeID.NodeIDSuffix) WizardPageContribution(org.knime.core.node.wizard.page.WizardPageContribution) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) ViewRequestHandlingException(org.knime.core.node.interactive.ViewRequestHandlingException)

Aggregations

IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)2 ViewRequestHandlingException (org.knime.core.node.interactive.ViewRequestHandlingException)2 WizardNode (org.knime.core.node.wizard.WizardNode)2 WizardPageContribution (org.knime.core.node.wizard.page.WizardPageContribution)2 NodeIDSuffix (org.knime.core.node.workflow.NodeID.NodeIDSuffix)2 ValidationError (org.knime.core.node.web.ValidationError)1