Search in sources :

Example 21 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class DsContextTest method testAggregationNewEntity.

@Test
public void testAggregationNewEntity() {
    // Master editor
    createMasterDsContext();
    final TestMasterEntity master = new TestMasterEntity();
    master.setMasterName("new_master");
    masterDs.setItem(master);
    final TestDetailEntity detail = new TestDetailEntity();
    detail.setMaster(master);
    createDetailDsContext();
    setupParentDs(detailsDs, detailDs, detail);
    detailDs.getItem().setDetailName("new_detail");
    dataService.commitValidator = null;
    detailDsContext.commit();
    assertEquals("Commit Detail DsContext", 0, dataService.commitCount);
    // Commit Master editor
    dataService.commitValidator = new TestDataSupplier.CommitValidator() {

        @Override
        public void validate(CommitContext context) {
            assertTrue(containsEntityInstance(context.getCommitInstances(), detail.getId()));
            assertTrue(containsEntityInstance(context.getCommitInstances(), master.getId()));
            for (Entity entity : context.getCommitInstances()) {
                if (entity.getId().equals(detail.getId()))
                    assertEquals("new_detail", ((TestDetailEntity) entity).getDetailName());
            }
        }
    };
    masterDsContext.commit();
    assertEquals("Commit Master DsContext", 1, dataService.commitCount);
}
Also used : TestPartEntity(com.haulmont.cuba.gui.data.impl.testmodel1.TestPartEntity) TestEmbeddableEntity(com.haulmont.cuba.gui.data.impl.testmodel1.TestEmbeddableEntity) TestMasterEntity(com.haulmont.cuba.gui.data.impl.testmodel1.TestMasterEntity) Entity(com.haulmont.cuba.core.entity.Entity) TestDetailEntity(com.haulmont.cuba.gui.data.impl.testmodel1.TestDetailEntity) TestMasterEntity(com.haulmont.cuba.gui.data.impl.testmodel1.TestMasterEntity) CommitContext(com.haulmont.cuba.core.global.CommitContext) TestDetailEntity(com.haulmont.cuba.gui.data.impl.testmodel1.TestDetailEntity) Test(org.junit.Test)

Example 22 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class CollectionPropertyDatasourceImpl method removeItem.

