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