1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.cdm.database;
|
5
|
|
6
|
import java.util.HashMap;
|
7
|
import java.util.Iterator;
|
8
|
import java.util.Map;
|
9
|
import java.util.UUID;
|
10
|
|
11
|
import org.apache.log4j.Logger;
|
12
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
import org.springframework.stereotype.Component;
|
14
|
|
15
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
16
|
import eu.etaxonomy.cdm.model.common.ILoadableTerm;
|
17
|
import eu.etaxonomy.cdm.model.common.Language;
|
18
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
19
|
import eu.etaxonomy.cdm.model.common.init.IVocabularyStore;
|
20
|
import eu.etaxonomy.cdm.model.common.init.TermLoader;
|
21
|
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
|
22
|
import eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao;
|
23
|
|
24
|
/**
|
25
|
* @author a.mueller
|
26
|
*
|
27
|
*/
|
28
|
/**
|
29
|
* @author AM
|
30
|
*
|
31
|
*/
|
32
|
@Component
|
33
|
public class VocabularyStoreImpl implements IVocabularyStore {
|
34
|
private static Logger logger = Logger.getLogger(VocabularyStoreImpl.class);
|
35
|
|
36
|
private boolean initialized = false;
|
37
|
|
38
|
private static final UUID uuidEnglish = UUID.fromString("e9f8cdb7-6819-44e8-95d3-e2d0690c3523");
|
39
|
|
40
|
public static final Language DEFAULT_LANGUAGE= makeDefaultLanguage();
|
41
|
private static Language makeDefaultLanguage() {
|
42
|
logger.info("make Default language ...");
|
43
|
Language defaultLanguage = new Language(uuidEnglish);
|
44
|
logger.info("make Default language end");
|
45
|
return defaultLanguage;
|
46
|
}
|
47
|
|
48
|
static protected Map<UUID, ILoadableTerm> definedTermsMap = null;
|
49
|
|
50
|
|
51
|
@Autowired
|
52
|
public ITermVocabularyDao vocabularyDao;
|
53
|
|
54
|
@Autowired
|
55
|
public IDefinedTermDao termDao;
|
56
|
|
57
|
/**
|
58
|
*
|
59
|
*/
|
60
|
public VocabularyStoreImpl() {
|
61
|
super();
|
62
|
}
|
63
|
|
64
|
|
65
|
/* (non-Javadoc)
|
66
|
* @see eu.etaxonomy.cdm.model.common.init.IVocabularySaver#saveOrUpdate(eu.etaxonomy.cdm.model.common.TermVocabulary)
|
67
|
*/
|
68
|
public void saveOrUpdate(TermVocabulary<DefinedTermBase> vocabulary) {
|
69
|
logger.info("vocabulary save or update start ...");
|
70
|
initialize();
|
71
|
Iterator<DefinedTermBase> termIterator = vocabulary.iterator();
|
72
|
while (termIterator.hasNext()){
|
73
|
logger.info("iterate ...");
|
74
|
DefinedTermBase<DefinedTermBase> term = termIterator.next();
|
75
|
if (definedTermsMap.get(term.getUuid()) != null){
|
76
|
term.setId(definedTermsMap.get(term.getUuid()).getId()); // to avoid duplicates in the default Language
|
77
|
}
|
78
|
definedTermsMap.put(term.getUuid(), term);
|
79
|
}
|
80
|
logger.info("vocabulary save or update before dao save ...");
|
81
|
vocabularyDao.saveOrUpdate(vocabulary);
|
82
|
logger.info("vocabulary save or update end.");
|
83
|
}
|
84
|
|
85
|
/* (non-Javadoc)
|
86
|
* @see eu.etaxonomy.cdm.model.common.init.IVocabularyStore#saveOrUpdate(eu.etaxonomy.cdm.model.common.IDefTerm)
|
87
|
*/
|
88
|
public void saveOrUpdate(ILoadableTerm term) {
|
89
|
initialize();
|
90
|
if (definedTermsMap.get(term.getUuid()) != null){
|
91
|
ILoadableTerm oldTerm = definedTermsMap.get(term.getUuid());
|
92
|
term.setId(oldTerm.getId()); // to avoid duplicates in the default Language
|
93
|
|
94
|
}
|
95
|
definedTermsMap.put(term.getUuid(), term);
|
96
|
// vocabularyDao.saveOrUpdate(term);
|
97
|
}
|
98
|
|
99
|
public DefinedTermBase<DefinedTermBase> getTermByUuid(UUID uuid) {
|
100
|
if (initialize()){
|
101
|
if (definedTermsMap.get(uuid) != null){
|
102
|
return (DefinedTermBase<DefinedTermBase>)definedTermsMap.get(uuid);
|
103
|
}else{
|
104
|
DefinedTermBase term = termDao.findByUuid(uuid);
|
105
|
definedTermsMap.put(term.getUuid(), term);
|
106
|
return term;
|
107
|
}
|
108
|
}else{
|
109
|
logger.error("Vocabulary Store could not be initialized");
|
110
|
throw new RuntimeException("Vocabulary Store could not be initialized");
|
111
|
}
|
112
|
}
|
113
|
|
114
|
public TermVocabulary<DefinedTermBase> getVocabularyByUuid(UUID uuid){
|
115
|
initialize();
|
116
|
return vocabularyDao.findByUuid(uuid);
|
117
|
}
|
118
|
|
119
|
public boolean initialize(){
|
120
|
return loadBasicTerms();
|
121
|
}
|
122
|
|
123
|
public boolean loadBasicTerms(){
|
124
|
if (! initialized){
|
125
|
logger.info("inititialize VocabularyStoreImpl ...");
|
126
|
try {
|
127
|
logger.info("000 ...");
|
128
|
Language defaultLanguage = (Language)termDao.findByUuid(DEFAULT_LANGUAGE.getUuid());
|
129
|
logger.info("aaa ...");
|
130
|
if (defaultLanguage == null){
|
131
|
logger.info("bbb ...");
|
132
|
logger.info("DL ..." + DEFAULT_LANGUAGE.hashCode());
|
133
|
termDao.saveOrUpdate(DEFAULT_LANGUAGE);
|
134
|
definedTermsMap = new HashMap<UUID, ILoadableTerm>();
|
135
|
definedTermsMap.put(DEFAULT_LANGUAGE.getUuid(), DEFAULT_LANGUAGE);
|
136
|
logger.info("ccc ...");
|
137
|
initialized = true;
|
138
|
TermLoader termLoader = new TermLoader(this);
|
139
|
termLoader.loadAllDefaultTerms();
|
140
|
}else if (definedTermsMap == null){
|
141
|
logger.info("ddd ...");
|
142
|
definedTermsMap = new HashMap<UUID, ILoadableTerm>();
|
143
|
definedTermsMap.put(defaultLanguage.getUuid(), defaultLanguage);
|
144
|
}
|
145
|
} catch (Exception e) {
|
146
|
logger.error("loadBasicTerms: Error ocurred when initializing and loading terms");
|
147
|
initialized = false;
|
148
|
return false;
|
149
|
}
|
150
|
initialized = true;
|
151
|
logger.info("inititialize VocabularyStoreImpl end ...");
|
152
|
}
|
153
|
return true;
|
154
|
}
|
155
|
|
156
|
}
|