use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class DynAttrPropertyFilterComponentGenerationStrategy method createDatatypeField.
@Override
protected Component createDatatypeField(ComponentGenerationContext context, AttributeDefinition attribute) {
Component field = super.createDatatypeField(context, attribute);
Range range = attribute.getMetaProperty().getRange();
if (field instanceof HasDatatype && range.isDatatype()) {
((HasDatatype<?>) field).setDatatype(range.asDatatype());
}
return field;
}
use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class ResponseBuilder method buildResponse.
/**
* Convert loaded entity to data fetcher return format (Map<String, Object>)
*
* @param entity loaded entity
* @param fetchPlan loaded entity properties
* @param metaClass entity meta class
* @param props we need pass full set of properties to have information about system props such '_instanceName'
* @return entity converted to response as Map<String, Object>
*/
public Map<String, Object> buildResponse(Entity entity, FetchPlan fetchPlan, MetaClass metaClass, Set<String> props) {
Map<String, Object> entityAsMap = new HashMap<>();
// check and evaluate _instanceName, if required
if (environmentUtils.hasInstanceNameProperty(props)) {
entityAsMap.put(NamingUtils.SYS_ATTR_INSTANCE_NAME, metadataTools.getInstanceName(entity));
}
// must include id
writeIdField(entity, entityAsMap);
// compose result object by iterating over fetch plan props
fetchPlan.getProperties().forEach(prop -> {
String propName = prop.getName();
MetaProperty metaProperty = metaClass.getProperty(propName);
Object fieldValue = EntityValues.getValue(entity, propName);
Range propertyRange = metaProperty.getRange();
if (fieldValue == null) {
entityAsMap.put(propName, null);
return;
}
if (propertyRange.isDatatype() || propertyRange.isEnum()) {
entityAsMap.put(propName, fieldValue);
return;
}
if (propertyRange.isClass()) {
Set<String> nestedProps = environmentUtils.getNestedProps(props, propName);
if (fieldValue instanceof Entity) {
entityAsMap.put(propName, buildResponse((Entity) fieldValue, prop.getFetchPlan(), propertyRange.asClass(), nestedProps));
return;
}
if (fieldValue instanceof Collection) {
Collection<Object> values = ((Collection<Entity>) fieldValue).stream().map(e -> buildResponse(e, prop.getFetchPlan(), propertyRange.asClass(), nestedProps)).collect(Collectors.toList());
entityAsMap.put(propName, values);
return;
}
}
log.warn("buildResponse: failed for {}.{} unsupported range type ", metaClass.getName(), prop.getName());
throw new IllegalStateException("Unsupported range type " + propertyRange);
});
return entityAsMap;
}
use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class EntityLogImpl method getAllAttributes.
protected Set<String> getAllAttributes(Object entity) {
if (entity == null) {
return null;
}
Set<String> attributes = new HashSet<>();
MetaClass metaClass = metadata.getClass(entity.getClass());
for (MetaProperty metaProperty : metaClass.getProperties()) {
Range range = metaProperty.getRange();
if (range.isClass() && range.getCardinality().isMany()) {
continue;
}
attributes.add(metaProperty.getName());
}
for (MetaProperty object : metadataTools.getAdditionalProperties(metaClass)) {
attributes.add(object.getName());
}
return attributes;
}
use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class AppSettingsGridLayoutBuilder method addRowToGrid.
protected void addRowToGrid(InstanceContainer container, GridLayout gridLayout, int currentRow, MetaProperty metaProperty) {
MetaClass metaClass = container.getEntityMetaClass();
Range range = metaProperty.getRange();
UiEntityAttributeContext attributeContext = new UiEntityAttributeContext(metaClass, metaProperty.getName());
accessManager.applyRegisteredConstraints(attributeContext);
if (!attributeContext.canView()) {
return;
}
if (range.isClass()) {
UiEntityContext entityContext = new UiEntityContext(range.asClass());
accessManager.applyRegisteredConstraints(entityContext);
if (!entityContext.isViewPermitted()) {
return;
}
}
// add label
Label fieldLabel = uiComponents.create(Label.class);
fieldLabel.setValue(getPropertyCaption(metaClass, metaProperty));
fieldLabel.setAlignment(io.jmix.ui.component.Component.Alignment.MIDDLE_LEFT);
gridLayout.add(fieldLabel, 0, currentRow + 1);
// current field
ValueSource valueSource = new ContainerValueSource<>(container, metaProperty.getName());
ComponentGenerationContext componentContext = new ComponentGenerationContext(metaClass, metaProperty.getName());
componentContext.setValueSource(valueSource);
gridLayout.add(createField(metaProperty, range, componentContext), 1, currentRow + 1);
// default value
ComponentGenerationContext componentContextForDefaultField = new ComponentGenerationContext(metaClass, metaProperty.getName());
ValueSource valueSourceForDefaultField = new ContainerValueSource<>(dataComponents.createInstanceContainer(metaClass.getJavaClass()), metaProperty.getName());
componentContextForDefaultField.setValueSource(valueSourceForDefaultField);
Field defaultValueField = createField(metaProperty, range, componentContextForDefaultField);
defaultValueField.setValue(appSettingsTools.getDefaultPropertyValue(metaClass.getJavaClass(), metaProperty.getName()));
defaultValueField.setEditable(false);
gridLayout.add(defaultValueField, 2, currentRow + 1);
}
use of io.jmix.core.metamodel.model.Range in project jmix by jmix-framework.
the class FetchPlanLoader method loadFetchPlanProperties.
public void loadFetchPlanProperties(Element fetchPlanElem, FetchPlanBuilder fetchPlanBuilder, boolean systemProperties, BiFunction<MetaClass, String, FetchPlan> refFetchPlanResolver) {
final MetaClass metaClass = metadata.getClass(fetchPlanBuilder.getEntityClass());
final String fetchPlanName = fetchPlanBuilder.getName();
Set<String> propertyNames = new HashSet<>();
for (Element propElem : fetchPlanElem.elements("property")) {
String propertyName = propElem.attributeValue("name");
if (propertyNames.contains(propertyName)) {
throw new DevelopmentException(String.format("Fetch plan %s/%s definition error: fetch plan declared property %s twice", metaClass.getName(), fetchPlanName, propertyName));
}
propertyNames.add(propertyName);
MetaProperty metaProperty = metaClass.getProperty(propertyName);
if (metaProperty == null) {
throw new DevelopmentException(String.format("Fetch plan %s/%s definition error: property %s doesn't exist", metaClass.getName(), fetchPlanName, propertyName));
}
FetchPlanBuilder refFetchPlanBuilder = null;
String refFetchPlanName = propElem.attributeValue("fetchPlan");
if (refFetchPlanName == null) {
refFetchPlanName = propElem.attributeValue("view");
}
MetaClass refMetaClass;
Range range = metaProperty.getRange();
if (range == null) {
throw new RuntimeException("cannot find range for meta property: " + metaProperty);
}
// by default specify "_base" fetchPlan in case for entity property it is missing
if (refFetchPlanName == null && range.isClass()) {
refFetchPlanName = FetchPlan.BASE;
}
final List<Element> propertyElements = propElem.elements("property");
boolean inlineFetchPlan = !propertyElements.isEmpty();
if (!range.isClass() && (refFetchPlanName != null || inlineFetchPlan)) {
throw new DevelopmentException(String.format("Fetch plan %s/%s definition error: property %s is not an entity", metaClass.getName(), fetchPlanName, propertyName));
}
if (refFetchPlanName != null) {
refMetaClass = getMetaClass(propElem, range);
refFetchPlanBuilder = fetchPlans.builder(refMetaClass.getJavaClass()).name(refFetchPlanName).addFetchPlan(refFetchPlanResolver.apply(refMetaClass, refFetchPlanName));
}
if (inlineFetchPlan) {
// try to import anonymous fetch plan
Class<?> rangeClass = range.asClass().getJavaClass();
if (refFetchPlanBuilder == null) {
refFetchPlanBuilder = fetchPlans.builder(rangeClass);
if (systemProperties)
refFetchPlanBuilder.addSystem();
}
loadFetchPlanProperties(propElem, refFetchPlanBuilder, systemProperties, refFetchPlanResolver);
}
FetchMode fetchMode = FetchMode.AUTO;
String fetch = propElem.attributeValue("fetch");
if (fetch != null)
fetchMode = FetchMode.valueOf(fetch);
fetchPlanBuilder.mergeProperty(propertyName, refFetchPlanBuilder != null ? refFetchPlanBuilder.build() : null, fetchMode);
}
}
Aggregations