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