use of ubic.gemma.model.common.AbstractAuditable in project Gemma by PavlidisLab.
the class AuditController method getAuditable.
private AbstractAuditable getAuditable(EntityDelegator e) {
if (e == null || e.getId() == null)
return null;
if (e.getClassDelegatingFor() == null)
return null;
Class<?> clazz;
AbstractAuditable result;
try {
clazz = Class.forName(e.getClassDelegatingFor());
} catch (ClassNotFoundException e1) {
throw new RuntimeException(e1);
}
if (ExpressionExperiment.class.isAssignableFrom(clazz)) {
result = expressionExperimentService.load(e.getId());
} else if (ArrayDesign.class.isAssignableFrom(clazz)) {
result = arrayDesignService.load(e.getId());
} else {
AuditController.log.warn("We don't support that class yet, sorry");
return null;
}
if (result == null) {
AuditController.log.warn("Entity with id = " + e.getId() + " not found");
}
return result;
}
use of ubic.gemma.model.common.AbstractAuditable in project Gemma by PavlidisLab.
the class AuditAdvice method processAssociations.
/**
* Fills in audit trails on newly created child objects after a 'create' or 'update'. It does not add 'update'
* events on the child objects.
* Thus if the update is on an expression experiment that has a new Characteristic, the Characteristic will have a
* 'create' event, and the EEE will get an added update event (via the addUpdateAuditEvent call elsewhere, not here)
*
* @see AclAdvice for similar code for ACLs
*/
private void processAssociations(String methodName, Object object, User user) {
if (object instanceof AuditTrail)
// don't audit audit trails.
return;
EntityPersister persister = crudUtils.getEntityPersister(object);
if (persister == null) {
throw new IllegalArgumentException("No persister found for " + object.getClass().getName());
}
CascadeStyle[] cascadeStyles = persister.getPropertyCascadeStyles();
String[] propertyNames = persister.getPropertyNames();
try {
for (int j = 0; j < propertyNames.length; j++) {
CascadeStyle cs = cascadeStyles[j];
String propertyName = propertyNames[j];
if (!this.specialCaseForAssociationFollow(object, propertyName) && (this.canSkipAssociationCheck(object, propertyName) || !crudUtils.needCascade(methodName, cs))) {
continue;
}
PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(object.getClass(), propertyName);
Object associatedObject = ReflectionUtil.getProperty(object, descriptor);
if (associatedObject == null)
continue;
Class<?> propertyType = descriptor.getPropertyType();
if (AbstractAuditable.class.isAssignableFrom(propertyType)) {
AbstractAuditable auditable = (AbstractAuditable) associatedObject;
try {
this.maybeAddCascadeCreateEvent(object, auditable, user);
this.processAssociations(methodName, auditable, user);
} catch (LazyInitializationException e) {
// be necessary.
if (AuditAdvice.log.isDebugEnabled())
AuditAdvice.log.debug("Caught lazy init error while processing " + auditable + ": " + e.getMessage() + " - skipping creation of cascade event.");
}
} else if (Collection.class.isAssignableFrom(propertyType)) {
Collection<?> associatedObjects = (Collection<?>) associatedObject;
try {
Hibernate.initialize(associatedObjects);
for (Object collectionMember : associatedObjects) {
if (AbstractAuditable.class.isAssignableFrom(collectionMember.getClass())) {
AbstractAuditable auditable = (AbstractAuditable) collectionMember;
try {
Hibernate.initialize(auditable);
this.maybeAddCascadeCreateEvent(object, auditable, user);
this.processAssociations(methodName, collectionMember, user);
} catch (LazyInitializationException e) {
if (AuditAdvice.log.isDebugEnabled())
AuditAdvice.log.debug("Caught lazy init error while processing " + auditable + ": " + e.getMessage() + " - skipping creation of cascade event.");
// If this happens, it means the object can't be 'new' so adding audit trail can't
// be necessary. But keep checking.
}
}
}
} catch (LazyInitializationException e) {
// be necessary.
if (AuditAdvice.log.isDebugEnabled())
AuditAdvice.log.debug("Caught lazy init error while processing " + object + ": " + e.getMessage() + " - skipping creation of cascade event.");
}
}
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
use of ubic.gemma.model.common.AbstractAuditable in project Gemma by PavlidisLab.
the class AuditAdvice method doAuditAdvice.
/**
* Entry point. This only takes action if the method involves AbstractAuditables.
*/
public void doAuditAdvice(JoinPoint pjp, Object retValue) {
final Signature signature = pjp.getSignature();
final String methodName = signature.getName();
final Object[] args = pjp.getArgs();
Object object = this.getPersistentObject(retValue, methodName, args);
if (object == null)
return;
User user = userManager.getCurrentUser();
if (user == null) {
AuditAdvice.log.info("User could not be determined (anonymous?), audit will be skipped.");
return;
}
if (object instanceof Collection) {
for (final Object o : (Collection<?>) object) {
if (AbstractAuditable.class.isAssignableFrom(o.getClass())) {
this.process(methodName, (AbstractAuditable) o, user);
}
}
} else if ((AbstractAuditable.class.isAssignableFrom(object.getClass()))) {
this.process(methodName, (AbstractAuditable) object, user);
}
}
use of ubic.gemma.model.common.AbstractAuditable in project Gemma by PavlidisLab.
the class AuditController method addAuditEvent.
@SuppressWarnings("unchecked")
public void addAuditEvent(EntityDelegator e, String auditEventType, String comment, String detail) {
AbstractAuditable entity = this.getAuditable(e);
if (entity == null) {
AuditController.log.warn("Couldn't find Auditable represented by " + e);
return;
}
Class<?> clazz;
try {
clazz = Class.forName("ubic.gemma.model.common.auditAndSecurity.eventType." + auditEventType);
} catch (ClassNotFoundException e1) {
throw new RuntimeException("Unknown event type: " + auditEventType);
}
AuditEvent auditEvent = auditTrailService.addUpdateEvent(entity, (Class<? extends AuditEventType>) clazz, comment, detail);
if (auditEvent == null) {
AuditController.log.error("Persisting the audit event failed! On auditable id " + entity.getId());
} else {
AuditController.log.info("created new event: " + auditEvent);
}
}
Aggregations