1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2012 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.model;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.springframework.beans.factory.config.BeanDefinition;
|
18
|
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
19
|
import org.springframework.core.type.filter.AssignableTypeFilter;
|
20
|
|
21
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
22
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
23
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
24
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
25
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
26
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
27
|
|
28
|
/**
|
29
|
* Enumeration of all abstract CDM base types. Each provides a set of all sub types.
|
30
|
* @author a.kohlbecker
|
31
|
* @date Sep 28, 2012
|
32
|
*
|
33
|
*/
|
34
|
public enum CdmBaseType {
|
35
|
|
36
|
/**
|
37
|
* refers to the baseClass {@link TaxonBase}
|
38
|
*/
|
39
|
TAXON(TaxonBase.class),
|
40
|
NONVIRALNAME(TaxonNameBase.class),
|
41
|
DESCRIPTION_ELEMENT(DescriptionElementBase.class),
|
42
|
DESCRIPTION(DescriptionBase.class),
|
43
|
SPECIMEN_OR_OBSERVATIONBASE(SpecimenOrObservationBase.class);
|
44
|
// TODO add all others
|
45
|
|
46
|
private Class<? extends CdmBase> baseClass;
|
47
|
|
48
|
private Set<Class<? extends CdmBase>> subClasses;
|
49
|
|
50
|
CdmBaseType(Class<? extends CdmBase> baseClass){
|
51
|
this.baseClass = baseClass;
|
52
|
try {
|
53
|
subClasses = subclassesFor(baseClass);
|
54
|
} catch (ClassNotFoundException e) {
|
55
|
throw new RuntimeException(e);
|
56
|
}
|
57
|
}
|
58
|
|
59
|
public static Set<Class<? extends CdmBase>> subclassesFor(Class<? extends CdmBase> clazz) throws ClassNotFoundException{
|
60
|
|
61
|
Set<Class<? extends CdmBase>> subClasses = new HashSet<Class<? extends CdmBase>>();
|
62
|
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
|
63
|
provider.addIncludeFilter(new AssignableTypeFilter(clazz));
|
64
|
|
65
|
// scan only in eu.etaxonomy.cdm.model
|
66
|
Set<BeanDefinition> components = provider.findCandidateComponents("eu/etaxonomy/cdm/model");
|
67
|
for (BeanDefinition component : components)
|
68
|
{
|
69
|
subClasses.add((Class<? extends CdmBase>) Class.forName(component.getBeanClassName()));
|
70
|
}
|
71
|
return subClasses;
|
72
|
}
|
73
|
|
74
|
public Set<Class<? extends CdmBase>> getSubClasses() {
|
75
|
return subClasses;
|
76
|
}
|
77
|
|
78
|
public Class<? extends CdmBase> getBaseClass() {
|
79
|
return baseClass;
|
80
|
}
|
81
|
|
82
|
public List<String> getSubClassNames() {
|
83
|
List<String> names = new ArrayList<String>(subClasses.size());
|
84
|
for(Class<? extends CdmBase> clazz : subClasses){
|
85
|
names.add(clazz.getName());
|
86
|
}
|
87
|
return names;
|
88
|
}
|
89
|
|
90
|
}
|