1
|
/**
|
2
|
* Copyright (C) 2007 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.remote.dto.assembler.converter;
|
10
|
|
11
|
import java.net.MalformedURLException;
|
12
|
import eu.etaxonomy.cdm.common.URI;
|
13
|
import java.net.URISyntaxException;
|
14
|
import java.net.URL;
|
15
|
|
16
|
import javax.servlet.ServletContext;
|
17
|
|
18
|
import org.apache.commons.lang.StringUtils;
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.dozer.CustomConverter;
|
21
|
import org.dozer.MappingException;
|
22
|
import org.springframework.web.context.ServletContextAware;
|
23
|
|
24
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
25
|
import eu.etaxonomy.cdm.model.common.LSID;
|
26
|
|
27
|
public class IdentifierConverter implements CustomConverter, ServletContextAware{
|
28
|
|
29
|
private ServletContext servletContext;
|
30
|
|
31
|
public static final Logger logger = Logger.getLogger(IdentifierConverter.class);
|
32
|
|
33
|
@SuppressWarnings("unchecked")
|
34
|
public Object convert(Object destination, Object source, Class destClass, Class sourceClass) {
|
35
|
if (source == null) {
|
36
|
return null;
|
37
|
}
|
38
|
if (source instanceof IdentifiableEntity) {
|
39
|
IdentifiableEntity identifiableEntity = (IdentifiableEntity)source;
|
40
|
URI uri = null;
|
41
|
try {
|
42
|
if(identifiableEntity.getLsid() != null && identifiableEntity.getLsid().getLsid() != null){
|
43
|
uri = new URI(((LSID)source).getLsid());
|
44
|
} else {
|
45
|
StringBuilder stringBuilder = new StringBuilder();
|
46
|
if(servletContext != null){
|
47
|
stringBuilder.append(servletContext.getContextPath()).append('/');
|
48
|
} else {
|
49
|
//happens only during testing
|
50
|
logger.warn("No ServletContext configured in Unitils");
|
51
|
}
|
52
|
String[] classNameTokens = StringUtils.split(source.getClass().getName(), '.');
|
53
|
stringBuilder.append(classNameTokens[classNameTokens.length - 2]).append('/');
|
54
|
stringBuilder.append(identifiableEntity.getUuid().toString());
|
55
|
uri = new URI(stringBuilder.toString());
|
56
|
}
|
57
|
} catch (URISyntaxException e) {
|
58
|
throwMappingException(destination, source);
|
59
|
}
|
60
|
if(uri != null){
|
61
|
if(String.class.isAssignableFrom(destClass)){
|
62
|
return uri.toString();
|
63
|
} else if(URL.class.isAssignableFrom(destClass)){
|
64
|
try {
|
65
|
return uri.toURL();
|
66
|
} catch (MalformedURLException e) {
|
67
|
logger.error(e);
|
68
|
}
|
69
|
} else if(URI.class.isAssignableFrom(destClass)){
|
70
|
return uri;
|
71
|
} else {
|
72
|
throwMappingException(destination, source);
|
73
|
}
|
74
|
}
|
75
|
} else {
|
76
|
throwMappingException(destination, source);
|
77
|
}
|
78
|
return null;
|
79
|
}
|
80
|
|
81
|
private void throwMappingException(Object destination, Object source) {
|
82
|
throw new MappingException("Converter TestCustomConverter used incorrectly. Arguments passed in were:"
|
83
|
+ destination + " and " + source);
|
84
|
}
|
85
|
|
86
|
/* (non-Javadoc)
|
87
|
* @see org.springframework.web.context.ServletContextAware#setServletContext(javax.servlet.ServletContext)
|
88
|
*/
|
89
|
@Override
|
90
|
public void setServletContext(ServletContext servletContext) {
|
91
|
this.servletContext = servletContext;
|
92
|
}
|
93
|
|
94
|
}
|