1
|
/**
|
2
|
* Copyright (C) 2007 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.Authentication;
|
18
|
import org.springframework.security.context.SecurityContextHolder;
|
19
|
|
20
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
21
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
22
|
import eu.etaxonomy.cdm.model.common.VersionableEntity;
|
23
|
import eu.etaxonomy.cdm.model.common.User;
|
24
|
import eu.etaxonomy.cdm.model.common.VersionableEntity;
|
25
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
26
|
|
27
|
/**
|
28
|
* @author a.mueller
|
29
|
* @created 04.03.2009
|
30
|
* @version 1.0
|
31
|
*/
|
32
|
public class CacheStrategyGenerator implements SaveOrUpdateEventListener {
|
33
|
private static final long serialVersionUID = -5511287200489449838L;
|
34
|
@SuppressWarnings("unused")
|
35
|
private static final Logger logger = Logger.getLogger(CacheStrategyGenerator.class);
|
36
|
|
37
|
public void onSaveOrUpdate(SaveOrUpdateEvent event) throws HibernateException {
|
38
|
Object entity = event.getObject();
|
39
|
if (entity != null){
|
40
|
Class<?> entityClazz = entity.getClass();
|
41
|
if(ICdmBase.class.isAssignableFrom(entityClazz)) {
|
42
|
ICdmBase cdmBase = (ICdmBase)entity;
|
43
|
if(cdmBase.getId() == 0) {
|
44
|
cdmBase.setCreated(new DateTime());
|
45
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
46
|
if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
|
47
|
User user = (User)authentication.getPrincipal();
|
48
|
cdmBase.setCreatedBy(user);
|
49
|
}
|
50
|
}
|
51
|
}
|
52
|
|
53
|
//title cache
|
54
|
if(IdentifiableEntity.class.isAssignableFrom(entityClazz)) {
|
55
|
IdentifiableEntity identifiableEntity = (IdentifiableEntity)entity;
|
56
|
identifiableEntity.getTitleCache();
|
57
|
}
|
58
|
|
59
|
//non-viral-name caches
|
60
|
if(NonViralName.class.isAssignableFrom(entityClazz)) {
|
61
|
NonViralName nonViralName = (NonViralName)entity;
|
62
|
nonViralName.getFullTitleCache();
|
63
|
nonViralName.getAuthorshipCache();
|
64
|
nonViralName.getNameCache();
|
65
|
}
|
66
|
|
67
|
|
68
|
}
|
69
|
|
70
|
|
71
|
}
|
72
|
}
|