1
|
package cz.zcu.kiv.backendapi.catalog;
|
2
|
|
3
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
4
|
import lombok.AllArgsConstructor;
|
5
|
import lombok.Data;
|
6
|
import lombok.NoArgsConstructor;
|
7
|
|
8
|
import java.util.*;
|
9
|
import java.util.regex.Matcher;
|
10
|
import java.util.regex.Pattern;
|
11
|
import java.util.stream.Collectors;
|
12
|
|
13
|
/**
|
14
|
* Class representing catalog item DTO
|
15
|
*/
|
16
|
@Data
|
17
|
@AllArgsConstructor
|
18
|
@NoArgsConstructor
|
19
|
public class CatalogItemDto {
|
20
|
private static final String INTEGER_PATTERN = "\\d+";
|
21
|
private static final String DOUBLE_PATTERN = "(\\d+[.]\\d+)|(\\d+)";
|
22
|
private static final String EMPTY_ENTRY = "–";
|
23
|
/**
|
24
|
* Id
|
25
|
*/
|
26
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
27
|
private UUID id;
|
28
|
|
29
|
/**
|
30
|
* Name of catalog item
|
31
|
*/
|
32
|
private String name;
|
33
|
|
34
|
/**
|
35
|
* Certainty
|
36
|
*/
|
37
|
private int certainty;
|
38
|
|
39
|
/**
|
40
|
* Longitude
|
41
|
*/
|
42
|
private double longitude;
|
43
|
|
44
|
/**
|
45
|
* Latitude
|
46
|
*/
|
47
|
private double latitude;
|
48
|
|
49
|
/**
|
50
|
* Bibliography
|
51
|
*/
|
52
|
private Set<String> bibliography = Collections.emptySet();
|
53
|
|
54
|
/**
|
55
|
* Countries
|
56
|
*/
|
57
|
private Set<String> countries = Collections.emptySet();
|
58
|
|
59
|
/**
|
60
|
* Written forms
|
61
|
*/
|
62
|
private Set<String> writtenForms = Collections.emptySet();
|
63
|
|
64
|
/**
|
65
|
* Alternative names
|
66
|
*/
|
67
|
private Set<String> alternativeNames = Collections.emptySet();
|
68
|
|
69
|
/**
|
70
|
* Types
|
71
|
*/
|
72
|
private Set<String> types = Collections.emptySet();
|
73
|
|
74
|
public CatalogItemDto(List<String> csvFields) {
|
75
|
this.name = csvFields.get(1);
|
76
|
Set<String> stringList = processListField(csvFields.get(2));
|
77
|
this.alternativeNames = new HashSet<>(stringList);
|
78
|
|
79
|
this.certainty = processIntField(csvFields.get(3));
|
80
|
this.latitude = processDoubleField(csvFields.get(4));
|
81
|
this.longitude = processDoubleField(csvFields.get(5));
|
82
|
|
83
|
stringList = processListField(csvFields.get(6));
|
84
|
this.writtenForms = new HashSet<>(stringList);
|
85
|
|
86
|
stringList = processListField(csvFields.get(7));
|
87
|
this.types = new HashSet<>(stringList);
|
88
|
|
89
|
stringList = processListField(csvFields.get(8));
|
90
|
this.countries = new HashSet<>(stringList);
|
91
|
|
92
|
stringList = processListField(csvFields.get(9));
|
93
|
this.bibliography = new HashSet<>(stringList);
|
94
|
}
|
95
|
|
96
|
private int processIntField(String field) {
|
97
|
Matcher matcher = Pattern.compile(INTEGER_PATTERN).matcher(field);
|
98
|
if (matcher.find()) {
|
99
|
return Integer.parseInt(matcher.group());
|
100
|
} else {
|
101
|
return 0;
|
102
|
}
|
103
|
}
|
104
|
|
105
|
private double processDoubleField(String field) {
|
106
|
Matcher matcher = Pattern.compile(DOUBLE_PATTERN).matcher(field);
|
107
|
if (matcher.find()) {
|
108
|
return Double.parseDouble(matcher.group());
|
109
|
} else {
|
110
|
return 0.0;
|
111
|
}
|
112
|
}
|
113
|
|
114
|
private Set<String> processListField(String field) {
|
115
|
if (field.isEmpty() || field.equals(EMPTY_ENTRY)) {
|
116
|
return new HashSet<>();
|
117
|
}
|
118
|
return Arrays.stream(field.split(",")).map(String::trim).filter(item -> !item.isEmpty()).collect(Collectors.toSet());
|
119
|
}
|
120
|
|
121
|
}
|