/
SakilaResource.java
142 lines (123 loc) · 4.06 KB
/
SakilaResource.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
package resources;
import org.apache.cxf.jaxrs.ext.search.SearchCondition;
import org.apache.cxf.jaxrs.ext.search.SearchContext;
import sakila.Actor;
import sakila.Film;
import sakila.Rental;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import java.util.ArrayList;
import java.util.List;
/**
* Sakila Restful Search Resource
*
* @author aruld@acm.org
*/
@Path("/sakila")
public class SakilaResource {
@Context
private SearchContext searchContext;
List<Actor> actors = new ArrayList<Actor>();
List<Film> films = new ArrayList<Film>();
List<Rental> rentals = new ArrayList<Rental>();
public SakilaResource() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Sakila");
EntityManager em = emf.createEntityManager();
@SuppressWarnings("unchecked")
List<Actor> actorList = em.createNamedQuery("Actor.findAll").getResultList();
for (Actor actor : actorList) {
//break the cyclic graph mess to satisfy JAXB
actor.setFilmActorCollection(null);
actors.add(actor);
}
@SuppressWarnings("unchecked")
List<Film> filmList = em.createNamedQuery("Film.findAll").getResultList();
for (Film film : filmList) {
//break the cyclic graph mess to satisfy JAXB
film.setFilmActorCollection(null);
film.setFilmCategoryCollection(null);
film.setInventoryCollection(null);
film.setLanguage(null);
film.setLanguage1(null);
films.add(film);
}
@SuppressWarnings("unchecked")
List<Rental> rentalList = em.createNamedQuery("Rental.findAll").getResultList();
for (Rental rental : rentalList) {
//break the cyclic graph mess to satisfy JAXB
rental.setCustomer(null);
rental.setInventory(null);
rental.setPaymentCollection(null);
rental.setStaff(null);
rentals.add(rental);
}
}
@GET
@Produces("application/xml")
@Path("actors")
public List<Actor> getActors() {
return actors;
}
@GET
@Produces("application/xml")
@Path("searchActors")
public List<Actor> searchActors() {
SearchCondition<Actor> sc = searchContext.getCondition(Actor.class);
if (sc == null) {
throw new NotFoundException("Invalid search query.");
}
System.out.println(sc.toSQL("actor"));
List<Actor> found = sc.findAll(actors);
if (found.size() == 0) {
throw new NotFoundException("No matching actor found.");
}
return found;
}
@GET
@Produces("application/xml")
@Path("films")
public List<Film> getFilms() {
return films;
}
@GET
@Produces("application/xml")
@Path("searchFilms")
public List<Film> searchFilms() {
SearchCondition<Film> sc = searchContext.getCondition(Film.class);
if (sc == null) {
throw new NotFoundException("Invalid search query.");
}
System.out.println(sc.toSQL("film"));
List<Film> found = sc.findAll(films);
if (found.size() == 0) {
throw new NotFoundException("No matching film found.");
}
return found;
}
@GET
@Produces("application/xml")
@Path("rentals")
public List<Rental> getRentals() {
return rentals;
}
@GET
@Produces("application/xml")
@Path("searchRentals")
public List<Rental> searchRentals() {
SearchCondition<Rental> sc = searchContext.getCondition(Rental.class);
if (sc == null) {
throw new NotFoundException("Invalid search query.");
}
System.out.println(sc.toSQL("rental"));
List<Rental> found = sc.findAll(rentals);
if (found.size() == 0) {
throw new NotFoundException("No matching rental found.");
}
return found;
}
}