Projekt

Obecné

Profil

Stáhnout (5.14 KB) Statistiky
| Větev: | Tag: | Revize:
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.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.regex.Matcher;
20
import java.util.regex.Pattern;
21
import java.util.stream.Collectors;
22

    
23
/**
24
 * Catalog entity representing catalog item
25
 */
26
@Getter
27
@Setter
28
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
29
@NoArgsConstructor
30
@Entity
31
@Table(name = "catalog_items")
32
public class CatalogItem {
33
    private static final String INTEGER_PATTERN = "\\d+";
34
    private static final String DOUBLE_PATTERN = "(\\d+[.]\\d+)|(\\d+)";
35
    private static final String EMPTY_ENTRY = "–";
36

    
37
    /**
38
     * Catalog item id
39
     */
40
    @Id
41
    @EqualsAndHashCode.Include
42
    private UUID id = UUID.randomUUID();
43

    
44
    /**
45
     * Name of geographic item
46
     */
47
    private String name = "";
48

    
49
    /**
50
     * Certainty
51
     */
52
    private Integer certainty;
53

    
54
    /**
55
     * Longitude
56
     */
57
    private Double longitude;
58

    
59
    /**
60
     * Latitude
61
     */
62
    private Double latitude;
63

    
64
    /**
65
     * Description
66
     */
67
    @Column(length = 100000)
68
    private String description = "";
69

    
70
    /**
71
     * Bibliography
72
     */
73
    @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
74
    @LazyCollection(LazyCollectionOption.FALSE)
75
    @Fetch(FetchMode.SUBSELECT)
76
    private Set<Bibliography> bibliography = new HashSet<>();
77

    
78
    /**
79
     * Countries
80
     */
81
    @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
82
    @LazyCollection(LazyCollectionOption.FALSE)
83
    @Fetch(FetchMode.SUBSELECT)
84
    private Set<Country> countries = new HashSet<>();
85

    
86
    /**
87
     * Written forms
88
     */
89
    @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
90
    @LazyCollection(LazyCollectionOption.FALSE)
91
    @Fetch(FetchMode.SUBSELECT)
92
    private Set<WrittenForm> writtenForms = new HashSet<>();
93

    
94
    /**
95
     * Alternative names
96
     */
97
    @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
98
    @LazyCollection(LazyCollectionOption.FALSE)
99
    @Fetch(FetchMode.SUBSELECT)
100
    private Set<AlternativeName> alternativeNames = new HashSet<>();
101

    
102
    /**
103
     * Set of user roles - many-to-many relationship
104
     */
105
    @ManyToMany(fetch = FetchType.EAGER)
106
    @Fetch(FetchMode.SUBSELECT)
107
    @JoinTable(
108
            name = "catalog_item_type",
109
            joinColumns = {
110
                    @JoinColumn(name = "catalog_item_id", referencedColumnName = "id")
111
            },
112
            inverseJoinColumns = {
113
                    @JoinColumn(name = "type", referencedColumnName = "type")
114
            }
115
    )
116
    private Set<Type> types = new HashSet<>();
117

    
118
    public CatalogItem(List<String> csvFields) {
119

    
120
        this.name = csvFields.get(1);
121
        Set<String> stringList = processListField(csvFields.get(2));
122
        this.alternativeNames = stringList.stream().map(s -> new AlternativeName(s, this)).collect(Collectors.toSet());
123

    
124
        this.certainty = processIntField(csvFields.get(3));
125
        this.latitude = processDoubleField(csvFields.get(4));
126
        this.longitude = processDoubleField(csvFields.get(5));
127

    
128
        stringList = processListField(csvFields.get(6));
129
        this.writtenForms = stringList.stream().map(s -> new WrittenForm(s, this)).collect(Collectors.toSet());
130

    
131
        stringList = processListField(csvFields.get(7));
132
        this.types = stringList.stream().map(Type::new).collect(Collectors.toSet());
133

    
134
        stringList = processListField(csvFields.get(8));
135
        this.countries = stringList.stream().map(s -> new Country(s, this)).collect(Collectors.toSet());
136

    
137
        stringList = processListField(csvFields.get(9));
138
        this.bibliography = stringList.stream().map(s -> new Bibliography(s, this)).collect(Collectors.toSet());
139
    }
140

    
141
    private Integer processIntField(String field) {
142
        Matcher matcher = Pattern.compile(INTEGER_PATTERN).matcher(field);
143
        if (matcher.find()) {
144
            return Integer.parseInt(matcher.group());
145
        } else {
146
            return null;
147
        }
148
    }
149

    
150
    private Double processDoubleField(String field) {
151
        Matcher matcher = Pattern.compile(DOUBLE_PATTERN).matcher(field);
152
        if (matcher.find()) {
153
            return Double.parseDouble(matcher.group());
154
        } else {
155
            return null;
156
        }
157
    }
158

    
159
    private Set<String> processListField(String field) {
160
        if (field.isEmpty() || field.equals(EMPTY_ENTRY)) {
161
            return new HashSet<>();
162
        }
163
        return Arrays.stream(field.split(",")).map(String::trim).filter(item -> !item.isEmpty()).collect(Collectors.toSet());
164
    }
165
}
(2-2/6)