4 package eu
.etaxonomy
.cdm
.database
;
6 import java
.util
.HashMap
;
7 import java
.util
.Iterator
;
11 import org
.apache
.log4j
.Logger
;
12 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
13 import org
.springframework
.stereotype
.Component
;
14 import org
.springframework
.transaction
.annotation
.Transactional
;
16 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
17 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
18 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
19 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
20 import eu
.etaxonomy
.cdm
.model
.common
.init
.IVocabularyStore
;
21 import eu
.etaxonomy
.cdm
.model
.common
.init
.TermLoader
;
22 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IDefinedTermDao
;
23 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ITermVocabularyDao
;
30 public class VocabularyStoreImpl
implements IVocabularyStore
{
31 private static Logger logger
= Logger
.getLogger(VocabularyStoreImpl
.class);
33 private boolean initialized
= false;
35 private static final UUID uuidEnglish
= UUID
.fromString("e9f8cdb7-6819-44e8-95d3-e2d0690c3523");
37 public static final Language DEFAULT_LANGUAGE
= makeDefaultLanguage();
38 private static Language
makeDefaultLanguage() {
39 Language defaultLanguage
= new Language(uuidEnglish
);
40 return defaultLanguage
;
43 static protected Map
<UUID
, DefinedTermBase
> definedTermsMap
= null;
47 public ITermVocabularyDao vocabularyDao
;
50 public IDefinedTermDao termDao
;
55 public VocabularyStoreImpl() {
61 * @see eu.etaxonomy.cdm.model.common.init.IVocabularySaver#saveOrUpdate(eu.etaxonomy.cdm.model.common.TermVocabulary)
63 public void saveOrUpdate2(TermVocabulary
<DefinedTermBase
> vocabulary
) {
65 Iterator
<DefinedTermBase
> termIterator
= vocabulary
.iterator();
66 while (termIterator
.hasNext()){
67 DefinedTermBase
<DefinedTermBase
> term
= termIterator
.next();
68 if (definedTermsMap
.get(term
.getUuid()) != null){
69 term
.setId(definedTermsMap
.get(term
.getUuid()).getId()); // to avoid duplicates in the default Language
71 definedTermsMap
.put(term
.getUuid(), term
);
73 vocabularyDao
.saveOrUpdate(vocabulary
);
76 public DefinedTermBase
getTermByUuid(UUID uuid
) {
78 if (definedTermsMap
.get(uuid
) != null){
79 return definedTermsMap
.get(uuid
);
81 DefinedTermBase term
= termDao
.findByUuid(uuid
);
82 definedTermsMap
.put(term
.getUuid(), term
);
86 logger
.error("Vocabulary Store could not be initialized");
87 throw new RuntimeException("Vocabulary Store could not be initialized");
91 public TermVocabulary
<DefinedTermBase
> getVocabularyByUuid(UUID uuid
){
93 return vocabularyDao
.findByUuid(uuid
);
96 public boolean initialize(){
97 return loadBasicTerms();
100 public boolean loadBasicTerms(){
102 logger
.info("inititialize VocabularyStoreImpl ...");
104 Language defaultLanguage
= (Language
)termDao
.findByUuid(DEFAULT_LANGUAGE
.getUuid());
106 if (defaultLanguage
== null){
107 termDao
.saveOrUpdate(DEFAULT_LANGUAGE
);
108 definedTermsMap
= new HashMap
<UUID
, DefinedTermBase
>();
109 definedTermsMap
.put(DEFAULT_LANGUAGE
.getUuid(), DEFAULT_LANGUAGE
);
111 TermLoader termLoader
= new TermLoader(this);
112 termLoader
.loadAllDefaultTerms();
113 }else if (definedTermsMap
== null){
114 definedTermsMap
= new HashMap
<UUID
, DefinedTermBase
>();
115 definedTermsMap
.put(defaultLanguage
.getUuid(), defaultLanguage
);
117 } catch (Exception e
) {
118 logger
.error("loadBasicTerms: Error ocurred when initializing and loading terms");
123 logger
.info("initTermsMap end ...");