1 | package com.wediscussmovies.project.model;
|
---|
2 |
|
---|
3 | import com.wediscussmovies.project.model.relation.MovieLikes;
|
---|
4 | import com.wediscussmovies.project.model.relation.MovieActors;
|
---|
5 | import com.wediscussmovies.project.model.relation.MovieGenres;
|
---|
6 | import com.wediscussmovies.project.model.relation.MovieRates;
|
---|
7 | import lombok.Data;
|
---|
8 | import org.hibernate.annotations.Fetch;
|
---|
9 |
|
---|
10 | import javax.persistence.*;
|
---|
11 | import java.sql.Date;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Comparator;
|
---|
14 | import java.util.Objects;
|
---|
15 |
|
---|
16 | @Entity
|
---|
17 | @Table(name = "movies", schema = "project", catalog = "db_202122z_va_prj_wediscussmovies")
|
---|
18 | @Data
|
---|
19 | public class Movie {
|
---|
20 | @GeneratedValue(strategy = GenerationType.IDENTITY)
|
---|
21 | @Id
|
---|
22 | @Column(name = "movie_id")
|
---|
23 | private int movieId;
|
---|
24 | @Basic
|
---|
25 | @Column(name = "title")
|
---|
26 | private String title;
|
---|
27 | @Basic
|
---|
28 | @Column(name = "description")
|
---|
29 | private String description;
|
---|
30 | @Basic
|
---|
31 | @Column(name = "image_url")
|
---|
32 | private String imageUrl;
|
---|
33 | @Basic
|
---|
34 | @Column(name = "airing_date")
|
---|
35 | private Date airingDate;
|
---|
36 | @Basic
|
---|
37 | @Column(name = "imdb_rating")
|
---|
38 | private Double imdbRating;
|
---|
39 |
|
---|
40 | @OneToMany(mappedBy = "movie", fetch = FetchType.LAZY)
|
---|
41 | private Collection<MovieActors> actors;
|
---|
42 | @OneToMany(mappedBy = "movie", fetch = FetchType.LAZY)
|
---|
43 | private Collection<MovieGenres> genres;
|
---|
44 | @OneToMany(mappedBy = "movie")
|
---|
45 | private Collection<MovieLikes> likes;
|
---|
46 | @OneToMany(mappedBy = "movie")
|
---|
47 | private Collection<MovieRates> rates;
|
---|
48 |
|
---|
49 | @ManyToOne
|
---|
50 | @JoinColumn(name = "director_id")
|
---|
51 | private Person director;
|
---|
52 |
|
---|
53 | public Movie() {
|
---|
54 |
|
---|
55 | }
|
---|
56 |
|
---|
57 | public Movie(String title, String description, String imageUrl, Date airingDate,
|
---|
58 | Double imdbRating, Person director) {
|
---|
59 | this.title = title;
|
---|
60 | this.description = description;
|
---|
61 | this.imageUrl = imageUrl;
|
---|
62 | this.airingDate = airingDate;
|
---|
63 | this.imdbRating = imdbRating;
|
---|
64 | this.director = director;
|
---|
65 |
|
---|
66 | }
|
---|
67 |
|
---|
68 | public Movie(int movieId, String title, Double imdbRating, String imageUrl) {
|
---|
69 | this.movieId = movieId;
|
---|
70 | this.title = title;
|
---|
71 | this.imdbRating = imdbRating;
|
---|
72 | this.imageUrl = imageUrl;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public String getDateFormatted(){
|
---|
76 | String dob = airingDate.toString();
|
---|
77 | String [] parts = dob.split("-");
|
---|
78 | return parts[2]+"/"+parts[1]+"/"+parts[0];
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public boolean equals(Object o) {
|
---|
83 | if (this == o) return true;
|
---|
84 | if (o == null || getClass() != o.getClass()) return false;
|
---|
85 | Movie movie = (Movie) o;
|
---|
86 | return movieId == movie.movieId;
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public int hashCode() {
|
---|
91 | return Objects.hash(movieId);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public static Comparator<Movie> comparatorTitle = Comparator.comparing(Movie::getTitle);
|
---|
95 |
|
---|
96 |
|
---|
97 | public String getShortTitle(){
|
---|
98 | int to = 20;
|
---|
99 | if (title.length() < to)
|
---|
100 | to = title.length();
|
---|
101 | if(to<20)
|
---|
102 | return title;
|
---|
103 | return title.substring(0, to) + "...";
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 |
|
---|
108 |
|
---|
109 | }
|
---|