Search in sources :

Example 41 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class DesktopTokenList method getMasterEntity.

@Nullable
protected Entity getMasterEntity(CollectionDatasource datasource) {
    if (datasource instanceof NestedDatasource) {
        Datasource masterDs = ((NestedDatasource) datasource).getMaster();
        com.google.common.base.Preconditions.checkState(masterDs != null);
        return masterDs.getItem();
    }
    return null;
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) NestedDatasource(com.haulmont.cuba.gui.data.NestedDatasource) NestedDatasource(com.haulmont.cuba.gui.data.NestedDatasource) Nullable(javax.annotation.Nullable)

Example 42 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class WebTokenList method getMasterEntity.

@Nullable
protected Entity getMasterEntity(CollectionDatasource datasource) {
    if (datasource instanceof NestedDatasource) {
        Datasource masterDs = ((NestedDatasource) datasource).getMaster();
        com.google.common.base.Preconditions.checkState(masterDs != null);
        return masterDs.getItem();
    }
    return null;
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) NestedDatasource(com.haulmont.cuba.gui.data.NestedDatasource) NestedDatasource(com.haulmont.cuba.gui.data.NestedDatasource) Nullable(javax.annotation.Nullable)

Example 43 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class WebTokenList method setLookup.

@Override
public void setLookup(boolean lookup) {
    if (this.lookup != lookup) {
        if (lookup) {
            lookupAction = new PickerField.LookupAction(lookupPickerField) {

                @Nonnull
                @Override
                protected Map<String, Object> prepareScreenParams() {
                    Map<String, Object> screenParams = super.prepareScreenParams();
                    if (isMultiSelect()) {
                        screenParams = new HashMap<>(screenParams);
                        WindowParams.MULTI_SELECT.set(screenParams, true);
                        // for backward compatibility
                        screenParams.put("multiSelect", "true");
                    }
                    return screenParams;
                }

                @SuppressWarnings("unchecked")
                @Override
                protected void handleLookupWindowSelection(Collection items) {
                    if (items.isEmpty()) {
                        return;
                    }
                    @SuppressWarnings("unchecked") Collection<Entity> selected = items;
                    CollectionDatasource optionsDatasource = lookupPickerField.getOptionsDatasource();
                    if (optionsDatasource != null && lookupPickerField.isRefreshOptionsOnLookupClose()) {
                        optionsDatasource.refresh();
                        if (datasource != null) {
                            for (Object obj : getDatasource().getItems()) {
                                Entity entity = (Entity) obj;
                                if (getOptionsDatasource().containsItem(entity.getId())) {
                                    datasource.updateItem(getOptionsDatasource().getItem(entity.getId()));
                                }
                            }
                        }
                    }
                    // add all selected items to tokens
                    if (itemChangeHandler != null) {
                        for (Entity newItem : selected) {
                            itemChangeHandler.addItem(newItem);
                        }
                    } else if (datasource != null) {
                        // get master entity and inverse attribute in case of nested datasource
                        Entity masterEntity = getMasterEntity(datasource);
                        MetaProperty inverseProp = getInverseProperty(datasource);
                        for (Entity newItem : selected) {
                            if (!datasource.containsItem(newItem.getId())) {
                                // Initialize reference to master entity
                                if (inverseProp != null && isInitializeMasterReference(inverseProp)) {
                                    newItem.setValue(inverseProp.getName(), masterEntity);
                                }
                                datasource.addItem(newItem);
                            }
                        }
                    }
                    afterSelect(items);
                    if (afterLookupSelectionHandler != null) {
                        afterLookupSelectionHandler.onSelect(items);
                    }
                }
            };
            lookupPickerField.addAction(lookupAction);
            if (getLookupScreen() != null) {
                lookupAction.setLookupScreen(getLookupScreen());
            }
            lookupAction.setLookupScreenOpenType(lookupOpenMode);
            lookupAction.setLookupScreenParams(lookupScreenParams);
            lookupAction.setLookupScreenDialogParams(lookupScreenDialogParams);
        } else {
            lookupPickerField.removeAction(lookupAction);
        }
        lookupAction.setAfterLookupCloseHandler((window, actionId) -> {
            if (afterLookupCloseHandler != null) {
                afterLookupCloseHandler.onClose(window, actionId);
            }
        });
        lookupAction.setAfterLookupSelectionHandler(items -> {
            if (afterLookupSelectionHandler != null) {
                afterLookupSelectionHandler.onSelect(items);
            }
        });
    }
    this.lookup = lookup;
    component.refreshComponent();
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Nonnull(javax.annotation.Nonnull) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 44 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class LookupPickerFieldTest method testValueLoadFromOptions.

@Test
public void testValueLoadFromOptions() {
    LookupPickerField component = factory.createComponent(LookupPickerField.class);
    // noinspection unchecked
    Datasource<User> testDs = new DsBuilder().setId("testDs").setJavaClass(User.class).setView(viewRepository.getView(User.class, View.LOCAL)).buildDatasource();
    testDs.setItem(new User());
    ((DatasourceImpl) testDs).valid();
    assertNull(component.getValue());
    Group g = new Group();
    testDs.getItem().setGroup(g);
    // noinspection unchecked
    CollectionDatasource<Group, UUID> groupsDs = new DsBuilder().setId("testDs").setJavaClass(Group.class).setView(viewRepository.getView(Group.class, View.LOCAL)).setRefreshMode(CollectionDatasource.RefreshMode.NEVER).setAllowCommit(false).buildCollectionDatasource();
    groupsDs.includeItem(g);
    Group g1 = new Group();
    g1.setId(g.getId());
    groupsDs.includeItem(g1);
    Group g2 = new Group();
    groupsDs.includeItem(g2);
    component.setOptionsDatasource(groupsDs);
    component.setDatasource(testDs, "group");
    assertTrue("Value should be from options ds", g1 == component.getValue());
    component.setValue(g2);
    Component.ValueChangeListener listener1 = e -> {
        assertEquals(g2, e.getPrevValue());
        assertEquals(g1, e.getValue());
    };
    component.addValueChangeListener(listener1);
    component.setValue(g);
}
Also used : PersistenceManagerClient(com.haulmont.cuba.client.sys.PersistenceManagerClient) Arrays(java.util.Arrays) Datasource(com.haulmont.cuba.gui.data.Datasource) NonStrictExpectations(mockit.NonStrictExpectations) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) Test(org.junit.Test) PersistenceManagerService(com.haulmont.cuba.core.app.PersistenceManagerService) UUID(java.util.UUID) AppBeans(com.haulmont.cuba.core.global.AppBeans) User(com.haulmont.cuba.security.entity.User) ArrayList(java.util.ArrayList) Group(com.haulmont.cuba.security.entity.Group) View(com.haulmont.cuba.core.global.View) Ignore(org.junit.Ignore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) Assert(org.junit.Assert) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Mocked(mockit.Mocked) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) UUID(java.util.UUID) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) Test(org.junit.Test)

