1 |
a8300f09
|
Andreas Kohlbecker
|
/**
|
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 |
30936368
|
Andreas Kohlbecker
|
import java.util.Collection;
|
13 |
3d03a83f
|
Andreas Kohlbecker
|
import java.util.HashMap;
|
14 |
a8300f09
|
Andreas Kohlbecker
|
import java.util.List;
|
15 |
3d03a83f
|
Andreas Kohlbecker
|
import java.util.Map;
|
16 |
a8300f09
|
Andreas Kohlbecker
|
|
17 |
30936368
|
Andreas Kohlbecker
|
import javax.persistence.Entity;
|
18 |
|
|
|
19 |
|
|
import org.springframework.core.type.filter.AnnotationTypeFilter;
|
20 |
a8300f09
|
Andreas Kohlbecker
|
|
21 |
|
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
22 |
30936368
|
Andreas Kohlbecker
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
23 |
a8300f09
|
Andreas Kohlbecker
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
24 |
|
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
25 |
8ab014c8
|
Cherian Mathew
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
26 |
f1a1b9be
|
Andreas Kohlbecker
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
27 |
a8300f09
|
Andreas Kohlbecker
|
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 |
6142d1d2
|
Andreas Kohlbecker
|
public enum CdmBaseType {
|
36 |
a8300f09
|
Andreas Kohlbecker
|
|
37 |
2d1bfb73
|
Andreas Kohlbecker
|
/**
|
38 |
|
|
* refers to the baseClass {@link TaxonBase}
|
39 |
|
|
*/
|
40 |
a8300f09
|
Andreas Kohlbecker
|
TAXON(TaxonBase.class),
|
41 |
8ab014c8
|
Cherian Mathew
|
NONVIRALNAME(TaxonNameBase.class),
|
42 |
a8300f09
|
Andreas Kohlbecker
|
DESCRIPTION_ELEMENT(DescriptionElementBase.class),
|
43 |
f1a1b9be
|
Andreas Kohlbecker
|
DESCRIPTION(DescriptionBase.class),
|
44 |
|
|
SPECIMEN_OR_OBSERVATIONBASE(SpecimenOrObservationBase.class);
|
45 |
a8300f09
|
Andreas Kohlbecker
|
// TODO add all others
|
46 |
|
|
|
47 |
|
|
private Class<? extends CdmBase> baseClass;
|
48 |
|
|
|
49 |
30936368
|
Andreas Kohlbecker
|
private Collection<Class<? extends CdmBase>> subClasses;
|
50 |
a8300f09
|
Andreas Kohlbecker
|
|
51 |
3d03a83f
|
Andreas Kohlbecker
|
static Map<Class<? extends CdmBase>, Class<? extends CdmBase>> subTypeToBaseTypeMap;
|
52 |
|
|
|
53 |
6142d1d2
|
Andreas Kohlbecker
|
CdmBaseType(Class<? extends CdmBase> baseClass){
|
54 |
a8300f09
|
Andreas Kohlbecker
|
this.baseClass = baseClass;
|
55 |
|
|
try {
|
56 |
|
|
subClasses = subclassesFor(baseClass);
|
57 |
3d03a83f
|
Andreas Kohlbecker
|
updateBaseTypeMap();
|
58 |
a8300f09
|
Andreas Kohlbecker
|
} catch (ClassNotFoundException e) {
|
59 |
|
|
throw new RuntimeException(e);
|
60 |
|
|
}
|
61 |
|
|
}
|
62 |
|
|
|
63 |
3d03a83f
|
Andreas Kohlbecker
|
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 |
30936368
|
Andreas Kohlbecker
|
private Collection<Class<? extends CdmBase>> subclassesFor(Class<? extends CdmBase> clazz) throws ClassNotFoundException{
|
73 |
a8300f09
|
Andreas Kohlbecker
|
|
74 |
30936368
|
Andreas Kohlbecker
|
boolean includeAbstract = true;
|
75 |
|
|
boolean includeInterfaces = false;
|
76 |
6c629eea
|
Andreas Kohlbecker
|
|
77 |
30936368
|
Andreas Kohlbecker
|
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 |
a8300f09
|
Andreas Kohlbecker
|
}
|
84 |
|
|
|
85 |
3d03a83f
|
Andreas Kohlbecker
|
public static Class<? extends CdmBase> baseTypeFor(Class<? extends CdmBase> cdmType){
|
86 |
|
|
return subTypeToBaseTypeMap.get(cdmType);
|
87 |
|
|
}
|
88 |
|
|
|
89 |
30936368
|
Andreas Kohlbecker
|
public Collection<Class<? extends CdmBase>> getSubClasses() {
|
90 |
a8300f09
|
Andreas Kohlbecker
|
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 |
|
|
}
|