1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2015 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.persistence.hibernate;
|
11
|
|
12
|
import java.util.List;
|
13
|
|
14
|
import org.hibernate.event.spi.PreInsertEvent;
|
15
|
import org.hibernate.event.spi.PreInsertEventListener;
|
16
|
import org.hibernate.event.spi.PreUpdateEvent;
|
17
|
import org.hibernate.event.spi.PreUpdateEventListener;
|
18
|
import org.joda.time.DateTime;
|
19
|
import org.springframework.security.core.Authentication;
|
20
|
import org.springframework.security.core.context.SecurityContextHolder;
|
21
|
|
22
|
import eu.etaxonomy.cdm.model.agent.Team;
|
23
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
24
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
25
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
26
|
import eu.etaxonomy.cdm.model.common.ITreeNode;
|
27
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
28
|
import eu.etaxonomy.cdm.model.common.User;
|
29
|
import eu.etaxonomy.cdm.model.common.VersionableEntity;
|
30
|
import eu.etaxonomy.cdm.model.molecular.Amplification;
|
31
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
32
|
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
|
33
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
34
|
|
35
|
/**
|
36
|
* @author cmathew
|
37
|
* @date 7 Jul 2015
|
38
|
*
|
39
|
*/
|
40
|
public class CdmPreDataChangeObservableListener implements PreInsertEventListener, PreUpdateEventListener {
|
41
|
private static final long serialVersionUID = -7581071903134036209L;
|
42
|
|
43
|
static String sep = ITreeNode.separator;
|
44
|
static String pref = ITreeNode.treePrefix;
|
45
|
|
46
|
@Override
|
47
|
public boolean onPreUpdate(PreUpdateEvent event) {
|
48
|
try {
|
49
|
Object entity = event.getEntity();
|
50
|
if (VersionableEntity.class.isAssignableFrom(entity.getClass())) {
|
51
|
VersionableEntity versionableEntity = (VersionableEntity)entity;
|
52
|
versionableEntity.setUpdated(new DateTime());
|
53
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
54
|
if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
|
55
|
User user = (User)authentication.getPrincipal();
|
56
|
versionableEntity.setUpdatedBy(user);
|
57
|
}
|
58
|
}
|
59
|
insertUpdateMerge(event.getEntity());
|
60
|
} finally {
|
61
|
return false;
|
62
|
}
|
63
|
}
|
64
|
|
65
|
@Override
|
66
|
public boolean onPreInsert(PreInsertEvent event) {
|
67
|
try {
|
68
|
Object entity = event.getEntity();
|
69
|
Class<?> entityClazz = entity.getClass();
|
70
|
if(ICdmBase.class.isAssignableFrom(entityClazz)) {
|
71
|
ICdmBase cdmBase = (ICdmBase)entity;
|
72
|
|
73
|
if (cdmBase.getCreated() == null){
|
74
|
cdmBase.setCreated(new DateTime());
|
75
|
}
|
76
|
if(cdmBase.getCreatedBy() == null) {
|
77
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
78
|
if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
|
79
|
User user = (User)authentication.getPrincipal();
|
80
|
cdmBase.setCreatedBy(user);
|
81
|
}
|
82
|
}
|
83
|
}
|
84
|
insertUpdateMerge(entity);
|
85
|
} finally {
|
86
|
return false;
|
87
|
}
|
88
|
|
89
|
}
|
90
|
|
91
|
//from former SaveOrUpdateOrMergeEntityListener
|
92
|
public static void insertUpdateMerge(Object entity){
|
93
|
if(entity != null && CdmBase.class.isAssignableFrom(entity.getClass())){
|
94
|
generateTreeIndex(entity);
|
95
|
cacheDeterminationNames(entity);
|
96
|
generateCaches(entity);
|
97
|
}
|
98
|
}
|
99
|
|
100
|
private static void cacheDeterminationNames(Object entity) {
|
101
|
if (entity instanceof DeterminationEvent) {
|
102
|
DeterminationEvent detEv = (DeterminationEvent)entity;
|
103
|
if (detEv.getTaxon() != null && detEv.getTaxonName() == null && detEv.getTaxon().getName() != null){
|
104
|
detEv.setTaxonName(detEv.getTaxon().getName());
|
105
|
}
|
106
|
}
|
107
|
}
|
108
|
|
109
|
private static void generateTreeIndex(Object entity) {
|
110
|
if (entity instanceof ITreeNode) {
|
111
|
ITreeNode<?> node = (ITreeNode<?>)entity;
|
112
|
reindex(node);
|
113
|
|
114
|
}
|
115
|
}
|
116
|
|
117
|
public static void generateCaches(Object entity){
|
118
|
if (entity != null){
|
119
|
Class<?> entityClazz = entity.getClass();
|
120
|
|
121
|
//non-viral-name caches
|
122
|
if(NonViralName.class.isAssignableFrom(entityClazz)) {
|
123
|
NonViralName<?> nonViralName = (NonViralName<?>)entity;
|
124
|
nonViralName.getAuthorshipCache();
|
125
|
nonViralName.getNameCache();
|
126
|
nonViralName.getTitleCache();
|
127
|
nonViralName.getFullTitleCache();
|
128
|
//team-or-person caches
|
129
|
}else if(TeamOrPersonBase.class.isAssignableFrom(entityClazz)){
|
130
|
TeamOrPersonBase<?> teamOrPerson = (TeamOrPersonBase<?>)entity;
|
131
|
String nomTitle = teamOrPerson.getNomenclaturalTitle();
|
132
|
if (teamOrPerson instanceof Team){
|
133
|
Team team =CdmBase.deproxy(teamOrPerson, Team.class);
|
134
|
team.setNomenclaturalTitle(nomTitle, team.isProtectedNomenclaturalTitleCache()); //nomTitle is not necessarily cached when it is created
|
135
|
}else{
|
136
|
teamOrPerson.setNomenclaturalTitle(nomTitle);
|
137
|
}
|
138
|
String titleCache = teamOrPerson.getTitleCache();
|
139
|
if (! teamOrPerson.isProtectedTitleCache()){
|
140
|
teamOrPerson.setTitleCache(titleCache, false);
|
141
|
}
|
142
|
|
143
|
//reference caches
|
144
|
}else if(Reference.class.isAssignableFrom(entityClazz)){
|
145
|
Reference<?> ref = (Reference<?>)entity;
|
146
|
ref.getAbbrevTitleCache();
|
147
|
ref.getTitleCache();
|
148
|
//title cache
|
149
|
}else if(IdentifiableEntity.class.isAssignableFrom(entityClazz)) {
|
150
|
IdentifiableEntity<?> identifiableEntity = (IdentifiableEntity)entity;
|
151
|
identifiableEntity.getTitleCache();
|
152
|
}else if(Amplification.class.isAssignableFrom(entityClazz)) {
|
153
|
Amplification amplification = (Amplification)entity;
|
154
|
amplification.updateCache();
|
155
|
}
|
156
|
|
157
|
}
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* @param event
|
162
|
* @param node
|
163
|
*/
|
164
|
private static <T extends ITreeNode> void reindex(T node) {
|
165
|
String oldChildIndex = node.treeIndex();
|
166
|
ITreeNode<?> parent = node.getParent();
|
167
|
String parentIndex = (parent == null) ? (sep + pref + node.treeId() + sep) : parent.treeIndex(); //TODO
|
168
|
if (node.getId() > 0){ //TODO
|
169
|
String newChildIndex = parentIndex + node.getId() + sep;
|
170
|
if (oldChildIndex == null || ! oldChildIndex.equals(newChildIndex)){
|
171
|
node.setTreeIndex(newChildIndex);
|
172
|
|
173
|
//TODO this is a greedy implementation, better use update by replace string
|
174
|
//either using and improving the below code or by using native SQL
|
175
|
//The current approach may run out of memory for large descendant sets.
|
176
|
List<T> childNodes = node.getChildNodes();
|
177
|
for (T child : childNodes){
|
178
|
if (child != null && ! child.equals(node)){ //node should not be it's own child, however just in case
|
179
|
reindex(child);
|
180
|
}
|
181
|
}
|
182
|
|
183
|
// String className = event.getEntityName();
|
184
|
// String updateQuery = " UPDATE %s tn " +
|
185
|
// " SET tn.treeIndex = Replace(tn.treeIndex, '%s', '%s') " +
|
186
|
// " WHERE tn.id <> "+ node.getId()+" ";
|
187
|
// updateQuery = String.format(updateQuery, className, oldChildIndex, parentIndex); //dummy
|
188
|
// System.out.println(updateQuery);
|
189
|
// EventSource session = event.getSession();
|
190
|
// Query query = session.createQuery(updateQuery);
|
191
|
// query.executeUpdate();
|
192
|
}
|
193
|
}
|
194
|
}
|
195
|
|
196
|
}
|