Revision f1a25720
Added by Katja Luther over 6 years ago
- ID f1a25720755daa96e8838a617a660dfced7b9f31
- Parent 23e50194
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/dto/assembler/converter/DateTimeConverter.java | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* Copyright (C) 2007 EDIT |
3 |
* European Distributed Institute of Taxonomy
|
|
3 |
* European Distributed Institute of Taxonomy |
|
4 | 4 |
* http://www.e-taxonomy.eu |
5 |
*
|
|
5 |
* |
|
6 | 6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
7 | 7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
8 | 8 |
*/ |
9 | 9 |
package eu.etaxonomy.cdm.remote.dto.assembler.converter; |
10 | 10 |
|
11 | 11 |
|
12 |
import java.time.ZonedDateTime; |
|
13 |
import java.util.GregorianCalendar; |
|
14 |
|
|
12 | 15 |
import javax.xml.datatype.DatatypeConfigurationException; |
13 | 16 |
import javax.xml.datatype.DatatypeFactory; |
14 | 17 |
import javax.xml.datatype.XMLGregorianCalendar; |
15 | 18 |
|
16 | 19 |
import org.dozer.CustomConverter; |
17 | 20 |
import org.dozer.MappingException; |
18 |
import org.joda.time.DateTime; |
|
21 |
|
|
19 | 22 |
|
20 | 23 |
public class DateTimeConverter implements CustomConverter { |
21 | 24 |
|
22 |
public Object convert(Object destination, Object source, Class destClass, Class sourceClass) { |
|
25 |
@Override |
|
26 |
public Object convert(Object destination, Object source, Class destClass, Class sourceClass) { |
|
23 | 27 |
if (source == null) { |
24 | 28 |
return null; |
25 | 29 |
} |
26 | 30 |
Object result = null; |
27 |
if (source instanceof DateTime) { |
|
28 |
if(destClass.equals(DateTime.class)){ |
|
29 |
result = new DateTime(((DateTime)source));
|
|
31 |
if (source instanceof ZonedDateTime) {
|
|
32 |
if(destClass.equals(ZonedDateTime.class)){
|
|
33 |
result = source;
|
|
30 | 34 |
} else if(destClass.equals(XMLGregorianCalendar.class)){ |
31 |
result = dataTypeFactory().newXMLGregorianCalendar(((DateTime)source).toGregorianCalendar()); //naive approach, may mot result in correct representation of partial datetime
|
|
32 |
}
|
|
35 |
result = dataTypeFactory().newXMLGregorianCalendar( GregorianCalendar.from((ZonedDateTime)source)); //naive approach, may mot result in correct representation of partial datetime
|
|
36 |
} |
|
33 | 37 |
} |
34 |
|
|
38 |
|
|
35 | 39 |
if(result == null){ |
36 | 40 |
throw new MappingException("Converter TestCustomConverter used incorrectly. Arguments passed in were:" |
37 | 41 |
+ destination + " and " + source); |
38 | 42 |
} |
39 | 43 |
return result; |
40 | 44 |
} |
41 |
|
|
45 |
|
|
42 | 46 |
/** |
43 | 47 |
* Cache the DatatypeFactory because newInstance is very expensive. |
44 | 48 |
*/ |
45 | 49 |
private static DatatypeFactory dataTypeFactory; |
46 |
|
|
50 |
|
|
47 | 51 |
/** |
48 | 52 |
* Returns a new instance of DatatypeFactory, or the cached one if previously created. |
49 | 53 |
* |
Also available in: Unified diff
first implementation for change from joda time to java8 time