2 * Copyright (C) 2012 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.model
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
13 import java
.util
.HashMap
;
14 import java
.util
.List
;
17 import javax
.persistence
.Entity
;
19 import org
.springframework
.core
.type
.filter
.AnnotationTypeFilter
;
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
.TaxonName
;
26 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
30 * Enumeration of all abstract CDM base types. Each provides a set of all sub types.
31 * @author a.kohlbecker
35 public enum CdmBaseType
{
38 * refers to the baseClass {@link TaxonBase}
40 TAXON(TaxonBase
.class),
41 NONVIRALNAME(TaxonName
.class),
42 DESCRIPTION_ELEMENT(DescriptionElementBase
.class),
43 DESCRIPTION(DescriptionBase
.class),
44 SPECIMEN_OR_OBSERVATIONBASE(SpecimenOrObservationBase
.class);
45 // TODO add all others
47 private Class
<?
extends CdmBase
> baseClass
;
49 private Collection
<Class
<?
extends CdmBase
>> subClasses
;
51 static Map
<Class
<?
extends CdmBase
>, Class
<?
extends CdmBase
>> subTypeToBaseTypeMap
;
53 CdmBaseType(Class
<?
extends CdmBase
> baseClass
){
54 this.baseClass
= baseClass
;
56 subClasses
= subclassesFor(baseClass
);
58 } catch (ClassNotFoundException e
) {
59 throw new RuntimeException(e
);
63 private void updateBaseTypeMap(){
64 if(CdmBaseType
.subTypeToBaseTypeMap
== null){
65 CdmBaseType
.subTypeToBaseTypeMap
= new HashMap
<Class
<?
extends CdmBase
>, Class
<?
extends CdmBase
>>();
67 for(Class
<?
extends CdmBase
> subClass
: subClasses
){
68 CdmBaseType
.subTypeToBaseTypeMap
.put(subClass
, baseClass
);
72 private Collection
<Class
<?
extends CdmBase
>> subclassesFor(Class
<?
extends CdmBase
> clazz
) throws ClassNotFoundException
{
74 boolean includeAbstract
= true;
75 boolean includeInterfaces
= false;
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");
85 public static Class
<?
extends CdmBase
> baseTypeFor(Class
<?
extends CdmBase
> cdmType
){
86 return subTypeToBaseTypeMap
.get(cdmType
);
89 public Collection
<Class
<?
extends CdmBase
>> getSubClasses() {
93 public Class
<?
extends CdmBase
> getBaseClass() {
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());