Revize ae654ef7
Přidáno uživatelem Jakub Šmíd před asi 2 roky(ů)
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItem.java | ||
---|---|---|
15 | 15 |
import org.hibernate.annotations.LazyCollectionOption; |
16 | 16 |
|
17 | 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; |
|
18 |
import java.util.LinkedHashSet; |
|
19 |
import java.util.Set; |
|
20 |
import java.util.UUID; |
|
23 | 21 |
|
24 | 22 |
/** |
25 | 23 |
* Catalog item entity representing catalog item |
... | ... | |
31 | 29 |
@Entity |
32 | 30 |
@Table(name = "catalog_items") |
33 | 31 |
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 | 32 |
/** |
39 | 33 |
* Catalog item id |
40 | 34 |
*/ |
... | ... | |
119 | 113 |
} |
120 | 114 |
) |
121 | 115 |
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 | 116 |
} |
Také k dispozici: Unified diff
Database exported to script
- removed initialization from application
- updated docker-compose.yml to create volume and add data from script
re #9746