Search in sources :

Example 61 with Value

use of com.enonic.xp.data.Value in project xp by enonic.

the class FindNodesByQueryCommandTest method query_instant_different_field_name_case.

@Test
public void query_instant_different_field_name_case() throws Exception {
    final Instant BASE_INSTANT = Instant.parse("2000-01-01T00:00:00.00Z");
    final PropertyTree data13 = new PropertyTree();
    data13.addInstant("myProperty", BASE_INSTANT.plus(10, ChronoUnit.DAYS));
    final PropertyTree data2 = new PropertyTree();
    data2.addInstant("myProperty", BASE_INSTANT.plus(20, ChronoUnit.DAYS));
    final Node node1 = createNode(CreateNodeParams.create().name("my-node-1").parent(NodePath.ROOT).data(data13).build());
    final Node node2 = createNode(CreateNodeParams.create().name("my-node-2").parent(NodePath.ROOT).data(data2).build());
    final Node node3 = createNode(CreateNodeParams.create().name("child-node").parent(node1.path()).data(data13).build());
    final Value from = ValueFactory.newDateTime(BASE_INSTANT);
    final Value to = ValueFactory.newDateTime(BASE_INSTANT.plus(15, ChronoUnit.DAYS));
    final QueryExpr queryExpr = QueryExpr.from(CompareExpr.eq(FieldExpr.from("MYProperty"), ValueExpr.instant(BASE_INSTANT.plus(10, ChronoUnit.DAYS).toString())));
    final NodeQuery query = NodeQuery.create().query(queryExpr).addPostFilter(RangeFilter.create().fieldName("MYProperty").from(from).to(to).build()).build();
    final FindNodesByQueryResult result = doFindByQuery(query);
    assertEquals(2, result.getNodeIds().getSize());
    assertTrue(result.getNodeIds().contains(node1.id()));
    assertTrue(result.getNodeIds().contains(node3.id()));
    assertFalse(result.getNodeIds().contains(node2.id()));
}
Also used : QueryExpr(com.enonic.xp.query.expr.QueryExpr) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) Instant(java.time.Instant) PropertyTree(com.enonic.xp.data.PropertyTree) Node(com.enonic.xp.node.Node) NodeQuery(com.enonic.xp.node.NodeQuery) Value(com.enonic.xp.data.Value) Test(org.junit.jupiter.api.Test)

Example 62 with Value

use of com.enonic.xp.data.Value in project xp by enonic.

the class FormDefaultValuesProcessorImpl method processFormItems.

private void processFormItems(final Iterable<FormItem> formItems, final PropertySet dataSet) {
    StreamSupport.stream(formItems.spliterator(), false).forEach(formItem -> {
        if (formItem.getType() == INPUT) {
            Input input = formItem.toInput();
            if (input.getDefaultValue() != null) {
                try {
                    final Value defaultValue = InputTypes.BUILTIN.resolve(input.getInputType()).createDefaultValue(input);
                    final PropertyPath propertyPath = PropertyPath.from(input.getName());
                    if (defaultValue != null && dataSet.getProperty(propertyPath) == null) {
                        if (input.getOccurrences().getMinimum() > 0) {
                            for (int i = 0; i < input.getOccurrences().getMinimum(); i++) {
                                dataSet.setProperty(input.getName(), i, defaultValue);
                            }
                        } else {
                            dataSet.setProperty(input.getName(), defaultValue);
                        }
                    }
                } catch (IllegalArgumentException ex) {
                    LOG.warn("Invalid default value for " + input.getInputType() + " input type with name '" + input.getName() + "': '" + input.getDefaultValue().getRootValue() + "'" + (ex.getMessage() == null ? "" : " - " + ex.getMessage()));
                }
            }
        } else if (formItem.getType() == FORM_ITEM_SET) {
            processFormItems(formItem.getName(), formItem.toFormItemSet().getFormItems(), dataSet, formItem.toFormItemSet().getOccurrences().getMinimum());
        } else if (formItem.getType() == LAYOUT && formItem.toLayout() instanceof FieldSet) {
            processFormItems((FieldSet) formItem.toLayout(), dataSet);
        } else if (formItem.getType() == FORM_OPTION_SET_OPTION) {
            FormOptionSetOption option = formItem.toFormOptionSetOption();
            if (option.isDefaultOption()) {
                dataSet.setProperty("_selected", ValueFactory.newString(formItem.getName()));
                if (dataSet.getProperty(formItem.getName()) == null) {
                    Property property = dataSet.setProperty(formItem.getName(), ValueFactory.newPropertySet(new PropertySet()));
                    processFormItems(option.getFormItems(), property.getSet());
                }
            }
        } else if (formItem.getType() == FORM_OPTION_SET) {
            processFormItems(formItem.getName(), formItem.toFormOptionSet().getFormItems(), dataSet, formItem.toFormOptionSet().getOccurrences().getMinimum());
        }
    });
}
Also used : Input(com.enonic.xp.form.Input) FieldSet(com.enonic.xp.form.FieldSet) Value(com.enonic.xp.data.Value) PropertyPath(com.enonic.xp.data.PropertyPath) PropertySet(com.enonic.xp.data.PropertySet) FormOptionSetOption(com.enonic.xp.form.FormOptionSetOption) Property(com.enonic.xp.data.Property)

Aggregations

Value (com.enonic.xp.data.Value)62 Test (org.junit.jupiter.api.Test)48 Input (com.enonic.xp.form.Input)24 Property (com.enonic.xp.data.Property)4 PropertySet (com.enonic.xp.data.PropertySet)4 PropertyTree (com.enonic.xp.data.PropertyTree)3 InputType (com.enonic.xp.inputtype.InputType)2 Instant (java.time.Instant)2 PropertyPath (com.enonic.xp.data.PropertyPath)1 FieldSet (com.enonic.xp.form.FieldSet)1 FormOptionSetOption (com.enonic.xp.form.FormOptionSetOption)1 IndexConfig (com.enonic.xp.index.IndexConfig)1 IndexValueProcessor (com.enonic.xp.index.IndexValueProcessor)1 AttachedBinary (com.enonic.xp.node.AttachedBinary)1 FindNodesByQueryResult (com.enonic.xp.node.FindNodesByQueryResult)1 Node (com.enonic.xp.node.Node)1 NodePath (com.enonic.xp.node.NodePath)1 NodeQuery (com.enonic.xp.node.NodeQuery)1 NodeVersion (com.enonic.xp.node.NodeVersion)1 NodeVersionQuery (com.enonic.xp.node.NodeVersionQuery)1