Projekt

Obecné

Profil

« Předchozí | Další » 

Revize abaa5f46

Přidáno uživatelem Jakub Šmíd před asi 2 roky(ů)

  • ID abaa5f461242c640ae1e7daaddb6bfc1b1419db9
  • Rodič c1df89f5

Experiment with database

Zobrazit rozdíly:

backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItem.java
5 5
import cz.zcu.kiv.backendapi.country.Country;
6 6
import cz.zcu.kiv.backendapi.type.Type;
7 7
import cz.zcu.kiv.backendapi.writtenform.WrittenForm;
8
import lombok.Data;
8
import lombok.Getter;
9 9
import lombok.NoArgsConstructor;
10
import lombok.Setter;
10 11
import org.hibernate.annotations.Fetch;
11 12
import org.hibernate.annotations.FetchMode;
12 13
import org.hibernate.annotations.LazyCollection;
13 14
import org.hibernate.annotations.LazyCollectionOption;
14 15

  
15 16
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;
17
import java.util.HashSet;
18
import java.util.Set;
19
import java.util.UUID;
20 20

  
21 21
/**
22 22
 * Catalog entity representing catalog item
23 23
 */
24
@Data
24
@Getter
25
@Setter
25 26
@NoArgsConstructor
26 27
@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
//})
28
@Table(name = "catalog_items")
35 29
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 30
    /**
40 31
     * Catalog item id
41 32
     */
......
66 57
    /**
67 58
     * Bibliography
68 59
     */
69
    @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
60
    @OneToMany(mappedBy = "catalogItem", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
70 61
    @LazyCollection(LazyCollectionOption.FALSE)
71 62
    @Fetch(FetchMode.SUBSELECT)
72
    private Set<Bibliography> bibliography = Collections.emptySet();
63
    private Set<Bibliography> bibliography = new HashSet<>();
73 64

  
74 65
    /**
75 66
     * Countries
76 67
     */
77
    @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
68
    @OneToMany(mappedBy = "catalogItem", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
78 69
    @LazyCollection(LazyCollectionOption.FALSE)
79 70
    @Fetch(FetchMode.SUBSELECT)
80
    private Set<Country> countries = Collections.emptySet();
71
    private Set<Country> countries = new HashSet<>();
81 72

  
82 73
    /**
83 74
     * Written forms
84 75
     */
85
    @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
76
    @OneToMany(mappedBy = "catalogItem", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
86 77
    @LazyCollection(LazyCollectionOption.FALSE)
87 78
    @Fetch(FetchMode.SUBSELECT)
88
    private Set<WrittenForm> writtenForms = Collections.emptySet();
79
    private Set<WrittenForm> writtenForms = new HashSet<>();
89 80

  
90 81
    /**
91 82
     * Alternative names
92 83
     */
93
    @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
84
    @OneToMany(mappedBy = "catalogItem", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
94 85
    @LazyCollection(LazyCollectionOption.FALSE)
95 86
    @Fetch(FetchMode.SUBSELECT)
96
    private Set<AlternativeName> alternativeNames = Collections.emptySet();
97

  
87
    private Set<AlternativeName> alternativeNames = new HashSet<>();
98 88
    /**
99 89
     * Set of user roles - many-to-many relationship
100 90
     */
......
109 99
                    @JoinColumn(name = "type", referencedColumnName = "type")
110 100
            }
111 101
    )
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
    }
102
    private Set<Type> types = new HashSet<>();
161 103
}

Také k dispozici: Unified diff