use of io.jmix.ui.component.data.meta.EntityValueSource in project jmix by jmix-framework.
the class ValueBinder method bind.
public <V> ValueBinding<V> bind(HasValue<V> component, ValueSource<V> valueSource) {
if (valueSource instanceof ApplicationContextAware) {
((ApplicationContextAware) valueSource).setApplicationContext(applicationContext);
}
ValueBindingImpl<V> binding = new ValueBindingImpl<>(component, valueSource);
if (component instanceof Component.Editable) {
((Component.Editable) component).setEditable(!valueSource.isReadOnly());
}
if (valueSource instanceof EntityValueSource) {
EntityValueSource entityValueSource = (EntityValueSource) valueSource;
MetaPropertyPath metaPropertyPath = entityValueSource.getMetaPropertyPath();
if (component instanceof Field) {
initRequired((Field) component, metaPropertyPath);
initBeanValidator((Field<?>) component, metaPropertyPath);
}
if (entityValueSource.isDataModelSecurityEnabled()) {
UiEntityAttributeContext attributeContext = new UiEntityAttributeContext(metaPropertyPath);
accessManager.applyRegisteredConstraints(attributeContext);
if (component instanceof Component.Editable) {
MetaClass metaClass = entityValueSource.getEntityMetaClass();
boolean permittedIfEmbedded = true;
if (entityValueSource instanceof ContainerValueSource) {
InstanceContainer<?> container = ((ContainerValueSource<?, ?>) entityValueSource).getContainer();
if (container instanceof Nested && metaClass != null && metadataTools.isJpaEmbeddable(metaClass)) {
String embeddedProperty = ((Nested) container).getProperty();
MetaClass masterMetaClass = ((Nested) container).getMaster().getEntityMetaClass();
UiEntityAttributeContext embeddedAttributeContext = new UiEntityAttributeContext(masterMetaClass, embeddedProperty);
accessManager.applyRegisteredConstraints(embeddedAttributeContext);
permittedIfEmbedded = embeddedAttributeContext.canModify();
}
if (permittedIfEmbedded && metaPropertyPath.length() > 1) {
for (MetaProperty property : metaPropertyPath.getMetaProperties()) {
if (metadataTools.isEmbedded(property)) {
UiEntityAttributeContext childAttributeContext = new UiEntityAttributeContext(property.getDomain(), property.getName());
accessManager.applyRegisteredConstraints(childAttributeContext);
if (!childAttributeContext.canModify()) {
permittedIfEmbedded = false;
break;
}
}
}
}
}
if (!attributeContext.canModify() || !permittedIfEmbedded) {
((Component.Editable) component).setEditable(false);
}
}
if (!attributeContext.canView()) {
component.setVisible(false);
}
}
}
binding.bind();
return binding;
}
use of io.jmix.ui.component.data.meta.EntityValueSource in project jmix by jmix-framework.
the class EditorBuilderProcessor method initEntity.
@Nullable
protected <E> E initEntity(EditorBuilder<E> builder, @Nullable CollectionContainer<E> container) {
E entity;
boolean oneToOneComposition = false;
EntityValueSource entityValueSource = null;
HasValue<E> field = builder.getField();
if (field instanceof HasValueSource) {
ValueSource valueSource = ((HasValueSource) field).getValueSource();
if (valueSource instanceof EntityValueSource) {
entityValueSource = (EntityValueSource) valueSource;
oneToOneComposition = isCompositionProperty(entityValueSource);
}
}
if (builder.getMode() == EditMode.CREATE || (oneToOneComposition && field.getValue() == null)) {
if (builder.getNewEntity() == null) {
entity = metadata.create(builder.getEntityClass());
} else {
entity = builder.getNewEntity();
}
if (container instanceof Nested) {
initializeNestedEntity(entity, (Nested) container);
}
if (oneToOneComposition) {
Object ownerEntity = entityValueSource.getItem();
MetaProperty inverseProp = entityValueSource.getMetaPropertyPath().getMetaProperty().getInverse();
if (inverseProp != null) {
EntityValues.setValue(entity, inverseProp.getName(), ownerEntity);
}
}
if (builder.getInitializer() != null) {
builder.getInitializer().accept(entity);
}
} else {
entity = builder.getEditedEntity();
}
return entity;
}
use of io.jmix.ui.component.data.meta.EntityValueSource in project jmix by jmix-framework.
the class AbstractTextArea method convertToPresentation.
@Override
protected String convertToPresentation(@Nullable V modelValue) throws ConversionException {
if (datatype != null) {
return nullToEmpty(datatype.format(modelValue, locale));
}
if (valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
Datatype<V> propertyDataType = entityValueSource.getMetaPropertyPath().getRange().asDatatype();
return nullToEmpty(propertyDataType.format(modelValue));
}
return nullToEmpty(super.convertToPresentation(modelValue));
}
use of io.jmix.ui.component.data.meta.EntityValueSource in project jmix by jmix-framework.
the class AbstractTextArea method getConversionErrorMessageInternal.
protected String getConversionErrorMessageInternal() {
String customErrorMessage = getConversionErrorMessage();
if (StringUtils.isNotEmpty(customErrorMessage)) {
return customErrorMessage;
}
Datatype<V> datatype = this.datatype;
if (datatype == null && valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
datatype = entityValueSource.getMetaPropertyPath().getRange().asDatatype();
}
if (datatype != null) {
String msg = getDatatypeConversionErrorMsg(datatype);
if (StringUtils.isNotEmpty(msg)) {
return msg;
}
}
return applicationContext.getBean(Messages.class).getMessage("databinding.conversion.error");
}
use of io.jmix.ui.component.data.meta.EntityValueSource in project jmix by jmix-framework.
the class DatePickerImpl method convertToModel.
@Nullable
@SuppressWarnings("unchecked")
@Override
protected V convertToModel(@Nullable LocalDate componentRawValue) throws ConversionException {
if (componentRawValue == null) {
return null;
}
LocalDateTime localDateTime = LocalDateTime.of(componentRawValue, LocalTime.MIDNIGHT);
ValueSource<V> valueSource = getValueSource();
if (valueSource instanceof EntityValueSource) {
MetaProperty metaProperty = ((EntityValueSource) valueSource).getMetaPropertyPath().getMetaProperty();
return (V) convertFromLocalDateTime(localDateTime, metaProperty.getRange().asDatatype().getJavaClass());
}
return (V) convertFromLocalDateTime(localDateTime, datatype == null ? Date.class : datatype.getJavaClass());
}
Aggregations