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);
}
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;
}
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);
}
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());
}
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;
}
}
}
}
Aggregations