Search in sources :

Example 6 with AuditTrail

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;
}
Also used : Field(java.lang.reflect.Field) User(ubic.gemma.model.common.auditAndSecurity.User) AuditTrailImpl(ubic.gemma.model.common.auditAndSecurity.AuditTrailImpl) AuditTrail(ubic.gemma.model.common.auditAndSecurity.AuditTrail)

Example 7 with AuditTrail

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());
    }
}
Also used : AuditEvent(ubic.gemma.model.common.auditAndSecurity.AuditEvent) AuditTrail(ubic.gemma.model.common.auditAndSecurity.AuditTrail)

Example 8 with AuditTrail

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);
}
Also used : AuditTrail(ubic.gemma.model.common.auditAndSecurity.AuditTrail) Before(org.junit.Before)

Example 9 with AuditTrail

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());
}
Also used : AuditEventType(ubic.gemma.model.common.auditAndSecurity.eventType.AuditEventType) AuditEvent(ubic.gemma.model.common.auditAndSecurity.AuditEvent) AuditTrail(ubic.gemma.model.common.auditAndSecurity.AuditTrail) ExpressionExperiment(ubic.gemma.model.expression.experiment.ExpressionExperiment) Test(org.junit.Test) BaseSpringWebTest(ubic.gemma.web.util.BaseSpringWebTest)

Example 10 with AuditTrail

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;
}
Also used : Auditable(ubic.gemma.model.common.Auditable) HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) HibernateProxy(org.hibernate.proxy.HibernateProxy) StopWatch(org.apache.commons.lang3.time.StopWatch) AuditEventValueObject(ubic.gemma.model.common.auditAndSecurity.AuditEventValueObject) AuditTrail(ubic.gemma.model.common.auditAndSecurity.AuditTrail)

Aggregations

AuditTrail (ubic.gemma.model.common.auditAndSecurity.AuditTrail)12 AuditEvent (ubic.gemma.model.common.auditAndSecurity.AuditEvent)5 StopWatch (org.apache.commons.lang3.time.StopWatch)3 Auditable (ubic.gemma.model.common.Auditable)3 AuditEventValueObject (ubic.gemma.model.common.auditAndSecurity.AuditEventValueObject)3 Query (org.hibernate.Query)2 AuditEventType (ubic.gemma.model.common.auditAndSecurity.eventType.AuditEventType)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 JoinPoint (org.aspectj.lang.JoinPoint)1 LazyInitializationException (org.hibernate.LazyInitializationException)1 CascadeStyle (org.hibernate.engine.CascadeStyle)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 HibernateProxy (org.hibernate.proxy.HibernateProxy)1 Before (org.junit.Before)1 Test (org.junit.Test)1 HibernateTemplate (org.springframework.orm.hibernate3.HibernateTemplate)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1