Search in sources :

Example 1 with ExtendedEntities

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

the class OriginalEntityLoadInfo method create.

/**
 * Create a new info instance.
 * @param entity    entity instance
 * @return          info instance
 */
public static OriginalEntityLoadInfo create(Entity entity) {
    Objects.requireNonNull(entity, "entity is null");
    Metadata metadata = AppBeans.get(Metadata.class);
    MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
    ExtendedEntities extendedEntities = AppBeans.get(ExtendedEntities.class);
    MetaClass metaClass = metadata.getSession().getClass(entity.getClass());
    MetaClass originalMetaClass = extendedEntities.getOriginalMetaClass(metaClass);
    if (originalMetaClass != null) {
        metaClass = originalMetaClass;
    }
    MetaProperty primaryKeyProperty = metadataTools.getPrimaryKeyProperty(metaClass);
    boolean stringKey = primaryKeyProperty != null && primaryKeyProperty.getJavaType().equals(String.class);
    return new OriginalEntityLoadInfo((UUID) EntityValues.getId(entity), metaClass, stringKey);
}
Also used : ExtendedEntities(io.jmix.core.ExtendedEntities) MetadataTools(io.jmix.core.MetadataTools) MetaClass(io.jmix.core.metamodel.model.MetaClass) Metadata(io.jmix.core.Metadata) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 2 with ExtendedEntities

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

the class ExcludeAction method doRemove.

@SuppressWarnings("unchecked")
@Override
protected void doRemove(Set<Entity> selected, boolean autocommit) {
    CollectionDatasource ds = target.getDatasource();
    if (ds instanceof NestedDatasource) {
        // Clear reference to master entity
        Datasource masterDs = ((NestedDatasource) ds).getMaster();
        MetaProperty metaProperty = ((NestedDatasource) ds).getProperty();
        if (masterDs != null && metaProperty != null) {
            MetaProperty inverseProp = metaProperty.getInverse();
            if (inverseProp != null) {
                ExtendedEntities extendedEntities = metadata.getExtendedEntities();
                Class inversePropClass = extendedEntities.getEffectiveClass(inverseProp.getDomain());
                Class dsClass = extendedEntities.getEffectiveClass(ds.getMetaClass());
                if (inversePropClass.isAssignableFrom(dsClass)) {
                    for (Entity item : selected) {
                        EntityValues.setValue(item, inverseProp.getName(), null);
                    }
                }
            }
        }
    }
    for (Entity item : selected) {
        ds.modifyItem(item);
        ds.excludeItem(item);
    }
    if (autocommit && (ds.getCommitMode() != Datasource.CommitMode.PARENT)) {
        try {
            ds.commit();
        } catch (RuntimeException e) {
            ds.refresh();
            throw e;
        }
    }
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) PropertyDatasource(com.haulmont.cuba.gui.data.PropertyDatasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) NestedDatasource(com.haulmont.cuba.gui.data.NestedDatasource) ExtendedEntities(io.jmix.core.ExtendedEntities) Entity(io.jmix.core.Entity) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) NestedDatasource(com.haulmont.cuba.gui.data.NestedDatasource) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 3 with ExtendedEntities

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

the class CreateAction method actionPerform.

/**
 * This method is invoked by action owner component. Don't override it, there are special methods to
 * customize behaviour below.
 *
 * @param component component invoking action
 */
