1
|
/**
|
2
|
* Copyright (C) 2018 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.api.service.taxonGraph;
|
10
|
|
11
|
import java.util.Objects;
|
12
|
|
13
|
import org.apache.commons.lang.ArrayUtils;
|
14
|
import org.apache.log4j.Level;
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.hibernate.HibernateException;
|
17
|
import org.hibernate.Session;
|
18
|
import org.hibernate.action.spi.BeforeTransactionCompletionProcess;
|
19
|
import org.hibernate.engine.spi.SessionImplementor;
|
20
|
import org.hibernate.event.spi.PostInsertEvent;
|
21
|
import org.hibernate.event.spi.PostUpdateEvent;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.application.IRunAs;
|
24
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
25
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
26
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
27
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.taxonGraph.AbstractHibernateTaxonGraphProcessor;
|
28
|
import eu.etaxonomy.cdm.persistence.dao.taxonGraph.TaxonGraphException;
|
29
|
|
30
|
/**
|
31
|
* see https://dev.e-taxonomy.eu/redmine/issues/7648
|
32
|
*
|
33
|
* @author a.kohlbecker
|
34
|
* @since Sep 26, 2018
|
35
|
*
|
36
|
*/
|
37
|
public class TaxonGraphBeforeTransactionCompleteProcess
|
38
|
extends AbstractHibernateTaxonGraphProcessor
|
39
|
implements BeforeTransactionCompletionProcess {
|
40
|
|
41
|
private static final Logger logger = Logger.getLogger(TaxonGraphBeforeTransactionCompleteProcess.class);
|
42
|
|
43
|
private String[] NAMEPARTS_OR_RANK_PROPS = new String[]{"genusOrUninomial", "specificEpithet", "rank"};
|
44
|
private String[] NOMREF_PROP = new String[]{"nomenclaturalSource.citation"};
|
45
|
|
46
|
private Session temporarySession;
|
47
|
|
48
|
private Level origLoggerLevel;
|
49
|
|
50
|
private Session parentSession;
|
51
|
|
52
|
private TaxonName entity;
|
53
|
|
54
|
private String[] propertyNames;
|
55
|
|
56
|
private Object[] oldState;
|
57
|
|
58
|
private Object[] state;
|
59
|
|
60
|
private boolean isInsertEvent;
|
61
|
|
62
|
private IRunAs runAs = null;
|
63
|
|
64
|
public TaxonGraphBeforeTransactionCompleteProcess(PostUpdateEvent event, IRunAs runAs){
|
65
|
entity = (TaxonName) event.getEntity();
|
66
|
propertyNames = event.getPersister().getPropertyNames();
|
67
|
oldState = event.getOldState();
|
68
|
state = event.getState();
|
69
|
this.runAs = runAs;
|
70
|
}
|
71
|
|
72
|
public TaxonGraphBeforeTransactionCompleteProcess(PostInsertEvent event, IRunAs runAs){
|
73
|
isInsertEvent = true;
|
74
|
entity = (TaxonName) event.getEntity();
|
75
|
propertyNames = event.getPersister().getPropertyNames();
|
76
|
oldState = null;
|
77
|
state = event.getState();
|
78
|
this.runAs = runAs;
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* {@inheritDoc}
|
83
|
*/
|
84
|
@Override
|
85
|
public void doBeforeTransactionCompletion(SessionImplementor session) {
|
86
|
|
87
|
try {
|
88
|
if(isInsertEvent){
|
89
|
createTempSession(session);
|
90
|
onNewTaxonName(entity);
|
91
|
getSession().flush();
|
92
|
} else {
|
93
|
if(checkStateChange(NAMEPARTS_OR_RANK_PROPS) > -1){
|
94
|
createTempSession(session);
|
95
|
onNameOrRankChange(entity);
|
96
|
getSession().flush();
|
97
|
}
|
98
|
int changedNomRefIndex = checkStateChange(NOMREF_PROP);
|
99
|
if(changedNomRefIndex > -1){
|
100
|
createTempSession(session);
|
101
|
onNomReferenceChange(entity, (Reference)oldState[changedNomRefIndex]);
|
102
|
getSession().flush();
|
103
|
}
|
104
|
}
|
105
|
} catch (TaxonGraphException e) {
|
106
|
throw new HibernateException(e);
|
107
|
}
|
108
|
finally {
|
109
|
if (getSession() != null ) {
|
110
|
// temporarySession.close(); // no need to close the session since the session is configured for auto close, see createTempSession()
|
111
|
if(origLoggerLevel != null){
|
112
|
Logger.getLogger("org.hibernate.SQL").setLevel(origLoggerLevel);
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
|
117
|
}
|
118
|
|
119
|
private int checkStateChange(String[] propertyNamesToCheck){
|
120
|
|
121
|
if(oldState == null){
|
122
|
return -1;
|
123
|
}
|
124
|
int propsCheckedCnt = 0;
|
125
|
for(int i = 0; i < propertyNames.length; i++){
|
126
|
if(ArrayUtils.contains(propertyNamesToCheck, propertyNames[i])){
|
127
|
propsCheckedCnt++;
|
128
|
if(!Objects.equals(oldState[i], state[i])){
|
129
|
return i;
|
130
|
}
|
131
|
if(propsCheckedCnt == propertyNamesToCheck.length){
|
132
|
return -1;
|
133
|
}
|
134
|
}
|
135
|
}
|
136
|
// this exception should be raised during the unit tests already and thus will never occur in production
|
137
|
throw new RuntimeException("TaxonName class misses at least one property of: " + ArrayUtils.toString(propertyNamesToCheck));
|
138
|
}
|
139
|
|
140
|
/**
|
141
|
* Concept of creation of sub-sessions found in AuditProcess.doBeforeTransactionCompletion(SessionImplementor session)
|
142
|
* and adapted to make it work for this case.
|
143
|
*
|
144
|
* @param session
|
145
|
*/
|
146
|
protected void createTempSession(SessionImplementor session) {
|
147
|
if(getSession() == null){
|
148
|
parentSession = (Session)session;
|
149
|
temporarySession = ((Session) session)
|
150
|
.sessionWithOptions().transactionContext()
|
151
|
// in contrast to AuditProcess.doBeforeTransactionCompletion we need the session to close automatically,
|
152
|
// otherwise the hibernate search indexer will suffer from LazyInitializationExceptions
|
153
|
.autoClose(true)
|
154
|
// in contrast to AuditProcess.doBeforeTransactionCompletion, the ConnectionReleaseMode.AFTER_TRANSACTION causes problems for us:
|
155
|
//.connectionReleaseMode( ConnectionReleaseMode.AFTER_TRANSACTION )
|
156
|
.openSession();
|
157
|
// origLoggerLevel = Logger.getLogger("org.hibernate.SQL").getLevel();
|
158
|
// Logger.getLogger("org.hibernate.SQL").setLevel(Level.TRACE);
|
159
|
}
|
160
|
}
|
161
|
|
162
|
public void onNewTaxonName(TaxonName taxonName) throws TaxonGraphException {
|
163
|
onNameOrRankChange(taxonName);
|
164
|
}
|
165
|
|
166
|
public void onNameOrRankChange(TaxonName taxonName) throws TaxonGraphException {
|
167
|
boolean isNotDeleted = parentSession.contains(taxonName) && taxonName.isPersited();
|
168
|
// TODO use audit event to check for deletion?
|
169
|
if(isNotDeleted){
|
170
|
try{
|
171
|
if(runAs != null){
|
172
|
runAs.apply();
|
173
|
}
|
174
|
Taxon taxon = assureSingleTaxon(taxonName);
|
175
|
updateEdges(taxon);
|
176
|
getSession().saveOrUpdate(taxon);
|
177
|
} finally {
|
178
|
if(runAs != null){
|
179
|
runAs.restore();
|
180
|
}
|
181
|
}
|
182
|
}
|
183
|
}
|
184
|
|
185
|
|
186
|
public void onNomReferenceChange(TaxonName taxonName, Reference oldNomReference) throws TaxonGraphException {
|
187
|
if(oldNomReference == null){
|
188
|
onNewTaxonName(taxonName);
|
189
|
}
|
190
|
boolean isNotDeleted = parentSession.contains(taxonName) && taxonName.isPersited();
|
191
|
// TODO use audit event to check for deletion?
|
192
|
if(isNotDeleted){
|
193
|
try {
|
194
|
if(runAs != null){
|
195
|
runAs.apply();
|
196
|
}
|
197
|
Taxon taxon = assureSingleTaxon(taxonName);
|
198
|
updateConceptReferenceInEdges(taxon, oldNomReference);
|
199
|
getSession().saveOrUpdate(taxon);
|
200
|
} finally {
|
201
|
if(runAs != null){
|
202
|
runAs.restore();
|
203
|
}
|
204
|
}
|
205
|
}
|
206
|
}
|
207
|
|
208
|
|
209
|
/**
|
210
|
* @return
|
211
|
*/
|
212
|
@Override
|
213
|
public Session getSession() {
|
214
|
return temporarySession;
|
215
|
}
|
216
|
|
217
|
|
218
|
}
|