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
|
package eu.etaxonomy.cdm.model.common.init;
|
10
|
|
11
|
import java.util.HashMap;
|
12
|
import java.util.Iterator;
|
13
|
import java.util.Map;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
|
18
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
19
|
import eu.etaxonomy.cdm.model.common.ILoadableTerm;
|
20
|
import eu.etaxonomy.cdm.model.common.Language;
|
21
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
22
|
|
23
|
/**
|
24
|
* @author a.mueller
|
25
|
*
|
26
|
*/
|
27
|
public class DefaultVocabularyStore implements IVocabularyStore {
|
28
|
static Logger logger = Logger.getLogger(DefaultVocabularyStore.class);
|
29
|
|
30
|
private static boolean isInitialized = false;
|
31
|
|
32
|
public static final Language DEFAULT_LANGUAGE() {
|
33
|
return new Language(Language.uuidEnglish);
|
34
|
}
|
35
|
|
36
|
|
37
|
static protected Map<UUID, ILoadableTerm> definedTermsMap = null;
|
38
|
static protected Map<UUID, TermVocabulary<DefinedTermBase>> termVocabularyMap = null;
|
39
|
|
40
|
|
41
|
/* (non-Javadoc)
|
42
|
* @see eu.etaxonomy.cdm.model.common.init.IVocabularyStore#getTermByUuid(java.util.UUID)
|
43
|
*/
|
44
|
public DefinedTermBase<DefinedTermBase> getTermByUuid(UUID uuid) {
|
45
|
if (!isInitialized && ! initialize()){
|
46
|
return null;
|
47
|
}
|
48
|
return (DefinedTermBase<DefinedTermBase>)definedTermsMap.get(uuid);
|
49
|
}
|
50
|
|
51
|
/* (non-Javadoc)
|
52
|
* @see eu.etaxonomy.cdm.model.common.init.IVocabularyStore#getVocabularyByUuid(java.util.UUID)
|
53
|
*/
|
54
|
public TermVocabulary<DefinedTermBase> getVocabularyByUuid(UUID uuid) {
|
55
|
if (!isInitialized && ! initialize()){
|
56
|
return null;
|
57
|
}
|
58
|
return termVocabularyMap.get(uuid);
|
59
|
}
|
60
|
|
61
|
|
62
|
/* (non-Javadoc)
|
63
|
* @see eu.etaxonomy.cdm.model.common.init.IVocabularyStore#saveOrUpdate(eu.etaxonomy.cdm.model.common.TermVocabulary)
|
64
|
*/
|
65
|
public void saveOrUpdate(TermVocabulary<DefinedTermBase> vocabulary) {
|
66
|
initialize();
|
67
|
Iterator<DefinedTermBase> termIterator = vocabulary.iterator();
|
68
|
while (termIterator.hasNext()){
|
69
|
DefinedTermBase<DefinedTermBase> term = termIterator.next();
|
70
|
if (definedTermsMap.get(term.getUuid()) != null){
|
71
|
term.setId(definedTermsMap.get(term.getUuid()).getId()); // to avoid duplicates in the default Language
|
72
|
}
|
73
|
definedTermsMap.put(term.getUuid(), term);
|
74
|
}
|
75
|
termVocabularyMap.put(vocabulary.getUuid(), vocabulary);
|
76
|
}
|
77
|
|
78
|
/* (non-Javadoc)
|
79
|
* @see eu.etaxonomy.cdm.model.common.init.IVocabularyStore#saveOrUpdate(eu.etaxonomy.cdm.model.common.TermVocabulary)
|
80
|
*/
|
81
|
public void saveOrUpdate(ILoadableTerm term) {
|
82
|
initialize();
|
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
|
termVocabularyMap.put(term.getVocabulary().getUuid(), term.getVocabulary());
|
88
|
}
|
89
|
|
90
|
|
91
|
public boolean initialize() {
|
92
|
if (definedTermsMap == null){
|
93
|
logger.info("initTermsMap start ...");
|
94
|
definedTermsMap = new HashMap<UUID, ILoadableTerm>();
|
95
|
try {
|
96
|
definedTermsMap.put(DEFAULT_LANGUAGE().getUuid(), DEFAULT_LANGUAGE());
|
97
|
TermLoader termLoader = new TermLoader(this);
|
98
|
termLoader.makeDefaultTermsInserted();
|
99
|
} catch (Exception e) {
|
100
|
logger.error("Error ocurred when loading terms");
|
101
|
return false;
|
102
|
}
|
103
|
logger.debug("initTermsMap end ...");
|
104
|
}
|
105
|
if (termVocabularyMap == null){
|
106
|
logger.info("initVocabularyMap start ...");
|
107
|
termVocabularyMap = new HashMap<UUID, TermVocabulary<DefinedTermBase>>();
|
108
|
logger.debug("initVocabularyMap end ...");
|
109
|
}
|
110
|
isInitialized =true;
|
111
|
return true;
|
112
|
}
|
113
|
|
114
|
}
|