Project

General

Profile

Download (3.78 KB) Statistics
| Branch: | Tag: | Revision:
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.core.Authentication;
18
import org.springframework.security.core.context.SecurityContextHolder;
19

    
20
import eu.etaxonomy.cdm.model.agent.Team;
21
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
22
import eu.etaxonomy.cdm.model.common.CdmBase;
23
import eu.etaxonomy.cdm.model.common.ICdmBase;
24
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
25
import eu.etaxonomy.cdm.model.common.User;
26
import eu.etaxonomy.cdm.model.name.NonViralName;
27
import eu.etaxonomy.cdm.persistence.dao.hibernate.HibernateProxyHelperExtended;
28

    
29
/**
30
 * @author a.mueller
31
 * @created 04.03.2009
32
 * @version 1.0
33
 */
34
public class CacheStrategyGenerator implements SaveOrUpdateEventListener {
35
	private static final long serialVersionUID = -5511287200489449838L;
36
	@SuppressWarnings("unused")
37
	private static final Logger logger = Logger.getLogger(CacheStrategyGenerator.class);
38

    
39
    public void onSaveOrUpdate(SaveOrUpdateEvent event) throws HibernateException {
40
        Object entity = event.getObject();
41
        if (entity != null){
42
            Class<?> entityClazz = entity.getClass();
43
            if(ICdmBase.class.isAssignableFrom(entityClazz)) {
44
	            ICdmBase cdmBase = (ICdmBase)entity;
45
            	cdmBase = (ICdmBase)HibernateProxyHelperExtended.getProxyTarget(cdmBase);  //needed for debugging of integration tests that are in error by mistake 
46
            	if(cdmBase.getId() == 0) {
47
				    if (cdmBase.getCreated() == null){
48
				    	cdmBase.setCreated(new DateTime());
49
					}
50
					Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
51
				    if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
52
				      User user = (User)authentication.getPrincipal();
53
				      cdmBase.setCreatedBy(user);
54
				    }
55
				}
56
	          }
57

    
58
        	//non-viral-name caches
59
        	if(NonViralName.class.isAssignableFrom(entityClazz)) {
60
        		NonViralName<?> nonViralName = (NonViralName<?>)entity;
61
        		nonViralName.getAuthorshipCache();
62
        		nonViralName.getNameCache();
63
        		nonViralName.getTitleCache();
64
        		nonViralName.getFullTitleCache();
65
        	//team-or-person caches
66
            }else if(TeamOrPersonBase.class.isAssignableFrom(entityClazz)){
67
            	TeamOrPersonBase teamOrPerson = (TeamOrPersonBase)entity;
68
            	String nomTitle = teamOrPerson.getNomenclaturalTitle();
69
            	if (teamOrPerson instanceof Team){
70
            		Team team =CdmBase.deproxy(teamOrPerson, Team.class); 
71
            		team.setNomenclaturalTitle(nomTitle, team.isProtectedNomenclaturalTitleCache()); //nomTitle is not necessarily cached when it is created
72
            	}else{
73
            		teamOrPerson.setNomenclaturalTitle(nomTitle);
74
            	}
75
            	String titleCache = teamOrPerson.getTitleCache();
76
            	if (! teamOrPerson.isProtectedTitleCache()){
77
                	teamOrPerson.setTitleCache(titleCache, false);
78
            	}
79
 
80
            //title cache
81
            }else if(IdentifiableEntity.class.isAssignableFrom(entityClazz)) {
82
        		IdentifiableEntity identifiableEntity = (IdentifiableEntity)entity;
83
        		identifiableEntity.getTitleCache();
84
            }
85
        	
86
        }
87
    }
88
}
(1-1/10)