use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class WebTable method setupAutowiredColumns.
@Override
protected void setupAutowiredColumns(EntityTableItems<E> entityTableSource) {
Collection<MetaPropertyPath> paths = getAutowiredProperties(entityTableSource);
for (MetaPropertyPath metaPropertyPath : paths) {
MetaProperty property = metaPropertyPath.getMetaProperty();
if (!property.getRange().getCardinality().isMany() && !metadataTools.isSystem(property)) {
io.jmix.ui.component.Table.Column<E> column = addColumn(metaPropertyPath);
String propertyName = property.getName();
MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(metaPropertyPath);
column.setCaption(messageTools.getPropertyCaption(propertyMetaClass, propertyName));
if (column instanceof Table.Column) {
((Table.Column<E>) column).setType(metaPropertyPath.getRangeJavaClass());
}
}
}
}
use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class WebTokenList method getViewForField.
/**
* If the value for a component is selected from the lookup screen there may be cases when lookup screen contains a list of entities loaded with a
* view that doesn't contain all attributes, required by the token list.
* <p>
* The method evaluates the view that was is for loading entities in the token list
*
* @return a view or null if the view cannot be evaluated
*/
@Nullable
protected FetchPlan getViewForField() {
ValueSource valueSource = getValueSource();
if (valueSource instanceof ContainerValueSource) {
ContainerValueSource containerValueSource = (ContainerValueSource) valueSource;
InstanceContainer container = containerValueSource.getContainer();
FetchPlan view = container.getFetchPlan();
if (view != null) {
MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
FetchPlan curView = view;
if (metaPropertyPath != null) {
for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
FetchPlanProperty viewProperty = curView.getProperty(metaProperty.getName());
if (viewProperty != null) {
curView = viewProperty.getFetchPlan();
if (curView == null)
return null;
}
}
MetaProperty inverseMetaProperty = metaPropertyPath.getMetaProperty().getInverse();
if (inverseMetaProperty != null && !inverseMetaProperty.getRange().getCardinality().isMany()) {
curView = fetchPlans.builder(curView).add(inverseMetaProperty.getName()).build();
}
}
if (curView != view) {
return curView;
}
}
}
return null;
}
use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class WebTokenList method updateMasterRefIfOptionsRefreshed.
/**
* Sets master-entity reference to the value and remove master-entity reference
* from options if they are not in nested container.
*
* @param value value items
*/
protected void updateMasterRefIfOptionsRefreshed(Collection<V> value) {
if (!isRefreshOptionsEnabled()) {
return;
}
if (!(getValueSource() instanceof ContainerValueSource)) {
return;
}
EntityOptions<V> options = (EntityOptions<V>) getOptions();
if (options == null) {
return;
}
ContainerValueSource valueSource = (ContainerValueSource) getValueSource();
MetaPropertyPath mpp = valueSource.getMetaPropertyPath();
MetaProperty inverseProperty = mpp.getMetaProperty().getInverse();
Object masterEntity = valueSource.getItem();
if (inverseProperty == null || masterEntity == null) {
return;
}
List<V> optionItems = getOptions().getOptions().collect(Collectors.toList());
for (V option : optionItems) {
// skip all options that did not load master-reference
if (!entityStates.isLoaded(option, inverseProperty.getName())) {
continue;
}
if (value.contains(option)) {
// reset master-entity reference
EntityValues.setValue(option, inverseProperty.getName(), masterEntity);
} else {
Entity ref = EntityValues.getValue(option, inverseProperty.getName());
if (ref == null) {
continue;
}
// remove ref to the master entity if option is not in value
if (Objects.equals(EntityValues.getId(ref), EntityValues.getId(masterEntity))) {
EntityValues.setValue(option, inverseProperty.getName(), null);
}
}
}
}
use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class WebEntityLinkField method afterCommitOpenedEntity.
@Override
protected void afterCommitOpenedEntity(Object item) {
if (getCollectionDatasourceFromOwner() == null) {
super.afterCommitOpenedEntity(item);
return;
}
MetaProperty metaProperty = getMetaPropertyForEditedValue();
if (metaProperty != null && metaProperty.getRange().isClass()) {
if (getValueSource() != null) {
boolean ownerDsModified = false;
boolean nonModifiedInTable = false;
DatasourceImplementation ownerDs = null;
if (getCollectionDatasourceFromOwner() != null) {
ownerDs = ((DatasourceImplementation) getCollectionDatasourceFromOwner());
nonModifiedInTable = !ownerDs.getItemsToUpdate().contains(((EntityValueSource) getValueSource()).getItem());
ownerDsModified = ownerDs.isModified();
}
// noinspection unchecked
setValueSilently((V) item);
// remove from items to update if it was not modified before setValue
if (ownerDs != null) {
if (nonModifiedInTable) {
ownerDs.getItemsToUpdate().remove(getDatasource().getItem());
}
ownerDs.setModified(ownerDsModified);
}
} else {
// noinspection unchecked
setValue((V) item);
}
// if we edit property with non Entity type and set ListComponent owner
} else if (owner != null) {
if (getCollectionDatasourceFromOwner() != null) {
// noinspection unchecked
getCollectionDatasourceFromOwner().updateItem((Entity) item);
}
if (owner instanceof Focusable) {
// focus owner
((Focusable) owner).focus();
}
// if we edit property with non Entity type
} else {
// noinspection unchecked
setValueSilently((V) item);
}
}
use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class EntityFetcher method fetch.
@SuppressWarnings("unchecked")
protected void fetch(Object entity, FetchPlan fetchPlan, Map<Object, Set<FetchPlan>> visited, boolean optimizeForDetached) {
Set<FetchPlan> fetchPlans = visited.get(entity);
if (fetchPlans == null) {
fetchPlans = new HashSet<>();
visited.put(entity, fetchPlans);
} else if (fetchPlans.contains(fetchPlan)) {
return;
}
fetchPlans.add(fetchPlan);
if (log.isTraceEnabled())
log.trace("Fetching instance " + entity);
MetaClass metaClass = metadata.getClass(entity.getClass());
for (FetchPlanProperty property : fetchPlan.getProperties()) {
MetaProperty metaProperty = metaClass.getProperty(property.getName());
if (!metaProperty.getRange().isClass() && !isLazyFetchedLocalAttribute(metaProperty) || !metadataTools.isJpa(metaProperty))
continue;
if (log.isTraceEnabled())
log.trace("Fetching property " + property.getName());
Object value = EntityValues.getValue(entity, property.getName());
FetchPlan propertyFetchPlan = property.getFetchPlan();
if (value != null && propertyFetchPlan != null) {
if (value instanceof Collection) {
for (Object item : new ArrayList(((Collection) value))) {
if (item instanceof Entity) {
if (entityStates.isDetached(item)) {
fetchReloaded(item, propertyFetchPlan, visited, optimizeForDetached, managed -> {
if (value instanceof List) {
List list = (List) value;
list.set(list.indexOf(item), managed);
} else {
Collection collection = (Collection) value;
collection.remove(item);
collection.add(managed);
}
});
} else {
fetch(item, propertyFetchPlan, visited, optimizeForDetached);
}
}
}
} else if (value instanceof Entity) {
boolean isEmbeddable = EntitySystemAccess.isEmbeddable(value);
if (!metaProperty.isReadOnly() && entityStates.isDetached(value) && !isEmbeddable) {
fetchReloaded(value, propertyFetchPlan, visited, optimizeForDetached, managed -> {
EntityValues.setValue(entity, property.getName(), managed);
});
} else {
fetch(value, propertyFetchPlan, visited, optimizeForDetached);
}
}
}
}
}
Aggregations