Search in sources :

Example 1 with Metadata

use of io.jmix.core.Metadata 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 Metadata

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

the class View method addSystemProperties.

public View addSystemProperties() {
    io.jmix.core.Metadata metadata = AppBeans.get(Metadata.class);
    MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
    MetaClass metaClass = metadata.getClass(getEntityClass());
    for (String propertyName : metadataTools.getSystemProperties(metaClass)) {
        addProperty(propertyName);
    }
    return this;
}
Also used : io.jmix.core(io.jmix.core) MetaClass(io.jmix.core.metamodel.model.MetaClass) Metadata(io.jmix.core.Metadata)

Example 3 with Metadata

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

the class EntityListenerUtils method getCurrentEntityManager.

public static EntityManager getCurrentEntityManager(Entity entity) {
    Metadata metadata = AppBeans.get(Metadata.class);
    String storeName = metadata.getClass(entity).getStore().getName();
    Persistence persistence = AppBeans.get(Persistence.class);
    return persistence.getEntityManager(storeName);
}
Also used : Persistence(com.haulmont.cuba.core.Persistence) Metadata(io.jmix.core.Metadata)

Example 4 with Metadata

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

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");
    EntityValues.setId(dest, EntityValues.getId(source));
    Metadata metadata = AppBeans.get(Metadata.class);
    for (MetaProperty srcProperty : metadata.getClass(source).getProperties()) {
        String name = srcProperty.getName();
        MetaProperty dstProperty = metadata.getClass(dest).findProperty(name);
        if (dstProperty != null && !dstProperty.isReadOnly()) {
            try {
                Object value = EntityValues.getValue(source, 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 Set))
                        dstCollection = new ArrayList<>(tmpCollection);
                    else
                        dstCollection = tmpCollection;
                    EntityValues.setValue(dest, name, dstCollection);
                } else {
                    EntityValues.setValue(dest, name, EntityValues.getValue(source, 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;
            }
        }
    }
    dest.__getEntityEntry().setDetached(source.__getEntityEntry().isDetached());
    dest.__getEntityEntry().setNew(source.__getEntityEntry().isNew());
// todo dynamic attributes
// destGenericEntity.setDynamicAttributes(sourceGenericEntity.getDynamicAttributes());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Entity(io.jmix.core.Entity) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) Metadata(io.jmix.core.Metadata) ArrayList(java.util.ArrayList) Collection(java.util.Collection) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 5 with Metadata

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

the class EntityCopyUtils method copyCompositionsBack.

public static void copyCompositionsBack(Entity source, Entity dest) {
    Preconditions.checkNotNullArgument(source, "source is null");
    Preconditions.checkNotNullArgument(dest, "dest is null");
    Metadata metadata = AppBeans.get(Metadata.class);
    for (MetaProperty srcProperty : metadata.getClass(source).getProperties()) {
        String name = srcProperty.getName();
        MetaProperty dstProperty = metadata.getClass(dest).findProperty(name);
        if (dstProperty != null && !dstProperty.isReadOnly()) {
            try {
                Object value = EntityValues.getValue(source, name);
                if (value != null && srcProperty.getRange().getCardinality().isMany() && srcProperty.getType() == MetaProperty.Type.COMPOSITION) {
                    EntityValues.setValue(dest, name, EntityValues.getValue(source, name), false);
                } else {
                    EntityValues.setValue(dest, name, EntityValues.getValue(source, 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;
            }
        }
    }
}
Also used : Metadata(io.jmix.core.Metadata) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Aggregations

Metadata (io.jmix.core.Metadata)6 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)3 MetaClass (io.jmix.core.metamodel.model.MetaClass)2 Persistence (com.haulmont.cuba.core.Persistence)1 io.jmix.core (io.jmix.core)1 Entity (io.jmix.core.Entity)1 ExtendedEntities (io.jmix.core.ExtendedEntities)1 MetadataTools (io.jmix.core.MetadataTools)1 ReferenceToEntity (io.jmix.data.entity.ReferenceToEntity)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 PostConstruct (javax.annotation.PostConstruct)1