1
|
package cz.zcu.kiv.backendapi.catalog;
|
2
|
|
3
|
import cz.zcu.kiv.backendapi.alternativename.AlternativeName;
|
4
|
import cz.zcu.kiv.backendapi.bibliography.Bibliography;
|
5
|
import cz.zcu.kiv.backendapi.country.Country;
|
6
|
import cz.zcu.kiv.backendapi.type.Type;
|
7
|
import cz.zcu.kiv.backendapi.writtenform.WrittenForm;
|
8
|
import lombok.Data;
|
9
|
import lombok.NoArgsConstructor;
|
10
|
import org.hibernate.annotations.Fetch;
|
11
|
import org.hibernate.annotations.FetchMode;
|
12
|
import org.hibernate.annotations.LazyCollection;
|
13
|
import org.hibernate.annotations.LazyCollectionOption;
|
14
|
|
15
|
import javax.persistence.*;
|
16
|
import java.util.*;
|
17
|
import java.util.regex.Matcher;
|
18
|
import java.util.regex.Pattern;
|
19
|
import java.util.stream.Collectors;
|
20
|
|
21
|
/**
|
22
|
* Catalog entity representing catalog item
|
23
|
*/
|
24
|
@Data
|
25
|
@NoArgsConstructor
|
26
|
@Entity
|
27
|
@Table(name = "catalog_item")
|
28
|
//@NamedEntityGraph(name = "graph", attributeNodes = {
|
29
|
// @NamedAttributeNode("bibliography"),
|
30
|
// @NamedAttributeNode("alternativeNames"),
|
31
|
// @NamedAttributeNode("countries"),
|
32
|
// @NamedAttributeNode("writtenForms"),
|
33
|
// @NamedAttributeNode("types"),
|
34
|
//})
|
35
|
public class CatalogItem {
|
36
|
private static final String INTEGER_PATTERN = "\\d+";
|
37
|
private static final String DOUBLE_PATTERN = "(\\d+[.]\\d+)|(\\d+)";
|
38
|
private static final String EMPTY_ENTRY = "–";
|
39
|
/**
|
40
|
* Catalog item id
|
41
|
*/
|
42
|
@Id
|
43
|
@GeneratedValue
|
44
|
private UUID id;
|
45
|
|
46
|
/**
|
47
|
* Name of geographic item
|
48
|
*/
|
49
|
private String name;
|
50
|
|
51
|
/**
|
52
|
* Certainty
|
53
|
*/
|
54
|
private int certainty;
|
55
|
|
56
|
/**
|
57
|
* Longitude
|
58
|
*/
|
59
|
private double longitude;
|
60
|
|
61
|
/**
|
62
|
* Latitude
|
63
|
*/
|
64
|
private double latitude;
|
65
|
|
66
|
/**
|
67
|
* Bibliography
|
68
|
*/
|
69
|
@OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
|
70
|
@LazyCollection(LazyCollectionOption.FALSE)
|
71
|
@Fetch(FetchMode.SUBSELECT)
|
72
|
private Set<Bibliography> bibliography = Collections.emptySet();
|
73
|
|
74
|
/**
|
75
|
* Countries
|
76
|
*/
|
77
|
@OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
|
78
|
@LazyCollection(LazyCollectionOption.FALSE)
|
79
|
@Fetch(FetchMode.SUBSELECT)
|
80
|
private Set<Country> countries = Collections.emptySet();
|
81
|
|
82
|
/**
|
83
|
* Written forms
|
84
|
*/
|
85
|
@OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
|
86
|
@LazyCollection(LazyCollectionOption.FALSE)
|
87
|
@Fetch(FetchMode.SUBSELECT)
|
88
|
private Set<WrittenForm> writtenForms = Collections.emptySet();
|
89
|
|
90
|
/**
|
91
|
* Alternative names
|
92
|
*/
|
93
|
@OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
|
94
|
@LazyCollection(LazyCollectionOption.FALSE)
|
95
|
@Fetch(FetchMode.SUBSELECT)
|
96
|
private Set<AlternativeName> alternativeNames = Collections.emptySet();
|
97
|
|
98
|
/**
|
99
|
* Set of user roles - many-to-many relationship
|
100
|
*/
|
101
|
@ManyToMany(fetch = FetchType.EAGER)
|
102
|
@Fetch(FetchMode.SUBSELECT)
|
103
|
@JoinTable(
|
104
|
name = "catalog_item_type",
|
105
|
joinColumns = {
|
106
|
@JoinColumn(name = "catalog_item_id", referencedColumnName = "id")
|
107
|
},
|
108
|
inverseJoinColumns = {
|
109
|
@JoinColumn(name = "type", referencedColumnName = "type")
|
110
|
}
|
111
|
)
|
112
|
private Set<Type> types = Collections.emptySet();
|
113
|
|
114
|
public CatalogItem(final List<String> csvFields) {
|
115
|
|
116
|
this.name = csvFields.get(1);
|
117
|
List<String> stringList = processListField(csvFields.get(2));
|
118
|
this.alternativeNames = stringList.stream().map(s -> new AlternativeName(s, this)).collect(Collectors.toSet());
|
119
|
|
120
|
this.certainty = processIntField(csvFields.get(3));
|
121
|
this.latitude = processDoubleField(csvFields.get(4));
|
122
|
this.longitude = processDoubleField(csvFields.get(5));
|
123
|
|
124
|
stringList = processListField(csvFields.get(6));
|
125
|
this.writtenForms = stringList.stream().map(s -> new WrittenForm(s, this)).collect(Collectors.toSet());
|
126
|
|
127
|
stringList = processListField(csvFields.get(7));
|
128
|
this.types = stringList.stream().map(Type::new).collect(Collectors.toSet());
|
129
|
|
130
|
stringList = processListField(csvFields.get(8));
|
131
|
this.countries = stringList.stream().map(s -> new Country(s, this)).collect(Collectors.toSet());
|
132
|
|
133
|
stringList = processListField(csvFields.get(9));
|
134
|
this.bibliography = stringList.stream().map(s -> new Bibliography(s, this)).collect(Collectors.toSet());
|
135
|
}
|
136
|
|
137
|
private int processIntField(String field) {
|
138
|
Matcher matcher = Pattern.compile(INTEGER_PATTERN).matcher(field);
|
139
|
if (matcher.find()) {
|
140
|
return Integer.parseInt(matcher.group());
|
141
|
} else {
|
142
|
return 0;
|
143
|
}
|
144
|
}
|
145
|
|
146
|
private double processDoubleField(String field) {
|
147
|
Matcher matcher = Pattern.compile(DOUBLE_PATTERN).matcher(field);
|
148
|
if (matcher.find()) {
|
149
|
return Double.parseDouble(matcher.group());
|
150
|
} else {
|
151
|
return 0.0;
|
152
|
}
|
153
|
}
|
154
|
|
155
|
private List<String> processListField(String field) {
|
156
|
if (field.isEmpty() || field.equals(EMPTY_ENTRY)) {
|
157
|
return new ArrayList<>();
|
158
|
}
|
159
|
return Arrays.stream(field.split(",")).map(String::trim).filter(item -> !item.isEmpty()).collect(Collectors.toList());
|
160
|
}
|
161
|
}
|