Example 45 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class LookupFieldDsTest method testUnsubscribeSubscribeOptions.

@Test
public void testUnsubscribeSubscribeOptions() {
    LookupField lookupField = factory.createComponent(LookupField.class);
    CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
    lookupField.setOptionsDatasource(groupsDs);
    List<Group> groups = new ArrayList<>(groupsDs.getItems());
    Datasource<User> userDs = getTestUserDatasource();
    userDs.getItem().setGroup(groups.get(0));
    lookupField.setDatasource(userDs, "group");
    // unbind
    lookupField.setOptionsDatasource(null);
    Datasource.ItemChangeListener<Group> listener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    groupsDs.addItemChangeListener(listener);
    lookupField.setValue(groups.get(1));
    // setup
    groupsDs.removeItemChangeListener(listener);
    boolean[] valueWasChanged = { false };
    listener = e -> valueWasChanged[0] = true;
    groupsDs.addItemChangeListener(listener);
    lookupField.setOptionsDatasource(groupsDs);
    lookupField.setValue(groups.get(2));
    assertEquals(true, valueWasChanged[0]);
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Datasource(com.haulmont.cuba.gui.data.Datasource) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) UUID(java.util.UUID) User(com.haulmont.cuba.security.entity.User) ArrayList(java.util.ArrayList) Group(com.haulmont.cuba.security.entity.Group) List(java.util.List) Ignore(org.junit.Ignore) LookupField(com.haulmont.cuba.gui.components.LookupField) Component(com.haulmont.cuba.gui.components.Component) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Assert.assertEquals(org.junit.Assert.assertEquals) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) ArrayList(java.util.ArrayList) LookupField(com.haulmont.cuba.gui.components.LookupField) UUID(java.util.UUID) Test(org.junit.Test)

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