3 * Copyright (C) 2012 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.model
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collection
;
14 import java
.util
.HashMap
;
15 import java
.util
.List
;
18 import javax
.persistence
.Entity
;
20 import org
.springframework
.core
.type
.filter
.AnnotationTypeFilter
;
22 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
23 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
24 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
25 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
26 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
27 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
31 * Enumeration of all abstract CDM base types. Each provides a set of all sub types.
32 * @author a.kohlbecker
36 public enum CdmBaseType
{
39 * refers to the baseClass {@link TaxonBase}
41 TAXON(TaxonBase
.class),
42 NONVIRALNAME(TaxonNameBase
.class),
43 DESCRIPTION_ELEMENT(DescriptionElementBase
.class),
44 DESCRIPTION(DescriptionBase
.class),
45 SPECIMEN_OR_OBSERVATIONBASE(SpecimenOrObservationBase
.class);
46 // TODO add all others
48 private Class
<?
extends CdmBase
> baseClass
;
50 private Collection
<Class
<?
extends CdmBase
>> subClasses
;
52 static Map
<Class
<?
extends CdmBase
>, Class
<?
extends CdmBase
>> subTypeToBaseTypeMap
;
54 CdmBaseType(Class
<?
extends CdmBase
> baseClass
){
55 this.baseClass
= baseClass
;
57 subClasses
= subclassesFor(baseClass
);
59 } catch (ClassNotFoundException e
) {
60 throw new RuntimeException(e
);
64 private void updateBaseTypeMap(){
65 if(CdmBaseType
.subTypeToBaseTypeMap
== null){
66 CdmBaseType
.subTypeToBaseTypeMap
= new HashMap
<Class
<?
extends CdmBase
>, Class
<?
extends CdmBase
>>();
68 for(Class
<?
extends CdmBase
> subClass
: subClasses
){
69 CdmBaseType
.subTypeToBaseTypeMap
.put(subClass
, baseClass
);
73 private Collection
<Class
<?
extends CdmBase
>> subclassesFor(Class
<?
extends CdmBase
> clazz
) throws ClassNotFoundException
{
75 boolean includeAbstract
= true;
76 boolean includeInterfaces
= false;
78 CdmTypeScanner
<CdmBase
> scanner
= new CdmTypeScanner
<CdmBase
>(includeAbstract
, includeInterfaces
);
79 scanner
.addIncludeFilter(new CdmAssignableTypeFilter(IdentifiableEntity
.class, includeAbstract
, includeInterfaces
));
80 scanner
.addIncludeFilter(new AnnotationTypeFilter(Entity
.class));
81 Collection
<Class
<?
extends CdmBase
>> classes
= scanner
.scanTypesIn("eu/etaxonomy/cdm/model");
86 public static Class
<?
extends CdmBase
> baseTypeFor(Class
<?
extends CdmBase
> cdmType
){
87 return subTypeToBaseTypeMap
.get(cdmType
);
90 public Collection
<Class
<?
extends CdmBase
>> getSubClasses() {
94 public Class
<?
extends CdmBase
> getBaseClass() {
98 public List
<String
> getSubClassNames() {
99 List
<String
> names
= new ArrayList
<String
>(subClasses
.size());
100 for(Class
<?
extends CdmBase
> clazz
: subClasses
){
101 names
.add(clazz
.getName());