1 |
90070603
|
Cherian Mathew
|
/**
|
2 |
|
|
* Copyright (C) 2015 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 |
|
|
package eu.etaxonomy.cdm.persistence.hibernate;
|
10 |
|
|
|
11 |
|
|
import org.hibernate.event.spi.PreInsertEvent;
|
12 |
|
|
import org.hibernate.event.spi.PreInsertEventListener;
|
13 |
|
|
import org.hibernate.event.spi.PreUpdateEvent;
|
14 |
|
|
import org.hibernate.event.spi.PreUpdateEventListener;
|
15 |
|
|
import org.joda.time.DateTime;
|
16 |
|
|
import org.springframework.security.core.Authentication;
|
17 |
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
18 |
|
|
|
19 |
97ee0157
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.agent.Team;
|
20 |
|
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
21 |
|
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
22 |
90070603
|
Cherian Mathew
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
23 |
97ee0157
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
24 |
90070603
|
Cherian Mathew
|
import eu.etaxonomy.cdm.model.common.User;
|
25 |
|
|
import eu.etaxonomy.cdm.model.common.VersionableEntity;
|
26 |
97ee0157
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.molecular.Amplification;
|
27 |
9dc896c9
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
28 |
97ee0157
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
|
29 |
867bd81d
|
Katja Luther
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
30 |
97ee0157
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
31 |
90070603
|
Cherian Mathew
|
|
32 |
|
|
/**
|
33 |
|
|
* @author cmathew
|
34 |
|
|
* @date 7 Jul 2015
|
35 |
|
|
*
|
36 |
|
|
*/
|
37 |
87ebfb12
|
Andreas Müller
|
public class CdmPreDataChangeListener
|
38 |
|
|
implements PreInsertEventListener, PreUpdateEventListener {
|
39 |
97ee0157
|
Andreas Müller
|
|
40 |
87ebfb12
|
Andreas Müller
|
private static final long serialVersionUID = -7581071903134036209L;
|
41 |
90070603
|
Cherian Mathew
|
|
42 |
|
|
@Override
|
43 |
|
|
public boolean onPreUpdate(PreUpdateEvent event) {
|
44 |
|
|
try {
|
45 |
|
|
Object entity = event.getEntity();
|
46 |
|
|
if (VersionableEntity.class.isAssignableFrom(entity.getClass())) {
|
47 |
|
|
VersionableEntity versionableEntity = (VersionableEntity)entity;
|
48 |
|
|
versionableEntity.setUpdated(new DateTime());
|
49 |
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
50 |
|
|
if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
|
51 |
|
|
User user = (User)authentication.getPrincipal();
|
52 |
|
|
versionableEntity.setUpdatedBy(user);
|
53 |
|
|
}
|
54 |
|
|
}
|
55 |
97ee0157
|
Andreas Müller
|
insertUpdateMerge(event.getEntity());
|
56 |
90070603
|
Cherian Mathew
|
} finally {
|
57 |
|
|
return false;
|
58 |
|
|
}
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
@Override
|
62 |
|
|
public boolean onPreInsert(PreInsertEvent event) {
|
63 |
|
|
try {
|
64 |
|
|
Object entity = event.getEntity();
|
65 |
|
|
Class<?> entityClazz = entity.getClass();
|
66 |
|
|
if(ICdmBase.class.isAssignableFrom(entityClazz)) {
|
67 |
|
|
ICdmBase cdmBase = (ICdmBase)entity;
|
68 |
|
|
|
69 |
|
|
if (cdmBase.getCreated() == null){
|
70 |
|
|
cdmBase.setCreated(new DateTime());
|
71 |
|
|
}
|
72 |
|
|
if(cdmBase.getCreatedBy() == null) {
|
73 |
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
74 |
|
|
if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
|
75 |
|
|
User user = (User)authentication.getPrincipal();
|
76 |
|
|
cdmBase.setCreatedBy(user);
|
77 |
|
|
}
|
78 |
|
|
}
|
79 |
|
|
}
|
80 |
97ee0157
|
Andreas Müller
|
insertUpdateMerge(entity);
|
81 |
90070603
|
Cherian Mathew
|
} finally {
|
82 |
|
|
return false;
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
}
|
86 |
|
|
|
87 |
97ee0157
|
Andreas Müller
|
//from former SaveOrUpdateOrMergeEntityListener
|
88 |
|
|
public static void insertUpdateMerge(Object entity){
|
89 |
|
|
if(entity != null && CdmBase.class.isAssignableFrom(entity.getClass())){
|
90 |
|
|
cacheDeterminationNames(entity);
|
91 |
|
|
generateCaches(entity);
|
92 |
b467476c
|
Katja Luther
|
|
93 |
97ee0157
|
Andreas Müller
|
}
|
94 |
|
|
}
|
95 |
|
|
|
96 |
5f7cb15c
|
Katja Luther
|
|
97 |
|
|
|
98 |
97ee0157
|
Andreas Müller
|
private static void cacheDeterminationNames(Object entity) {
|
99 |
|
|
if (entity instanceof DeterminationEvent) {
|
100 |
|
|
DeterminationEvent detEv = (DeterminationEvent)entity;
|
101 |
|
|
if (detEv.getTaxon() != null && detEv.getTaxonName() == null && detEv.getTaxon().getName() != null){
|
102 |
|
|
detEv.setTaxonName(detEv.getTaxon().getName());
|
103 |
|
|
}
|
104 |
|
|
}
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
public static void generateCaches(Object entity){
|
109 |
|
|
if (entity != null){
|
110 |
3c6c0d2c
|
Andreas Müller
|
entity = CdmBase.deproxy(entity);
|
111 |
97ee0157
|
Andreas Müller
|
Class<?> entityClazz = entity.getClass();
|
112 |
|
|
|
113 |
4010865d
|
Andreas Müller
|
if (IdentifiableEntity.class.isAssignableFrom(entityClazz)){
|
114 |
d818257c
|
Andreas Müller
|
IdentifiableEntity<?> identifiableEntity = (IdentifiableEntity<?>)entity;
|
115 |
9dc896c9
|
Andreas Müller
|
if(TaxonName.class.isAssignableFrom(entityClazz)) {
|
116 |
e6d7b501
|
Andreas Müller
|
//non-viral-name caches (was NonViralName)
|
117 |
9dc896c9
|
Andreas Müller
|
TaxonName nonViralName = (TaxonName)entity;
|
118 |
4010865d
|
Andreas Müller
|
nonViralName.getAuthorshipCache();
|
119 |
|
|
nonViralName.getNameCache();
|
120 |
|
|
nonViralName.getTitleCache();
|
121 |
|
|
nonViralName.getFullTitleCache();
|
122 |
|
|
}else if(TeamOrPersonBase.class.isAssignableFrom(entityClazz)){
|
123 |
a4c9dfc9
|
Andreas Müller
|
//team-or-person caches
|
124 |
4010865d
|
Andreas Müller
|
TeamOrPersonBase<?> teamOrPerson = (TeamOrPersonBase<?>)entity;
|
125 |
|
|
String nomTitle = teamOrPerson.getNomenclaturalTitle();
|
126 |
|
|
if (teamOrPerson instanceof Team){
|
127 |
d818257c
|
Andreas Müller
|
Team team = (Team)teamOrPerson;
|
128 |
3c6c0d2c
|
Andreas Müller
|
//nomTitle is not necessarily cached when it is created
|
129 |
|
|
team.setNomenclaturalTitle(nomTitle, team.isProtectedNomenclaturalTitleCache());
|
130 |
4010865d
|
Andreas Müller
|
}else{
|
131 |
|
|
teamOrPerson.setNomenclaturalTitle(nomTitle);
|
132 |
|
|
}
|
133 |
|
|
String titleCache = teamOrPerson.getTitleCache();
|
134 |
|
|
if (! teamOrPerson.isProtectedTitleCache()){
|
135 |
|
|
teamOrPerson.setTitleCache(titleCache, false);
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
//reference caches
|
139 |
|
|
}else if(Reference.class.isAssignableFrom(entityClazz)){
|
140 |
531fc7bc
|
Andreas Müller
|
Reference ref = (Reference)entity;
|
141 |
4010865d
|
Andreas Müller
|
ref.getAbbrevTitleCache();
|
142 |
|
|
ref.getTitleCache();
|
143 |
958adbe2
|
Andreas Müller
|
//specimen
|
144 |
867bd81d
|
Katja Luther
|
}else if (SpecimenOrObservationBase.class.isAssignableFrom(entityClazz)){
|
145 |
2fd31c0f
|
Andreas Müller
|
SpecimenOrObservationBase<?> specimen = (SpecimenOrObservationBase<?>)entity;
|
146 |
e8b30a54
|
Katja Luther
|
if (!specimen.isProtectedTitleCache()){
|
147 |
958adbe2
|
Andreas Müller
|
specimen.setTitleCache(specimen.generateTitle(), false);
|
148 |
e8b30a54
|
Katja Luther
|
}
|
149 |
958adbe2
|
Andreas Müller
|
//any other
|
150 |
|
|
} else{
|
151 |
867bd81d
|
Katja Luther
|
// identifiableEntity.setTitleCache(identifiableEntity.generateTitle(), identifiableEntity.isProtectedTitleCache());
|
152 |
|
|
identifiableEntity.getTitleCache();
|
153 |
97ee0157
|
Andreas Müller
|
}
|
154 |
958adbe2
|
Andreas Müller
|
|
155 |
826c86cb
|
Andreas Müller
|
//titleCache should never be empty, even if protected #5763, #5849
|
156 |
4010865d
|
Andreas Müller
|
if (identifiableEntity.isProtectedTitleCache() && identifiableEntity.hasEmptyTitleCache()){
|
157 |
|
|
identifiableEntity.setTitleCache(null, false);
|
158 |
|
|
identifiableEntity.getTitleCache();
|
159 |
97ee0157
|
Andreas Müller
|
}
|
160 |
826c86cb
|
Andreas Müller
|
if (identifiableEntity.hasEmptyTitleCache()){
|
161 |
|
|
identifiableEntity.setTitleCache(identifiableEntity.toString(), false);
|
162 |
|
|
}
|
163 |
|
|
|
164 |
97ee0157
|
Andreas Müller
|
|
165 |
|
|
}else if(Amplification.class.isAssignableFrom(entityClazz)) {
|
166 |
|
|
Amplification amplification = (Amplification)entity;
|
167 |
|
|
amplification.updateCache();
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
}
|
171 |
|
|
|
172 |
90070603
|
Cherian Mathew
|
}
|