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.database;
|
11
|
|
12
|
import java.util.HashMap;
|
13
|
import java.util.Map;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import javax.annotation.PostConstruct;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
import org.springframework.stereotype.Component;
|
21
|
import org.springframework.transaction.PlatformTransactionManager;
|
22
|
import org.springframework.transaction.TransactionDefinition;
|
23
|
import org.springframework.transaction.TransactionStatus;
|
24
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
25
|
|
26
|
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
|
27
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
28
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
29
|
import eu.etaxonomy.cdm.model.common.init.TermLoader;
|
30
|
import eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao;
|
31
|
|
32
|
/**
|
33
|
* Spring bean class to initialize the {@link IVocabularyStore IVocabularyStore}.
|
34
|
* To initialize the store the {@link TermLoader TermLoader} and the {@link IVocabularyStore IVocabularyStore}
|
35
|
* are injected via spring and the initializeTerms method is called as an init-method (@PostConstruct).
|
36
|
|
37
|
* @author a.mueller
|
38
|
*/
|
39
|
|
40
|
@Component
|
41
|
public class PersistentTermInitializer extends DefaultTermInitializer {
|
42
|
private static final Logger logger = Logger.getLogger(PersistentTermInitializer.class);
|
43
|
|
44
|
private boolean omit = false;
|
45
|
protected ITermVocabularyDao vocabularyDao;
|
46
|
|
47
|
/**
|
48
|
* After a bit of head-scratching I found section 3.5.1.3. in the current spring
|
49
|
* reference manual - @PostConstruct / afterPropertiesSet() is called
|
50
|
* immediatly after the bean is constructed, prior to any AOP interceptors being
|
51
|
* wrapped round the bean. Thus, we have to use programmatic transactions, not
|
52
|
* annotations or pointcuts.
|
53
|
*/
|
54
|
protected PlatformTransactionManager transactionManager;
|
55
|
protected DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();
|
56
|
|
57
|
public PersistentTermInitializer() {
|
58
|
txDefinition.setName("PersistentTermInitializer.initialize()");
|
59
|
txDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
60
|
}
|
61
|
|
62
|
public void setOmit(boolean omit) {
|
63
|
this.omit = omit;
|
64
|
}
|
65
|
|
66
|
@Autowired
|
67
|
public void setVocabularyDao(ITermVocabularyDao vocabularyDao) {
|
68
|
this.vocabularyDao = vocabularyDao;
|
69
|
}
|
70
|
|
71
|
@Autowired
|
72
|
public void setTransactionManager(PlatformTransactionManager transactionManager) {
|
73
|
this.transactionManager = transactionManager;
|
74
|
}
|
75
|
|
76
|
@PostConstruct
|
77
|
@Override
|
78
|
public void initialize(){
|
79
|
logger.debug("PersistentTermInitializer initialize()");
|
80
|
if (omit){
|
81
|
logger.info("PersistentTermInitializer.omit == true, returning without initializing terms");
|
82
|
return;
|
83
|
} else {
|
84
|
Map<UUID,DefinedTermBase> terms = new HashMap<UUID,DefinedTermBase>();
|
85
|
logger.info("PersistentTermInitializer.omit == false, initializing " + terms.size() + " term classes");
|
86
|
for(Class clazz : classesToInitialize) {
|
87
|
UUID vocabularyUuid = firstPass(clazz,terms);
|
88
|
secondPass(clazz,vocabularyUuid,terms);
|
89
|
}
|
90
|
|
91
|
}
|
92
|
}
|
93
|
|
94
|
protected void secondPass(Class clazz, UUID vocabularyUuid,Map<UUID,DefinedTermBase> terms) {
|
95
|
TransactionStatus txStatus = transactionManager.getTransaction(txDefinition);
|
96
|
|
97
|
TermVocabulary persistedVocabulary = vocabularyDao.findByUuid(vocabularyUuid);
|
98
|
|
99
|
for(Object obj : persistedVocabulary.getTerms()) {
|
100
|
DefinedTermBase d = (DefinedTermBase)obj;
|
101
|
terms.put(d.getUuid(), d);
|
102
|
|
103
|
}
|
104
|
|
105
|
logger.debug("Setting defined Terms for class " + clazz.getSimpleName());
|
106
|
super.setDefinedTerms(clazz, persistedVocabulary);
|
107
|
|
108
|
transactionManager.commit(txStatus);
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* T
|
113
|
* @param clazz
|
114
|
* @param persistedTerms
|
115
|
* @return
|
116
|
*/
|
117
|
public UUID firstPass(Class clazz, Map<UUID, DefinedTermBase> persistedTerms) {
|
118
|
TransactionStatus txStatus = transactionManager.getTransaction(txDefinition);
|
119
|
logger.warn("loading terms for " + clazz.getSimpleName());
|
120
|
Map<UUID,DefinedTermBase> terms = new HashMap<UUID,DefinedTermBase>();
|
121
|
|
122
|
for(DefinedTermBase d : persistedTerms.values()) {
|
123
|
terms.put(d.getUuid(), d);
|
124
|
}
|
125
|
|
126
|
TermVocabulary loadedVocabulary = termLoader.loadTerms((Class<? extends DefinedTermBase>)clazz, terms);
|
127
|
|
128
|
UUID vocabularyUuid = loadedVocabulary.getUuid();
|
129
|
|
130
|
logger.warn("loading vocabulary " + vocabularyUuid);
|
131
|
TermVocabulary persistedVocabulary = vocabularyDao.findByUuid(vocabularyUuid);
|
132
|
if(persistedVocabulary == null) { // i.e. there is no persisted vocabulary
|
133
|
logger.warn("vocabulary " + vocabularyUuid + " does not exist - saving");
|
134
|
saveVocabulary(loadedVocabulary);
|
135
|
} else {
|
136
|
logger.warn("vocabulary " + vocabularyUuid + " does exist and already has " + persistedVocabulary.size() + " terms");
|
137
|
boolean persistedVocabularyHasMissingTerms = false;
|
138
|
for(Object t : loadedVocabulary.getTerms()) {
|
139
|
if(!persistedVocabulary.getTerms().contains(t)) {
|
140
|
persistedVocabularyHasMissingTerms = true;
|
141
|
persistedVocabulary.addTerm((DefinedTermBase)t);
|
142
|
}
|
143
|
}
|
144
|
if(persistedVocabularyHasMissingTerms) {
|
145
|
logger.warn("vocabulary " + vocabularyUuid + " exists but does not have all the required terms - updating");
|
146
|
updateVocabulary(persistedVocabulary);
|
147
|
}
|
148
|
}
|
149
|
transactionManager.commit(txStatus);
|
150
|
return vocabularyUuid;
|
151
|
}
|
152
|
|
153
|
private void updateVocabulary(TermVocabulary vocabulary) {
|
154
|
TransactionStatus txStatus = transactionManager.getTransaction(txDefinition);
|
155
|
vocabularyDao.update(vocabulary);
|
156
|
transactionManager.commit(txStatus);
|
157
|
}
|
158
|
|
159
|
private void saveVocabulary(TermVocabulary vocabulary) {
|
160
|
TransactionStatus txStatus = transactionManager.getTransaction(txDefinition);
|
161
|
vocabularyDao.save(vocabulary);
|
162
|
transactionManager.commit(txStatus);
|
163
|
}
|
164
|
}
|