-
Notifications
You must be signed in to change notification settings - Fork 1
/
FilmRepository.java
246 lines (211 loc) · 7.7 KB
/
FilmRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package org.speedment.repository;
import com.speedment.jpastreamer.application.JPAStreamer;
import com.speedment.jpastreamer.streamconfiguration.StreamConfiguration;
import io.quarkus.hibernate.orm.panache.PanacheRepository;
import io.quarkus.panache.common.Parameters;
import io.quarkus.panache.common.Sort;
import org.speedment.model.Actor;
import org.speedment.model.Film;
import org.speedment.model.Film$;
import javax.enterprise.context.ApplicationScoped;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ApplicationScoped
public class FilmRepository implements PanacheRepository<Film> {
private final static int PAGE_SIZE = 20;
private final JPAStreamer jpaStreamer = JPAStreamer.of(this::getEntityManager);
/**
*
* Lists limit number of films
*
* @param limit the maximum number of films to list
* @return a list of all films
*/
// Panache
public List<Film> listFilms(int limit) {
return findAll().range(0, limit - 1).list();
}
// JPAStreamer
public Stream<Film> listFilmsStreamer(int limit) {
return filmStream().limit(limit);
}
/**
* Finds the first film with a title that matches the provided {@code title}.
*
* @param title title of the film
* @return first film with a matching title, or null if no film matches the query
*/
// Panache
public Film findByTitle(String title) {
return find("title", title)
.firstResult();
}
// JPAStreamer
public Film findByTitleStreamer(String title) {
return filmStream()
.filter(Film$.title.equal(title))
.findFirst().orElse(null);
}
/**
* Returns the description of the film with the provided {@code title}.
*
* @param title title of the film
* @return the description of the first film with a matching title, or null if no film matches the title
*/
// Panache
public String findDescriptionByTitle(String title) {
return find("select f.description from Film f where f.title like :title", Parameters.with("title", title))
.project(String.class)
.firstResult();
}
// JPAStreamer
public String findDescriptionByTitleStreamer(String title) {
return filmStream()
.filter(Film$.title.equal(title))
.map(Film$.description)
.findFirst().orElse(null);
}
/**
* Lists all films sorted first by reversed rating and then by title
*
* @return list of films sorted first by rating and then by title
*/
// Panache
public List<Film> listFilmsSortedByRatingAndLength(int limit) {
return findAll(Sort.by("rating", Sort.Direction.Descending).and("title", Sort.Direction.Ascending))
.range(0, limit - 1)
.list();
}
// JPAStreamer
public Stream<Film> listFilmsSortedByRatingAndLengthStreamer(int limit) {
return filmStream()
.sorted(Film$.rating.reversed().thenComparing(Film$.title.comparator()))
.limit(limit);
}
/**
* Sorts the films by length, and pages the result.
*
* @param page the page number
* @return a stream of the paged results
*/
// Panache
public Stream<Film> paging(int page) {
return findAll(Sort.by("length"))
.page(page, PAGE_SIZE)
.stream();
}
// JPAStreamer
public Stream<Film> pagingStreamer(int page) {
return filmStream()
.sorted(Film$.length)
.skip((long) PAGE_SIZE * page)
.limit(PAGE_SIZE);
}
/**
* Lists {@code limit} films that starts with the provided String {@code startsWith},
* sorted from shortest to longest.
*
* @param start start string
* @param limit maximum number of results to retrieve
* @return a list of films sorted in ascending length
*/
// Panache
public Stream<Film> titleStartsWithSortedByLengthLimited(String start, int limit) {
return find("title like :start",
Sort.by("length"),
Parameters.with("start", start + "%"))
.range(0, limit)
.stream();
}
// JPAStreamer
public Stream<Film> titleStartsWithSortedByLengthLimitedStreamer(String start, int limit) {
return filmStream()
.filter(Film$.title.startsWith(start))
.sorted(Film$.length)
.limit(limit);
}
/**
* Lists {@code limit} films that are at least as long as the provided {@code length}.
*
* @param length minimum film length
* @return films that are at least as long as the provided length
*/
// Panache
public List<Film> listLonger(short length) {
return list("length >= ?1", length);
}
// JPAStreamer
public Stream<Film> listLongerStreamer(short length) {
return filmStream()
.filter(Film$.length.greaterOrEqual(length));
}
/**
* Updates the description of all films that are longer than the provided length to desc.
*
* @param desc the new description
* @param length the minimum length of films that should be updated, exclusive
*/
// Panache - executes a single update query
@Transactional
public void updateDescription(String desc, short length) {
update("description = ?1 where length > ?2", desc, length);
}
// Panache - executes one update query for every film to be updated
@Transactional
public void updateDescriptionStream(String desc, short length) {
try (Stream<Film> films = streamAll()) {
films
.filter(t -> t.getLength() > length)
.forEach(f -> {
f.setDescription(desc);
persist(f);
});
}
}
// JPAStreamer - executes one update query for every film to be updated in its current state
@Transactional
public void updateDescriptionStreamer(String desc, short length) {
filmStream()
.filter(Film$.length.greaterThan(length))
.forEach(f -> {
f.setDescription(desc);
persist(f);
});
}
/**
* Returns a map containing all films with a title that starts with the provided String.
*
* @param startsWith the start of the film title
* @return a mapping between films and their cast
*/
// Panache
public Map<Film, Set<Actor>> getCast(String startsWith) {
return find("from Film f LEFT JOIN fetch f.actors where title like :title",
Parameters.with("title", startsWith + "%"))
.stream()
.collect(
Collectors.toMap(Function.identity(),
Film::getActors
)
);
}
// JPAStreamer
public Map<Film, Set<Actor>> getCastStreamer(String startsWith) {
// Left join is default
return jpaStreamer.stream(StreamConfiguration.of(Film.class).joining(Film$.actors))
.filter(Film$.title.startsWith(startsWith))
.collect(
Collectors.toMap(Function.identity(),
Film::getActors
)
);
}
private Stream<Film> filmStream() {
return jpaStreamer.stream(Film.class);
}
}