use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class GsonSerializationSupport method writeFields.
@SuppressWarnings("unchecked")
protected void writeFields(JsonWriter out, Entity entity) throws IOException {
MetaClass metaClass = metadata.getClass(entity);
for (MetaProperty property : metaClass.getProperties()) {
if (!"id".equalsIgnoreCase(property.getName()) && !property.isReadOnly() && !exclude(entity.getClass(), property.getName()) && entityStates.isLoaded(entity, property.getName())) {
Range propertyRange = property.getRange();
if (propertyRange.isDatatype()) {
writeSimpleProperty(out, entity, property);
} else if (propertyRange.isClass()) {
Object value = EntityValues.getValue(entity, property.getName());
if (value instanceof Entity) {
out.name(property.getName());
writeEntity(out, (Entity) value);
} else if (value instanceof Collection) {
out.name(property.getName());
writeCollection(out, (Collection) value);
}
} else if (propertyRange.isEnum()) {
Object value = EntityValues.getValue(entity, property.getName());
out.name(property.getName());
out.value(propertyRange.asEnumeration().format(value));
}
}
}
}
use of io.jmix.core.metamodel.model.Range 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.metamodel.model.Range in project jmix by jmix-framework.
the class MaskedFieldImpl 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();
Range range = entityValueSource.getMetaPropertyPath().getRange();
if (range.isDatatype()) {
Datatype<V> propertyDataType = range.asDatatype();
return nullToEmpty(propertyDataType.format(modelValue, locale));
}
}
return nullToEmpty(super.convertToPresentation(modelValue));
}
use of io.jmix.core.metamodel.model.Range 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.metamodel.model.Range in project jmix by jmix-framework.
the class DomainModelBuilder method addProperty.
private void addProperty(EntityBuilder builder, MetaClass metaClass, MetaProperty prop) {
String name = prop.getName();
String userFriendlyName = null;
if (loadCaptions) {
userFriendlyName = messageTools.getPropertyCaption(metaClass, prop.getName());
}
boolean isEmbedded = metadataTools.isEmbedded(prop);
MetaProperty.Type type = prop.getType();
Class<?> javaType = prop.getJavaType();
Range range = prop.getRange();
switch(type) {
case EMBEDDED:
case COMPOSITION:
case ASSOCIATION:
if (range.isClass()) {
MetaClass rangeClass = range.asClass();
if (range.getCardinality().isMany()) {
builder.addCollectionReferenceAttribute(name, rangeClass.getName(), userFriendlyName);
} else {
builder.addReferenceAttribute(name, rangeClass.getName(), userFriendlyName, isEmbedded);
}
} else {
builder.addSingleValueAttribute(javaType, name, userFriendlyName);
}
break;
case ENUM:
builder.addSingleValueAttribute(javaType, name, userFriendlyName);
break;
case DATATYPE:
builder.addSingleValueAttribute(javaType, name, userFriendlyName);
break;
}
}
Aggregations