1
|
/**
|
2
|
* Copyright (C) 2012 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;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Collection;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
|
17
|
import javax.persistence.Entity;
|
18
|
|
19
|
import org.springframework.core.type.filter.AnnotationTypeFilter;
|
20
|
|
21
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
22
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
23
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
24
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
25
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
26
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
27
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
28
|
|
29
|
/**
|
30
|
* Enumeration of all abstract CDM base types. Each provides a set of all sub types.
|
31
|
* @author a.kohlbecker
|
32
|
* @date Sep 28, 2012
|
33
|
*
|
34
|
*/
|
35
|
public enum CdmBaseType {
|
36
|
|
37
|
/**
|
38
|
* refers to the baseClass {@link TaxonBase}
|
39
|
*/
|
40
|
TAXON(TaxonBase.class),
|
41
|
NONVIRALNAME(TaxonNameBase.class),
|
42
|
DESCRIPTION_ELEMENT(DescriptionElementBase.class),
|
43
|
DESCRIPTION(DescriptionBase.class),
|
44
|
SPECIMEN_OR_OBSERVATIONBASE(SpecimenOrObservationBase.class);
|
45
|
// TODO add all others
|
46
|
|
47
|
private Class<? extends CdmBase> baseClass;
|
48
|
|
49
|
private Collection<Class<? extends CdmBase>> subClasses;
|
50
|
|
51
|
static Map<Class<? extends CdmBase>, Class<? extends CdmBase>> subTypeToBaseTypeMap;
|
52
|
|
53
|
CdmBaseType(Class<? extends CdmBase> baseClass){
|
54
|
this.baseClass = baseClass;
|
55
|
try {
|
56
|
subClasses = subclassesFor(baseClass);
|
57
|
updateBaseTypeMap();
|
58
|
} catch (ClassNotFoundException e) {
|
59
|
throw new RuntimeException(e);
|
60
|
}
|
61
|
}
|
62
|
|
63
|
private void updateBaseTypeMap(){
|
64
|
if(CdmBaseType.subTypeToBaseTypeMap == null){
|
65
|
CdmBaseType.subTypeToBaseTypeMap = new HashMap<Class<? extends CdmBase>, Class<? extends CdmBase>>();
|
66
|
}
|
67
|
for(Class<? extends CdmBase> subClass : subClasses){
|
68
|
CdmBaseType.subTypeToBaseTypeMap.put(subClass, baseClass);
|
69
|
}
|
70
|
}
|
71
|
|
72
|
private Collection<Class<? extends CdmBase>> subclassesFor(Class<? extends CdmBase> clazz) throws ClassNotFoundException{
|
73
|
|
74
|
boolean includeAbstract = true;
|
75
|
boolean includeInterfaces = false;
|
76
|
|
77
|
CdmTypeScanner<CdmBase> scanner = new CdmTypeScanner<CdmBase>(includeAbstract, includeInterfaces);
|
78
|
scanner.addIncludeFilter(new CdmAssignableTypeFilter(IdentifiableEntity.class, includeAbstract, includeInterfaces));
|
79
|
scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
|
80
|
Collection<Class<? extends CdmBase>> classes = scanner.scanTypesIn("eu/etaxonomy/cdm/model");
|
81
|
|
82
|
return classes;
|
83
|
}
|
84
|
|
85
|
public static Class<? extends CdmBase> baseTypeFor(Class<? extends CdmBase> cdmType){
|
86
|
return subTypeToBaseTypeMap.get(cdmType);
|
87
|
}
|
88
|
|
89
|
public Collection<Class<? extends CdmBase>> getSubClasses() {
|
90
|
return subClasses;
|
91
|
}
|
92
|
|
93
|
public Class<? extends CdmBase> getBaseClass() {
|
94
|
return baseClass;
|
95
|
}
|
96
|
|
97
|
public List<String> getSubClassNames() {
|
98
|
List<String> names = new ArrayList<String>(subClasses.size());
|
99
|
for(Class<? extends CdmBase> clazz : subClasses){
|
100
|
names.add(clazz.getName());
|
101
|
}
|
102
|
return names;
|
103
|
}
|
104
|
|
105
|
}
|