Project

General

Profile

Download (2.01 KB) Statistics
| Branch: | Tag: | Revision:
1 78c2bf74 Katja Luther
/**
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 5df69434 Katja Luther
*/
9 78c2bf74 Katja Luther
10 f3d694cb ben.clark
package eu.etaxonomy.cdm.persistence.hibernate;
11
12 cc355ab4 Andreas Müller
import org.apache.log4j.Logger;
13 f3d694cb ben.clark
import org.hibernate.HibernateException;
14 a13c5f66 Andreas Müller
import org.hibernate.event.spi.SaveOrUpdateEvent;
15
import org.hibernate.event.spi.SaveOrUpdateEventListener;
16 f3d694cb ben.clark
import org.joda.time.DateTime;
17 a784f00f Katja Luther
import org.springframework.security.core.Authentication;
18
import org.springframework.security.core.context.SecurityContextHolder;
19 f3d694cb ben.clark
20
import eu.etaxonomy.cdm.model.common.VersionableEntity;
21 d5579203 Andreas Müller
import eu.etaxonomy.cdm.model.permission.User;
22 bce1dda8 Katja Luther
23 f3d694cb ben.clark
24
public class UpdateEntityListener implements SaveOrUpdateEventListener {
25 cc355ab4 Andreas Müller
	private static final long serialVersionUID = -3295612929556041686L;
26
	@SuppressWarnings("unused")
27
	private static final Logger logger = Logger.getLogger(UpdateEntityListener.class);
28 f3d694cb ben.clark
29 5df69434 Katja Luther
	@Override
30
    public void onSaveOrUpdate(SaveOrUpdateEvent event)	throws HibernateException {
31 f3d694cb ben.clark
		Object entity = event.getObject();
32
		if(entity != null && VersionableEntity.class.isAssignableFrom(entity.getClass())) {
33 5df69434 Katja Luther
34 f3d694cb ben.clark
			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 793ad48e Andreas Müller
			    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 5df69434 Katja Luther
			}
45 f3d694cb ben.clark
		}
46
	}
47
}