use of io.jmix.core.Entity in project jmix by jmix-framework.
the class CollectionPropertyDatasourceImpl method initCollection.
protected void initCollection() {
Entity item = masterDs.getItem();
if (item == null)
throw new IllegalStateException("Item is null");
Class<?> type = metaProperty.getJavaType();
if (List.class.isAssignableFrom(type)) {
EntityValues.setValue(item, metaProperty.getName(), new ArrayList());
} else if (Set.class.isAssignableFrom(type)) {
EntityValues.setValue(item, metaProperty.getName(), new LinkedHashSet());
} else {
throw new UnsupportedOperationException("Type " + type + " not supported, should implement List or Set");
}
if (EntityValues.getValue(item, metaProperty.getName()) == null) {
throw new RuntimeException("Cannot set collection property " + metaProperty.getName() + ". Probably not contained in view.");
}
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class RelatedEntitiesBean method getRelatedIds.
protected List<Object> getRelatedIds(Collection<? extends Entity> selectedParents, MetaDataDescriptor descriptor) {
if (selectedParents.isEmpty()) {
return Collections.emptyList();
} else {
List<Object> parentIds = new ArrayList<>();
for (Entity e : selectedParents) {
parentIds.add(EntityValues.getId(e));
}
// noinspection UnnecessaryLocalVariable
List<Object> relatedIds = relatedEntitiesService.getRelatedIds(parentIds, descriptor.getMetaClass().getName(), descriptor.getMetaProperty().getName());
return relatedIds;
}
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class EmbeddedDatasourceImpl method setItem.
@Override
public void setItem(T item) {
backgroundWorker.checkUIAccess();
if (getItem() != null) {
metadata.getTools().copy(item, getItem());
itemsToUpdate.add(item);
} else {
final Entity parentItem = masterDs.getItem();
EntityValues.setValue(parentItem, metaProperty.getName(), item);
}
setModified(true);
((DatasourceImplementation) masterDs).modified(masterDs.getItem());
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class EmbeddedDatasourceImpl method getItem.
@Override
public T getItem() {
backgroundWorker.checkUIAccess();
final Entity item = masterDs.getItem();
return getItem(item);
}
use of io.jmix.core.Entity 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());
}
Aggregations