Search in sources :

Example 31 with CollectionDatasource

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();
}
Also used : CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource)

Example 32 with CollectionDatasource

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);
    }
}
Also used : RestorablePermissionDatasource(com.haulmont.cuba.gui.app.security.ds.RestorablePermissionDatasource) PermissionVariant(com.haulmont.cuba.gui.app.security.entity.PermissionVariant) AttributePermissionVariant(com.haulmont.cuba.gui.app.security.entity.AttributePermissionVariant) Datasource(com.haulmont.cuba.gui.data.Datasource) PermissionType(com.haulmont.cuba.security.entity.PermissionType) RestorablePermissionDatasource(com.haulmont.cuba.gui.app.security.ds.RestorablePermissionDatasource) UUID(java.util.UUID) AppBeans(com.haulmont.cuba.core.global.AppBeans) Metadata(com.haulmont.cuba.core.global.Metadata) Objects(java.util.Objects) UiPermissionVariant(com.haulmont.cuba.gui.app.security.entity.UiPermissionVariant) Permission(com.haulmont.cuba.security.entity.Permission) Role(com.haulmont.cuba.security.entity.Role) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Permission(com.haulmont.cuba.security.entity.Permission) Metadata(com.haulmont.cuba.core.global.Metadata)

Example 33 with CollectionDatasource

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;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource)

Example 34 with CollectionDatasource

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());
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) DataSupplier(com.haulmont.cuba.gui.data.DataSupplier) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource)

Example 35 with CollectionDatasource

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;
    }
}
Also used : CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) DynamicAttributesMetaProperty(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Element(org.dom4j.Element) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)

Aggregations

CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)67 Datasource (com.haulmont.cuba.gui.data.Datasource)39 UUID (java.util.UUID)24 Group (com.haulmont.cuba.security.entity.Group)23 User (com.haulmont.cuba.security.entity.User)23 ArrayList (java.util.ArrayList)23 Ignore (org.junit.Ignore)23 Test (org.junit.Test)23 Component (com.haulmont.cuba.gui.components.Component)20 List (java.util.List)19 Assert.assertEquals (org.junit.Assert.assertEquals)19 Assert.assertTrue (org.junit.Assert.assertTrue)19 Entity (com.haulmont.cuba.core.entity.Entity)15 MetaClass (com.haulmont.chile.core.model.MetaClass)10 MetaProperty (com.haulmont.chile.core.model.MetaProperty)10 LookupField (com.haulmont.cuba.gui.components.LookupField)8 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)7 LookupPickerField (com.haulmont.cuba.gui.components.LookupPickerField)7 Element (org.dom4j.Element)7 Assert.assertNotNull (org.junit.Assert.assertNotNull)7