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;
|
10
|
|
11
|
import java.lang.reflect.Constructor;
|
12
|
import java.util.HashMap;
|
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.init.ITermInitializer;
|
19
|
import eu.etaxonomy.cdm.model.common.init.ITermLoader;
|
20
|
import eu.etaxonomy.cdm.model.common.init.MockCdmCacher;
|
21
|
import eu.etaxonomy.cdm.model.common.init.TermLoader;
|
22
|
|
23
|
/**
|
24
|
* @author a.mueller
|
25
|
*
|
26
|
*/
|
27
|
public class DefaultTermInitializer implements ITermInitializer {
|
28
|
@SuppressWarnings("unused")
|
29
|
private static final Logger logger = Logger.getLogger(DefaultTermInitializer.class);
|
30
|
protected ITermLoader termLoader = new TermLoader();
|
31
|
|
32
|
public void initialize() {
|
33
|
termLoader.unloadAllTerms();
|
34
|
doInitialize();
|
35
|
}
|
36
|
|
37
|
protected void doInitialize(){
|
38
|
Map<UUID,DefinedTermBase> terms = new HashMap<UUID,DefinedTermBase>();
|
39
|
|
40
|
// for(Class<? extends DefinedTermBase<?>> clazz : classesToInitialize) {
|
41
|
for(VocabularyEnum vocabularyEnum : VocabularyEnum.values()) {
|
42
|
// Class<? extends DefinedTermBase<?>> clazz = vocabularyEnum.getClazz();
|
43
|
TermVocabulary<?> voc = termLoader.loadTerms(vocabularyEnum, terms);
|
44
|
setDefinedTerms(vocabularyEnum.getClazz(),voc);
|
45
|
}
|
46
|
//preliminary to make cdmlib-model test running until we completely moved to CdmCacher
|
47
|
new MockCdmCacher();
|
48
|
|
49
|
}
|
50
|
|
51
|
protected void setDefinedTerms(Class<? extends DefinedTermBase<?>> clazz, TermVocabulary<?> vocabulary) {
|
52
|
DefinedTermBase newInstance;
|
53
|
newInstance = getInstance(clazz);
|
54
|
newInstance.setDefaultTerms(vocabulary);
|
55
|
}
|
56
|
|
57
|
private <T extends DefinedTermBase> T getInstance(Class<? extends DefinedTermBase> termClass) {
|
58
|
try {
|
59
|
Constructor<T> c = ((Class<T>)termClass).getDeclaredConstructor();
|
60
|
c.setAccessible(true);
|
61
|
T termInstance = c.newInstance();
|
62
|
return termInstance;
|
63
|
} catch (Exception e) {
|
64
|
throw new RuntimeException(e);
|
65
|
}
|
66
|
}
|
67
|
|
68
|
|
69
|
}
|