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.io.FileNotFoundException;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import javax.annotation.PostConstruct;
|
20
|
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.springframework.beans.factory.annotation.Autowired;
|
23
|
import org.springframework.stereotype.Component;
|
24
|
|
25
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
26
|
import eu.etaxonomy.cdm.model.common.ILoadableTerm;
|
27
|
import eu.etaxonomy.cdm.model.common.Language;
|
28
|
import eu.etaxonomy.cdm.model.common.NoDefinedTermClassException;
|
29
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
30
|
import eu.etaxonomy.cdm.model.common.init.IVocabularyStore;
|
31
|
import eu.etaxonomy.cdm.model.common.init.TermLoader;
|
32
|
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
|
33
|
import eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao;
|
34
|
|
35
|
/**
|
36
|
* @author a.mueller
|
37
|
*
|
38
|
*/
|
39
|
/**
|
40
|
* @author AM
|
41
|
*
|
42
|
*/
|
43
|
@Component
|
44
|
public class VocabularyStoreImpl implements IVocabularyStore {
|
45
|
private static Logger logger = Logger.getLogger(VocabularyStoreImpl.class);
|
46
|
|
47
|
private boolean initialized = false;
|
48
|
private static final UUID uuidEnglish = UUID.fromString("e9f8cdb7-6819-44e8-95d3-e2d0690c3523");
|
49
|
|
50
|
private static Language DEFAULT_LANGUAGE = null;
|
51
|
private static void makeDefaultLanguage() {
|
52
|
logger.debug("make Default language ...");
|
53
|
DEFAULT_LANGUAGE = Language.NewInstance(uuidEnglish);
|
54
|
}
|
55
|
|
56
|
static protected Map<UUID, ILoadableTerm> definedTermsMap = null;
|
57
|
|
58
|
|
59
|
@Autowired
|
60
|
public ITermVocabularyDao vocabularyDao;
|
61
|
|
62
|
@Autowired
|
63
|
public IDefinedTermDao termDao;
|
64
|
|
65
|
/**
|
66
|
*
|
67
|
*/
|
68
|
public VocabularyStoreImpl() {
|
69
|
super();
|
70
|
}
|
71
|
|
72
|
|
73
|
/* (non-Javadoc)
|
74
|
* @see eu.etaxonomy.cdm.model.common.init.IVocabularySaver#saveOrUpdate(eu.etaxonomy.cdm.model.common.TermVocabulary)
|
75
|
*/
|
76
|
public void saveOrUpdate(TermVocabulary<DefinedTermBase> vocabulary) {
|
77
|
logger.info("vocabulary save or update start ...");
|
78
|
initialize();
|
79
|
Iterator<DefinedTermBase> termIterator = vocabulary.iterator();
|
80
|
while (termIterator.hasNext()){
|
81
|
logger.debug("iterate ...");
|
82
|
DefinedTermBase<DefinedTermBase> term = termIterator.next();
|
83
|
if (definedTermsMap.get(term.getUuid()) != null){
|
84
|
term.setId(definedTermsMap.get(term.getUuid()).getId()); // to avoid duplicates in the default Language
|
85
|
}
|
86
|
definedTermsMap.put(term.getUuid(), term);
|
87
|
}
|
88
|
logger.debug("vocabulary save or update before dao save ...");
|
89
|
vocabularyDao.saveOrUpdate(vocabulary);
|
90
|
logger.debug("vocabulary save or update end.");
|
91
|
}
|
92
|
|
93
|
/* (non-Javadoc)
|
94
|
* @see eu.etaxonomy.cdm.model.common.init.IVocabularyStore#saveOrUpdate(eu.etaxonomy.cdm.model.common.IDefTerm)
|
95
|
*/
|
96
|
public void saveOrUpdate(ILoadableTerm term) {
|
97
|
initialize();
|
98
|
if (definedTermsMap.get(term.getUuid()) != null){
|
99
|
ILoadableTerm oldTerm = definedTermsMap.get(term.getUuid());
|
100
|
term.setId(oldTerm.getId()); // to avoid duplicates in the default Language
|
101
|
|
102
|
}
|
103
|
definedTermsMap.put(term.getUuid(), term);
|
104
|
// vocabularyDao.saveOrUpdate(term);
|
105
|
}
|
106
|
|
107
|
public DefinedTermBase<DefinedTermBase> getTermByUuid(UUID uuid) {
|
108
|
if (initialize()){
|
109
|
if (definedTermsMap.get(uuid) != null){
|
110
|
return (DefinedTermBase<DefinedTermBase>)definedTermsMap.get(uuid);
|
111
|
}else{
|
112
|
DefinedTermBase term = termDao.findByUuid(uuid);
|
113
|
if (term != null){
|
114
|
definedTermsMap.put(term.getUuid(), term);
|
115
|
}
|
116
|
return term;
|
117
|
}
|
118
|
}else{
|
119
|
logger.error("Vocabulary Store could not be initialized");
|
120
|
throw new RuntimeException("Vocabulary Store could not be initialized");
|
121
|
}
|
122
|
}
|
123
|
|
124
|
public TermVocabulary<DefinedTermBase> getVocabularyByUuid(UUID uuid){
|
125
|
if (! initialize()){
|
126
|
return null;
|
127
|
}else{
|
128
|
return vocabularyDao.findByUuid(uuid);
|
129
|
}
|
130
|
}
|
131
|
|
132
|
// private TermLoader termLoader; //doesn't work yet in service layer for some unclear resason
|
133
|
// @PostConstruct
|
134
|
// private void init(){
|
135
|
// try {
|
136
|
// logger.info("init ...");
|
137
|
// DefinedTermBase.setVocabularyStore(this);
|
138
|
// termLoader = new TermLoader(this);
|
139
|
// if (! termLoader.basicTermsExist(this)){
|
140
|
// try {
|
141
|
// termLoader.loadAllDefaultTerms();
|
142
|
// } catch (FileNotFoundException e) {
|
143
|
// logger.error(e.getMessage());
|
144
|
// } catch (NoDefinedTermClassException e) {
|
145
|
// logger.error(e.getMessage());
|
146
|
// }
|
147
|
// }
|
148
|
// } catch (RuntimeException e) {
|
149
|
// logger.error("RuntimeException when initializing Terms");
|
150
|
// e.printStackTrace();
|
151
|
// throw e;
|
152
|
// }
|
153
|
// }
|
154
|
|
155
|
|
156
|
public boolean initialize(){
|
157
|
boolean result = true;
|
158
|
if (! initialized){
|
159
|
logger.info("inititialize VocabularyStoreImpl ...");
|
160
|
try {
|
161
|
logger.debug("setVocabularyStore ...");
|
162
|
DefinedTermBase.setVocabularyStore(this);
|
163
|
makeDefaultLanguage();
|
164
|
logger.debug("defaultLanguage ...");
|
165
|
Language defaultLanguage = (Language)termDao.findByUuid(DEFAULT_LANGUAGE.getUuid());
|
166
|
logger.info("222 ...");
|
167
|
if (defaultLanguage == null){
|
168
|
termDao.saveOrUpdate(DEFAULT_LANGUAGE);
|
169
|
definedTermsMap = new HashMap<UUID, ILoadableTerm>();
|
170
|
definedTermsMap.put(DEFAULT_LANGUAGE.getUuid(), DEFAULT_LANGUAGE);
|
171
|
logger.info("ccc ...");
|
172
|
initialized = true;
|
173
|
TermLoader termLoader = new TermLoader(this);
|
174
|
//termLoader.setVocabularyStore(this);
|
175
|
result = result && termLoader.makeDefaultTermsInserted(this);
|
176
|
}else if (definedTermsMap == null){
|
177
|
definedTermsMap = new HashMap<UUID, ILoadableTerm>();
|
178
|
definedTermsMap.put(defaultLanguage.getUuid(), defaultLanguage);
|
179
|
}
|
180
|
initialized = true;
|
181
|
result = result && loadProgrammaticallyNeededTerms();
|
182
|
initialized = result;
|
183
|
logger.info("inititialize VocabularyStoreImpl end ...");
|
184
|
} catch (Exception e) {
|
185
|
logger.error("loadBasicTerms: Error ocurred when initializing and loading terms");
|
186
|
initialized = false;
|
187
|
return false;
|
188
|
}
|
189
|
|
190
|
}
|
191
|
return result;
|
192
|
}
|
193
|
|
194
|
|
195
|
private boolean loadProgrammaticallyNeededTerms(){
|
196
|
List<DefinedTermBase> list = termDao.list(1000, 0);
|
197
|
logger.info("Size:" + list.size());
|
198
|
for (DefinedTermBase defTerm : list){
|
199
|
saveOrUpdate(defTerm);
|
200
|
}
|
201
|
return true;
|
202
|
}
|
203
|
|
204
|
}
|