Rename plural classes to singular, make XmlHelper types coherent

No functional changes, only nicer code
This commit is contained in:
karl 2019-12-01 18:57:31 +01:00
parent 0b8aaae4a6
commit 28a73932ae
11 changed files with 53 additions and 51 deletions

View File

@ -1,6 +1,5 @@
package at.technikumwien.movies;
import javax.sound.sampled.Port;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.URL;
@ -15,11 +14,12 @@ public class MoviesWebServiceClient {
MoviesWebService port = service.getPort(MoviesWebService.class);
// TODO: Move to a better place
// Import movies
List<Movies> movies = XmlHelper.xmlToMovies("movietest.xml");
List<Movie> movies = XmlHelper.xmlToMovies("movietest.xml");
port.importMovies(movies);
// Export all existing movies
XmlHelper.moviesToXml(new MovieList(port.getAllMovies()), "movies_in_db.xml");
XmlHelper.moviesToXml(port.getAllMovies(), "movies_in_db.xml");
}
}

View File

@ -6,7 +6,9 @@ import java.io.File;
import java.util.List;
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);
Marshaller marshaller = jaxbContext.createMarshaller();
@ -14,11 +16,11 @@ public class XmlHelper {
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(movies, System.out);
marshaller.marshal(movies, new File(filename));
marshaller.marshal(movieList, System.out);
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);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

View File

@ -17,7 +17,7 @@ import java.time.LocalDate;
@NamedQueries({
@NamedQuery(
name = "Actors.getIdByProperties",
query = "SELECT a FROM Actors a WHERE " +
query = "SELECT a FROM Actor a WHERE " +
"a.firstname LIKE :firstname AND " +
"a.lastname LIKE :lastname AND " +
"a.sex = :sex AND " +
@ -25,11 +25,11 @@ import java.time.LocalDate;
),
@NamedQuery(
name = "Actors.selectAllActors",
query = "SELECT n FROM Actors n"
query = "SELECT n FROM Actor n"
)
})
@XmlRootElement
public class Actors {
public class Actor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@XmlTransient
@ -51,7 +51,7 @@ public class Actors {
@XmlAttribute
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);
}
}

View File

@ -15,16 +15,16 @@ import java.util.List;
@NamedQueries({
@NamedQuery(
name = "Movies.selectAll",
query = "SELECT n FROM Movies n"
query = "SELECT n FROM Movie n"
),
@NamedQuery(
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
public class Movies {
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@XmlTransient
@ -57,14 +57,14 @@ public class Movies {
inverseJoinColumns = @JoinColumn(name = "fk_actors_id"))
@XmlElementWrapper(name = "actors")
@XmlElement(name = "actor")
private List<Actors> actors;
private List<Actor> actors;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "fk_studios_id")
@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.description = description;
this.genre = genre;

View File

@ -13,5 +13,5 @@ import java.util.List;
@NoArgsConstructor
public class MovieList {
@XmlElement(name="movie")
public List<Movies> movies;
public List<Movie> movies;
}

View File

@ -7,11 +7,11 @@ import java.util.List;
@WebService
public interface MoviesWebService {
@WebMethod
List<Movies> getAllMovies();
List<Movie> getAllMovies();
@WebMethod
List<Movies> getMoviesByTitle(String title);
List<Movie> getMoviesByTitle(String title);
@WebMethod
void importMovies(List<Movies> movies);
void importMovies(List<Movie> movies);
}

View File

@ -17,17 +17,17 @@ import javax.xml.bind.annotation.XmlTransient;
@NamedQueries({
@NamedQuery(
name = "Studios.getIdByProperties",
query = "SELECT a FROM Studios a WHERE " +
query = "SELECT a FROM Studio a WHERE " +
"a.countrycode LIKE :countrycode AND " +
"a.name LIKE :name AND " +
"a.postcode LIKE :postcode"
),
@NamedQuery(
name = "Studios.selectAllStudios",
query = "SELECT n FROM Studios n"
query = "SELECT n FROM Studio n"
)
})
public class Studios {
public class Studio {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@XmlTransient
@ -43,7 +43,7 @@ public class Studios {
@XmlAttribute
private String postcode;
public Studios(String name, String countrycode, String postcode) {
public Studio(String name, String countrycode, String postcode) {
this(null, name, countrycode, postcode);
}
}

View File

@ -11,7 +11,7 @@ public class MoviesManaged {
@Inject
private MoviesService moviesService;
public List<Movies> getAllMovies(){
public List<Movie> getAllMovies(){
return moviesService.findAll();
}
}

View File

@ -19,63 +19,63 @@ public class MoviesService {
@PersistenceContext(unitName = "MoviesPU")
private EntityManager em;
public Movies findById(long id) {
public Movie findById(long id) {
LOGGER.info("findById() >> id=" + id);
Movies movies = em.find(Movies.class, id);
if (movies == null) {
Movie movie = em.find(Movie.class, id);
if (movie == null) {
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);
return em.createNamedQuery("Movies.selectByTitle", Movies.class)
return em.createNamedQuery("Movies.selectByTitle", Movie.class)
.setParameter("title", "%" + title + "%")
.getResultList();
}
public List<Movies> findAll() {
public List<Movie> 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();
}
public void removeById(long id) {
LOGGER.info("removeById() >> id=" + id);
Movies movies = findById(id);
em.remove(movies); //managed news required
Movie movie = findById(id);
em.remove(movie); //managed news required
}
public List<Actors> findAllActors() {
public List<Actor> findAllActors() {
LOGGER.info("findAllActors)");
return em.createNamedQuery("Actors.selectAllActors", Actors.class)
return em.createNamedQuery("Actors.selectAllActors", Actor.class)
.getResultList();
}
public List<Studios> findAllStudios() {
public List<Studio> findAllStudios() {
LOGGER.info("findAllStudios)");
return em.createNamedQuery("Studios.selectAllStudios", Studios.class)
return em.createNamedQuery("Studios.selectAllStudios", Studio.class)
.getResultList();
}
// TODO maybe check if the movie already exists?
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void save(List<Movies> movies) {
public void save(List<Movie> movies) {
try {
for (Movies movie : movies) {
for (Movie movie : movies) {
// Get studio for the movie from the database
Studios movieStudio = movie.getStudio();
Studio movieStudio = movie.getStudio();
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("countrycode", movieStudio.getCountrycode())
.setParameter("postcode", movieStudio.getPostcode())
@ -94,9 +94,9 @@ public class MoviesService {
}
// Get actor for the movie from the database
for (Actors movieActor : movie.getActors()) {
for (Actor movieActor : movie.getActors()) {
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("lastname", movieActor.getLastname())
.setParameter("sex", movieActor.getSex())
@ -118,7 +118,7 @@ public class MoviesService {
}
LOGGER.info("save() >> movies" + movies);
for (Movies movieToImport : movies) {
for (Movie movieToImport : movies) {
em.merge(movieToImport);
}
} catch (Exception e) {

View File

@ -17,17 +17,17 @@ public class MoviesWebServiceImpl implements MoviesWebService {
private MoviesService moviesService;
@Override
public List<Movies> getAllMovies() {
public List<Movie> getAllMovies() {
return moviesService.findAll();
}
@Override
public List<Movies> getMoviesByTitle(String title) {
public List<Movie> getMoviesByTitle(String title) {
return moviesService.findByTitle(title);
}
@Override
public void importMovies(List<Movies> movies) {
public void importMovies(List<Movie> movies) {
moviesService.save(movies);
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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>
<actor firstname="Test" lastname="Actor" sex="MALE" birthdate="01.01.1888"/>
</actors>