Project

General

Profile

Download (2.1 KB) Statistics
| Branch: | Tag: | Revision:
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.term;
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.term.init.ITermInitializer;
19
import eu.etaxonomy.cdm.model.term.init.ITermLoader;
20
import eu.etaxonomy.cdm.model.term.init.TermLoader;
21

    
22
/**
23
 * @author a.mueller
24
 */
25
public class DefaultTermInitializer implements ITermInitializer {
26

    
27
    @SuppressWarnings("unused")
28
	private static final Logger logger = Logger.getLogger(DefaultTermInitializer.class);
29
	protected ITermLoader termLoader = new TermLoader();
30

    
31
	private boolean omit = false;
32

    
33
	@Override
34
    public void initialize() {
35
		if (!omit){
36
		    termLoader.unloadAllTerms();
37
		    doInitialize();
38
		}
39
	}
40

    
41
	protected void doInitialize(){
42
		Map<UUID,DefinedTermBase> terms = new HashMap<>();
43

    
44
//		for(Class<? extends DefinedTermBase<?>> clazz : classesToInitialize) {
45
		for(VocabularyEnum vocabularyEnum : VocabularyEnum.values()) {
46
//			Class<? extends DefinedTermBase<?>> clazz = vocabularyEnum.getClazz();
47
			TermVocabulary<?> voc  = termLoader.loadTerms(vocabularyEnum, terms);
48
			setDefinedTerms(vocabularyEnum.getClazz(),voc);
49
		}
50
	}
51

    
52
	protected void setDefinedTerms(Class<? extends DefinedTermBase<?>> clazz, TermVocabulary<?> vocabulary) {
53
		DefinedTermBase newInstance;
54
		newInstance = getInstance(clazz);
55
		newInstance.setDefaultTerms(vocabulary);
56
	}
57

    
58
	private  <T extends DefinedTermBase> T getInstance(Class<? extends DefinedTermBase> termClass) {
59
		try {
60
			Constructor<T> c = ((Class<T>)termClass).getDeclaredConstructor();
61
			c.setAccessible(true);
62
			T termInstance = c.newInstance();
63
			return termInstance;
64
		} catch (Exception e) {
65
			throw new RuntimeException(e);
66
		}
67
	}
68

    
69
    public void setOmit(boolean omit) {
70
        this.omit = omit;
71
    }
72

    
73
    public boolean isOmit() {
74
        return omit;
75
    }
76
}
(1-1/33)