Search in sources :

Example 1 with WebViewContent

use of org.knime.core.node.web.WebViewContent 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 2 with WebViewContent

use of org.knime.core.node.web.WebViewContent 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 3 with WebViewContent

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

the class SinglePageManager method createWizardPageViewValueMap.

/**
 * Creates a map of node id string to JSON view value string for all appropriate wizard nodes from a given node id.
 *
 * @param containerNodeID the node id to create the view value map for
 * @return a map containing all appropriate view values
 * @throws IOException on serialization error
 */
public Map<String, String> createWizardPageViewValueMap(final NodeID containerNodeID) throws IOException {
    SinglePageWebResourceController sec = getController(containerNodeID);
    Map<NodeIDSuffix, WebViewContent> viewMap = sec.getWizardPageViewValueMap();
    Map<String, String> resultMap = new HashMap<String, String>();
    for (Entry<NodeIDSuffix, WebViewContent> entry : viewMap.entrySet()) {
        WebViewContent c = entry.getValue();
        resultMap.put(entry.getKey().toString(), new String(((ByteArrayOutputStream) c.saveToStream()).toByteArray()));
    }
    return resultMap;
}
Also used : WebViewContent(org.knime.core.node.web.WebViewContent) NodeIDSuffix(org.knime.core.node.workflow.NodeID.NodeIDSuffix) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SinglePageWebResourceController(org.knime.core.node.workflow.SinglePageWebResourceController)

Aggregations

HashMap (java.util.HashMap)3 WebViewContent (org.knime.core.node.web.WebViewContent)3 NodeIDSuffix (org.knime.core.node.workflow.NodeID.NodeIDSuffix)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 ValidationError (org.knime.core.node.web.ValidationError)2 WizardNode (org.knime.core.node.wizard.WizardNode)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 SinglePageWebResourceController (org.knime.core.node.workflow.SinglePageWebResourceController)1