Search in sources :

Example 6 with ValidationError

use of org.knime.core.node.web.ValidationError in project knime-core by knime.

the class WizardPageManager method applyViewValuesToCurrentPage.

/**
 * Applies a given map of view values to the current subnode in wizard execution.
 *
 * @param valueMap a map with {@link NodeIDSuffix} string as key and parsed view value as value
 * @return A JSON-serialized string containing the validation result, null if validation succeeded.
 * @throws IOException on JSON serialization errors
 */
public String applyViewValuesToCurrentPage(final Map<String, String> valueMap) throws IOException {
    try (WorkflowLock lock = getWorkflowManager().lock()) {
        Map<String, String> viewContentMap = validateValueMap(valueMap);
        Map<String, ValidationError> validationResults = null;
        if (!valueMap.isEmpty()) {
            WizardExecutionController wec = getWizardExecutionController();
            validationResults = wec.loadValuesIntoCurrentPage(viewContentMap);
        }
        return serializeValidationResult(validationResults);
    }
}
Also used : WorkflowLock(org.knime.core.node.workflow.WorkflowLock) WizardExecutionController(org.knime.core.node.workflow.WizardExecutionController) ValidationError(org.knime.core.node.web.ValidationError)

Example 7 with ValidationError

use of org.knime.core.node.web.ValidationError 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 Null or empty map if validation succeeds, map of errors otherwise
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Map<String, ValidationError> validateViewValuesInternal(final Map<String, String> viewValues, final NodeID subnodeID, final Map<NodeID, WizardNode> wizardNodeSet) {
    if (subnodeID == null) {
        LOGGER.error("No node ID supplied for validating view values of wizard page");
        return null;
    }
    WorkflowManager manager = m_manager;
    assert manager.isLockedByCurrentThread();
    Map<String, ValidationError> resultMap = new LinkedHashMap<String, ValidationError>();
    for (Map.Entry<String, String> entry : viewValues.entrySet()) {
        NodeID.NodeIDSuffix suffix = NodeID.NodeIDSuffix.fromString(entry.getKey());
        NodeID id = suffix.prependParent(manager.getID());
        CheckUtils.checkState(id.hasPrefix(subnodeID), "The wizard page content for ID %s (suffix %s) " + "does not belong to the current Wrapped Metanode (ID %s)", id, entry.getKey(), subnodeID);
        WizardNode wizardNode = wizardNodeSet.get(id);
        CheckUtils.checkState(wizardNode != null, "No wizard node with ID %s in Wrapped Metanode, valid IDs are: " + "%s", id, ConvenienceMethods.getShortStringFrom(wizardNodeSet.entrySet(), 10));
        @SuppressWarnings("null") WebViewContent newViewValue = wizardNode.createEmptyViewValue();
        if (newViewValue == null) {
            // node has no view value
            continue;
        }
        ValidationError validationError = null;
        try {
            newViewValue.loadFromStream(new ByteArrayInputStream(entry.getValue().getBytes(Charset.forName("UTF-8"))));
            validationError = wizardNode.validateViewValue(newViewValue);
        } catch (Exception e) {
            resultMap.put(entry.getKey(), new ValidationError("Could not deserialize JSON value: " + entry.getValue() + ": \n" + e.getMessage()));
        }
        if (validationError != null) {
            resultMap.put(entry.getKey(), validationError);
        }
    }
    if (!resultMap.isEmpty()) {
        return resultMap;
    }
    return Collections.emptyMap();
}
Also used : WebViewContent(org.knime.core.node.web.WebViewContent) WizardNode(org.knime.core.node.wizard.WizardNode) NodeIDSuffix(org.knime.core.node.workflow.NodeID.NodeIDSuffix) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ValidationError(org.knime.core.node.web.ValidationError) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with ValidationError

use of org.knime.core.node.web.ValidationError in project knime-core by knime.

the class WebResourceController method loadValuesIntoPageInternal.

