1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.persistence.hibernate;
|
11
|
|
12
|
import org.apache.log4j.Logger;
|
13
|
import org.hibernate.HibernateException;
|
14
|
import org.hibernate.event.SaveOrUpdateEvent;
|
15
|
import org.hibernate.event.SaveOrUpdateEventListener;
|
16
|
import org.joda.time.DateTime;
|
17
|
import org.springframework.security.core.Authentication;
|
18
|
import org.springframework.security.core.context.SecurityContextHolder;
|
19
|
|
20
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
21
|
import eu.etaxonomy.cdm.model.common.User;
|
22
|
|
23
|
public class SaveEntityListener implements SaveOrUpdateEventListener {
|
24
|
private static final long serialVersionUID = -4295612947856041686L;
|
25
|
@SuppressWarnings("unused")
|
26
|
private static final Logger logger = Logger.getLogger(SaveEntityListener.class);
|
27
|
|
28
|
public void onSaveOrUpdate(SaveOrUpdateEvent event) throws HibernateException {
|
29
|
Object entity = event.getObject();
|
30
|
if (entity != null){
|
31
|
Class<?> entityClazz = entity.getClass();
|
32
|
if(ICdmBase.class.isAssignableFrom(entityClazz)) {
|
33
|
ICdmBase cdmBase = (ICdmBase)entity;
|
34
|
cdmBase.setCreated(new DateTime());
|
35
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
36
|
if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
|
37
|
User user = (User)authentication.getPrincipal();
|
38
|
cdmBase.setCreatedBy(user);
|
39
|
}
|
40
|
}
|
41
|
|
42
|
}
|
43
|
}
|
44
|
}
|