Project

General

Profile

Download (1.65 KB) Statistics
| Branch: | Tag: | Revision:
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.User;
21
import eu.etaxonomy.cdm.model.common.VersionableEntity;
22

    
23
public class UpdateEntityListener implements SaveOrUpdateEventListener {
24
	private static final long serialVersionUID = -3295612929556041686L;
25
	@SuppressWarnings("unused")
26
	private static final Logger logger = Logger.getLogger(UpdateEntityListener.class);
27

    
28
	public void onSaveOrUpdate(SaveOrUpdateEvent event)	throws HibernateException {
29
		Object entity = event.getObject();
30
		if(entity != null && VersionableEntity.class.isAssignableFrom(entity.getClass())) {
31
			VersionableEntity versionableEntity = (VersionableEntity)entity;
32
			versionableEntity.setUpdated(new DateTime());
33
			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
34
			if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
35
			  User user = (User)authentication.getPrincipal();
36
			  versionableEntity.setUpdatedBy(user);
37
			} 
38
		}
39
	}
40
}
(10-10/10)