use of io.jmix.core.MetadataTools in project jmix by jmix-framework.
the class CollectionDsHelper method createProperties.
public static List<MetaPropertyPath> createProperties(FetchPlan fetchPlan, MetaClass metaClass) {
List<MetaPropertyPath> properties = new ArrayList<>();
MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
if (fetchPlan != null && metadataTools.isJpaEntity(metaClass)) {
for (FetchPlanProperty property : fetchPlan.getProperties()) {
final String name = property.getName();
final MetaProperty metaProperty = metaClass.findProperty(name);
if (metaProperty == null) {
String message = String.format("Unable to find property %s for entity %s", name, metaClass.getName());
throw new DevelopmentException(message);
}
if (!metadataTools.isJpa(metaProperty)) {
String message = String.format("Specified transient property %s in view for datasource with persistent entity %s", name, metaClass.getName());
LoggerFactory.getLogger(CollectionDsHelper.class).warn(message);
continue;
}
final Range range = metaProperty.getRange();
if (range == null) {
continue;
}
final Range.Cardinality cardinality = range.getCardinality();
if (!cardinality.isMany()) {
properties.add(new MetaPropertyPath(metaClass, metaProperty));
}
}
// add all non-persistent properties
for (MetaProperty metaProperty : metaClass.getProperties()) {
if (!metadataTools.isJpa(metaProperty)) {
properties.add(new MetaPropertyPath(metaClass, metaProperty));
}
}
} else {
if (fetchPlan != null) {
LoggerFactory.getLogger(CollectionDsHelper.class).warn("Specified view {} for datasource with not persistent entity {}", fetchPlan.getName(), metaClass.getName());
}
for (MetaProperty metaProperty : metaClass.getProperties()) {
final Range range = metaProperty.getRange();
if (range == null)
continue;
final Range.Cardinality cardinality = range.getCardinality();
if (!cardinality.isMany()) {
properties.add(new MetaPropertyPath(metaClass, metaProperty));
}
}
}
return properties;
}
use of io.jmix.core.MetadataTools in project jmix by jmix-framework.
the class DsBuilder method buildDatasource.
public Datasource buildDatasource() {
init();
Datasource datasource;
try {
if (master == null && property == null) {
if (dsClass == null) {
datasource = createDatasource();
} else {
datasource = (Datasource) dsClass.newInstance();
}
datasource.setup(dsContext, dataSupplier, id, metaClass, view);
} else {
boolean isEmbedded = false;
if (master != null) {
MetaClass metaClass = master.getMetaClass();
MetaProperty metaProperty = metaClass.findProperty(property);
MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
isEmbedded = metadataTools.isEmbedded(metaProperty);
}
if (dsClass == null) {
datasource = isEmbedded ? createEmbeddedDatasource() : createPropertyDatasource();
} else {
datasource = (Datasource) dsClass.newInstance();
}
((NestedDatasource) datasource).setup(id, master, property);
}
} catch (IllegalAccessException | InstantiationException e) {
throw new RuntimeException(e);
}
datasource.setAllowCommit(allowCommit);
registerDatasource(datasource);
return datasource;
}
use of io.jmix.core.MetadataTools in project jmix by jmix-framework.
the class TextFieldImpl method convertToPresentation.
@SuppressWarnings("unchecked")
@Override
protected String convertToPresentation(@Nullable V modelValue) throws ConversionException {
if (formatter != null) {
return nullToEmpty(formatter.apply(modelValue));
}
if (datatype != null) {
return nullToEmpty(datatype.format(modelValue, locale));
}
if (valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
Range range = entityValueSource.getMetaPropertyPath().getRange();
if (range.isDatatype()) {
Datatype<V> propertyDataType = range.asDatatype();
return nullToEmpty(propertyDataType.format(modelValue, locale));
} else {
setEditable(false);
if (modelValue == null)
return "";
if (range.isClass()) {
MetadataTools metadataTools = applicationContext.getBean(MetadataTools.class);
if (range.getCardinality().isMany()) {
return ((Collection<Object>) modelValue).stream().map(metadataTools::getInstanceName).collect(Collectors.joining(", "));
} else {
return metadataTools.getInstanceName(modelValue);
}
} else if (range.isEnum()) {
Messages messages = applicationContext.getBean(Messages.class);
return messages.getMessage((Enum) modelValue);
}
}
}
return nullToEmpty(super.convertToPresentation(modelValue));
}
use of io.jmix.core.MetadataTools in project jmix by jmix-framework.
the class DatasourceValueSource method setApplicationContext.
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
MetaClass metaClass = datasource instanceof RuntimePropsDatasource ? ((RuntimePropsDatasource<E>) datasource).resolveCategorizedEntityClass() : datasource.getMetaClass();
MetadataTools metadataTools = applicationContext.getBean(MetadataTools.class);
this.metaPropertyPath = metadataTools.resolveMetaPropertyPath(metaClass, property);
this.datasource.addStateChangeListener(this::datasourceStateChanged);
this.datasource.addItemChangeListener(this::datasourceItemChanged);
this.datasource.addItemPropertyChangeListener(this::datasourceItemPropertyChanged);
if (datasource.getState() == Datasource.State.VALID) {
setState(BindingState.ACTIVE);
}
}
use of io.jmix.core.MetadataTools in project jmix by jmix-framework.
the class LegacyCollectionDsValueSource method setApplicationContext.
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
metadata = applicationContext.getBean(Metadata.class);
if (datasource instanceof NestedDatasource) {
NestedDatasource nestedDs = (NestedDatasource) this.datasource;
MetaProperty nestedDsProperty = nestedDs.getProperty();
MetadataTools metadataTools = applicationContext.getBean(MetadataTools.class);
MetaClass masterDsEntityClass = nestedDs.getMaster().getMetaClass();
MetaPropertyPath masterDsMpp = metadataTools.resolveMetaPropertyPath(masterDsEntityClass, nestedDsProperty.getName());
if (masterDsMpp.getMetaProperty() != nestedDsProperty) {
log.debug("Master Datasource property doesn't match with specified nested datasource property");
} else {
metaPropertyPath = masterDsMpp;
}
}
datasource.addCollectionChangeListener(this::collectionChanged);
}
Aggregations