@Override
public void removeItem(T item) {
    checkNotNullArgument(item, "item is null");
    checkState();
    checkPermission();
    Collection<T> collection = getCollection();
    if (collection != null) {
        if (this.item != null && this.item.equals(item)) {
            setItem(null);
        }
        // In case of 2nd-level composition and using List as attribute type, there may be duplicated instances
        // after repeated editing of newly added item. So remove them all.
        Iterator<T> iterator = collection.iterator();
        while (iterator.hasNext()) {
            T entity = iterator.next();
            if (entity.equals(item)) {
                detachListener(entity);
                iterator.remove();
            }
        }
        modified = true;
        if (cascadeProperty) {
            final Entity parentItem = masterDs.getItem();
            // noinspection unchecked
            ((DatasourceImplementation) masterDs).modified(parentItem);
        } else {
            deleted(item);
        }
        fireCollectionChanged(Operation.REMOVE, Collections.singletonList(item));
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity)

Example 23 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class DsContextImpl method registerDependency.

@Override
public void registerDependency(final Datasource datasource, final Datasource dependFrom, final String propertyName) {
    Datasource ds = dependencies.get(datasource);
    if (ds != null) {
        if (ds.equals(dependFrom)) {
            return;
        } else {
            throw new UnsupportedOperationException("Datasource couldn't depend from two different sources");
        }
    }
    // noinspection unchecked
    dependFrom.addItemChangeListener(e -> {
        if (datasource.getState() == Datasource.State.VALID) {
            datasource.refresh();
        }
    });
    if (dependFrom instanceof CollectionDatasource) {
        // noinspection unchecked
        ((CollectionDatasource) dependFrom).addCollectionChangeListener(e -> {
            if (e.getOperation() == CollectionDatasource.Operation.REFRESH) {
                datasource.refresh();
            }
        });
    }
    // noinspection unchecked
    dependFrom.addItemPropertyChangeListener(e -> {
        if (propertyName != null) {
            // parameter can use a property path with more than one element, e.g. :ds$driversDs.status.id,
            // but we should listen the first level property
            String listeningProperty = propertyName.split("\\.")[0];
            if (listeningProperty.equals(e.getProperty())) {
                final Entity item = Datasource.State.VALID.equals(dependFrom.getState()) ? dependFrom.getItem() : null;
                if (Objects.equals(item, e.getItem())) {
                    datasource.refresh();
                }
            }
        }
    });
    dependencies.put(datasource, dependFrom);
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity)

Example 24 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class EmbeddedDatasourceImpl method committed.

@Override
public void committed(Set<Entity> entities) {
    Entity item = masterDs.getItem();
    Entity newItem = null;
    Entity previousItem = null;
    if (item != null) {
        Iterator<Entity> commitIter = entities.iterator();
        while (commitIter.hasNext() && (previousItem == null) && (newItem == null)) {
            Entity commitItem = commitIter.next();
            if (commitItem.equals(item)) {
                previousItem = item;
                newItem = commitItem;
            }
        }
        if (previousItem != null) {
            detachListener(getItem(previousItem));
        }
        if (newItem != null) {
            attachListener(getItem(newItem));
        }
    }
    modified = false;
    clearCommitLists();
}
Also used : EmbeddableEntity(com.haulmont.cuba.core.entity.EmbeddableEntity) Entity(com.haulmont.cuba.core.entity.Entity)

Example 25 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class EntityCopyUtils method copyCompositions.

public static void copyCompositions(Entity source, Entity dest) {
    Preconditions.checkNotNullArgument(source, "source is null");
    Preconditions.checkNotNullArgument(dest, "dest is null");
    if (source instanceof BaseDbGeneratedIdEntity && dest instanceof BaseDbGeneratedIdEntity) {
        ((BaseDbGeneratedIdEntity) dest).setId(((BaseDbGeneratedIdEntity) source).getId());
    }
    for (MetaProperty srcProperty : source.getMetaClass().getProperties()) {
        String name = srcProperty.getName();
        MetaProperty dstProperty = dest.getMetaClass().getProperty(name);
        if (dstProperty != null && !dstProperty.isReadOnly()) {
            try {
                Object value = source.getValue(name);
                if (value != null && srcProperty.getRange().getCardinality().isMany() && srcProperty.getType() == MetaProperty.Type.COMPOSITION) {
                    // noinspection unchecked
                    Collection<Entity> srcCollection = (Collection) value;
                    // Copy first to a Set to remove duplicates that could be created on repeated editing newly
                    // added items
                    Collection<Entity> tmpCollection = new LinkedHashSet<>();
                    for (Entity item : srcCollection) {
                        Entity copy = copyCompositions(item);
                        tmpCollection.add(copy);
                    }
                    Collection<Entity> dstCollection;
                    if (value instanceof List)
                        dstCollection = new ArrayList<>(tmpCollection);
                    else
                        dstCollection = tmpCollection;
                    dest.setValue(name, dstCollection);
                } else {
                    dest.setValue(name, source.getValue(name));
                }
            } catch (RuntimeException e) {
                Throwable cause = ExceptionUtils.getRootCause(e);
                if (cause == null)
                    cause = e;
                // ignore exception on copy for not loaded fields
                if (!isNotLoadedAttributeException(cause))
                    throw e;
            }
        }
    }
    if (source instanceof BaseGenericIdEntity && dest instanceof BaseGenericIdEntity) {
        BaseGenericIdEntity destGenericEntity = (BaseGenericIdEntity) dest;
        BaseGenericIdEntity<?> sourceGenericEntity = (BaseGenericIdEntity<?>) source;
        BaseEntityInternalAccess.setDetached(destGenericEntity, BaseEntityInternalAccess.isDetached(sourceGenericEntity));
        BaseEntityInternalAccess.setNew(destGenericEntity, BaseEntityInternalAccess.isNew(sourceGenericEntity));
        destGenericEntity.setDynamicAttributes(sourceGenericEntity.getDynamicAttributes());
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BaseDbGeneratedIdEntity(com.haulmont.cuba.core.entity.BaseDbGeneratedIdEntity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) ArrayList(java.util.ArrayList) BaseDbGeneratedIdEntity(com.haulmont.cuba.core.entity.BaseDbGeneratedIdEntity) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

Entity (com.haulmont.cuba.core.entity.Entity)203 MetaClass (com.haulmont.chile.core.model.MetaClass)51 MetaProperty (com.haulmont.chile.core.model.MetaProperty)44 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)40 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)18 Test (org.junit.Test)15 Inject (javax.inject.Inject)14 java.util (java.util)12 EntityManager (com.haulmont.cuba.core.EntityManager)11 ParseException (java.text.ParseException)11 Element (org.dom4j.Element)11 Logger (org.slf4j.Logger)11 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)10 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)9 LoggerFactory (org.slf4j.LoggerFactory)9 Query (com.haulmont.cuba.core.Query)8 Server (com.haulmont.cuba.core.entity.Server)8 Transaction (com.haulmont.cuba.core.Transaction)7 Datasource (com.haulmont.cuba.gui.data.Datasource)7 Collectors (java.util.stream.Collectors)7