/**
 * Tries to load a map of view values to all appropriate views contained in a given subnode.
 *
 * @param viewContentMap the values to load
 * @param subnodeID the id fo the subnode containing the appropriate view nodes
 * @param validate true, if validation is supposed to be done before applying the values, false otherwise
 * @param useAsDefault true, if the given value map is supposed to be applied as new node defaults (overwrite node
 *            settings), false otherwise (apply temporarily)
 * @return Null or empty map if validation succeeds, map of errors otherwise
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Map<String, ValidationError> loadValuesIntoPageInternal(final Map<String, String> viewContentMap, final NodeID subnodeID, final boolean validate, final boolean useAsDefault) {
    if (subnodeID == null) {
        LOGGER.error("No node ID supplied for loading values into wizard page");
        return null;
    }
    WorkflowManager manager = m_manager;
    assert manager.isLockedByCurrentThread();
    LOGGER.debugWithFormat("Loading view content into wizard nodes (%d)", viewContentMap.size());
    SubNodeContainer subNodeNC = manager.getNodeContainer(subnodeID, SubNodeContainer.class, true);
    Map<NodeID, WizardNode> wizardNodeSet = getWizardNodeSetForVerifiedID(subnodeID);
    if (validate) {
        Map<String, ValidationError> validationResult = validateViewValuesInternal(viewContentMap, subnodeID, wizardNodeSet);
        if (!validationResult.isEmpty()) {
            return validationResult;
        }
    }
    // validation succeeded, reset subnode and apply
    if (!subNodeNC.getInternalState().isExecuted()) {
        // this used to be an error but see SRV-745
        LOGGER.warnWithFormat("Wrapped metanode (%s) not fully executed on appyling new values -- " + "consider to change wrapped metanode layout to have self-contained executable units", subNodeNC.getNameWithID());
    }
    manager.resetSubnodeForViewUpdate(subnodeID, this);
    for (Map.Entry<String, String> entry : viewContentMap.entrySet()) {
        NodeID.NodeIDSuffix suffix = NodeID.NodeIDSuffix.fromString(entry.getKey());
        NodeID id = suffix.prependParent(manager.getID());
        WizardNode wizardNode = wizardNodeSet.get(id);
        WebViewContent newViewValue = wizardNode.createEmptyViewValue();
        if (newViewValue == null) {
            // node has no view value
            continue;
        }
        try {
            newViewValue.loadFromStream(new ByteArrayInputStream(entry.getValue().getBytes(Charset.forName("UTF-8"))));
            wizardNode.loadViewValue(newViewValue, useAsDefault);
            if (useAsDefault) {
                subNodeNC.getWorkflowManager().getNodeContainer(id, SingleNodeContainer.class, true).saveNodeSettingsToDefault();
            }
        } catch (Exception e) {
            LOGGER.error("Failed to load view value into node \"" + id + "\" although validation succeeded", e);
        }
    }
    manager.configureNodeAndSuccessors(subnodeID, true);
    return Collections.emptyMap();
}
Also used : WebViewContent(org.knime.core.node.web.WebViewContent) WizardNode(org.knime.core.node.wizard.WizardNode) NodeIDSuffix(org.knime.core.node.workflow.NodeID.NodeIDSuffix) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) ValidationError(org.knime.core.node.web.ValidationError) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with ValidationError

use of org.knime.core.node.web.ValidationError in project knime-core by knime.

the class WebResourceController method validateViewValueForWizardNode.

private static ValidationError validateViewValueForWizardNode(final WizardNode wizardNode, final String viewValue) throws IOException {
    WebViewContent newViewValue = wizardNode.createEmptyViewValue();
    if (newViewValue == null) {
        // node has no view value
        return null;
    }
    ValidationError validationError = null;
    newViewValue.loadFromStream(new ByteArrayInputStream(viewValue.getBytes(StandardCharsets.UTF_8)));
    validationError = wizardNode.validateViewValue(newViewValue);
    return validationError;
}
Also used : WebViewContent(org.knime.core.node.web.WebViewContent) ByteArrayInputStream(java.io.ByteArrayInputStream) ValidationError(org.knime.core.node.web.ValidationError)

Aggregations

ValidationError (org.knime.core.node.web.ValidationError)9 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Test (org.junit.Test)3 WebViewContent (org.knime.core.node.web.WebViewContent)3 WizardNode (org.knime.core.node.wizard.WizardNode)3 NodeIDSuffix (org.knime.core.node.workflow.NodeID.NodeIDSuffix)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 CollectionValidationError (org.knime.core.wizard.SubnodeViewableModel.CollectionValidationError)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)1 NodeModel (org.knime.core.node.NodeModel)1 DefaultReexecutionCallback (org.knime.core.node.interactive.DefaultReexecutionCallback)1 ViewRequestHandlingException (org.knime.core.node.interactive.ViewRequestHandlingException)1 WizardPage (org.knime.core.node.wizard.page.WizardPage)1 WizardPageContribution (org.knime.core.node.wizard.page.WizardPageContribution)1 WizardHold (org.knime.core.node.workflow.TestWizardExec_Loop_Simple.WizardHold)1