1
|
package eu.etaxonomy.cdm.persistence.hibernate;
|
2
|
|
3
|
import org.apache.log4j.Logger;
|
4
|
import org.hibernate.HibernateException;
|
5
|
import org.hibernate.event.SaveOrUpdateEvent;
|
6
|
import org.hibernate.event.SaveOrUpdateEventListener;
|
7
|
import org.joda.time.DateTime;
|
8
|
import org.springframework.security.Authentication;
|
9
|
import org.springframework.security.context.SecurityContextHolder;
|
10
|
|
11
|
import eu.etaxonomy.cdm.model.common.User;
|
12
|
import eu.etaxonomy.cdm.model.common.VersionableEntity;
|
13
|
|
14
|
public class UpdateEntityListener implements SaveOrUpdateEventListener {
|
15
|
private static final long serialVersionUID = -3295612929556041686L;
|
16
|
@SuppressWarnings("unused")
|
17
|
private static final Logger logger = Logger.getLogger(UpdateEntityListener.class);
|
18
|
|
19
|
public void onSaveOrUpdate(SaveOrUpdateEvent event)
|
20
|
throws HibernateException {
|
21
|
Object entity = event.getObject();
|
22
|
if(entity != null && VersionableEntity.class.isAssignableFrom(entity.getClass())) {
|
23
|
VersionableEntity versionableEntity = (VersionableEntity)entity;
|
24
|
versionableEntity.setUpdated(new DateTime());
|
25
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
26
|
if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
|
27
|
User user = (User)authentication.getPrincipal();
|
28
|
versionableEntity.setUpdatedBy(user);
|
29
|
}
|
30
|
}
|
31
|
}
|
32
|
}
|