1 |
51116396
|
Andreas Müller
|
/**
|
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.model.common.CdmBase;
|
22 |
|
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
23 |
|
|
|
24 |
|
|
/**
|
25 |
|
|
* @author a.mueller
|
26 |
|
|
*
|
27 |
|
|
*/
|
28 |
|
|
@Component
|
29 |
|
|
public class CdmHibernateInterceptor extends EmptyInterceptor {
|
30 |
|
|
private static final long serialVersionUID = 2536017420460052854L;
|
31 |
|
|
private static final Logger logger = Logger.getLogger(CdmHibernateInterceptor.class);
|
32 |
|
|
|
33 |
|
|
//FIXME problem is circular dependency (see VocabularyStoreImpl.staticInitialized
|
34 |
|
|
// @Autowired
|
35 |
|
|
// VocabularyStoreImpl vocabularyStore;
|
36 |
|
|
|
37 |
|
|
private int updates;
|
38 |
|
|
private int creates;
|
39 |
|
|
private int loads;
|
40 |
|
|
|
41 |
|
|
public void onDelete(Object entity,
|
42 |
|
|
Serializable id,
|
43 |
|
|
Object[] state,
|
44 |
|
|
String[] propertyNames,
|
45 |
|
|
Type[] types) {
|
46 |
|
|
// do nothing
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
public boolean onFlushDirty(Object entity,
|
50 |
|
|
Serializable id,
|
51 |
|
|
Object[] currentState,
|
52 |
|
|
Object[] previousState,
|
53 |
|
|
String[] propertyNames,
|
54 |
|
|
Type[] types) {
|
55 |
|
|
|
56 |
|
|
boolean result = false;
|
57 |
|
|
if ( entity instanceof CdmBase ) {
|
58 |
|
|
updates++;
|
59 |
|
|
//result &= checkTransientDefinedTerms(currentState);
|
60 |
|
|
}
|
61 |
|
|
return result;
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
public boolean onLoad(Object entity,
|
65 |
|
|
Serializable id,
|
66 |
|
|
Object[] state,
|
67 |
|
|
String[] propertyNames,
|
68 |
|
|
Type[] types) {
|
69 |
|
|
if ( entity instanceof CdmBase ) {
|
70 |
|
|
logger.warn("id = " +id);
|
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 |
|
|
|