use of com.haulmont.cuba.gui.model.InstanceContainer in project cuba by cuba-platform.
the class AbstractComponentGenerationStrategy method setOptionsLoader.
protected void setOptionsLoader(CategoryAttribute categoryAttribute, LookupField lookupField, ContainerValueSource valueSource) {
InstanceContainer<?> container = valueSource.getContainer();
Entity entity = container.getItemOrNull();
if (entity != null) {
List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) entity, categoryAttribute);
// noinspection unchecked
lookupField.setOptions(new ListOptions(options));
}
container.addItemChangeListener(e -> {
List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) e.getItem(), categoryAttribute);
// noinspection unchecked
lookupField.setOptions(new ListOptions(options));
});
List<CategoryAttribute> dependsOnAttributes = categoryAttribute.getConfiguration().getDependsOnAttributes();
if (dependsOnAttributes != null && !dependsOnAttributes.isEmpty()) {
List<String> dependsOnAttributesCodes = dependsOnAttributes.stream().map(a -> DynamicAttributesUtils.encodeAttributeCode(a.getCode())).collect(Collectors.toList());
container.addItemPropertyChangeListener(e -> {
if (dependsOnAttributesCodes.contains(e.getProperty())) {
List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) e.getItem(), categoryAttribute);
// noinspection unchecked
lookupField.setOptions(new ListOptions(options));
if (!options.contains(lookupField.getValue())) {
// noinspection unchecked
lookupField.setValue(null);
}
}
});
}
}
use of com.haulmont.cuba.gui.model.InstanceContainer in project cuba by cuba-platform.
the class RemoveOperation method commitIfNeeded.
protected void commitIfNeeded(Collection<? extends Entity> entitiesToRemove, CollectionContainer container, ScreenData screenData) {
List<? extends Entity> entitiesToCommit = entitiesToRemove.stream().filter(entity -> !entityStates.isNew(entity)).collect(Collectors.toList());
boolean needCommit = !entitiesToCommit.isEmpty();
if (container instanceof Nested) {
InstanceContainer masterContainer = ((Nested) container).getMaster();
String property = ((Nested) container).getProperty();
MetaClass masterMetaClass = masterContainer.getEntityMetaClass();
MetaProperty metaProperty = masterMetaClass.getPropertyNN(property);
needCommit = needCommit && (metaProperty.getType() != MetaProperty.Type.COMPOSITION);
}
if (needCommit) {
CommitContext commitContext = new CommitContext();
for (Entity entity : entitiesToCommit) {
commitContext.addInstanceToRemove(entity);
}
dataManager.commit(commitContext);
for (Entity entity : entitiesToRemove) {
screenData.getDataContext().evict(entity);
}
} else {
for (Entity entity : entitiesToRemove) {
screenData.getDataContext().remove(entity);
}
}
}
use of com.haulmont.cuba.gui.model.InstanceContainer in project cuba by cuba-platform.
the class LookupBuilderProcessor method handleSelectionWithContainer.
protected <E extends Entity> void handleSelectionWithContainer(LookupBuilder<E> builder, CollectionContainer<E> collectionDc, Collection<E> itemsFromLookup) {
if (itemsFromLookup.isEmpty()) {
return;
}
Collection<E> selectedItems = transform(itemsFromLookup, builder);
boolean initializeMasterReference = false;
Entity 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 = masterItem.getMetaClass().getPropertyNN(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());
View viewForCollectionContainer = clientConfig.getReloadUnfetchedAttributesFromLookupScreens() && collectionDc.getEntityMetaClass() != null && metadataTools.isPersistent(collectionDc.getEntityMetaClass()) ? getViewForCollectionContainer(collectionDc, initializeMasterReference, inverseMetaProperty) : null;
for (E item : selectedItems) {
if (!collectionDc.containsItem(item.getId())) {
if (viewForCollectionContainer != null && !entityStates.isLoadedWithView(item, viewForCollectionContainer)) {
item = dataManager.reload(item, viewForCollectionContainer);
}
// track changes in the related instance
E mergedItem = dataContext.merge(item);
if (initializeMasterReference) {
// change reference, now it will be marked as modified
mergedItem.setValue(inverseMetaProperty.getName(), masterItem);
}
mergedItems.add(mergedItem);
}
}
collectionDc.getMutableItems().addAll(mergedItems);
}
use of com.haulmont.cuba.gui.model.InstanceContainer in project cuba by cuba-platform.
the class LookupBuilderProcessor method getViewForCollectionContainer.
/**
* See {@link #getViewForField(HasValue)} javadoc.
*
* @return a view or null if the view cannot be evaluated
*/
@Nullable
protected <E extends Entity> View getViewForCollectionContainer(CollectionContainer<E> collectionDc, boolean initializeMasterReference, MetaProperty inverseMetaProperty) {
View view = null;
if (collectionDc instanceof Nested) {
InstanceContainer masterDc = ((Nested) collectionDc).getMaster();
View masterView = masterDc.getView();
if (masterView != null) {
String property = ((Nested) collectionDc).getProperty();
ViewProperty viewProperty = masterView.getProperty(property);
if (viewProperty != null) {
view = viewProperty.getView();
if (view != null && initializeMasterReference && inverseMetaProperty != null) {
view.addProperty(inverseMetaProperty.getName());
}
}
}
} else {
view = collectionDc.getView();
}
return view;
}
use of com.haulmont.cuba.gui.model.InstanceContainer in project cuba by cuba-platform.
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 View getViewForField() {
ValueSource valueSource = getValueSource();
if (valueSource instanceof ContainerValueSource) {
ContainerValueSource containerValueSource = (ContainerValueSource) valueSource;
InstanceContainer container = containerValueSource.getContainer();
View view = container.getView();
if (view != null) {
MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
View curView = view;
if (metaPropertyPath != null) {
for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
ViewProperty viewProperty = curView.getProperty(metaProperty.getName());
if (viewProperty != null) {
curView = viewProperty.getView();
if (curView == null)
return null;
}
}
MetaProperty inverseMetaProperty = metaPropertyPath.getMetaProperty().getInverse();
if (inverseMetaProperty != null && !inverseMetaProperty.getRange().getCardinality().isMany()) {
curView.addProperty(inverseMetaProperty.getName());
}
}
if (curView != view) {
return curView;
}
}
}
return null;
}
Aggregations