use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class ExcludeAction method checkRemovePermission.
@Override
protected boolean checkRemovePermission() {
CollectionDatasource ds = target.getDatasource();
if (ds instanceof PropertyDatasource) {
PropertyDatasource propertyDatasource = (PropertyDatasource) ds;
MetaClass parentMetaClass = propertyDatasource.getMaster().getMetaClass();
MetaProperty metaProperty = propertyDatasource.getProperty();
boolean attrPermitted = security.isEntityAttrPermitted(parentMetaClass, metaProperty.getName(), EntityAttrAccess.MODIFY);
if (!attrPermitted) {
return false;
}
}
return true;
}
use of com.haulmont.chile.core.model.MetaProperty 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.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class DeletePolicyProcessor method processOnDeleteInverse.
protected void processOnDeleteInverse(List<MetaProperty> properties) {
for (MetaProperty property : properties) {
MetaClass metaClass = property.getDomain();
List<MetaClass> persistentEntities = new ArrayList<>();
if (isPersistent(metaClass))
persistentEntities.add(metaClass);
for (MetaClass descendant : metaClass.getDescendants()) {
if (isPersistent(descendant))
persistentEntities.add(descendant);
}
for (MetaClass persistentEntity : persistentEntities) {
OnDeleteInverse annotation = property.getAnnotatedElement().getAnnotation(OnDeleteInverse.class);
DeletePolicy deletePolicy = annotation.value();
switch(deletePolicy) {
case DENY:
if (referenceExists(persistentEntity.getName(), property))
throw new DeletePolicyException(this.metaClass.getName(), persistentEntity.getName());
break;
case CASCADE:
cascade(persistentEntity.getName(), property);
break;
case UNLINK:
unlink(persistentEntity.getName(), property);
break;
}
}
}
}
use of com.haulmont.chile.core.model.MetaProperty 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();
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class EclipseLinkSessionEventListener method preLogin.
@Override
public void preLogin(SessionEvent event) {
Session session = event.getSession();
setPrintInnerJoinOnClause(session);
Map<Class, ClassDescriptor> descriptorMap = session.getDescriptors();
for (Map.Entry<Class, ClassDescriptor> entry : descriptorMap.entrySet()) {
MetaClass metaClass = metadata.getSession().getClassNN(entry.getKey());
ClassDescriptor desc = entry.getValue();
setCacheable(metaClass, desc, session);
if (Entity.class.isAssignableFrom(desc.getJavaClass())) {
// set DescriptorEventManager that doesn't invoke listeners for base classes
desc.setEventManager(new DescriptorEventManager() {
@Override
public void notifyListeners(DescriptorEvent event) {
if (hasAnyListeners()) {
for (int index = 0; index < getEventListeners().size(); index++) {
DescriptorEventListener listener = (DescriptorEventListener) getEventListeners().get(index);
notifyListener(listener, event);
}
}
}
});
desc.getEventManager().addListener(descriptorEventListener);
}
if (SoftDelete.class.isAssignableFrom(desc.getJavaClass())) {
desc.getQueryManager().setAdditionalCriteria("this.deleteTs is null");
desc.setDeletePredicate(entity -> entity instanceof SoftDelete && PersistenceHelper.isLoaded(entity, "deleteTs") && ((SoftDelete) entity).isDeleted());
}
List<DatabaseMapping> mappings = desc.getMappings();
for (DatabaseMapping mapping : mappings) {
// support UUID
String attributeName = mapping.getAttributeName();
MetaProperty metaProperty = metaClass.getPropertyNN(attributeName);
if (metaProperty.getRange().isDatatype()) {
if (metaProperty.getJavaType().equals(UUID.class)) {
((DirectToFieldMapping) mapping).setConverter(UuidConverter.getInstance());
setDatabaseFieldParameters(session, mapping.getField());
}
} else if (metaProperty.getRange().isClass() && !metaProperty.getRange().getCardinality().isMany()) {
MetaClass refMetaClass = metaProperty.getRange().asClass();
MetaProperty refPkProperty = metadata.getTools().getPrimaryKeyProperty(refMetaClass);
if (refPkProperty != null && refPkProperty.getJavaType().equals(UUID.class)) {
for (DatabaseField field : ((OneToOneMapping) mapping).getForeignKeyFields()) {
setDatabaseFieldParameters(session, field);
}
}
}
// embedded attributes
if (mapping instanceof AggregateObjectMapping) {
EmbeddedParameters embeddedParameters = metaProperty.getAnnotatedElement().getAnnotation(EmbeddedParameters.class);
if (embeddedParameters != null && !embeddedParameters.nullAllowed())
((AggregateObjectMapping) mapping).setIsNullAllowed(false);
}
if (mapping.isOneToManyMapping()) {
OneToManyMapping oneToManyMapping = (OneToManyMapping) mapping;
if (SoftDelete.class.isAssignableFrom(oneToManyMapping.getReferenceClass())) {
oneToManyMapping.setAdditionalJoinCriteria(new ExpressionBuilder().get("deleteTs").isNull());
}
}
if (mapping.isOneToOneMapping()) {
OneToOneMapping oneToOneMapping = (OneToOneMapping) mapping;
if (SoftDelete.class.isAssignableFrom(oneToOneMapping.getReferenceClass())) {
if (mapping.isManyToOneMapping()) {
oneToOneMapping.setSoftDeletionForBatch(false);
oneToOneMapping.setSoftDeletionForValueHolder(false);
} else {
OneToOne oneToOne = metaProperty.getAnnotatedElement().getAnnotation(OneToOne.class);
if (oneToOne != null) {
if (Strings.isNullOrEmpty(oneToOne.mappedBy())) {
oneToOneMapping.setSoftDeletionForBatch(false);
oneToOneMapping.setSoftDeletionForValueHolder(false);
} else {
oneToOneMapping.setAdditionalJoinCriteria(new ExpressionBuilder().get("deleteTs").isNull());
}
}
}
}
}
}
}
}
Aggregations