Project

General

Profile

Download (2.01 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.spi.SaveOrUpdateEvent;
15
import org.hibernate.event.spi.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

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

    
29
	@Override
30
    public void onSaveOrUpdate(SaveOrUpdateEvent event)	throws HibernateException {
31
		Object entity = event.getObject();
32
		if(entity != null && VersionableEntity.class.isAssignableFrom(entity.getClass())) {
33

    
34
			VersionableEntity versionableEntity = (VersionableEntity)entity;
35
			versionableEntity.setUpdated(new DateTime());
36
			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
37
			if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
38
			    User user = (User)authentication.getPrincipal();
39
			    versionableEntity.setUpdatedBy(user);
40
    			/*CdmPermissionEvaluator permissionEvaluator = new CdmPermissionEvaluator();
41
    			if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), entity, CdmPermission.UPDATE)){
42
    					throw new EvaluationFailedException("Permission evaluation failed for " + event.getEntity());
43
    			}*/
44
			}
45
		}
46
	}
47
}
(22-22/23)