Search in sources :

Example 1 with Entity

use of io.jmix.core.Entity in project jmix-docs by Haulmont.

the class EntityData method onInit.

@Subscribe
protected void onInit(InitEvent event) {
    ListDataProvider dataProvider = new ListDataProvider();
    dataProvider.addItem(new EntityDataItem((Entity) valueDescription(75, "Sky")));
    dataProvider.addItem(new EntityDataItem((Entity) valueDescription(7, "Shady side of pyramid")));
    dataProvider.addItem(new EntityDataItem((Entity) valueDescription(18, "Sunny side of pyramid")));
    chart.setDataProvider(dataProvider);
}
Also used : ListDataProvider(io.jmix.ui.data.impl.ListDataProvider) Entity(io.jmix.core.Entity) EntityDataItem(io.jmix.ui.data.impl.EntityDataItem)

Example 2 with Entity

use of io.jmix.core.Entity in project jmix by jmix-framework.

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(EntityValues.getId(item))) {
                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(io.jmix.core.Entity) DataSupplier(com.haulmont.cuba.gui.data.DataSupplier) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource)

Example 3 with Entity

use of io.jmix.core.Entity in project jmix by jmix-framework.

the class DsContextImpl method createCommitContext.

protected CommitContext createCommitContext(DataSupplier dataservice, Map<DataSupplier, Collection<Datasource<Entity>>> commitData) {
    CommitContext context = new CommitContext();
    context.setDiscardCommitted(discardCommitted);
    for (Datasource<Entity> datasource : commitData.get(dataservice)) {
        if (datasource instanceof DatasourceImplementation) {
            DatasourceImplementation<Entity> implementation = (DatasourceImplementation) datasource;
            boolean listenersEnabled = implementation.enableListeners(false);
            try {
                for (Entity entity : implementation.getItemsToCreate()) {
                    addToContext(entity, datasource, context.getEntitiesToSave(), context.getFetchPlans());
                }
                for (Entity entity : implementation.getItemsToUpdate()) {
                    addToContext(entity, datasource, context.getEntitiesToSave(), context.getFetchPlans());
                }
                for (Entity entity : implementation.getItemsToDelete()) {
                    addToContext(entity, datasource, context.getEntitiesToRemove(), context.getFetchPlans());
                }
            } finally {
                implementation.enableListeners(listenersEnabled);
            }
        }
    }
    repairReferences(context);
    return context;
}
Also used : Entity(io.jmix.core.Entity) CommitContext(com.haulmont.cuba.core.global.CommitContext)

Example 4 with Entity

use of io.jmix.core.Entity in project jmix by jmix-framework.

the class DsContextImpl method replaceMasterCopies.

// Replace the reference to master entity with actual entity containing in the master datasource,
// because in case of nested property datasources there may be references to cloned master entities.
protected void replaceMasterCopies(Entity entity, NestedDatasource datasource) {
    Datasource masterDs = datasource.getMaster();
    MetaProperty metaProperty = datasource.getProperty();
    if (masterDs != null && metaProperty != null) {
        MetaProperty inverseProp = metaProperty.getInverse();
        if (inverseProp != null && !inverseProp.getRange().getCardinality().isMany()) {
            MetaClass metaClass = metadata.getExtendedEntities().getEffectiveMetaClass(inverseProp.getDomain());
            if (metaClass.equals(datasource.getMetaClass()) && (PersistenceHelper.isLoaded(entity, inverseProp.getName()) && // replace master only if it's already set
            EntityValues.getValue(entity, inverseProp.getName()) != null)) {
                Object masterItem = null;
                if (masterDs instanceof CollectionDatasource) {
                    Entity value = EntityValues.getValue(entity, inverseProp.getName());
                    if (value != null) {
                        Object id = EntityValues.getId(value);
                        // noinspection unchecked
                        masterItem = ((CollectionDatasource) masterDs).getItem(id);
                    }
                } else {
                    masterItem = masterDs.getItem();
                }
                if (masterItem != null) {
                    // CAUTION need to rework this mechanism in case of two or more nested collection datasources
                    EntityValues.setValue(entity, inverseProp.getName(), masterItem, false);
                }
            }
        }
    }
}
Also used : Entity(io.jmix.core.Entity) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 5 with Entity

use of io.jmix.core.Entity in project jmix by jmix-framework.

the class DsContextImpl method commit.

@Override
public boolean commit() {
    Map<DataSupplier, Collection<Datasource<Entity>>> commitData = collectCommitData();
    boolean committed = false;
    if (!commitData.isEmpty()) {
        DataSupplier dataservice = getDataSupplier();
        Set<DataSupplier> suppliers = commitData.keySet();
        if (suppliers.size() == 1 && Objects.equals(suppliers.iterator().next(), dataservice)) {
            CommitContext context = createCommitContext(dataservice, commitData);
            fireBeforeCommit(context);
            Set<Entity> committedEntities = dataservice.commit(context);
            fireAfterCommit(context, committedEntities);
            notifyAllDsCommited(dataservice, committedEntities);
            committed = true;
        } else {
            throw new UnsupportedOperationException();
        }
    }
    for (DsContext childDsContext : children) {
        boolean c = commitToParent(childDsContext.getAll());
        committed = committed || c;
    }
    boolean c = commitToParent(datasourceMap.values());
    committed = committed || c;
    return committed;
}
Also used : Entity(io.jmix.core.Entity) CommitContext(com.haulmont.cuba.core.global.CommitContext)

Aggregations

Entity (io.jmix.core.Entity)94 MetaClass (io.jmix.core.metamodel.model.MetaClass)20 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)18 CommitContext (com.haulmont.cuba.core.global.CommitContext)10 CoreTest (com.haulmont.cuba.core.testsupport.CoreTest)10 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)10 Test (org.junit.jupiter.api.Test)10 Server (com.haulmont.cuba.core.model.common.Server)8 Datasource (com.haulmont.cuba.gui.data.Datasource)8 Collectors (java.util.stream.Collectors)8 Autowired (org.springframework.beans.factory.annotation.Autowired)8 FetchPlan (io.jmix.core.FetchPlan)7 Logger (org.slf4j.Logger)7 Metadata (io.jmix.core.Metadata)6 MetadataTools (io.jmix.core.MetadataTools)6 EntityValues (io.jmix.core.entity.EntityValues)6 java.util (java.util)6 ArrayList (java.util.ArrayList)6 Nullable (javax.annotation.Nullable)6 LoggerFactory (org.slf4j.LoggerFactory)6