use of ubic.gemma.model.common.Auditable in project Gemma by PavlidisLab.
the class AuditEventDaoImpl method retainHavingEvent.
@Override
public void retainHavingEvent(final Collection<? extends Auditable> a, final Class<? extends AuditEventType> type) {
final Map<Auditable, AuditEvent> events = this.getLastEvent(a, type);
CollectionUtils.filter(a, new Predicate() {
@Override
public boolean evaluate(Object arg0) {
// noinspection SuspiciousMethodCalls // this is perfectly fine since we are passing this directly into the filter
return events.containsKey(arg0);
}
});
}
use of ubic.gemma.model.common.Auditable 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;
}
use of ubic.gemma.model.common.Auditable in project Gemma by PavlidisLab.
the class AuditEventDaoImpl method getLastEvent.
private Map<Auditable, AuditEvent> getLastEvent(final Collection<? extends Auditable> auditables, Class<? extends AuditEventType> type) {
Map<Auditable, AuditEvent> result = new HashMap<>();
if (auditables.size() == 0)
return result;
final Map<AuditTrail, Auditable> atMap = this.getAuditTrailMap(auditables);
List<String> classes = this.getClassHierarchy(type);
// language=HQL
final String queryString = "select trail, ae from AuditTrailImpl trail " + "inner join trail.events ae inner join ae.eventType et inner join fetch ae.performer where trail in (:trails) " + "and et.class in (:classes) order by ae.date desc, ae.id desc ";
StopWatch timer = new StopWatch();
timer.start();
Collection<AuditTrail> batch = new ArrayList<>();
int batchSize = 100;
for (AuditTrail at : atMap.keySet()) {
batch.add(at);
if (batch.size() == batchSize) {
org.hibernate.Query queryObject = this.getSessionFactory().getCurrentSession().createQuery(queryString);
queryObject.setParameterList("trails", batch);
queryObject.setParameterList("classes", classes);
queryObject.setReadOnly(true);
List<?> qr = queryObject.list();
if (qr == null || qr.isEmpty()) {
batch.clear();
continue;
}
this.putAllQrs(result, qr, atMap);
batch.clear();
}
}
if (!batch.isEmpty()) {
org.hibernate.Query queryObject = this.getSessionFactory().getCurrentSession().createQuery(queryString);
// if too many will fail.
queryObject.setParameterList("trails", batch);
queryObject.setParameterList("classes", classes);
queryObject.setReadOnly(true);
List<?> qr = queryObject.list();
if (qr == null || qr.isEmpty())
return result;
this.putAllQrs(result, qr, atMap);
}
timer.stop();
if (timer.getTime() > 500) {
AbstractDao.log.info("Last event of type " + type.getSimpleName() + " retrieved for " + auditables.size() + " items in " + timer.getTime() + "ms");
}
return result;
}
use of ubic.gemma.model.common.Auditable in project Gemma by PavlidisLab.
the class AuditEventDaoImpl method getLastEvents.
@Override
public Map<Class<? extends AuditEventType>, Map<Auditable, AuditEvent>> getLastEvents(Collection<? extends Auditable> auditables, Collection<Class<? extends AuditEventType>> types) {
StopWatch timer = new StopWatch();
timer.start();
Map<Class<? extends AuditEventType>, Map<Auditable, AuditEvent>> results = new HashMap<>();
if (auditables.size() == 0)
return results;
for (Class<? extends AuditEventType> t : types) {
results.put(t, new HashMap<Auditable, AuditEvent>());
}
final Map<AuditTrail, Auditable> atMap = this.getAuditTrailMap(auditables);
List<String> classes = this.getClassHierarchy(types);
// language=HQL
final String queryString = "select et, trail, event from AuditTrailImpl trail " + "inner join trail.events event inner join event.eventType et inner join fetch event.performer where trail in (:trails) " + "and et.class in (:classes) order by event.date desc, event.id desc ";
Query queryObject = this.getSessionFactory().getCurrentSession().createQuery(queryString);
queryObject.setParameterList("trails", atMap.keySet());
queryObject.setParameterList("classes", classes);
List<?> qr = queryObject.list();
for (Object o : qr) {
Object[] ar = (Object[]) o;
AuditEventType ty = (AuditEventType) ar[0];
AuditTrail t = (AuditTrail) ar[1];
AuditEvent e = (AuditEvent) ar[2];
/*
* This is a bit inefficient. Loop needed because returned type is Impl (and probably a proxy). But probably
* query is the bottleneck.
*/
for (Class<? extends AuditEventType> ti : types) {
if (ti.isAssignableFrom(ty.getClass())) {
Map<Auditable, AuditEvent> innerMap = results.get(ti);
assert innerMap != null;
// only replace event if its date is more recent.
Auditable ae = atMap.get(t);
if (!innerMap.containsKey(ae) || innerMap.get(ae).getDate().compareTo(e.getDate()) < 0) {
innerMap.put(atMap.get(t), e);
}
break;
}
}
}
timer.stop();
if (timer.getTime() > 1000) {
AbstractDao.log.info("Last events retrieved for " + types.size() + " different types for " + auditables.size() + " items in " + timer.getTime() + "ms");
}
return results;
}
use of ubic.gemma.model.common.Auditable in project Gemma by PavlidisLab.
the class AuditController method getEvents.
public Collection<AuditEventValueObject> getEvents(EntityDelegator e) {
Collection<AuditEventValueObject> result = new HashSet<>();
Auditable entity = this.getAuditable(e);
if (entity == null) {
return result;
}
assert entity.getAuditTrail().getId() != null;
Collection<AuditEvent> events = auditEventService.getEvents(entity);
for (AuditEvent ev : events) {
if (ev == null)
continue;
/*
* Hide generic update events.
*/
if (ev.getAction().equals(AuditAction.UPDATE) && ev.getEventType() == null)
continue;
result.add(new AuditEventValueObject(ev));
}
return result;
}
Aggregations