Rename plural classes to singular, make XmlHelper types coherent
No functional changes, only nicer code
This commit is contained in:
parent
0b8aaae4a6
commit
28a73932ae
@ -1,6 +1,5 @@
|
|||||||
package at.technikumwien.movies;
|
package at.technikumwien.movies;
|
||||||
|
|
||||||
import javax.sound.sampled.Port;
|
|
||||||
import javax.xml.namespace.QName;
|
import javax.xml.namespace.QName;
|
||||||
import javax.xml.ws.Service;
|
import javax.xml.ws.Service;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@ -15,11 +14,12 @@ public class MoviesWebServiceClient {
|
|||||||
MoviesWebService port = service.getPort(MoviesWebService.class);
|
MoviesWebService port = service.getPort(MoviesWebService.class);
|
||||||
|
|
||||||
// TODO: Move to a better place
|
// TODO: Move to a better place
|
||||||
|
|
||||||
// Import movies
|
// Import movies
|
||||||
List<Movies> movies = XmlHelper.xmlToMovies("movietest.xml");
|
List<Movie> movies = XmlHelper.xmlToMovies("movietest.xml");
|
||||||
port.importMovies(movies);
|
port.importMovies(movies);
|
||||||
|
|
||||||
// Export all existing movies
|
// Export all existing movies
|
||||||
XmlHelper.moviesToXml(new MovieList(port.getAllMovies()), "movies_in_db.xml");
|
XmlHelper.moviesToXml(port.getAllMovies(), "movies_in_db.xml");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,9 @@ import java.io.File;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class XmlHelper {
|
public class XmlHelper {
|
||||||
public static void moviesToXml(MovieList movies, String filename) throws Exception {
|
public static void moviesToXml(List<Movie> movies, String filename) throws Exception {
|
||||||
|
MovieList movieList = new MovieList(movies);
|
||||||
|
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(MovieList.class);
|
JAXBContext jaxbContext = JAXBContext.newInstance(MovieList.class);
|
||||||
|
|
||||||
Marshaller marshaller = jaxbContext.createMarshaller();
|
Marshaller marshaller = jaxbContext.createMarshaller();
|
||||||
@ -14,11 +16,11 @@ public class XmlHelper {
|
|||||||
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
|
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
|
||||||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||||
|
|
||||||
marshaller.marshal(movies, System.out);
|
marshaller.marshal(movieList, System.out);
|
||||||
marshaller.marshal(movies, new File(filename));
|
marshaller.marshal(movieList, new File(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Movies> xmlToMovies(String filename) throws JAXBException {
|
public static List<Movie> xmlToMovies(String filename) throws JAXBException {
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(MovieList.class);
|
JAXBContext jaxbContext = JAXBContext.newInstance(MovieList.class);
|
||||||
|
|
||||||
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
||||||
|
@ -17,7 +17,7 @@ import java.time.LocalDate;
|
|||||||
@NamedQueries({
|
@NamedQueries({
|
||||||
@NamedQuery(
|
@NamedQuery(
|
||||||
name = "Actors.getIdByProperties",
|
name = "Actors.getIdByProperties",
|
||||||
query = "SELECT a FROM Actors a WHERE " +
|
query = "SELECT a FROM Actor a WHERE " +
|
||||||
"a.firstname LIKE :firstname AND " +
|
"a.firstname LIKE :firstname AND " +
|
||||||
"a.lastname LIKE :lastname AND " +
|
"a.lastname LIKE :lastname AND " +
|
||||||
"a.sex = :sex AND " +
|
"a.sex = :sex AND " +
|
||||||
@ -25,11 +25,11 @@ import java.time.LocalDate;
|
|||||||
),
|
),
|
||||||
@NamedQuery(
|
@NamedQuery(
|
||||||
name = "Actors.selectAllActors",
|
name = "Actors.selectAllActors",
|
||||||
query = "SELECT n FROM Actors n"
|
query = "SELECT n FROM Actor n"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@XmlRootElement
|
@XmlRootElement
|
||||||
public class Actors {
|
public class Actor {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
@XmlTransient
|
@XmlTransient
|
||||||
@ -51,7 +51,7 @@ public class Actors {
|
|||||||
@XmlAttribute
|
@XmlAttribute
|
||||||
private LocalDate birthdate;
|
private LocalDate birthdate;
|
||||||
|
|
||||||
public Actors(String firstname, String lastname, Sex sex, LocalDate birthdate) {
|
public Actor(String firstname, String lastname, Sex sex, LocalDate birthdate) {
|
||||||
this(null, firstname, lastname, sex, birthdate);
|
this(null, firstname, lastname, sex, birthdate);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -15,16 +15,16 @@ import java.util.List;
|
|||||||
@NamedQueries({
|
@NamedQueries({
|
||||||
@NamedQuery(
|
@NamedQuery(
|
||||||
name = "Movies.selectAll",
|
name = "Movies.selectAll",
|
||||||
query = "SELECT n FROM Movies n"
|
query = "SELECT n FROM Movie n"
|
||||||
),
|
),
|
||||||
@NamedQuery(
|
@NamedQuery(
|
||||||
name = "Movies.selectByTitle",
|
name = "Movies.selectByTitle",
|
||||||
query = "SELECT n FROM Movies n WHERE n.title LIKE :title"
|
query = "SELECT n FROM Movie n WHERE n.title LIKE :title"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@XmlRootElement
|
@XmlRootElement
|
||||||
public class Movies {
|
public class Movie {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
@XmlTransient
|
@XmlTransient
|
||||||
@ -57,14 +57,14 @@ public class Movies {
|
|||||||
inverseJoinColumns = @JoinColumn(name = "fk_actors_id"))
|
inverseJoinColumns = @JoinColumn(name = "fk_actors_id"))
|
||||||
@XmlElementWrapper(name = "actors")
|
@XmlElementWrapper(name = "actors")
|
||||||
@XmlElement(name = "actor")
|
@XmlElement(name = "actor")
|
||||||
private List<Actors> actors;
|
private List<Actor> actors;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.EAGER)
|
@ManyToOne(fetch = FetchType.EAGER)
|
||||||
@JoinColumn(name = "fk_studios_id")
|
@JoinColumn(name = "fk_studios_id")
|
||||||
@XmlElement
|
@XmlElement
|
||||||
private Studios studio;
|
private Studio studio;
|
||||||
|
|
||||||
public Movies(String title, String description, Genre genre, int length, int releaseyear) {
|
public Movie(String title, String description, Genre genre, int length, int releaseyear) {
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.genre = genre;
|
this.genre = genre;
|
@ -13,5 +13,5 @@ import java.util.List;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class MovieList {
|
public class MovieList {
|
||||||
@XmlElement(name="movie")
|
@XmlElement(name="movie")
|
||||||
public List<Movies> movies;
|
public List<Movie> movies;
|
||||||
}
|
}
|
||||||
|
@ -7,11 +7,11 @@ import java.util.List;
|
|||||||
@WebService
|
@WebService
|
||||||
public interface MoviesWebService {
|
public interface MoviesWebService {
|
||||||
@WebMethod
|
@WebMethod
|
||||||
List<Movies> getAllMovies();
|
List<Movie> getAllMovies();
|
||||||
|
|
||||||
@WebMethod
|
@WebMethod
|
||||||
List<Movies> getMoviesByTitle(String title);
|
List<Movie> getMoviesByTitle(String title);
|
||||||
|
|
||||||
@WebMethod
|
@WebMethod
|
||||||
void importMovies(List<Movies> movies);
|
void importMovies(List<Movie> movies);
|
||||||
}
|
}
|
||||||
|
@ -17,17 +17,17 @@ import javax.xml.bind.annotation.XmlTransient;
|
|||||||
@NamedQueries({
|
@NamedQueries({
|
||||||
@NamedQuery(
|
@NamedQuery(
|
||||||
name = "Studios.getIdByProperties",
|
name = "Studios.getIdByProperties",
|
||||||
query = "SELECT a FROM Studios a WHERE " +
|
query = "SELECT a FROM Studio a WHERE " +
|
||||||
"a.countrycode LIKE :countrycode AND " +
|
"a.countrycode LIKE :countrycode AND " +
|
||||||
"a.name LIKE :name AND " +
|
"a.name LIKE :name AND " +
|
||||||
"a.postcode LIKE :postcode"
|
"a.postcode LIKE :postcode"
|
||||||
),
|
),
|
||||||
@NamedQuery(
|
@NamedQuery(
|
||||||
name = "Studios.selectAllStudios",
|
name = "Studios.selectAllStudios",
|
||||||
query = "SELECT n FROM Studios n"
|
query = "SELECT n FROM Studio n"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
public class Studios {
|
public class Studio {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
@XmlTransient
|
@XmlTransient
|
||||||
@ -43,7 +43,7 @@ public class Studios {
|
|||||||
@XmlAttribute
|
@XmlAttribute
|
||||||
private String postcode;
|
private String postcode;
|
||||||
|
|
||||||
public Studios(String name, String countrycode, String postcode) {
|
public Studio(String name, String countrycode, String postcode) {
|
||||||
this(null, name, countrycode, postcode);
|
this(null, name, countrycode, postcode);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,7 +11,7 @@ public class MoviesManaged {
|
|||||||
@Inject
|
@Inject
|
||||||
private MoviesService moviesService;
|
private MoviesService moviesService;
|
||||||
|
|
||||||
public List<Movies> getAllMovies(){
|
public List<Movie> getAllMovies(){
|
||||||
return moviesService.findAll();
|
return moviesService.findAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,63 +19,63 @@ public class MoviesService {
|
|||||||
@PersistenceContext(unitName = "MoviesPU")
|
@PersistenceContext(unitName = "MoviesPU")
|
||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
|
||||||
public Movies findById(long id) {
|
public Movie findById(long id) {
|
||||||
LOGGER.info("findById() >> id=" + id);
|
LOGGER.info("findById() >> id=" + id);
|
||||||
|
|
||||||
Movies movies = em.find(Movies.class, id);
|
Movie movie = em.find(Movie.class, id);
|
||||||
if (movies == null) {
|
if (movie == null) {
|
||||||
throw new EntityNotFoundException("can't find movie with id=" + id);
|
throw new EntityNotFoundException("can't find movie with id=" + id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return movies;
|
return movie;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Movies> findByTitle(String title) {
|
public List<Movie> findByTitle(String title) {
|
||||||
LOGGER.info("findByTitle() >> title=" + title);
|
LOGGER.info("findByTitle() >> title=" + title);
|
||||||
|
|
||||||
return em.createNamedQuery("Movies.selectByTitle", Movies.class)
|
return em.createNamedQuery("Movies.selectByTitle", Movie.class)
|
||||||
.setParameter("title", "%" + title + "%")
|
.setParameter("title", "%" + title + "%")
|
||||||
.getResultList();
|
.getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Movies> findAll() {
|
public List<Movie> findAll() {
|
||||||
LOGGER.info("findAll()");
|
LOGGER.info("findAll()");
|
||||||
|
|
||||||
return em.createNamedQuery("Movies.selectAll", Movies.class) //JPQL -> java persistence query language
|
return em.createNamedQuery("Movies.selectAll", Movie.class) //JPQL -> java persistence query language
|
||||||
.getResultList();
|
.getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeById(long id) {
|
public void removeById(long id) {
|
||||||
LOGGER.info("removeById() >> id=" + id);
|
LOGGER.info("removeById() >> id=" + id);
|
||||||
|
|
||||||
Movies movies = findById(id);
|
Movie movie = findById(id);
|
||||||
em.remove(movies); //managed news required
|
em.remove(movie); //managed news required
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Actors> findAllActors() {
|
public List<Actor> findAllActors() {
|
||||||
LOGGER.info("findAllActors)");
|
LOGGER.info("findAllActors)");
|
||||||
|
|
||||||
return em.createNamedQuery("Actors.selectAllActors", Actors.class)
|
return em.createNamedQuery("Actors.selectAllActors", Actor.class)
|
||||||
.getResultList();
|
.getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Studios> findAllStudios() {
|
public List<Studio> findAllStudios() {
|
||||||
LOGGER.info("findAllStudios)");
|
LOGGER.info("findAllStudios)");
|
||||||
|
|
||||||
return em.createNamedQuery("Studios.selectAllStudios", Studios.class)
|
return em.createNamedQuery("Studios.selectAllStudios", Studio.class)
|
||||||
.getResultList();
|
.getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO maybe check if the movie already exists?
|
// TODO maybe check if the movie already exists?
|
||||||
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
|
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
|
||||||
public void save(List<Movies> movies) {
|
public void save(List<Movie> movies) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (Movies movie : movies) {
|
for (Movie movie : movies) {
|
||||||
// Get studio for the movie from the database
|
// Get studio for the movie from the database
|
||||||
Studios movieStudio = movie.getStudio();
|
Studio movieStudio = movie.getStudio();
|
||||||
LOGGER.info("Get a studio" + movieStudio);
|
LOGGER.info("Get a studio" + movieStudio);
|
||||||
List<Studios> studioListResult = em.createNamedQuery("Studios.getIdByProperties", Studios.class)
|
List<Studio> studioListResult = em.createNamedQuery("Studios.getIdByProperties", Studio.class)
|
||||||
.setParameter("name", movieStudio.getName())
|
.setParameter("name", movieStudio.getName())
|
||||||
.setParameter("countrycode", movieStudio.getCountrycode())
|
.setParameter("countrycode", movieStudio.getCountrycode())
|
||||||
.setParameter("postcode", movieStudio.getPostcode())
|
.setParameter("postcode", movieStudio.getPostcode())
|
||||||
@ -94,9 +94,9 @@ public class MoviesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get actor for the movie from the database
|
// Get actor for the movie from the database
|
||||||
for (Actors movieActor : movie.getActors()) {
|
for (Actor movieActor : movie.getActors()) {
|
||||||
LOGGER.info("Get an actor " + movieActor);
|
LOGGER.info("Get an actor " + movieActor);
|
||||||
List<Actors> actorListResult = em.createNamedQuery("Actors.getIdByProperties", Actors.class)
|
List<Actor> actorListResult = em.createNamedQuery("Actors.getIdByProperties", Actor.class)
|
||||||
.setParameter("firstname", movieActor.getFirstname())
|
.setParameter("firstname", movieActor.getFirstname())
|
||||||
.setParameter("lastname", movieActor.getLastname())
|
.setParameter("lastname", movieActor.getLastname())
|
||||||
.setParameter("sex", movieActor.getSex())
|
.setParameter("sex", movieActor.getSex())
|
||||||
@ -118,7 +118,7 @@ public class MoviesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
LOGGER.info("save() >> movies" + movies);
|
LOGGER.info("save() >> movies" + movies);
|
||||||
for (Movies movieToImport : movies) {
|
for (Movie movieToImport : movies) {
|
||||||
em.merge(movieToImport);
|
em.merge(movieToImport);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -17,17 +17,17 @@ public class MoviesWebServiceImpl implements MoviesWebService {
|
|||||||
private MoviesService moviesService;
|
private MoviesService moviesService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Movies> getAllMovies() {
|
public List<Movie> getAllMovies() {
|
||||||
return moviesService.findAll();
|
return moviesService.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Movies> getMoviesByTitle(String title) {
|
public List<Movie> getMoviesByTitle(String title) {
|
||||||
return moviesService.findByTitle(title);
|
return moviesService.findByTitle(title);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void importMovies(List<Movies> movies) {
|
public void importMovies(List<Movie> movies) {
|
||||||
moviesService.save(movies);
|
moviesService.save(movies);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||||
<movies>
|
<movies>
|
||||||
<movie title="Test Movie VI" description="Not that great" genre="COMEDY" length="101" releaseyear="2021">
|
<movie title="Test Movie VII" description="Same as Test Movie IV" genre="COMEDY" length="119" releaseyear="2022">
|
||||||
<actors>
|
<actors>
|
||||||
<actor firstname="Test" lastname="Actor" sex="MALE" birthdate="01.01.1888"/>
|
<actor firstname="Test" lastname="Actor" sex="MALE" birthdate="01.01.1888"/>
|
||||||
</actors>
|
</actors>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user