use of io.jmix.core.metamodel.model.MetaProperty 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);
}
}
}
}
}
use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class DsContextImpl method repairReferences.
@SuppressWarnings("unchecked")
protected void repairReferences(Entity entity, Entity contextEntity) {
MetaClass metaClass = metadata.getClass(entity.getClass());
MetaClass contextEntityMetaClass = metadata.getClass(contextEntity.getClass());
for (MetaProperty property : metaClass.getProperties()) {
if (!property.getRange().isClass() || !property.getRange().asClass().equals(contextEntityMetaClass) || !PersistenceHelper.isLoaded(entity, property.getName()))
continue;
Object value = EntityValues.getValue(entity, property.getName());
if (value != null) {
if (property.getRange().getCardinality().isMany()) {
Collection collection = (Collection) value;
for (Object item : new ArrayList(collection)) {
if (contextEntity.equals(item) && contextEntity != item) {
if (collection instanceof List) {
List list = (List) collection;
list.set(list.indexOf(item), contextEntity);
} else {
collection.remove(item);
collection.add(contextEntity);
}
}
}
} else {
if (contextEntity.equals(value) && contextEntity != value) {
EntityValues.setValue(entity, property.getName(), contextEntity);
}
}
}
}
}
use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class PropertyDatasourceImpl method commit.
@SuppressWarnings("unchecked")
@Override
public void commit() {
if (!allowCommit) {
return;
}
if (getCommitMode() == CommitMode.PARENT) {
if (parentDs == null) {
throw new IllegalStateException("parentDs is null while commitMode=PARENT");
}
if (parentDs instanceof CollectionDatasource) {
CollectionDatasource parentCollectionDs = (CollectionDatasource) parentDs;
for (Entity item : itemsToCreate) {
if (parentCollectionDs.containsItem(EntityValues.getId(item))) {
parentCollectionDs.modifyItem(item);
} else {
parentCollectionDs.addItem(item);
}
}
for (Entity item : itemsToUpdate) {
parentCollectionDs.modifyItem(item);
}
for (Entity item : itemsToDelete) {
parentCollectionDs.removeItem(item);
}
// after repeated edit of new items the parent datasource can contain items-to-create which are deleted
// in this datasource, so we need to delete them
Collection<Entity> parentItemsToCreate = ((DatasourceImplementation) parentCollectionDs).getItemsToCreate();
for (Entity createdItem : new ArrayList<>(parentItemsToCreate)) {
if (!this.itemsToCreate.contains(createdItem)) {
MetaProperty inverseProp = metaProperty.getInverse();
// delete only if they have the same master item
if (inverseProp != null && PersistenceHelper.isLoaded(createdItem, inverseProp.getName()) && Objects.equals(EntityValues.getValue(createdItem, inverseProp.getName()), masterDs.getItem())) {
parentCollectionDs.removeItem(createdItem);
}
}
}
} else {
Entity item = null;
if (!itemsToCreate.isEmpty()) {
item = itemsToCreate.iterator().next();
} else if (!itemsToUpdate.isEmpty()) {
item = itemsToUpdate.iterator().next();
} else if (!itemsToDelete.isEmpty()) {
item = itemsToDelete.iterator().next();
}
if (item != null) {
parentDs.setItem(item);
}
}
clearCommitLists();
modified = false;
}
}
use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class ValueDatasourceDelegate method beforeLoadValues.
protected ValueLoadContext beforeLoadValues(Map<String, Object> params) {
ValueLoadContext context = new ValueLoadContext();
ValueLoadContext.Query q = (ValueLoadContext.Query) ds.createDataQuery(context, params);
if (q == null) {
ds.detachListener(ds.data.values());
ds.data.clear();
return null;
}
if (ds.firstResult > 0)
q.setFirstResult(ds.firstResult);
if (ds.maxResults > 0) {
q.setMaxResults(ds.maxResults);
}
if (storeName != null)
context.setStoreName(storeName);
context.setHint(PersistenceHints.SOFT_DELETION, ds.isSoftDeletion());
context.setIdName(idName);
for (MetaProperty property : ds.metaClass.getProperties()) {
context.addProperty(property.getName());
}
ds.dataLoadError = null;
return context;
}
use of io.jmix.core.metamodel.model.MetaProperty in project jmix by jmix-framework.
the class ValueDatasourceDelegate method convertEnumValues.
protected void convertEnumValues(KeyValueEntity entity, List<MetaProperty> enumProperties) {
try {
for (MetaProperty enumProperty : enumProperties) {
Object enumValue = entity.getValue(enumProperty.getName());
if (enumValue != null) {
Enumeration enumeration = enumProperty.getRange().asEnumeration();
entity.setValue(enumProperty.getName(), enumeration.parse(String.valueOf(enumValue)));
}
}
} catch (ParseException e) {
throw new RuntimeException("Unable to convert enum id to enum instance for EnumClass");
}
}
Aggregations