/** * Shorter method to create a PropertyBox with given <code>properties</code> set. * @param <P> Actual property type * @param properties Set of properties of the PropertyBox to create * @return A new {@link PropertyBox} builder */ @SafeVarargs static <P extends Property> PropertyBox create(P... properties) { return builder(properties).build(); }
@Override public T convertToPresentation(PropertyBox value, ValueContext context) { if (value != null) { return value.getValue(property); } return null; }
public void propertyBox() { // tag::propertybox[] final PathProperty<Long> ID = PathProperty.create("id", Long.class); final StringProperty NAME = StringProperty.create("name"); final PropertySet<?> PROPERTIES = PropertySet.of(ID, NAME); PropertyBox propertyBox = PropertyBox.create(ID, NAME); // <1> propertyBox = PropertyBox.create(PROPERTIES); // <2> propertyBox.setValue(ID, 1L); // <3> propertyBox.setValue(NAME, "testName"); // <4> propertyBox = PropertyBox.builder(PROPERTIES).set(ID, 1L).set(NAME, "testName").build(); // <5> Long id = propertyBox.getValue(ID); // <6> String name = propertyBox.getValueIfPresent(NAME).orElse("default"); // <7> boolean containsNotNullId = propertyBox.containsValue(ID); // <8> PropertyBox ids = propertyBox.cloneBox(ID); // <9> // end::propertybox[] }
/** * Get the value of given <code>property</code> using current value, if available. * @param <T> Property type * @param property Property for which to obtain the value * @return Property value, empty if current value is not available or the given property is not available in current * value */ private <T> Optional<T> getCurrentPropertyValue(Property<T> property) { if (value != null && value.contains(property)) { return Optional.ofNullable(value.getValue(property)); } return Optional.empty(); }
public void propertyBox3() { // tag::propertybox3[] final StringProperty NAME = StringProperty.create("name"); final StringProperty SURNAME = StringProperty.create("surname"); final VirtualProperty<String> FULL_NAME = VirtualProperty.create(String.class, propertyBox -> { // <1> return propertyBox.getValue(NAME) + " " + propertyBox.getValue(SURNAME); }); PropertyBox propertyBox = PropertyBox.create(NAME, SURNAME, FULL_NAME); // <2> propertyBox.setValue(NAME, "John"); propertyBox.setValue(SURNAME, "Doe"); String fullName = propertyBox.getValue(FULL_NAME); // <3> // end::propertybox3[] }
public void propertyBox5() { // tag::propertybox5[] final NumericProperty<Integer> ID = NumericProperty.integerType("id"); final StringProperty NAME = StringProperty.create("name"); PropertyBox propertyBox = PropertyBox.builder(ID, NAME) .hashCodeProvider(pb -> Optional.ofNullable(pb.getValue(ID))) // <1> .equalsHandler((pb, other) -> (other instanceof PropertyBox) // <2> && ((PropertyBox) other).getValue(ID).equals(pb.getValue(ID))) .build(); // end::propertybox5[] }
/** * Get the value of given <code>property</code> using given <code>propertyBox</code>. * @param <T> Property type * @param propertyBox PropertyBox * @param property Property * @return Property value */ protected <T> T getPropertyValue(PropertyBox propertyBox, Property<T> property) { if (VirtualProperty.class.isAssignableFrom(property.getClass())) { if (((VirtualProperty<T>) property).getValueProvider() != null) { return ((VirtualProperty<T>) property).getValueProvider().getPropertyValue(propertyBox); } return null; } if (propertyBox.containsValue(property)) { return propertyBox.getValue(property); } return null; }
public void propertyBox2() { // tag::propertybox2[] final PathProperty<Long> ID = PathProperty.create("id", Long.class).withValidator(Validator.notNull()); // <1> final StringProperty NAME = StringProperty.create("name").withValidator(Validator.notBlank()); // <2> final PropertySet<?> PROPERTIES = PropertySet.of(ID, NAME); PropertyBox propertyBox = PropertyBox.create(PROPERTIES); propertyBox.setValue(ID, null); // <3> propertyBox = PropertyBox.builder(PROPERTIES).invalidAllowed(true).build(); // <4> propertyBox.validate(); // <5> // end::propertybox2[] }
/** * Clone this PropertyBox using given <code>propertySet</code>, i.e. create a new PropertyBox and copy all given set * property values from this box to the newly created box. * @param <P> Actual property type * @param propertySet Property set of the cloned PropertyBox (not null) * @return A new, cloned, PropertyBox instance with given property set */ @SuppressWarnings("unchecked") default <P extends Property> PropertyBox cloneBox(PropertySet<P> propertySet) { ObjectUtils.argumentNotNull(propertySet, "Property set must be not null"); Builder builder = builder(propertySet).invalidAllowed(true); propertySet.forEach(p -> { if (!p.isReadOnly()) { this.getValueIfPresent(p).ifPresent(v -> builder.set(p, v)); } }); return builder.invalidAllowed(this.isInvalidAllowed()).build(); }
@SuppressWarnings("unchecked") @Override public B dataSource(ItemDataProvider<PropertyBox> dataProvider, final Property... identifierProperties) { ObjectUtils.argumentNotNull(identifierProperties, "Identifier properties must be not null"); if (identifierProperties.length == 0) { throw new IllegalArgumentException("Identifier properties must be not empty"); } final PropertySet<?> propertySet = PropertySet.builderOf(identifierProperties) .identifiers(Arrays.asList(identifierProperties)).build(); return dataSource(dataProvider, item -> { PropertyBox.Builder builder = PropertyBox.builder(propertySet); for (Property p : identifierProperties) { item.getValueIfPresent(p).ifPresent(v -> builder.set(p, v)); } return builder.build(); }); }
@SuppressWarnings("unchecked") @Override @Deprecated public O add(Map<Path<?>, Object> values) { if (values != null && !values.isEmpty()) { final PropertySet<?> propertySet = getDefinition().getPropertySet().orElse(asPropertySet(values.keySet())); final PropertyBox propertyBox = PropertyBox.builder(propertySet).invalidAllowed(true).build(); final PathPropertySetAdapter adapter = PathPropertySetAdapter.create(propertySet); for (Entry<Path<?>, Object> entry : values.entrySet()) { adapter.getProperty(entry.getKey()).ifPresent(p -> { if (entry.getValue() != null) { propertyBox.setValue((Property<Object>) p, entry.getValue()); } }); } getDefinition().addValue(propertyBox); return getActualOperation(); } return getActualOperation(); }
@SuppressWarnings("unchecked") @Override protected Column<PropertyBox> generateDefaultGridColumn( ItemListingColumn<Property<?>, PropertyBox, ?> configuration) { final Property<?> property = configuration.getProperty(); // check component if (Component.class.isAssignableFrom(property.getType())) { final Property<? extends Component> componentProperty = (Property<? extends Component>) property; return getGrid().addComponentColumn(item -> { if (item.contains(property)) { return item.getValue(componentProperty); } return null; }); } // default provider using property presenter return getGrid().addColumn(item -> { if (item.contains(property)) { return item.present(property); } return null; }); }
/** * Clone this PropertyBox, i.e. create a new PropertyBox with same property set of this box and copy all the * property values from this box to the newly created box. * @return A new, cloned, PropertyBox instance */ default PropertyBox cloneBox() { return builder(this).invalidAllowed(this.isInvalidAllowed()).copyValues(this).build(); }
/** * Decode given document into a {@link PropertyBox} using given adapter to match the document field names and * property paths. * @param context Resolution context * @param parent Optional parent field name expression * @param document Document to decode * @param propertyBox PropertyBox to use to collect the decoded values * @param adapter PropertyBox adapter * @throws InvalidExpressionException If an error occurred */ private static void decodeDocument(final MongoDocumentContext<?> context, final String parent, Map<String, Object> document, PropertyBox propertyBox, PathPropertyBoxAdapter adapter) throws InvalidExpressionException { final List<Property<?>> properties = new ArrayList<>(context.getPropertySet().asList()); document.entrySet().stream().forEach(entry -> { decodeDocumentField(context, propertyBox, adapter, parent, entry.getKey(), entry.getValue()) .ifPresent(p -> { properties.remove(p); }); }); // check properties with converter and force value set for (Property<?> property : properties) { if (property.getConverter().isPresent() && propertyBox.contains(property)) { propertyBox.setValue(property, null); } } }
if (oid != null) { boolean idPropertyHasValue = documentContext.getDocumentIdProperty().map(p -> value.containsValue(p)) .orElse(false); if (value.contains(idProperty)) { value.setValue((Property<Object>) idProperty, idPropertyValue);
@Override public Optional<Setter<PropertyBox, V>> getSetter() { if (property.isReadOnly()) { return Optional.empty(); } return Optional.of((pb, value) -> { pb.setValue(property, value); }); }
@Override public ID getItemId(PropertyBox item) { if (item != null) { return item.getValueIfPresent(identifierProperty) .orElseThrow(() -> new DataAccessException("The identifier property [" + identifierProperty + "] is not present in PropertyBox item [" + item + "]")); } return null; }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public PropertyBox read(final PropertyBox propertyBox, final T instance, final boolean ignoreMissing) { ObjectUtils.argumentNotNull(propertyBox, "PropertyBox must be not null"); ObjectUtils.argumentNotNull(instance, "Bean instance must be not null"); propertyBox.stream().filter(p -> !p.isReadOnly()).filter(p -> Path.class.isAssignableFrom(p.getClass())) .map(p -> (Path<?>) p).forEach(p -> { getProperty(p, ignoreMissing).ifPresent( bp -> propertyBox.setValue((Property) p, read(bp, instance, (Class<Object>) p.getType()))); }); return propertyBox; }
final boolean wasInvalidAllowed = propertyBox.isInvalidAllowed(); propertyBox.setInvalidAllowed(true); propertyBox.setValue(b.getProperty(), b.getElement().getValue()); propertyBox.setInvalidAllowed(false);
@Override public <T> boolean containsValue(Path<T> path) { return getProperty(path).map(p -> getPropertyBox().containsValue(p)).orElse(false); }