Projekt

Obecné

Profil

Stáhnout (7.45 KB) Statistiky
| Větev: | Tag: | Revize:
1
package cz.zcu.kiv.backendapi.external;
2

    
3
import io.swagger.v3.oas.annotations.media.Schema;
4
import lombok.*;
5

    
6
import javax.persistence.*;
7
import java.util.*;
8
import java.util.regex.Matcher;
9
import java.util.regex.Pattern;
10
import java.util.stream.Collectors;
11

    
12
/**
13
 * Class representing external catalog items
14
 */
15
@Getter
16
@Setter
17
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
18
@NoArgsConstructor
19
@AllArgsConstructor
20
@Entity
21
@Table(name = "external_catalog_items")
22
@Schema(name = "ExternalCatalogItemDto")
23
public class ExternalCatalogItem {
24
    /**
25
     * Map where key is country code and value is name of country
26
     */
27
    protected static final Map<String, String> COUNTRY_CODES;
28

    
29
    /**
30
     * Integer pattern for finding int value in string
31
     */
32
    private static final Pattern INTEGER_PATTERN = Pattern.compile("\\d+");
33

    
34
    /**
35
     * Double pattern for finding double value in string
36
     */
37
    private static final Pattern DOUBLE_PATTERN = Pattern.compile("(\\d+[.]\\d+)|(\\d+)");
38

    
39
    static {
40
        COUNTRY_CODES = new LinkedHashMap<>();
41
        COUNTRY_CODES.put("IQ", "Iraq");
42
        COUNTRY_CODES.put("IR", "Iran");
43
        COUNTRY_CODES.put("SY", "Syria");
44
        COUNTRY_CODES.put("TR", "Turkey");
45
        COUNTRY_CODES.put("JO", "Jordan");
46
        COUNTRY_CODES.put("LB", "Lebanon");
47
        COUNTRY_CODES.put("IL", "Israel");
48
        COUNTRY_CODES.put("PS", "Palestine");
49
        COUNTRY_CODES.put("CY", "Cyprus");
50
        COUNTRY_CODES.put("EG", "Egypt");
51
        COUNTRY_CODES.put("SD", "Sudan");
52
        COUNTRY_CODES.put("BH", "Bahrain");
53
        COUNTRY_CODES.put("KW", "Kuwait");
54
        COUNTRY_CODES.put("OM", "Oman");
55
        COUNTRY_CODES.put("SA", "Saudi Arabia");
56
        COUNTRY_CODES.put("AE", "United Arab Emirates");
57
        COUNTRY_CODES.put("YE", "Yemen");
58
        COUNTRY_CODES.put("AF", "Afghanistan");
59
        COUNTRY_CODES.put("AM", "Armenia");
60
        COUNTRY_CODES.put("GE", "Georgia");
61
    }
62

    
63
    /**
64
     * Id
65
     */
66
    @Id
67
    @EqualsAndHashCode.Include
68
    UUID id = UUID.randomUUID();
69

    
70
    /**
71
     * External source
72
     */
73
    @Enumerated(EnumType.STRING)
74
    private ExternalSource externalSource;
75

    
76
    /**
77
     * Latitude
78
     */
79
    private Double latitude;
80

    
81
    /**
82
     * Longitude
83
     */
84
    private Double longitude;
85

    
86
    /**
87
     * Precision of location
88
     */
89
    private String locationPrecision;
90

    
91
    /**
92
     * Max date
93
     */
94
    private Integer maxDate;
95

    
96
    /**
97
     * Min date
98
     */
99
    private Integer minDate;
100

    
101
    /**
102
     * Keys for time periods
103
     */
104
    @Column(length = 1000)
105
    private String timePeriodKeys;
106

    
107
    /**
108
     * Pid
109
     */
110
    private String pid;
111

    
112
    /**
113
     * Set of names
114
     */
115
    @Convert(converter = SetToStringConverter.class)
116
    @Column(length = 20000)
117
    private Set<String> names = new HashSet<>();
118

    
119

    
120
    /**
121
     * Feature code
122
     */
123
    private String featureCode;
124

    
125
    /**
126
     * Country
127
     */
128
    private String country;
129

    
130
    /**
131
     * Accuracy
132
     */
133
    private Integer accuracy;
134

    
135
    /**
136
     * GeoName ID
137
     */
138
    private Long geonameId;
139

    
140
    /**
141
     * Pleiades ID
142
     */
143
    private Long pleiadesId;
144

    
145
    /**
146
     * Osm ID
147
     */
148
    private Long osmId;
149

    
150
    /**
151
     * Creates new external catalog item from list of string containing information and catalog source type
152
     *
153
     * @param fieldList      list of string containing information about the catalog item
154
     * @param externalSource external source type
155
     */
156
    public ExternalCatalogItem(List<String> fieldList, ExternalSource externalSource) {
157
        this.externalSource = externalSource;
158
        if (externalSource == ExternalSource.PLEIADES) {
159
            this.locationPrecision = fieldList.get(8);
160
            this.maxDate = processIntField(fieldList.get(9));
161
            this.minDate = processIntField(fieldList.get(10));
162
            this.pid = fieldList.get(16);
163
            this.latitude = processDoubleField(fieldList.get(17));
164
            this.longitude = processDoubleField(fieldList.get(18));
165
            this.timePeriodKeys = fieldList.get(22);
166
            this.names.add(fieldList.get(24));
167
            this.names.addAll(processListField(fieldList.get(12)));
168
            this.names.addAll(processListField(fieldList.get(14)));
169
        } else if (externalSource == ExternalSource.GEONAMES) {
170
            this.geonameId = processLongField(fieldList.get(0));
171
            this.names.add(fieldList.get(1));
172
            this.names.add(fieldList.get(2));
173
            this.names.addAll(processListField(fieldList.get(3)));
174
            this.latitude = processDoubleField(fieldList.get(4));
175
            this.longitude = processDoubleField(fieldList.get(5));
176
            this.featureCode = fieldList.get(7);
177
            this.country = convertCountryCode(fieldList.get(8));
178
        } else if (externalSource == ExternalSource.CIGS) {
179
            this.accuracy = processIntField(fieldList.get(1));
180
            this.names.add(fieldList.get(3));
181
            this.names.add(fieldList.get(4));
182
            this.names.add(fieldList.get(5));
183
            this.names.add(fieldList.get(6));
184
            this.names.add(fieldList.get(7));
185
            this.names.add(fieldList.get(8));
186
            this.names.add(fieldList.get(9));
187
            this.names.add(fieldList.get(10));
188
            this.names.add(fieldList.get(11));
189
            this.pleiadesId = processLongField(fieldList.get(12));
190
            this.osmId = processLongField(fieldList.get(13));
191
            this.geonameId = processLongField(fieldList.get(15));
192
            this.longitude = processDoubleField(fieldList.get(23));
193
            this.latitude = processDoubleField(fieldList.get(24));
194
        }
195
    }
196

    
197
    /**
198
     * Converts country code to country name
199
     *
200
     * @param countryCode country code
201
     * @return country name
202
     */
203
    private String convertCountryCode(String countryCode) {
204
        if (COUNTRY_CODES.containsKey(countryCode)) {
205
            return COUNTRY_CODES.get(countryCode);
206
        }
207
        return countryCode;
208
    }
209

    
210
    /**
211
     * Processes field of type long
212
     *
213
     * @param field field
214
     * @return long if long found in field, null otherwise
215
     */
216
    private Long processLongField(String field) {
217
        Matcher matcher = INTEGER_PATTERN.matcher(field);
218
        if (matcher.find()) {
219
            return Long.parseLong(matcher.group());
220
        } else {
221
            return null;
222
        }
223
    }
224

    
225
    /**
226
     * Processes field of type int
227
     *
228
     * @param field field
229
     * @return int if int found in field, null otherwise
230
     */
231
    private Integer processIntField(String field) {
232
        Matcher matcher = INTEGER_PATTERN.matcher(field);
233
        if (matcher.find()) {
234
            return Integer.parseInt(matcher.group());
235
        } else {
236
            return null;
237
        }
238
    }
239

    
240
    /**
241
     * Processes field of type double
242
     *
243
     * @param field field
244
     * @return double if double found in field, null otherwise
245
     */
246
    private Double processDoubleField(String field) {
247
        Matcher matcher = DOUBLE_PATTERN.matcher(field);
248
        if (matcher.find()) {
249
            return Double.parseDouble(matcher.group());
250
        } else {
251
            return null;
252
        }
253
    }
254

    
255
    /**
256
     * Processes list field
257
     *
258
     * @param field list field (string seperated by commas)
259
     * @return set from list field
260
     */
261
    private Set<String> processListField(String field) {
262
        if (field.isEmpty()) {
263
            return new HashSet<>();
264
        }
265
        return Arrays.stream(field.split(",")).map(String::trim).filter(item -> !item.isEmpty()).collect(Collectors.toSet());
266
    }
267
}
(2-2/7)