Projekt

Obecné

Profil

Stáhnout (7.69 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 LinkedHashSet<>();
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
        } else if (externalSource == ExternalSource.ANE) {
195
            this.names.add(fieldList.get(0));
196
            this.longitude = processDoubleField(fieldList.get(1));
197
            this.latitude = processDoubleField(fieldList.get(2));
198
        }
199
    }
200

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

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

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

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

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