use of io.jmix.ui.model.InstanceContainer in project jmix by jmix-framework.
the class PivotTableLoader method loadDataContainer.
protected void loadDataContainer(PivotTable pivotTable, Element element) {
String dataContainerId = element.attributeValue("dataContainer");
if (StringUtils.isNotEmpty(dataContainerId)) {
FrameOwner frameOwner = getComponentContext().getFrame().getFrameOwner();
ScreenData screenData = UiControllerUtils.getScreenData(frameOwner);
CollectionContainer dataContainer;
InstanceContainer container = screenData.getContainer(dataContainerId);
if (container instanceof CollectionContainer) {
dataContainer = (CollectionContainer) container;
} else {
throw new GuiDevelopmentException("Not a CollectionContainer: " + dataContainerId, context);
}
pivotTable.setDataProvider(new ContainerDataProvider(dataContainer));
}
}
use of io.jmix.ui.model.InstanceContainer in project jmix by jmix-framework.
the class EditorBuilderProcessor method initializeNestedEntity.
protected <E> void initializeNestedEntity(E entity, Nested container) {
InstanceContainer masterContainer = container.getMaster();
String property = container.getProperty();
MetaClass masterMetaClass = masterContainer.getEntityMetaClass();
MetaProperty metaProperty = masterMetaClass.getProperty(property);
MetaProperty inverseProp = metaProperty.getInverse();
if (inverseProp != null && !inverseProp.getRange().getCardinality().isMany()) {
Class<?> inversePropClass = extendedEntities.getEffectiveClass(inverseProp.getDomain());
Class<?> containerEntityClass = extendedEntities.getEffectiveClass(((CollectionContainer) container).getEntityMetaClass());
if (inversePropClass.isAssignableFrom(containerEntityClass)) {
EntityValues.setValue(entity, inverseProp.getName(), masterContainer.getItem());
}
}
}
use of io.jmix.ui.model.InstanceContainer in project jmix by jmix-framework.
the class EditorBuilderProcessor method setupParentDataContext.
@Nullable
protected DataContext setupParentDataContext(FrameOwner origin, Screen screen, @Nullable InstanceContainer container, @Nullable DataContext parentContext) {
DataContext dataContext = parentContext;
if (dataContext == null && container instanceof Nested) {
InstanceContainer masterContainer = ((Nested) container).getMaster();
String property = ((Nested) container).getProperty();
MetaClass masterMetaClass = masterContainer.getEntityMetaClass();
MetaProperty metaProperty = masterMetaClass.getProperty(property);
if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) {
dataContext = UiControllerUtils.getScreenData(origin).getDataContextOrNull();
}
}
if (dataContext != null) {
DataContext childContext = UiControllerUtils.getScreenData(screen).getDataContextOrNull();
checkDataContext(screen, childContext);
childContext.setParent(dataContext);
}
return dataContext;
}
use of io.jmix.ui.model.InstanceContainer in project jmix by jmix-framework.
the class LookupBuilderProcessor method getFetchPlanForField.
/**
* The method evaluates the fetch plan that is used for the entity in the given {@code field}
* <p>
* If the value for a component (e.g. {@link EntityPicker}) is selected from lookup screen then there may be cases
* when in entities in lookup screen some attributes required in the editor are not loaded.
*
* @return a view or {@code null} if the fetch plan cannot be evaluated
*/
@Nullable
protected FetchPlan getFetchPlanForField(HasValue field) {
if (field instanceof HasValueSource) {
ValueSource valueSource = ((HasValueSource) field).getValueSource();
if (valueSource instanceof ContainerValueSource) {
ContainerValueSource containerValueSource = (ContainerValueSource) valueSource;
InstanceContainer container = containerValueSource.getContainer();
FetchPlan fetchPlan = container.getFetchPlan();
if (fetchPlan != null) {
MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
FetchPlan curFetchPlan = fetchPlan;
for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
FetchPlanProperty viewProperty = curFetchPlan.getProperty(metaProperty.getName());
if (viewProperty != null) {
curFetchPlan = viewProperty.getFetchPlan();
}
if (curFetchPlan == null)
break;
}
if (curFetchPlan != fetchPlan) {
return curFetchPlan;
}
}
}
}
return null;
}
use of io.jmix.ui.model.InstanceContainer in project jmix by jmix-framework.
the class LookupBuilderProcessor method handleSelectionWithContainer.
protected <E> void handleSelectionWithContainer(LookupBuilder<E> builder, CollectionContainer<E> collectionDc, Collection<E> itemsFromLookup) {
if (itemsFromLookup.isEmpty()) {
return;
}
Collection<E> selectedItems = transform(itemsFromLookup, builder);
boolean initializeMasterReference = false;
Object masterItem = null;
MetaProperty inverseMetaProperty = null;
// update holder reference if needed
if (collectionDc instanceof Nested) {
InstanceContainer masterDc = ((Nested) collectionDc).getMaster();
String property = ((Nested) collectionDc).getProperty();
masterItem = masterDc.getItem();
MetaProperty metaProperty = metadata.getClass(masterItem).getProperty(property);
inverseMetaProperty = metaProperty.getInverse();
if (inverseMetaProperty != null && !inverseMetaProperty.getRange().getCardinality().isMany()) {
Class<?> inversePropClass = extendedEntities.getEffectiveClass(inverseMetaProperty.getDomain());
Class<?> dcClass = extendedEntities.getEffectiveClass(collectionDc.getEntityMetaClass());
initializeMasterReference = inversePropClass.isAssignableFrom(dcClass);
}
}
DataContext dataContext = UiControllerUtils.getScreenData(builder.getOrigin()).getDataContext();
List<E> mergedItems = new ArrayList<>(selectedItems.size());
FetchPlan viewForCollectionContainer = screenProperties.isReloadUnfetchedAttributesFromLookupScreens() && collectionDc.getEntityMetaClass() != null && metadataTools.isJpaEntity(collectionDc.getEntityMetaClass()) ? getFetchPlanForCollectionContainer(collectionDc, initializeMasterReference, inverseMetaProperty) : null;
for (E item : selectedItems) {
if (!collectionDc.containsItem(EntityValues.getId(item))) {
if (viewForCollectionContainer != null && !entityStates.isLoadedWithFetchPlan(item, viewForCollectionContainer)) {
// noinspection unchecked
item = (E) dataManager.load(Id.of(item)).fetchPlan(viewForCollectionContainer).one();
}
// track changes in the related instance
E mergedItem = dataContext.merge(item);
if (initializeMasterReference) {
// change reference, now it will be marked as modified
EntityValues.setValue(mergedItem, inverseMetaProperty.getName(), masterItem);
}
mergedItems.add(mergedItem);
}
}
collectionDc.getMutableItems().addAll(mergedItems);
}
Aggregations