use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class ItemWrapper method unsubscribe.
@Override
public void unsubscribe() {
if (item instanceof CollectionDatasource) {
CollectionDatasource datasource = (CollectionDatasource) item;
// noinspection unchecked
datasource.removeCollectionChangeListener(weakCollectionChangeListener);
weakCollectionChangeListener = null;
}
item = null;
metaClass = null;
properties.values().forEach(PropertyWrapper::unsubscribe);
properties.clear();
}
use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class PermissionUiHelper method createPermissionItem.
/**
* Add or edit permission item in datasource
* @param ds Datasource
* @param roleDs Role datasource
* @param permissionTarget Permission identifier
* @param type Permission type
* @param value Permission value
*/
public static void createPermissionItem(CollectionDatasource<Permission, UUID> ds, Datasource<Role> roleDs, final String permissionTarget, PermissionType type, Integer value) {
Permission permission = null;
for (Permission p : ds.getItems()) {
if (Objects.equals(p.getTarget(), permissionTarget)) {
permission = p;
break;
}
}
if (permission == null) {
// restore entity instead of create
if (ds instanceof RestorablePermissionDatasource) {
RestorablePermissionDatasource datasource = (RestorablePermissionDatasource) ds;
permission = datasource.findRemovedEntity(p -> p != null && Objects.equals(p.getTarget(), permissionTarget));
if (permission != null) {
datasource.restoreEntity(permission);
}
}
}
if (permission == null) {
Metadata metadata = AppBeans.get(Metadata.NAME);
Permission newPermission = metadata.create(Permission.class);
newPermission.setRole(roleDs.getItem());
newPermission.setTarget(permissionTarget);
newPermission.setType(type);
newPermission.setValue(value);
ds.addItem(newPermission);
} else {
permission.setValue(value);
}
}
use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class ListEditorPopupWindow method createComponentForEntity.
protected Field createComponentForEntity() {
Preconditions.checkNotNullArgument(entityName, getMessage("entityNameParamNotDefined"));
MetaClass metaClass = metadata.getClassNN(entityName);
Field componentForEntity;
if (BooleanUtils.isNotTrue(useLookupField)) {
PickerField pickerField = componentsFactory.createComponent(PickerField.class);
pickerField.setMetaClass(metaClass);
if (!Strings.isNullOrEmpty(entityJoinClause) || !Strings.isNullOrEmpty(entityWhereClause)) {
PickerField.LookupAction lookupAction = dynamicAttributesGuiTools.createLookupAction(pickerField, entityJoinClause, entityWhereClause);
pickerField.addAction(lookupAction);
} else {
pickerField.addLookupAction();
if (!Strings.isNullOrEmpty(lookupScreen)) {
PickerField.LookupAction lookupAction = (PickerField.LookupAction) pickerField.getAction(PickerField.LookupAction.NAME);
if (lookupAction != null) {
lookupAction.setLookupScreen(lookupScreen);
}
}
}
componentForEntity = pickerField;
} else {
LookupField lookupField = componentsFactory.createComponent(LookupField.class);
CollectionDatasource optionsDs;
if (!Strings.isNullOrEmpty(entityJoinClause) || !Strings.isNullOrEmpty(entityWhereClause)) {
optionsDs = dynamicAttributesGuiTools.createOptionsDatasourceForLookup(metaClass, entityJoinClause, entityWhereClause);
} else {
optionsDs = DsBuilder.create().setMetaClass(metaClass).setViewName(View.MINIMAL).buildCollectionDatasource();
optionsDs.refresh();
}
lookupField.setOptionsDatasource(optionsDs);
componentForEntity = lookupField;
}
componentForEntity.addValueChangeListener(e -> {
Entity selectedEntity = (Entity) e.getValue();
if (selectedEntity != null && !valueExists(selectedEntity)) {
this.addValueToLayout(selectedEntity, ListEditorHelper.getValueCaption(selectedEntity, itemType, timeZone));
}
componentForEntity.setValue(null);
});
return componentForEntity;
}
use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class DatasourceImpl method commit.
@SuppressWarnings("unchecked")
@Override
public void commit() {
backgroundWorker.checkUIAccess();
if (!allowCommit) {
return;
}
if (getCommitMode() == CommitMode.DATASTORE) {
final DataSupplier supplier = getDataSupplier();
Entity committedItem = supplier.commit(item, getView());
committed(Collections.singleton(committedItem));
} else if (getCommitMode() == CommitMode.PARENT) {
if (parentDs == null) {
throw new IllegalStateException("parentDs is null while commitMode=PARENT");
}
if (parentDs instanceof CollectionDatasource) {
CollectionDatasource ds = (CollectionDatasource) parentDs;
if (ds.containsItem(item.getId())) {
ds.modifyItem(item);
} else {
ds.addItem(item);
// This is necessary for nested property datasources to work correctly
ds.setItem(item);
}
} else {
parentDs.setItem(item);
if (AppBeans.get(EntityStates.class).isNew(item)) {
((DatasourceImplementation) parentDs).modified(item);
}
}
clearCommitLists();
modified = false;
} else {
throw new UnsupportedOperationException("Unsupported commitMode: " + getCommitMode());
}
}
use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class AbstractComponentGenerationStrategy method createEntityField.
protected Component createEntityField(ComponentGenerationContext context, MetaPropertyPath mpp) {
String linkAttribute = null;
Element xmlDescriptor = context.getXmlDescriptor();
if (xmlDescriptor != null) {
linkAttribute = xmlDescriptor.attributeValue("link");
}
if (!Boolean.parseBoolean(linkAttribute)) {
CollectionDatasource optionsDatasource = context.getOptionsDatasource();
if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
DynamicAttributesMetaProperty metaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
CategoryAttribute attribute = metaProperty.getAttribute();
if (Boolean.TRUE.equals(attribute.getLookup())) {
DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
optionsDatasource = dynamicAttributesGuiTools.createOptionsDatasourceForLookup(metaProperty.getRange().asClass(), attribute.getJoinClause(), attribute.getWhereClause());
}
}
PickerField pickerField;
if (optionsDatasource == null) {
pickerField = componentsFactory.createComponent(PickerField.class);
setDatasource(pickerField, context);
if (mpp.getMetaProperty().getType() == MetaProperty.Type.ASSOCIATION) {
pickerField.addLookupAction();
if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
DynamicAttributesMetaProperty dynamicAttributesMetaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
dynamicAttributesGuiTools.initEntityPickerField(pickerField, dynamicAttributesMetaProperty.getAttribute());
}
boolean actionsByMetaAnnotations = ComponentsHelper.createActionsByMetaAnnotations(pickerField);
if (!actionsByMetaAnnotations) {
pickerField.addClearAction();
}
} else {
pickerField.addOpenAction();
pickerField.addClearAction();
}
} else {
LookupPickerField lookupPickerField = componentsFactory.createComponent(LookupPickerField.class);
setDatasource(lookupPickerField, context);
lookupPickerField.setOptionsDatasource(optionsDatasource);
pickerField = lookupPickerField;
ComponentsHelper.createActionsByMetaAnnotations(pickerField);
}
if (xmlDescriptor != null) {
String captionProperty = xmlDescriptor.attributeValue("captionProperty");
if (StringUtils.isNotEmpty(captionProperty)) {
pickerField.setCaptionMode(CaptionMode.PROPERTY);
pickerField.setCaptionProperty(captionProperty);
}
}
return pickerField;
} else {
EntityLinkField linkField = componentsFactory.createComponent(EntityLinkField.class);
setDatasource(linkField, context);
setLinkFieldAttributes(linkField, context);
return linkField;
}
}
Aggregations