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