2 * Copyright (C) 2007 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
.io
.jaxb
;
11 import java
.util
.HashMap
;
12 import java
.util
.UUID
;
13 import java
.util
.concurrent
.Callable
;
15 import javax
.xml
.bind
.ValidationEventHandler
;
17 import org
.apache
.logging
.log4j
.LogManager
;
18 import org
.apache
.logging
.log4j
.Logger
;
19 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
20 import org
.xml
.sax
.SAXException
;
22 import com
.sun
.xml
.bind
.IDResolver
;
24 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.ICollectionService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.IDescriptionService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IMediaService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
29 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
30 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
31 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
32 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
33 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
34 import eu
.etaxonomy
.cdm
.api
.service
.ITermTreeService
;
35 import eu
.etaxonomy
.cdm
.api
.service
.IUserService
;
36 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
37 import eu
.etaxonomy
.cdm
.jaxb
.UUIDAdapter
;
38 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
39 import eu
.etaxonomy
.cdm
.model
.agent
.INomenclaturalAuthor
;
40 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
41 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
42 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
43 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
44 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
45 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
46 import eu
.etaxonomy
.cdm
.model
.permission
.User
;
47 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
49 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
50 import eu
.etaxonomy
.cdm
.model
.term
.TermTree
;
51 import eu
.etaxonomy
.cdm
.model
.term
.TermVocabulary
;
53 public class CdmIDResolver
extends IDResolver
{
55 private static final Logger logger
= LogManager
.getLogger();
57 private IUserService userService
;
59 private IAgentService agentService
;
61 private ITermService termService
;
63 private IVocabularyService vocabularyService
;
65 private IDescriptionService descriptionService
;
67 private ITermTreeService termTreeService
;
69 private IMediaService mediaService
;
71 private INameService nameService
;
73 private IOccurrenceService occurrenceService
;
75 private ICollectionService collectionService
;
77 private IReferenceService referenceService
;
79 private ITaxonService taxonService
;
82 public void setUserService(IUserService userService
) {
83 this.userService
= userService
;
87 public void setAgentService(IAgentService agentService
) {
88 this.agentService
= agentService
;
92 public void setTermService(ITermService termService
) {
93 this.termService
= termService
;
97 public void setVocabularyService(IVocabularyService vocabularyService
) {
98 this.vocabularyService
= vocabularyService
;
102 public void setDescriptionService(IDescriptionService descriptionService
) {
103 this.descriptionService
= descriptionService
;
107 public void setTermTreeService(ITermTreeService termTreeService
) {
108 this.termTreeService
= termTreeService
;
112 public void setMediaService(IMediaService mediaService
) {
113 this.mediaService
= mediaService
;
117 public void setNameService(INameService nameService
) {
118 this.nameService
= nameService
;
122 public void setOccurrenceService(IOccurrenceService occurrenceService
) {
123 this.occurrenceService
= occurrenceService
;
127 public void setCollectionService(ICollectionService collectionService
) {
128 this.collectionService
= collectionService
;
132 public void setReferenceService(IReferenceService referenceService
) {
133 this.referenceService
= referenceService
;
137 public void setTaxonService(ITaxonService taxonService
) {
138 this.taxonService
= taxonService
;
141 private HashMap
<String
,Object
> idmap
= null;
144 public void startDocument(ValidationEventHandler eventHandler
) throws SAXException
{
151 public void bind(String id
, Object obj
) throws SAXException
{
153 idmap
= new HashMap
<>();
160 public Callable
<Object
> resolve(final String id
, final Class targetType
) throws SAXException
{
161 return new Callable
<Object
>() {
163 public Object
call() throws Exception
{
164 logger
.info("Resolving " + id
+ " for class " + targetType
);
166 if(idmap
==null || !idmap
.containsKey(id
)) {
168 String uuidPart
= id
.substring(UUIDAdapter
.UUID_URN_PREFIX
.length());
169 UUID uuid
= UUID
.fromString(uuidPart
);
170 logger
.info(uuid
+ " not in idmap, looking in database");
171 if(targetType
.equals(User
.class)) {
172 return resolveObject(uuid
, targetType
, userService
);
173 } else if(AgentBase
.class.isAssignableFrom(targetType
) || INomenclaturalAuthor
.class.isAssignableFrom(targetType
)) {
174 return resolveObject(uuid
, targetType
, agentService
);
175 } else if(DefinedTermBase
.class.isAssignableFrom(targetType
)) {
176 return resolveObject(uuid
, targetType
, termService
);
177 } else if(TermVocabulary
.class.isAssignableFrom(targetType
)) {
178 return resolveObject(uuid
, targetType
, vocabularyService
);
179 } else if(DescriptionBase
.class.isAssignableFrom(targetType
)) {
180 return resolveObject(uuid
, targetType
, descriptionService
);
181 } else if(TermTree
.class.isAssignableFrom(targetType
)) {
182 return resolveObject(uuid
, targetType
, termTreeService
);
183 } else if(Media
.class.isAssignableFrom(targetType
)) {
184 return resolveObject(uuid
, targetType
, mediaService
);
185 } else if(TaxonName
.class.isAssignableFrom(targetType
)) {
186 return resolveObject(uuid
, targetType
, nameService
);
187 } else if(SpecimenOrObservationBase
.class.isAssignableFrom(targetType
)) {
188 return resolveObject(uuid
, targetType
, occurrenceService
);
189 } else if(Collection
.class.isAssignableFrom(targetType
)) {
190 return resolveObject(uuid
, targetType
, collectionService
);
191 } else if(Reference
.class.isAssignableFrom(targetType
)) {
192 return resolveObject(uuid
, targetType
, referenceService
);
193 } else if(TaxonBase
.class.isAssignableFrom(targetType
)) {
194 return resolveObject(uuid
, targetType
, taxonService
);
195 } else if(HomotypicalGroup
.class.isAssignableFrom(targetType
)) {
196 Object object
= nameService
.findHomotypicalGroup(uuid
);
198 throw new SAXException(targetType
.getSimpleName() + " with " + uuid
+ " not found");
203 * Collections of IDREFS do not have a type at runtime
204 * https://jaxb.dev.java.net/issues/show_bug.cgi?id=546
206 * Maybe in the future we'll be able to add targetType to IDREF
207 * but the spec has to be changed first so no fix is likely at the moment
211 AgentBase
<?
> agent
= agentService
.find(uuid
);
215 DefinedTermBase
<?
> term
= termService
.find(uuid
);
219 Media media
= mediaService
.find(uuid
);
223 throw new SAXException(targetType
.getSimpleName() + " with " + uuid
+ " not found");
226 return idmap
.get(id
);
232 private Object
resolveObject(UUID uuid
, Class targetType
, IService service
) throws SAXException
{
233 Object object
= service
.find(uuid
);
235 throw new SAXException(targetType
.getSimpleName() + " with " + uuid
+ " not found");