1
|
/**
|
2
|
* Copyright (C) 2018 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.vaadin.util.converter;
|
10
|
|
11
|
import java.text.ParseException;
|
12
|
import java.util.Locale;
|
13
|
|
14
|
import org.apache.logging.log4j.LogManager;
|
15
|
|
16
|
import com.vaadin.data.Validator;
|
17
|
import com.vaadin.data.util.converter.Converter;
|
18
|
|
19
|
import eu.etaxonomy.cdm.model.location.Point;
|
20
|
import eu.etaxonomy.cdm.model.location.Point.Sexagesimal;
|
21
|
|
22
|
/**
|
23
|
* Converts longitute and latitute string representations into
|
24
|
* double values. At the same time this class can be used as Validator.
|
25
|
*
|
26
|
* @author a.kohlbecker
|
27
|
* @since Nov 20, 2018
|
28
|
*
|
29
|
*/
|
30
|
public class GeoLocationConverterValidator implements Converter<String, Double>, Validator {
|
31
|
|
32
|
public enum Axis {
|
33
|
LONGITUDE, LATITUDE;
|
34
|
}
|
35
|
|
36
|
private static final long serialVersionUID = -1780028474672132160L;
|
37
|
|
38
|
private Axis axis;
|
39
|
|
40
|
public GeoLocationConverterValidator(Axis axis){
|
41
|
this.axis = axis;
|
42
|
}
|
43
|
|
44
|
@Override
|
45
|
public Double convertToModel(String value, Class<? extends Double> targetType, Locale locale)
|
46
|
throws com.vaadin.data.util.converter.Converter.ConversionException {
|
47
|
|
48
|
if(value == null){
|
49
|
return null;
|
50
|
}
|
51
|
try {
|
52
|
if(axis == Axis.LONGITUDE){
|
53
|
return Point.parseLongitude(value);
|
54
|
} else {
|
55
|
return Point.parseLatitude(value);
|
56
|
}
|
57
|
} catch (ParseException e) {
|
58
|
LogManager.getLogger(getClass()).error(e);
|
59
|
throw new ConversionException(e);
|
60
|
}
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
public String convertToPresentation(Double value, Class<? extends String> targetType, Locale locale)
|
65
|
throws com.vaadin.data.util.converter.Converter.ConversionException {
|
66
|
|
67
|
if(value == null){
|
68
|
return null;
|
69
|
}
|
70
|
if(axis == Axis.LONGITUDE){
|
71
|
return Sexagesimal.valueOf(value, false).toString();
|
72
|
} else {
|
73
|
return Sexagesimal.valueOf(value, true).toString();
|
74
|
}
|
75
|
|
76
|
}
|
77
|
|
78
|
@Override
|
79
|
public Class<Double> getModelType() {
|
80
|
return Double.class;
|
81
|
}
|
82
|
|
83
|
@Override
|
84
|
public Class<String> getPresentationType() {
|
85
|
return String.class;
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* {@inheritDoc}
|
90
|
*/
|
91
|
@Override
|
92
|
public void validate(Object value) throws InvalidValueException {
|
93
|
if(value != null && value instanceof String && !((String)value).isEmpty()){
|
94
|
try {
|
95
|
convertToModel((String)value, Double.class, null);
|
96
|
} catch (com.vaadin.data.util.converter.Converter.ConversionException e) {
|
97
|
throw new InvalidValueException("Invalid " + axis.name().toLowerCase());
|
98
|
}
|
99
|
}
|
100
|
|
101
|
|
102
|
}
|
103
|
|
104
|
}
|