Projekt

Obecné

Profil

Stáhnout (4.51 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.Getter;
9
import lombok.NoArgsConstructor;
10
import lombok.Setter;
11
import org.hibernate.annotations.LazyCollection;
12
import org.hibernate.annotations.LazyCollectionOption;
13

    
14
import javax.persistence.*;
15
import java.util.*;
16
import java.util.regex.Matcher;
17
import java.util.regex.Pattern;
18
import java.util.stream.Collectors;
19

    
20
/**
21
 * Catalog entity representing catalog entry
22
 */
23
//@Data
24
@Getter
25
@Setter
26
@NoArgsConstructor
27
@Entity
28
@Table(name = "catalog")
29
public class CatalogEntry {
30
    private static final String INTEGER_PATTERN = "\\d+";
31
    private static final String DOUBLE_PATTERN = "(\\d+[.]\\d+)|(\\d+)";
32
    private static final String EMPTY_ENTRY = "–";
33
    /**
34
     * Catalog entry id
35
     */
36
    @Id
37
    @GeneratedValue
38
    private UUID id;
39

    
40
    /**
41
     * Name of geographic entry
42
     */
43
    private String name;
44

    
45
    /**
46
     * Certainty
47
     */
48
    private int certainty;
49

    
50
    /**
51
     * Longitude
52
     */
53
    private double longitude;
54

    
55
    /**
56
     * Latitude
57
     */
58
    private double latitude;
59

    
60
    /**
61
     * Bibliography
62
     */
63
    @OneToMany(mappedBy = "catalog", cascade = CascadeType.ALL)
64
    @LazyCollection(LazyCollectionOption.FALSE)
65
    private Set<Bibliography> bibliography = Collections.emptySet();
66

    
67
    /**
68
     * Countries
69
     */
70
    @OneToMany(mappedBy = "catalog", cascade = CascadeType.ALL)
71
    @LazyCollection(LazyCollectionOption.FALSE)
72
    private Set<Country> countries = Collections.emptySet();
73

    
74
    /**
75
     * Written forms
76
     */
77
    @OneToMany(mappedBy = "catalog", cascade = CascadeType.ALL)
78
    @LazyCollection(LazyCollectionOption.FALSE)
79
    private Set<WrittenForm> writtenForms = Collections.emptySet();
80

    
81
    /**
82
     * Alternative names
83
     */
84
    @OneToMany(mappedBy = "catalog", cascade = CascadeType.ALL)
85
    @LazyCollection(LazyCollectionOption.FALSE)
86
    private Set<AlternativeName> alternativeNames = Collections.emptySet();
87

    
88
    /**
89
     * Set of user roles - many-to-many relationship
90
     */
91
    @ManyToMany(fetch = FetchType.EAGER)
92
    @JoinTable(
93
            name = "catalog_type",
94
            joinColumns = {
95
                    @JoinColumn(name = "catalog_id", referencedColumnName = "id")
96
            },
97
            inverseJoinColumns = {
98
                    @JoinColumn(name = "type", referencedColumnName = "type")
99
            }
100
    )
101
    private Set<Type> types = Collections.emptySet();
102

    
103
    public CatalogEntry(final List<String> csvFields) {
104

    
105
        this.name = csvFields.get(1);
106
        List<String> stringList = processListField(csvFields.get(2));
107
        this.alternativeNames = stringList.stream().map(s->new AlternativeName(s, this)).collect(Collectors.toSet());
108

    
109
        this.certainty = processIntField(csvFields.get(3));
110
        this.latitude = processDoubleField(csvFields.get(4));
111
        this.longitude = processDoubleField(csvFields.get(5));
112

    
113
        stringList = processListField(csvFields.get(6));
114
        this.writtenForms = stringList.stream().map(s->new WrittenForm(s, this)).collect(Collectors.toSet());
115

    
116
        stringList = processListField(csvFields.get(7));
117
        this.types = stringList.stream().map(Type::new).collect(Collectors.toSet());
118

    
119
        stringList = processListField(csvFields.get(8));
120
        this.countries = stringList.stream().map(s->new Country(s, this)).collect(Collectors.toSet());
121

    
122
        stringList = processListField(csvFields.get(9));
123
        this.bibliography = stringList.stream().map(s->new Bibliography(s, this)).collect(Collectors.toSet());
124
    }
125

    
126
    private int processIntField(String field) {
127
        Matcher matcher = Pattern.compile(INTEGER_PATTERN).matcher(field);
128
        if (matcher.find()) {
129
            return Integer.parseInt(matcher.group());
130
        } else {
131
            return 0;
132
        }
133
    }
134

    
135
    private double processDoubleField(String field) {
136
        Matcher matcher = Pattern.compile(DOUBLE_PATTERN).matcher(field);
137
        if (matcher.find()) {
138
            return Double.parseDouble(matcher.group());
139
        } else {
140
            return 0.0;
141
        }
142
    }
143

    
144
    private List<String> processListField(String field) {
145
        if (field.isEmpty() || field.equals(EMPTY_ENTRY)) {
146
            return new ArrayList<>();
147
        }
148
        return Arrays.stream(field.split(",")).map(String::trim).filter(item -> !item.isEmpty()).collect(Collectors.toList());
149
    }
150
}
(2-2/6)