use of ubic.gemma.model.common.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.
the class AuditAdvice method addCreateAuditEvent.
/**
* Adds 'create' AuditEvent to audit trail of the passed AbstractAuditable.
*
* @param note Additional text to add to the automatically generated note.
*/
private void addCreateAuditEvent(final AbstractAuditable auditable, User user, final String note) {
if (this.isNullOrTransient(auditable))
return;
AuditTrail auditTrail = auditable.getAuditTrail();
this.ensureInSession(auditTrail);
if (auditTrail != null && !auditTrail.getEvents().isEmpty()) {
// while persisting parent objects. That's okay.
if (AuditAdvice.log.isDebugEnabled())
AuditAdvice.log.debug("Call to addCreateAuditEvent but the auditTrail already has events. AuditTrail id: " + auditTrail.getId());
return;
}
String details = "Create " + auditable.getClass().getSimpleName() + " " + auditable.getId() + note;
try {
auditHelper.addCreateAuditEvent(auditable, details, user);
if (AuditAdvice.log.isDebugEnabled()) {
AuditAdvice.log.debug("Audited event: " + (note.length() > 0 ? note : "[no note]") + " on " + auditable.getClass().getSimpleName() + ":" + auditable.getId() + " by " + user.getUserName());
}
} catch (UsernameNotFoundException e) {
AuditAdvice.log.warn("No user, cannot add 'create' event");
}
}
use of ubic.gemma.model.common.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.
the class AuditAdvice method addUpdateAuditEvent.
private void addUpdateAuditEvent(final AbstractAuditable auditable, User user) {
assert auditable != null;
AuditTrail auditTrail = auditable.getAuditTrail();
this.ensureInSession(auditTrail);
if (auditTrail == null || auditTrail.getEvents().isEmpty()) {
/*
* Note: This can happen for ExperimentalFactors when loading from GEO etc. because of the bidirectional
* association and the way we persist them. See ExpressionPersister. (actually this seems to be fixed...)
*/
AuditAdvice.log.error("No create event for update method call on " + auditable + ", performing 'create' instead");
this.addCreateAuditEvent(auditable, user, " - Event added on update of existing object.");
} else {
String note = "Updated " + auditable.getClass().getSimpleName() + " " + auditable.getId();
auditHelper.addUpdateAuditEvent(auditable, note, user);
if (AuditAdvice.log.isDebugEnabled()) {
AuditAdvice.log.debug("Audited event: " + note + " on " + auditable.getClass().getSimpleName() + ":" + auditable.getId() + " by " + user.getUserName());
}
}
}
use of ubic.gemma.model.common.auditAndSecurity.AuditTrail 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.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.
the class AuditEventDaoImpl method putAllQrs.
private void putAllQrs(Map<Auditable, AuditEvent> result, List<?> qr, Map<AuditTrail, Auditable> atMap) {
for (Object o : qr) {
Object[] ar = (Object[]) o;
AuditTrail t = (AuditTrail) ar[0];
AuditEvent e = (AuditEvent) ar[1];
// only one event per object, please - the most recent.
if (result.containsKey(atMap.get(t)))
continue;
result.put(atMap.get(t), e);
}
}
use of ubic.gemma.model.common.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.
the class AuditHelperImpl method addAuditTrailIfNeeded.
private void addAuditTrailIfNeeded(Auditable auditable) {
if (auditable.getAuditTrail() != null) {
auditable.getAuditTrail();
return;
}
// no need to persist it here
AuditTrail auditTrail = AuditTrail.Factory.newInstance();
auditable.setAuditTrail(auditTrail);
}
Aggregations