use of io.jmix.core.metamodel.model.Range in project jmix-docs by Haulmont.
the class CustomerService method printOrderProperties.
public void printOrderProperties() {
// <1>
MetaClass metaClass = metadata.getClass(Order.class);
for (MetaProperty metaProperty : metaClass.getProperties()) {
// <2>
// <3>
String propertyName = metaProperty.getName();
// <4>
MetaProperty.Type propertyType = metaProperty.getType();
// <5>
Class<?> javaType = metaProperty.getJavaType();
// <6>
Range propertyRange = metaProperty.getRange();
String info = "name: " + propertyName + "\n type: " + propertyType + "\n Java type: " + javaType + "\n range: " + propertyRange;
if (propertyRange.isClass()) {
// <7>
// <8>
MetaClass refMetaClass = propertyRange.asClass();
// <9>
Range.Cardinality cardinality = propertyRange.getCardinality();
info += "\n reference to: " + refMetaClass;
info += "\n cardinality: " + cardinality;
} else if (propertyRange.isEnum()) {
// <10>
// <11>
Enumeration<?> enumeration = propertyRange.asEnumeration();
info += "\n enum: " + enumeration;
} else if (propertyRange.isDatatype()) {
// <12>
// <13>
Datatype<Object> propertyDatatype = propertyRange.asDatatype();
info += "\n data type: " + propertyDatatype;
}
System.out.println(info);
}
}
use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class DynAttrMetaPropertyPathResolver method resolveMetaPropertyPath.
@Override
public MetaPropertyPath resolveMetaPropertyPath(MetaClass metaClass, String propertyPath) {
String[] properties = propertyPath.split("\\.");
MetaProperty[] metaProperties = new MetaProperty[properties.length];
MetaProperty currentProperty;
MetaClass currentClass = metaClass;
for (int i = 0; i < properties.length; i++) {
if (currentClass == null) {
return null;
}
currentProperty = currentClass.findProperty(properties[i]);
if (currentProperty == null) {
if (!DynAttrUtils.isDynamicAttributeProperty(properties[i])) {
return null;
}
AttributeDefinition attribute = dynAttrMetadata.getAttributeByCode(currentClass, DynAttrUtils.getAttributeCodeFromProperty(properties[i])).orElse(null);
if (attribute == null) {
return null;
}
currentProperty = attribute.getMetaProperty();
}
Range range = currentProperty.getRange();
currentClass = range.isClass() ? range.asClass() : null;
metaProperties[i] = currentProperty;
}
return new MetaPropertyPath(metaClass, metaProperties);
}
use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class DateIntervalUtils method isIntervalTypeSupportsDatatype.
/**
* @param mpp meta property path
* @param value date interval
* @return {@code true} if date interval type supports provided property type
*/
public boolean isIntervalTypeSupportsDatatype(BaseDateInterval value, MetaPropertyPath mpp) {
Preconditions.checkNotNullArgument(mpp);
Preconditions.checkNotNullArgument(value);
Range range = mpp.getRange();
return range.isDatatype();
}
use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class EntityImportPlanJsonBuilderImpl method buildFromJsonObject.
protected EntityImportPlan buildFromJsonObject(JsonObject jsonObject, MetaClass metaClass) {
EntityImportPlanBuilder importPlanBuilder = entityImportPlans.builder(metaClass.getJavaClass());
ExportImportEntityContext importContext = new ExportImportEntityContext(metaClass);
accessManager.applyRegisteredConstraints(importContext);
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
String propertyName = entry.getKey();
MetaProperty metaProperty = metaClass.findProperty(propertyName);
if (metaProperty == null) {
continue;
}
Range propertyRange = metaProperty.getRange();
Class<?> propertyType = metaProperty.getJavaType();
if (propertyRange.isDatatype() || propertyRange.isEnum()) {
if (importContext.canImported(propertyName)) {
importPlanBuilder.addLocalProperty(propertyName);
}
} else if (propertyRange.isClass()) {
if (Entity.class.isAssignableFrom(propertyType)) {
if (metaProperty.getType() == MetaProperty.Type.EMBEDDED) {
MetaClass propertyMetaClass = metadata.getClass(propertyType);
JsonElement propertyJsonObject = entry.getValue();
if (!propertyJsonObject.isJsonObject()) {
throw new RuntimeException("JsonObject was expected for property " + propertyName);
}
if (importContext.canImported(propertyName)) {
EntityImportPlan propertyImportPlan = buildFromJsonObject(propertyJsonObject.getAsJsonObject(), propertyMetaClass);
importPlanBuilder.addEmbeddedProperty(propertyName, propertyImportPlan);
}
} else {
MetaClass propertyMetaClass = metadata.getClass(propertyType);
if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) {
JsonElement propertyJsonObject = entry.getValue();
if (importContext.canImported(propertyName)) {
if (propertyJsonObject.isJsonNull()) {
// in case of null we must add such import behavior to update the reference with null value later
if (metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_ONE) {
importPlanBuilder.addManyToOneProperty(propertyName, ReferenceImportBehaviour.IGNORE_MISSING);
} else {
importPlanBuilder.addOneToOneProperty(propertyName, ReferenceImportBehaviour.IGNORE_MISSING);
}
} else {
if (!propertyJsonObject.isJsonObject()) {
throw new RuntimeException("JsonObject was expected for property " + propertyName);
}
EntityImportPlan propertyImportPlan = buildFromJsonObject(propertyJsonObject.getAsJsonObject(), propertyMetaClass);
if (metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_ONE) {
importPlanBuilder.addManyToOneProperty(propertyName, propertyImportPlan);
} else {
importPlanBuilder.addOneToOneProperty(propertyName, propertyImportPlan);
}
}
}
} else {
if (importContext.canImported(propertyName))
if (metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_ONE) {
importPlanBuilder.addManyToOneProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING);
} else {
importPlanBuilder.addOneToOneProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING);
}
}
}
} else if (Collection.class.isAssignableFrom(propertyType)) {
MetaClass propertyMetaClass = metaProperty.getRange().asClass();
switch(metaProperty.getRange().getCardinality()) {
case MANY_TO_MANY:
{
if (importContext.canImported(propertyName))
importPlanBuilder.addManyToManyProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING, CollectionImportPolicy.REMOVE_ABSENT_ITEMS);
break;
}
case ONE_TO_MANY:
{
if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) {
JsonElement compositionJsonArray = entry.getValue();
if (!compositionJsonArray.isJsonArray()) {
throw new RuntimeException("JsonArray was expected for property " + propertyName);
}
EntityImportPlan propertyImportPlan = buildFromJsonArray(compositionJsonArray.getAsJsonArray(), propertyMetaClass);
if (importContext.canImported(propertyName))
importPlanBuilder.addOneToManyProperty(propertyName, propertyImportPlan, CollectionImportPolicy.REMOVE_ABSENT_ITEMS);
}
break;
}
default:
// ignore other options
break;
}
}
}
}
return importPlanBuilder.build();
}
use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class KeyValueMetaPropertyBuilder method build.
public KeyValueMetaProperty build(MetaClass metaClass, String name, Class javaClass) {
MetaProperty.Type type;
Range range;
Session metadataSession = metadata.getSession();
if (Entity.class.isAssignableFrom(javaClass)) {
range = new ClassRange(metadataSession.findClass(javaClass));
type = MetaProperty.Type.ASSOCIATION;
} else if (EnumClass.class.isAssignableFrom(javaClass)) {
@SuppressWarnings("unchecked") EnumerationImpl enumeration = new EnumerationImpl(javaClass);
range = new EnumerationRange(enumeration);
type = MetaProperty.Type.ENUM;
} else {
@SuppressWarnings("unchecked") Datatype datatype = datatypeRegistry.get(javaClass);
range = new DatatypeRange(datatype);
type = MetaProperty.Type.DATATYPE;
}
return new KeyValueMetaProperty(metaClass, name, javaClass, range, type);
}
Aggregations