Projekt

Obecné

Profil

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

    
3
import lombok.EqualsAndHashCode;
4
import lombok.Getter;
5
import lombok.NoArgsConstructor;
6
import lombok.Setter;
7

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

    
14
/**
15
 * Class representing external catalog items
16
 */
17
@Getter
18
@Setter
19
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
20
@NoArgsConstructor
21
@Entity
22
@Table(name = "external_catalog_items")
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
     * Feature code
121
     */
122
    private String featureCode;
123

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

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

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

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

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

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

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

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

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

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

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