use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class ExcludeAction method doRemove.
@SuppressWarnings("unchecked")
@Override
protected void doRemove(Set<Entity> selected, boolean autocommit) {
@SuppressWarnings({ "unchecked" }) 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) {
item.setValue(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;
}
}
}
use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class DeletePolicyProcessor method getReference.
protected Entity getReference(Entity entity, MetaProperty property) {
if (PersistenceHelper.isLoaded(entity, property.getName()))
return entity.getValue(property.getName());
else {
Query query = entityManager.createQuery("select e." + property.getName() + " from " + entity.getMetaClass().getName() + " e where e." + primaryKeyName + " = ?1");
query.setParameter(1, entity.getId());
Object refEntity = query.getFirstResult();
return (Entity) refEntity;
}
}
use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class DeletePolicyProcessor method cascade.
protected void cascade(String entityName, MetaProperty property) {
String template = property.getRange().getCardinality().isMany() ? "select e from %s e join e.%s c where c." + primaryKeyName + " = ?1" : "select e from %s e where e.%s." + primaryKeyName + " = ?1";
String qstr = String.format(template, entityName, property.getName());
Query query = entityManager.createQuery(qstr);
query.setParameter(1, entity.getId());
List<Entity> list = query.getResultList();
for (Entity e : list) {
entityManager.remove(e);
}
}
use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class DeletePolicyProcessor method processOnDelete.
protected void processOnDelete(List<MetaProperty> properties) {
for (MetaProperty property : properties) {
MetaClass metaClass = property.getRange().asClass();
OnDelete annotation = property.getAnnotatedElement().getAnnotation(OnDelete.class);
DeletePolicy deletePolicy = annotation.value();
switch(deletePolicy) {
case DENY:
if (property.getRange().getCardinality().isMany()) {
if (!isCollectionEmpty(property))
throw new DeletePolicyException(this.metaClass.getName(), metaClass.getName());
} else {
Object value = getReference(entity, property);
if (value != null)
throw new DeletePolicyException(this.metaClass.getName(), metaClass.getName());
}
break;
case CASCADE:
if (property.getRange().getCardinality().isMany()) {
Collection<Entity> value = getCollection(property);
if (value != null && !value.isEmpty()) {
for (Entity e : value) {
entityManager.remove(e);
}
}
} else {
Entity value = getReference(entity, property);
if (value != null && checkIfEntityBelongsToMaster(property, value)) {
if (!(value instanceof SoftDelete)) {
if (PersistenceHelper.isLoaded(entity, property.getName())) {
entity.setValue(property.getName(), null);
entityManager.remove(value);
} else {
hardDeleteNotLoadedReference(entity, property, value);
}
} else {
entityManager.remove(value);
}
}
}
break;
case UNLINK:
if (property.getRange().getCardinality().isMany()) {
if (metadata.getTools().isOwningSide(property)) {
Collection<Entity> value = entity.getValue(property.getName());
if (value != null) {
value.clear();
}
} else if (property.getInverse() != null) {
Collection<Entity> value = getCollection(property);
if (value != null) {
value.forEach(e -> setReferenceNull(e, property.getInverse()));
}
} else {
throw new UnsupportedOperationException("Unable to unlink nested collection items");
}
} else {
if (metadata.getTools().isOwningSide(property)) {
setReferenceNull(entity, property);
} else {
Entity value = getReference(entity, property);
if (value != null && property.getInverse() != null) {
setReferenceNull(value, property.getInverse());
}
}
}
break;
}
}
}
use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class DeletePolicyProcessor method isCollectionEmpty.
protected boolean isCollectionEmpty(MetaProperty property) {
MetaProperty inverseProperty = property.getInverse();
if (inverseProperty == null) {
log.warn("Inverse property not found for property {}", property);
Collection<Entity> value = entity.getValue(property.getName());
return value == null || value.isEmpty();
}
String invPropName = inverseProperty.getName();
String collectionPkName = metadata.getTools().getPrimaryKeyName(property.getRange().asClass());
String qlStr = "select e." + collectionPkName + " from " + property.getRange().asClass().getName() + " e where e." + invPropName + "." + primaryKeyName + " = ?1";
Query query = entityManager.createQuery(qlStr);
query.setParameter(1, entity.getId());
query.setMaxResults(1);
List<Entity> list = query.getResultList();
return list.isEmpty();
}
Aggregations