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()){ return null;}
|
46
|
return (DefinedTermBase<DefinedTermBase>)definedTermsMap.get(uuid);
|
47
|
}
|
48
|
|
49
|
/* (non-Javadoc)
|
50
|
* @see eu.etaxonomy.cdm.model.common.init.IVocabularyStore#getVocabularyByUuid(java.util.UUID)
|
51
|
*/
|
52
|
public TermVocabulary<DefinedTermBase> getVocabularyByUuid(UUID uuid) {
|
53
|
if (!isInitialized && ! initialize()){ return null;}
|
54
|
return termVocabularyMap.get(uuid);
|
55
|
}
|
56
|
|
57
|
|
58
|
/* (non-Javadoc)
|
59
|
* @see eu.etaxonomy.cdm.model.common.init.IVocabularyStore#saveOrUpdate(eu.etaxonomy.cdm.model.common.TermVocabulary)
|
60
|
*/
|
61
|
public void saveOrUpdate(TermVocabulary<DefinedTermBase> vocabulary) {
|
62
|
logger.info("dddd");
|
63
|
initialize();
|
64
|
Iterator<DefinedTermBase> termIterator = vocabulary.iterator();
|
65
|
while (termIterator.hasNext()){
|
66
|
DefinedTermBase<DefinedTermBase> term = termIterator.next();
|
67
|
if (definedTermsMap.get(term.getUuid()) != null){
|
68
|
term.setId(definedTermsMap.get(term.getUuid()).getId()); // to avoid duplicates in the default Language
|
69
|
}
|
70
|
definedTermsMap.put(term.getUuid(), term);
|
71
|
}
|
72
|
termVocabularyMap.put(vocabulary.getUuid(), vocabulary);
|
73
|
}
|
74
|
|
75
|
/* (non-Javadoc)
|
76
|
* @see eu.etaxonomy.cdm.model.common.init.IVocabularyStore#saveOrUpdate(eu.etaxonomy.cdm.model.common.TermVocabulary)
|
77
|
*/
|
78
|
public void saveOrUpdate(ILoadableTerm term) {
|
79
|
initialize();
|
80
|
if (definedTermsMap.get(term.getUuid()) != null){
|
81
|
term.setId(definedTermsMap.get(term.getUuid()).getId()); // to avoid duplicates in the default Language
|
82
|
}
|
83
|
definedTermsMap.put(term.getUuid(), term);
|
84
|
termVocabularyMap.put(term.getVocabulary().getUuid(), term.getVocabulary());
|
85
|
}
|
86
|
|
87
|
|
88
|
public boolean initialize() {
|
89
|
if (definedTermsMap == null){
|
90
|
logger.info("initTermsMap start ...");
|
91
|
definedTermsMap = new HashMap<UUID, ILoadableTerm>();
|
92
|
try {
|
93
|
definedTermsMap.put(DEFAULT_LANGUAGE().getUuid(), DEFAULT_LANGUAGE());
|
94
|
TermLoader termLoader = new TermLoader(this);
|
95
|
termLoader.makeDefaultTermsInserted();
|
96
|
} catch (Exception e) {
|
97
|
logger.error("Error ocurred when loading terms");
|
98
|
return false;
|
99
|
}
|
100
|
logger.debug("initTermsMap end ...");
|
101
|
}
|
102
|
if (termVocabularyMap == null){
|
103
|
logger.info("initVocabularyMap start ...");
|
104
|
termVocabularyMap = new HashMap<UUID, TermVocabulary<DefinedTermBase>>();
|
105
|
logger.debug("initVocabularyMap end ...");
|
106
|
}
|
107
|
isInitialized =true;
|
108
|
return true;
|
109
|
}
|
110
|
|
111
|
}
|