use of com.vaadin.flow.internal.StateNode in project flow by vaadin.
the class MapSyncRpcHandlerTest method syncJSON_jsonIsForStateNodeInList_propertySetToStateNodeCopy.
@Test
public void syncJSON_jsonIsForStateNodeInList_propertySetToStateNodeCopy() throws Exception {
// Let's use element's ElementPropertyMap for testing.
TestComponent component = new TestComponent();
Element element = component.getElement();
UI ui = new UI();
ui.add(component);
StateNode node = element.getNode();
// Set model value directly via ElementPropertyMap
ElementPropertyMap propertyMap = node.getFeature(ElementPropertyMap.class);
propertyMap.setUpdateFromClientFilter(name -> true);
ModelList modelList = propertyMap.resolveModelList("foo");
// fake StateNode has been created for the model
StateNode item = new StateNode(ElementPropertyMap.class);
modelList.add(item);
item.getFeature(ElementPropertyMap.class).setProperty("bar", "baz");
// Use the model node id for JSON object which represents a value to
// update
JsonObject json = Json.createObject();
json.put("nodeId", item.getId());
// send sync request
sendSynchronizePropertyEvent(element, ui, TEST_PROPERTY, json);
// Now the model node should be copied and available as the
// TEST_PROPERTY value
Serializable testPropertyValue = propertyMap.getProperty(TEST_PROPERTY);
Assert.assertTrue(testPropertyValue instanceof StateNode);
StateNode newNode = (StateNode) testPropertyValue;
Assert.assertNotEquals(item.getId(), newNode.getId());
Assert.assertEquals("baz", newNode.getFeature(ElementPropertyMap.class).getProperty("bar"));
}
use of com.vaadin.flow.internal.StateNode in project flow by vaadin.
the class MapSyncRpcHandlerTest method syncJSON_jsonIsNotListItemAndNotPropertyValue_propertySetToJSON.
@Test
public void syncJSON_jsonIsNotListItemAndNotPropertyValue_propertySetToJSON() throws Exception {
// Let's use element's ElementPropertyMap for testing.
TestComponent component = new TestComponent();
Element element = component.getElement();
UI ui = new UI();
ui.add(component);
StateNode node = element.getNode();
TestComponent anotherComonent = new TestComponent();
StateNode anotherNode = anotherComonent.getElement().getNode();
ElementPropertyMap.getModel(node).setUpdateFromClientFilter(name -> true);
// Use the model node id for JSON object which represents a value to
// update
JsonObject json = Json.createObject();
json.put("nodeId", anotherNode.getId());
// send sync request
sendSynchronizePropertyEvent(element, ui, "foo", json);
Serializable testPropertyValue = node.getFeature(ElementPropertyMap.class).getProperty("foo");
Assert.assertNotSame(anotherNode, testPropertyValue);
Assert.assertTrue(testPropertyValue instanceof JsonValue);
}
use of com.vaadin.flow.internal.StateNode in project flow by vaadin.
the class ElementPropertyMap method getOrCreateModelList.
/**
* Gets a model list using the given key.
* <p>
* If the key is not mapped to a value, creates a model list for the key.
*
* @param key
* the key to use for the lookup
* @return a model list attached to the given key, possibly created in this
* method
*/
private ModelList getOrCreateModelList(String key) {
Serializable value = getProperty(key);
if (value == null) {
value = new StateNode(Collections.singletonList(ModelList.class));
setProperty(key, value);
}
assert value instanceof StateNode;
assert ((StateNode) value).hasFeature(ModelList.class);
return ((StateNode) value).getFeature(ModelList.class);
}
use of com.vaadin.flow.internal.StateNode in project flow by vaadin.
the class ElementPropertyMap method isUpdateFromClientAllowedByFilter.
private AllowUpdate isUpdateFromClientAllowedByFilter(StateNode node, String key, boolean log) {
if (node.hasFeature(ElementPropertyMap.class)) {
ElementPropertyMap propertyMap = node.getFeature(ElementPropertyMap.class);
if (propertyMap.updateFromClientFilter != null) {
// // TODO to be removed with polymer template support removal
boolean allow = propertyMap.updateFromClientFilter.test(key);
if (!allow && log) {
getLogger().warn("Ignoring model update for {}. " + "For security reasons, the property must have a " + "two-way binding in the template, be annotated" + " with @AllowClientUpdates in the model, or be defined as synchronized.", key);
}
return allow ? AllowUpdate.EXPLICITLY_ALLOW : AllowUpdate.EXPLICITLY_DISALLOW;
}
}
StateNode parent = node.getParent();
if (parent == null) {
return AllowUpdate.NO_EXPLICIT_STATUS;
}
if (parent.hasFeature(ElementPropertyMap.class)) {
ElementPropertyMap parentMap = parent.getFeature(ElementPropertyMap.class);
Optional<String> parentProperty = parentMap.getPropertyNames().filter(property -> node.equals(parentMap.get(property))).findFirst();
if (parentProperty.isPresent()) {
String property = new StringBuilder(parentProperty.get()).append('.').append(key).toString();
return isUpdateFromClientAllowedByFilter(parent, property, log);
}
}
if (parent.hasFeature(ModelList.class)) {
ModelList list = parent.getFeature(ModelList.class);
if (list.contains(node)) {
return isUpdateFromClientAllowedByFilter(parent, key, log);
}
}
return AllowUpdate.NO_EXPLICIT_STATUS;
}
use of com.vaadin.flow.internal.StateNode in project flow by vaadin.
the class ElementPropertyMap method getOrCreateModelMap.
/**
* Gets a model map using the given key.
* <p>
* If the key is not mapped to a value, creates a model map for the key.
*
* @param key
* the key to use for the lookup
* @return a model map attached to the given key, possibly created in this
* method
*/
private ElementPropertyMap getOrCreateModelMap(String key) {
Serializable value = getProperty(key);
if (value == null) {
value = new StateNode(Collections.singletonList(ElementPropertyMap.class));
setProperty(key, value);
}
assert value instanceof StateNode;
assert ((StateNode) value).hasFeature(ElementPropertyMap.class);
return ((StateNode) value).getFeature(ElementPropertyMap.class);
}
Aggregations