1 package eu
.etaxonomy
.cdm
.io
.jaxb
;
3 import java
.util
.HashMap
;
5 import java
.util
.concurrent
.Callable
;
7 import javax
.xml
.bind
.ValidationEventHandler
;
9 import org
.apache
.log4j
.Logger
;
10 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
11 import org
.xml
.sax
.SAXException
;
13 import com
.sun
.xml
.bind
.IDResolver
;
15 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
16 import eu
.etaxonomy
.cdm
.api
.service
.ICollectionService
;
17 import eu
.etaxonomy
.cdm
.api
.service
.IDescriptionService
;
18 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureTreeService
;
19 import eu
.etaxonomy
.cdm
.api
.service
.IMediaService
;
20 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
21 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
22 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
23 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
24 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.IUserService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
28 import eu
.etaxonomy
.cdm
.jaxb
.UUIDAdapter
;
29 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
30 import eu
.etaxonomy
.cdm
.model
.agent
.INomenclaturalAuthor
;
31 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
33 import eu
.etaxonomy
.cdm
.model
.common
.User
;
34 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
35 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
36 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
37 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
38 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
39 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
40 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
41 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
42 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
44 public class CdmIDResolver
extends IDResolver
{
45 private static final Logger logger
= Logger
.getLogger(CdmIDResolver
.class);
47 IUserService userService
;
49 IAgentService agentService
;
51 ITermService termService
;
53 IVocabularyService vocabularyService
;
55 IDescriptionService descriptionService
;
57 IFeatureTreeService featureTreeService
;
59 IMediaService mediaService
;
61 INameService nameService
;
63 IOccurrenceService occurrenceService
;
65 ICollectionService collectionService
;
67 IReferenceService referenceService
;
69 ITaxonService taxonService
;
72 public void setUserService(IUserService userService
) {
73 this.userService
= userService
;
77 public void setAgentService(IAgentService agentService
) {
78 this.agentService
= agentService
;
82 public void setTermService(ITermService termService
) {
83 this.termService
= termService
;
87 public void setVocabularyService(IVocabularyService vocabularyService
) {
88 this.vocabularyService
= vocabularyService
;
92 public void setDescriptionService(IDescriptionService descriptionService
) {
93 this.descriptionService
= descriptionService
;
97 public void setFeatureTreeService(IFeatureTreeService featureTreeService
) {
98 this.featureTreeService
= featureTreeService
;
102 public void setMediaService(IMediaService mediaService
) {
103 this.mediaService
= mediaService
;
107 public void setNameService(INameService nameService
) {
108 this.nameService
= nameService
;
112 public void setOccurrenceService(IOccurrenceService occurrenceService
) {
113 this.occurrenceService
= occurrenceService
;
117 public void setCollectionService(ICollectionService collectionService
) {
118 this.collectionService
= collectionService
;
122 public void setReferenceService(IReferenceService referenceService
) {
123 this.referenceService
= referenceService
;
127 public void setTaxonService(ITaxonService taxonService
) {
128 this.taxonService
= taxonService
;
131 private HashMap
<String
,Object
> idmap
= null;
134 public void startDocument(ValidationEventHandler eventHandler
) throws SAXException
{
141 public void bind(String id
, Object obj
) throws SAXException
{
143 idmap
= new HashMap
<String
,Object
>();
150 public Callable
<?
> resolve(final String id
, final Class targetType
) throws SAXException
{
151 return new Callable() {
152 public Object
call() throws Exception
{
153 logger
.info("Resolving " + id
+ " for class " + targetType
);
155 if(idmap
==null || !idmap
.containsKey(id
)) {
157 String uuidPart
= id
.substring(UUIDAdapter
.UUID_URN_PREFIX
.length());
158 UUID uuid
= UUID
.fromString(uuidPart
);
159 logger
.info(uuid
+ " not in idmap, looking in database");
160 if(targetType
.equals(User
.class)) {
161 return resolveObject(uuid
, targetType
, userService
);
162 } else if(AgentBase
.class.isAssignableFrom(targetType
) || INomenclaturalAuthor
.class.isAssignableFrom(targetType
)) {
163 return resolveObject(uuid
, targetType
, agentService
);
164 } else if(DefinedTermBase
.class.isAssignableFrom(targetType
)) {
165 return resolveObject(uuid
, targetType
, termService
);
166 } else if(TermVocabulary
.class.isAssignableFrom(targetType
)) {
167 return resolveObject(uuid
, targetType
, vocabularyService
);
168 } else if(DescriptionBase
.class.isAssignableFrom(targetType
)) {
169 return resolveObject(uuid
, targetType
, descriptionService
);
170 } else if(FeatureTree
.class.isAssignableFrom(targetType
)) {
171 return resolveObject(uuid
, targetType
, featureTreeService
);
172 } else if(Media
.class.isAssignableFrom(targetType
)) {
173 return resolveObject(uuid
, targetType
, mediaService
);
174 } else if(TaxonNameBase
.class.isAssignableFrom(targetType
)) {
175 return resolveObject(uuid
, targetType
, nameService
);
176 } else if(SpecimenOrObservationBase
.class.isAssignableFrom(targetType
)) {
177 return resolveObject(uuid
, targetType
, occurrenceService
);
178 } else if(Collection
.class.isAssignableFrom(targetType
)) {
179 return resolveObject(uuid
, targetType
, collectionService
);
180 } else if(Reference
.class.isAssignableFrom(targetType
)) {
181 return resolveObject(uuid
, targetType
, referenceService
);
182 } else if(TaxonBase
.class.isAssignableFrom(targetType
)) {
183 return resolveObject(uuid
, targetType
, taxonService
);
184 } else if(HomotypicalGroup
.class.isAssignableFrom(targetType
)) {
185 Object object
= nameService
.findHomotypicalGroup(uuid
);
187 throw new SAXException(targetType
.getSimpleName() + " with " + uuid
+ " not found");
192 * Collections of IDREFS do not have a type at runtime
193 * https://jaxb.dev.java.net/issues/show_bug.cgi?id=546
195 * Maybe in the future we'll be able to add targetType to IDREF
196 * but the spec has to be changed first so no fix is likely at the moment
200 AgentBase agent
= agentService
.find(uuid
);
204 DefinedTermBase term
= termService
.find(uuid
);
208 Media media
= mediaService
.find(uuid
);
212 throw new SAXException(targetType
.getSimpleName() + " with " + uuid
+ " not found");
215 return idmap
.get(id
);
221 private Object
resolveObject(UUID uuid
, Class targetType
, IService service
) throws SAXException
{
222 Object object
= service
.find(uuid
);
224 throw new SAXException(targetType
.getSimpleName() + " with " + uuid
+ " not found");