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 java.io.Serializable;
|
13
|
import java.util.UUID;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.hibernate.EmptyInterceptor;
|
17
|
import org.hibernate.Transaction;
|
18
|
import org.hibernate.type.Type;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
|
21
|
import eu.etaxonomy.cdm.database.VocabularyStoreImpl;
|
22
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
23
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
24
|
|
25
|
/**
|
26
|
* @author a.mueller
|
27
|
*
|
28
|
*/
|
29
|
@Component
|
30
|
public class CdmHibernateInterceptor extends EmptyInterceptor {
|
31
|
private static final long serialVersionUID = 2536017420460052854L;
|
32
|
private static final Logger logger = Logger.getLogger(CdmHibernateInterceptor.class);
|
33
|
|
34
|
//FIXME problem is circular dependency (see VocabularyStoreImpl.staticInitialized
|
35
|
// @Autowired
|
36
|
// VocabularyStoreImpl vocabularyStore;
|
37
|
|
38
|
private int updates;
|
39
|
private int creates;
|
40
|
private int loads;
|
41
|
|
42
|
public void onDelete(Object entity,
|
43
|
Serializable id,
|
44
|
Object[] state,
|
45
|
String[] propertyNames,
|
46
|
Type[] types) {
|
47
|
// do nothing
|
48
|
}
|
49
|
|
50
|
public boolean onFlushDirty(Object entity,
|
51
|
Serializable id,
|
52
|
Object[] currentState,
|
53
|
Object[] previousState,
|
54
|
String[] propertyNames,
|
55
|
Type[] types) {
|
56
|
|
57
|
boolean result = false;
|
58
|
if ( entity instanceof CdmBase ) {
|
59
|
updates++;
|
60
|
//result &= checkTransientDefinedTerms(currentState);
|
61
|
}
|
62
|
return result;
|
63
|
}
|
64
|
|
65
|
public boolean onLoad(Object entity,
|
66
|
Serializable id,
|
67
|
Object[] state,
|
68
|
String[] propertyNames,
|
69
|
Type[] types) {
|
70
|
if ( entity instanceof CdmBase ) {
|
71
|
loads++;
|
72
|
}
|
73
|
return false;
|
74
|
}
|
75
|
|
76
|
public boolean onSave(Object entity,
|
77
|
Serializable id,
|
78
|
Object[] state,
|
79
|
String[] propertyNames,
|
80
|
Type[] types) {
|
81
|
|
82
|
boolean result = false;
|
83
|
if ( entity instanceof CdmBase ) {
|
84
|
creates++;
|
85
|
//result &= checkTransientDefinedTerms(state);
|
86
|
}
|
87
|
return result;
|
88
|
}
|
89
|
|
90
|
|
91
|
private boolean checkTransientDefinedTerms(Object[] state){
|
92
|
boolean result = false;
|
93
|
if (VocabularyStoreImpl.isInitialized()){
|
94
|
//System.out.println("Save: " + entity);
|
95
|
int i = -1;
|
96
|
for (Object singleState : state){
|
97
|
i++;
|
98
|
if (singleState instanceof DefinedTermBase){
|
99
|
DefinedTermBase term = ((DefinedTermBase)singleState);
|
100
|
if (term.getId() != 0){
|
101
|
continue;
|
102
|
}else{
|
103
|
//System.out.println(" " + singleState.getClass());
|
104
|
UUID uuid = term.getUuid();
|
105
|
DefinedTermBase storedTermBase = VocabularyStoreImpl.getCurrentVocabularyStore().getTermByUuid(uuid);
|
106
|
if (storedTermBase == null){
|
107
|
logger.warn("DefinedTermBase with uuid "+ uuid +" could not be found in vocabulary store. Term stays transient.");
|
108
|
}else if (uuid.equals(storedTermBase.getUuid())){
|
109
|
logger.debug("Changed transient term");
|
110
|
state[i] = storedTermBase;
|
111
|
result = true;
|
112
|
}else{
|
113
|
throw new IllegalStateException("UUID is not equal.");
|
114
|
}
|
115
|
}
|
116
|
|
117
|
}
|
118
|
}
|
119
|
}else{ //not initialized
|
120
|
|
121
|
}
|
122
|
return result;
|
123
|
}
|
124
|
|
125
|
public void afterTransactionCompletion(Transaction tx) {
|
126
|
if ( tx.wasCommitted() ) {
|
127
|
logger.debug("Creations: " + creates + ", Updates: " + updates + ", Loads: " + loads);
|
128
|
}
|
129
|
updates=0;
|
130
|
creates=0;
|
131
|
loads=0;
|
132
|
}
|
133
|
|
134
|
|
135
|
|
136
|
}
|