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