use of ubic.gemma.model.common.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.
the class AuditTrailDaoImpl method addEvent.
@Override
public AuditEvent addEvent(final Auditable auditable, final AuditEvent auditEvent) {
if (auditEvent.getAction() == null) {
throw new IllegalArgumentException("auditEvent was missing a required field");
}
assert auditEvent.getDate() != null;
if (auditEvent.getPerformer() == null) {
// could be null, if anonymous.
User user = getUser();
Field f = FieldUtils.getField(AuditEvent.class, "performer", true);
assert f != null;
try {
f.set(auditEvent, user);
} catch (IllegalArgumentException | IllegalAccessException e) {
// shouldn't happen, but just in case...
throw new RuntimeException(e);
}
}
AuditTrail trail = auditable.getAuditTrail();
if (trail == null) {
/*
* Note: this step should be done by the AuditAdvice when the entity was first created, so this is just
* defensive.
*/
logger.warn("AuditTrail was null. It should have been initialized by the AuditAdvice when the entity was first created.");
trail = AuditTrail.Factory.newInstance();
auditable.setAuditTrail(trail);
} else {
/*
* This assumes that nobody else in this session has modified this audit trail.
*/
if (trail.getId() != null)
trail = (AuditTrail) this.getSessionFactory().getCurrentSession().get(AuditTrailImpl.class, trail.getId());
}
trail.addEvent(auditEvent);
this.getSessionFactory().getCurrentSession().saveOrUpdate(trail);
auditable.setAuditTrail(trail);
return auditEvent;
}
use of ubic.gemma.model.common.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.
the class AuditAdviceTest method checkAuditTrail.
private void checkAuditTrail(Auditable c, Collection<Long> trailIds, Collection<Long> eventIds) {
AuditTrail auditTrail = c.getAuditTrail();
assertNotNull("No audit trail for " + c, auditTrail);
trailIds.add(auditTrail.getId());
assertTrue("Trail but no events for " + c, auditTrail.getEvents().size() > 0);
for (AuditEvent ae : auditTrail.getEvents()) {
eventIds.add(ae.getId());
}
}
use of ubic.gemma.model.common.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.
the class BibliographicReferenceServiceTest method setup.
/*
* Call to create should persist the BibliographicReference and DatabaseEntry (cascade=all).
*/
@Before
public void setup() {
testBibRef = BibliographicReference.Factory.newInstance();
de = this.getTestPersistentDatabaseEntry("PubMed");
/* Set the DatabaseEntry. */
testBibRef.setPubAccession(de);
AuditTrail ad = AuditTrail.Factory.newInstance();
ad = (AuditTrail) persisterHelper.persist(ad);
testBibRef.setAuditTrail(ad);
bibliographicReferenceService.create(testBibRef);
}
use of ubic.gemma.model.common.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.
the class AuditControllerTest method testAddUpdateEvent.
@Test
public void testAddUpdateEvent() {
ExpressionExperiment e = this.getTestPersistentExpressionExperiment();
EntityDelegator ed = new EntityDelegator(e);
assertEquals("ubic.gemma.model.expression.experiment.ExpressionExperiment", ed.getClassDelegatingFor());
auditController.addAuditEvent(ed, "CommentedEvent", "foo", "bar");
e = expressionExperimentService.load(e.getId());
assertNotNull(e);
e = expressionExperimentService.thawLite(e);
assertNotNull(e);
AuditTrail auditTrail = e.getAuditTrail();
assertNotNull(auditTrail);
AuditEvent lastEvent = auditTrail.getLast();
assertNotNull(lastEvent);
AuditEventType eventType = lastEvent.getEventType();
assertNotNull(eventType);
assertEquals(CommentedEvent.class, eventType.getClass());
}
use of ubic.gemma.model.common.auditAndSecurity.AuditTrail in project Gemma by PavlidisLab.
the class AuditEventDaoImpl method getAuditTrailMap.
/**
* Essential thawRawAndProcessed the auditables to the point we get the AuditTrail proxies for them.
*/
@SuppressWarnings("unchecked")
private Map<AuditTrail, Auditable> getAuditTrailMap(final Collection<? extends Auditable> auditables) {
/*
* This is the fastest way I've found to thawRawAndProcessed the audit trails of a whole bunch of auditables. Because Auditable
* is not mapped, we have to query for each class separately ... just in case the user has passed a
* heterogeneous collection.
*/
final Map<AuditTrail, Auditable> atMap = new HashMap<>();
Map<String, Collection<Auditable>> classMap = new HashMap<>();
for (Auditable a : auditables) {
Class<? extends Auditable> clazz = a.getClass();
/*
* proxy?
*/
String clazzName = clazz.getName();
if (a instanceof HibernateProxy) {
clazzName = ((HibernateProxy) a).getHibernateLazyInitializer().getEntityName();
}
if (!classMap.containsKey(clazzName)) {
classMap.put(clazzName, new HashSet<Auditable>());
}
classMap.get(clazzName).add(a);
}
StopWatch timer = new StopWatch();
timer.start();
HibernateTemplate template = new HibernateTemplate(this.getSessionFactory());
template.setCacheQueries(true);
template.setQueryCacheRegion("org.hibernate.cache.StandardQueryCache");
for (String clazz : classMap.keySet()) {
final String trailQuery = "select a, a.auditTrail from " + clazz + " a where a in (:auditables) ";
List<?> res = template.findByNamedParam(trailQuery, "auditables", classMap.get(clazz));
for (Object o : res) {
Object[] ar = (Object[]) o;
AuditTrail t = (AuditTrail) ar[1];
Auditable a = (Auditable) ar[0];
atMap.put(t, a);
}
timer.stop();
if (timer.getTime() > 1000) {
AbstractDao.log.info("Audit trails retrieved for " + auditables.size() + " " + clazz + " items in " + timer.getTime() + "ms");
}
timer.reset();
timer.start();
}
return atMap;
}
Aggregations