|
4 | 4 | import static javax.persistence.CascadeType.REFRESH; |
5 | 5 | import static javax.persistence.FetchType.EAGER; |
6 | 6 |
|
7 | | -import java.util.ArrayList; |
8 | 7 | import java.util.Calendar; |
9 | | -import java.util.List; |
| 8 | +import java.util.HashSet; |
| 9 | +import java.util.Set; |
10 | 10 |
|
11 | 11 | import javax.persistence.Column; |
12 | 12 | import javax.persistence.Entity; |
|
17 | 17 | import javax.persistence.TemporalType; |
18 | 18 | import javax.validation.constraints.Size; |
19 | 19 |
|
20 | | -import org.hibernate.annotations.CreationTimestamp; |
21 | | -import org.hibernate.annotations.Fetch; |
22 | | -import org.hibernate.annotations.FetchMode; |
23 | 20 | import org.hibernate.annotations.UpdateTimestamp; |
24 | 21 |
|
| 22 | +import com.fasterxml.jackson.annotation.JsonIdentityInfo; |
| 23 | +import com.fasterxml.jackson.annotation.JsonIdentityReference; |
| 24 | +import com.fasterxml.jackson.annotation.ObjectIdGenerators; |
| 25 | + |
25 | 26 | import edu.tamu.app.enums.NoteType; |
| 27 | +import edu.tamu.app.model.validation.NoteValidator; |
26 | 28 | import edu.tamu.framework.model.BaseEntity; |
27 | 29 |
|
28 | 30 | @Entity |
29 | 31 | public class Note extends BaseEntity { |
30 | 32 |
|
31 | | - @Size(min = 1) |
| 33 | + @Size(min = 3) |
32 | 34 | @Column(nullable = false) |
33 | 35 | private String title; |
34 | 36 |
|
35 | | - @Fetch(FetchMode.SELECT) |
36 | | - @ManyToMany(cascade = { REFRESH, MERGE}, fetch = EAGER) |
37 | | - private List<Service> services; |
38 | | - |
| 37 | + @ManyToMany(fetch = EAGER, cascade = { REFRESH, MERGE }) |
| 38 | + @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, scope = Service.class, property = "id") |
| 39 | + @JsonIdentityReference(alwaysAsId = true) |
| 40 | + private Set<Service> services; |
| 41 | + |
39 | 42 | private NoteType noteType; |
40 | 43 |
|
41 | 44 | private String body; |
42 | 45 |
|
43 | 46 | @Temporal(TemporalType.DATE) |
44 | 47 | private Calendar scheduledPostingStart; |
45 | | - |
| 48 | + |
46 | 49 | @Temporal(TemporalType.DATE) |
47 | 50 | private Calendar scheduledPostingEnd; |
48 | | - |
| 51 | + |
49 | 52 | @Temporal(TemporalType.TIMESTAMP) |
50 | 53 | @UpdateTimestamp |
51 | 54 | private Calendar lastModified; |
52 | | - |
| 55 | + |
53 | 56 | @JoinColumn(nullable = false) |
54 | 57 | @ManyToOne(cascade = REFRESH) |
55 | 58 | private AppUser author; |
56 | | - |
| 59 | + |
57 | 60 | public Note() { |
58 | | - setServices(new ArrayList<Service>()); |
| 61 | + setModelValidator(new NoteValidator()); |
| 62 | + setServices(new HashSet<Service>()); |
59 | 63 | } |
60 | | - |
61 | | - public Note(String name, AppUser author) { |
| 64 | + |
| 65 | + public Note(String title, AppUser author) { |
62 | 66 | this(); |
63 | | - setTitle(name); |
| 67 | + setTitle(title); |
64 | 68 | setAuthor(author); |
65 | 69 | } |
66 | | - |
| 70 | + |
| 71 | + public Note(String title, AppUser author, NoteType noteType, String body) { |
| 72 | + this(title, author); |
| 73 | + setNoteType(noteType); |
| 74 | + setBody(body); |
| 75 | + } |
| 76 | + |
| 77 | + public Note(String title, AppUser author, NoteType noteType, String body, Set<Service> services) { |
| 78 | + this(title, author, noteType, body); |
| 79 | + setServices(services); |
| 80 | + } |
| 81 | + |
67 | 82 | public String getTitle() { |
68 | 83 | return title; |
69 | 84 | } |
70 | 85 |
|
71 | 86 | public void setTitle(String title) { |
72 | 87 | this.title = title; |
73 | 88 | } |
74 | | - |
75 | | - public List<Service> getServices() { |
| 89 | + |
| 90 | + public Set<Service> getServices() { |
76 | 91 | return services; |
77 | 92 | } |
78 | | - |
79 | | - public void setServices(List<Service> services) { |
| 93 | + |
| 94 | + public void setServices(Set<Service> services) { |
80 | 95 | this.services = services; |
81 | 96 | } |
82 | | - |
| 97 | + |
| 98 | + public void removeService(Service service) { |
| 99 | + this.services.remove(service); |
| 100 | + } |
| 101 | + |
83 | 102 | public NoteType getNoteType() { |
84 | 103 | return noteType; |
85 | 104 | } |
|
0 commit comments