use of org.eclipse.persistence.descriptors.DescriptorEventListener 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());
}
}
}
}
}
}
}
}
use of org.eclipse.persistence.descriptors.DescriptorEventListener in project OpenAttestation by OpenAttestation.
the class AuditEventHandler method customize.
@Override
public void customize(ClassDescriptor cd) throws Exception {
try {
// at runtime... load the audit event handler implementation from audit-handler package
Class t = Class.forName("com.intel.mtwilson.audit.handler.AuditEventHandlerImpl");
Object impl = t.newInstance();
DescriptorEventListener listener = (DescriptorEventListener) impl;
cd.getDescriptorEventManager().addListener(listener);
} catch (Exception e) {
log.error("Cannot attach audit listener: " + e.toString(), e);
}
}
Aggregations