Added checking by import of movies, if actors and studio are already in the database. Birthday needs to be add for checking. Maybe check if movie already exists? All or none principle needs checking
This commit is contained in:
parent
a870c28d21
commit
7c0cb4150d
@ -13,14 +13,20 @@ import java.time.LocalDate;
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "t_actors")
|
@Table(name = "t_actors")
|
||||||
|
@NamedQueries({
|
||||||
@NamedQuery(
|
@NamedQuery(
|
||||||
name = "Actors.getIdByProperties",
|
name = "Actors.getIdByProperties",
|
||||||
query = "SELECT a.id FROM Actors a WHERE " +
|
query = "SELECT a FROM Actors 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 " +
|
||||||
"a.birthdate = :birthdate"
|
//"a.birthdate = :birthdate"
|
||||||
|
),
|
||||||
|
@NamedQuery(
|
||||||
|
name = "Actors.selectAllActors",
|
||||||
|
query = "SELECT n FROM Actors n"
|
||||||
)
|
)
|
||||||
|
})
|
||||||
@XmlRootElement
|
@XmlRootElement
|
||||||
public class Actors {
|
public class Actors {
|
||||||
@XmlAttribute(name = "id")
|
@XmlAttribute(name = "id")
|
||||||
@ -37,7 +43,7 @@ public class Actors {
|
|||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private Sex sex;
|
private Sex sex;
|
||||||
|
|
||||||
@Column(nullable = false)
|
//@Column(nullable = false)
|
||||||
private LocalDate birthdate;
|
private LocalDate birthdate;
|
||||||
|
|
||||||
public Actors(String firstname, String lastname, Sex sex, LocalDate birthdate) {
|
public Actors(String firstname, String lastname, Sex sex, LocalDate birthdate) {
|
||||||
|
@ -5,7 +5,6 @@ import lombok.*;
|
|||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import javax.xml.bind.annotation.XmlAttribute;
|
import javax.xml.bind.annotation.XmlAttribute;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@ -14,14 +13,17 @@ import java.util.List;
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "t_movies")
|
@Table(name = "t_movies")
|
||||||
|
@NamedQueries({
|
||||||
@NamedQuery(
|
@NamedQuery(
|
||||||
name = "Movies.selectAll",
|
name = "Movies.selectAll",
|
||||||
query = "SELECT n FROM Movies n"
|
query = "SELECT n FROM Movies n"
|
||||||
)
|
),
|
||||||
@NamedQuery(
|
@NamedQuery(
|
||||||
name = "Movies.selectByTitle",
|
name = "Movies.selectByTitle",
|
||||||
query = "SELECT n FROM Movies n WHERE n.title LIKE :title"
|
query = "SELECT n FROM Movies n WHERE n.title LIKE :title"
|
||||||
)
|
)
|
||||||
|
})
|
||||||
|
|
||||||
@XmlRootElement
|
@XmlRootElement
|
||||||
public class Movies {
|
public class Movies {
|
||||||
// TODO: XmlAttributes may need different names
|
// TODO: XmlAttributes may need different names
|
||||||
|
@ -10,6 +10,19 @@ import javax.persistence.*;
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "t_studios")
|
@Table(name = "t_studios")
|
||||||
|
@NamedQueries({
|
||||||
|
@NamedQuery(
|
||||||
|
name = "Studios.getIdByProperties",
|
||||||
|
query = "SELECT a FROM Studios 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"
|
||||||
|
)
|
||||||
|
})
|
||||||
public class Studios {
|
public class Studios {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@ -5,11 +5,11 @@ import javax.ejb.*;
|
|||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.EntityNotFoundException;
|
import javax.persistence.EntityNotFoundException;
|
||||||
import javax.persistence.PersistenceContext;
|
import javax.persistence.PersistenceContext;
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@Stateless
|
@Stateless
|
||||||
|
@TransactionManagement(value=TransactionManagementType.CONTAINER)
|
||||||
public class MoviesService {
|
public class MoviesService {
|
||||||
private static final Logger LOGGER = Logger.getLogger(MoviesService.class.getName());
|
private static final Logger LOGGER = Logger.getLogger(MoviesService.class.getName());
|
||||||
|
|
||||||
@ -52,18 +52,80 @@ public class MoviesService {
|
|||||||
em.remove(movies); //managed news required
|
em.remove(movies); //managed news required
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(Movies movies) {
|
public List<Actors> findAllActors() {
|
||||||
// // TODO
|
LOGGER.info("findAllActors)");
|
||||||
// for (Actors actor : movies.getActors()) {
|
|
||||||
// ResultSet rs = em.createNamedQuery("Actors.getIdByParameters", Actors.class)
|
|
||||||
// .setParameter() // TODO
|
|
||||||
// .getResultList();
|
|
||||||
//
|
|
||||||
// // Check if we have a result, if yes -> take first, actor.id = resultID
|
|
||||||
// // If no result: context.setRollbackOnly();
|
|
||||||
// }
|
|
||||||
LOGGER.info("save() >> movies" + movies);
|
|
||||||
|
|
||||||
em.merge(movies);
|
return em.createNamedQuery("Actors.selectAllActors", Actors.class)
|
||||||
|
.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Studios> findAllStudios() {
|
||||||
|
LOGGER.info("findAllStudios)");
|
||||||
|
|
||||||
|
return em.createNamedQuery("Studios.selectAllStudios", Studios.class)
|
||||||
|
.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO maybe check if the movie already exists?
|
||||||
|
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
|
||||||
|
public void save(Movies movie) {
|
||||||
|
|
||||||
|
// TODO maybe get just the id and improve checking in for loop?
|
||||||
|
// Get all actors and studios from the database
|
||||||
|
List<Actors> allActors = findAllActors();
|
||||||
|
List<Studios> allStudios = findAllStudios();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get studio for the movie from the database
|
||||||
|
Studios movieStudio = movie.getStudio();
|
||||||
|
|
||||||
|
// If the studio is missing in the database, roll back only
|
||||||
|
for (Studios studio : allStudios) {
|
||||||
|
if (studio.getName().equals(movieStudio.getName()) & studio.getCountrycode().equals(movieStudio.getCountrycode())
|
||||||
|
& studio.getPostcode().equals(movieStudio.getPostcode())) {
|
||||||
|
// Set id
|
||||||
|
movieStudio.setId(studio.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (movieStudio.getId() == null) {
|
||||||
|
LOGGER.info("Rollback! Movie studio: " + movieStudio);
|
||||||
|
context.setRollbackOnly();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO use also birthday
|
||||||
|
// Get actor for the movie from the database
|
||||||
|
for (Actors movieActor : movie.getActors()) {
|
||||||
|
LOGGER.info("Get an actor " + movieActor);
|
||||||
|
List actorListResult = em.createNamedQuery("Actors.getIdByProperties", Actors.class)
|
||||||
|
.setParameter("firstname", movieActor.getFirstname())
|
||||||
|
.setParameter("lastname", movieActor.getLastname())
|
||||||
|
.setParameter("sex", movieActor.getSex())
|
||||||
|
//.setParameter("birthdate", movieActor.getBirthdate())
|
||||||
|
.getResultList();
|
||||||
|
|
||||||
|
// If any actor is missing in the database, roll back only
|
||||||
|
for (Actors actor : allActors) {
|
||||||
|
if (actor.getFirstname().equals(movieActor.getFirstname())
|
||||||
|
& actor.getLastname().equals(movieActor.getLastname())
|
||||||
|
& actor.getSex().equals(movieActor.getSex())) {
|
||||||
|
// & actor.getBirthdate().equals(movieActor.getBirthdate())) {
|
||||||
|
// Set id
|
||||||
|
movieActor.setId(actor.getId());
|
||||||
|
}
|
||||||
|
if (movieActor.getId() == null) {
|
||||||
|
LOGGER.info("Rollback! Movie actor: " + movieActor);
|
||||||
|
context.setRollbackOnly();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LOGGER.info("save() >> movies" + movie);
|
||||||
|
em.merge(movie);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -25,8 +25,8 @@ public class MoviesServlet extends HttpServlet {
|
|||||||
"<body>" +
|
"<body>" +
|
||||||
"<h1>Movies</h1>");
|
"<h1>Movies</h1>");
|
||||||
|
|
||||||
moviesService.findAll().forEach(news -> html.append("<h2>" + news.getTitle() + "</h2>" +
|
moviesService.findAll().forEach(movies -> html.append("<h2>" + movies.getTitle() + "</h2>" +
|
||||||
"<p>" + news.getTitle() + "</p>"));
|
"<p>" + movies.getActors() + "</p>"));
|
||||||
|
|
||||||
html.append("</body>" +
|
html.append("</body>" +
|
||||||
"</html>");
|
"</html>");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user