@Override
public void actionPerform(Component component) {
    if (beforeActionPerformedHandler != null) {
        if (!beforeActionPerformedHandler.beforeActionPerformed())
            return;
    }
    final CollectionDatasource datasource = target.getDatasource();
    Entity item = createEntity();
    if (target instanceof Tree) {
        String hierarchyProperty = ((Tree) target).getHierarchyProperty();
        Entity parentItem = datasource.getItem();
        // datasource.getItem() may contain deleted item
        if (parentItem != null && !datasource.containsItem(EntityValues.getId(parentItem))) {
            parentItem = null;
        }
        EntityValues.setValue(item, hierarchyProperty, parentItem);
    }
    if (datasource instanceof NestedDatasource) {
        // Initialize reference to master entity
        Datasource masterDs = ((NestedDatasource) datasource).getMaster();
        MetaProperty metaProperty = ((NestedDatasource) datasource).getProperty();
        if (masterDs != null && metaProperty != null) {
            MetaProperty inverseProp = metaProperty.getInverse();
            if (inverseProp != null) {
                ExtendedEntities extendedEntities = metadata.getExtendedEntities();
                Class inversePropClass = extendedEntities.getEffectiveClass(inverseProp.getDomain());
                Class dsClass = extendedEntities.getEffectiveClass(datasource.getMetaClass());
                if (inversePropClass.isAssignableFrom(dsClass)) {
                    EntityValues.setValue(item, inverseProp.getName(), masterDs.getItem());
                }
            }
        }
    }
    setInitialValuesToItem(item);
    Datasource parentDs = null;
    if (datasource instanceof PropertyDatasource) {
        MetaProperty metaProperty = ((PropertyDatasource) datasource).getProperty();
        if (metaProperty.getType().equals(MetaProperty.Type.COMPOSITION)) {
            parentDs = datasource;
        }
    }
    Map<String, Object> params = prepareWindowParams();
    internalOpenEditor(datasource, item, parentDs, params);
}
Also used : ExtendedEntities(io.jmix.core.ExtendedEntities) Entity(io.jmix.core.Entity) Tree(io.jmix.ui.component.Tree) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 4 with ExtendedEntities

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

the class LegacyCollectionDsValueSource method canUpdateMasterRefs.

protected boolean canUpdateMasterRefs() {
    MetaPropertyPath mpp = getMaster().getMetaClass().getPropertyPath(metaPropertyPath.toPathString());
    if (mpp == null) {
        return false;
    }
    if (!mpp.getMetaProperty().getRange().getCardinality().isMany()) {
        return false;
    }
    MetaProperty inverseProperty = mpp.getMetaProperty().getInverse();
    if (inverseProperty == null || inverseProperty.getType() != MetaProperty.Type.ASSOCIATION || inverseProperty.getRange().getCardinality().isMany()) {
        return false;
    }
    ExtendedEntities extendedEntities = metadata.getExtendedEntities();
    Class invPropEntityClass = extendedEntities.getEffectiveClass(inverseProperty.getDomain());
    Class datasourceEntityClass = datasource.getMetaClass().getJavaClass();
    // noinspection unchecked
    return invPropEntityClass.isAssignableFrom(datasourceEntityClass);
}
Also used : ExtendedEntities(io.jmix.core.ExtendedEntities) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 5 with ExtendedEntities

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

the class EntityChangedEvent method getResolvableType.

/**
 * INTERNAL.
 */
@Internal
@Override
public ResolvableType getResolvableType() {
    Metadata metadata = AppBeans.get(Metadata.class);
    ExtendedEntities extendedEntities = metadata.getExtendedEntities();
    MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(metadata.getClassNN(entityId.getEntityClass()));
    MetaProperty pkProperty = metadata.getTools().getPrimaryKeyProperty(metaClass);
    if (pkProperty == null) {
        throw new IllegalStateException("Unable to send EntityChangedEvent for " + metaClass + " because it has no primary key");
    }
    return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forClass(metaClass.getJavaClass()), ResolvableType.forClass(pkProperty.getJavaType()));
}
Also used : ExtendedEntities(io.jmix.core.ExtendedEntities) MetaClass(io.jmix.core.metamodel.model.MetaClass) Metadata(com.haulmont.cuba.core.global.Metadata) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Internal(io.jmix.core.annotation.Internal)

Aggregations

ExtendedEntities (io.jmix.core.ExtendedEntities)5 MetaClass (io.jmix.core.metamodel.model.MetaClass)5 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)5 Entity (io.jmix.core.Entity)2 Metadata (com.haulmont.cuba.core.global.Metadata)1 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)1 Datasource (com.haulmont.cuba.gui.data.Datasource)1 NestedDatasource (com.haulmont.cuba.gui.data.NestedDatasource)1 PropertyDatasource (com.haulmont.cuba.gui.data.PropertyDatasource)1 Metadata (io.jmix.core.Metadata)1 MetadataTools (io.jmix.core.MetadataTools)1 Internal (io.jmix.core.annotation.Internal)1 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)1 Tree (io.jmix.ui.component.Tree)1