use of com.haulmont.cuba.gui.model.DataContext in project cuba by cuba-platform.
the class RefreshAction method execute.
/**
* Executes the action.
*/
@Override
public void execute() {
if (target == null) {
throw new IllegalStateException("RefreshAction target is not set");
}
if (target.getItems() instanceof EmptyDataUnit) {
return;
}
if (!(target.getItems() instanceof ContainerDataUnit)) {
throw new IllegalStateException("RefreshAction target is null or does not implement SupportsContainerBinding");
}
CollectionContainer container = ((ContainerDataUnit) target.getItems()).getContainer();
if (container == null) {
throw new IllegalStateException("RefreshAction target is not bound to CollectionContainer");
}
DataLoader loader = null;
if (container instanceof HasLoader) {
loader = ((HasLoader) container).getLoader();
}
if (loader != null) {
DataContext dataContext = loader.getDataContext();
if (dataContext != null) {
for (Object entity : container.getItems()) {
dataContext.evict((Entity) entity);
}
}
loader.load();
} else {
log.warn("RefreshAction '{}' target container has no loader, refresh is impossible", getId());
}
}
use of com.haulmont.cuba.gui.model.DataContext 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.DataContext in project cuba by cuba-platform.
the class ClearAction method execute.
/**
* Executes the action.
*/
@SuppressWarnings("unchecked")
@Override
public void execute() {
// remove entity if it is a composition
Object value = pickerField.getValue();
ValueSource valueSource = pickerField.getValueSource();
if (value != null && !value.equals(pickerField.getEmptyValue()) && valueSource instanceof EntityValueSource) {
EntityValueSource entityValueSource = (EntityValueSource) pickerField.getValueSource();
Entity entity = (Entity) pickerField.getValue();
if (entityValueSource.getMetaPropertyPath() != null && entityValueSource.getMetaPropertyPath().getMetaProperty().getType() == MetaProperty.Type.COMPOSITION) {
FrameOwner screen = pickerField.getFrame().getFrameOwner();
DataContext dataContext = UiControllerUtils.getScreenData(screen).getDataContext();
dataContext.remove(entity);
}
}
// Set the value as if the user had set it
pickerField.setValueFromUser(pickerField.getEmptyValue());
}
Aggregations