Project

General

Profile

Download (2 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 java.time.ZonedDateTime;
13

    
14
import org.apache.log4j.Logger;
15
import org.hibernate.HibernateException;
16
import org.hibernate.event.spi.SaveOrUpdateEvent;
17
import org.hibernate.event.spi.SaveOrUpdateEventListener;
18
import org.springframework.security.core.Authentication;
19
import org.springframework.security.core.context.SecurityContextHolder;
20

    
21
import eu.etaxonomy.cdm.model.common.User;
22
import eu.etaxonomy.cdm.model.common.VersionableEntity;
23

    
24

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

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

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

    
48

    
49
	}
50
}
(20-20/21)