|
| 1 | +package edu.tamu.app.model; |
| 2 | + |
| 3 | +import static javax.persistence.FetchType.EAGER; |
| 4 | +import static org.hibernate.annotations.FetchMode.SELECT; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import javax.persistence.CascadeType; |
| 10 | +import javax.persistence.Entity; |
| 11 | +import javax.persistence.JoinTable; |
| 12 | +import javax.persistence.OneToMany; |
| 13 | +import javax.persistence.UniqueConstraint; |
| 14 | + |
| 15 | +import org.hibernate.annotations.Fetch; |
| 16 | + |
| 17 | +import com.fasterxml.jackson.annotation.JsonIdentityInfo; |
| 18 | +import com.fasterxml.jackson.annotation.JsonIdentityReference; |
| 19 | +import com.fasterxml.jackson.annotation.ObjectIdGenerators; |
| 20 | + |
| 21 | +@Entity |
| 22 | +public class FeatureProposal extends AbstractIdea { |
| 23 | + |
| 24 | + @OneToMany(fetch = EAGER, cascade = { CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE }) |
| 25 | + @JoinTable(uniqueConstraints = @UniqueConstraint(columnNames = { "feature_proposal_id", "ideas_id" })) |
| 26 | + @Fetch(value = SELECT) |
| 27 | + private List<Idea> ideas; |
| 28 | + |
| 29 | + @OneToMany(fetch = EAGER, cascade = { CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE }) |
| 30 | + @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, scope = User.class, property = "id") |
| 31 | + @JsonIdentityReference(alwaysAsId = true) |
| 32 | + @JoinTable(uniqueConstraints = @UniqueConstraint(columnNames = { "feature_proposal_id", "voters_id" })) |
| 33 | + @Fetch(value = SELECT) |
| 34 | + private List<User> voters; |
| 35 | + |
| 36 | + public FeatureProposal() { |
| 37 | + super(); |
| 38 | + setup(); |
| 39 | + } |
| 40 | + |
| 41 | + public FeatureProposal(String title, String description) { |
| 42 | + super(title, description); |
| 43 | + setup(); |
| 44 | + } |
| 45 | + |
| 46 | + public FeatureProposal(String title, String description, User author) { |
| 47 | + super(title, description, author); |
| 48 | + setup(); |
| 49 | + } |
| 50 | + |
| 51 | + public FeatureProposal(String title, String description, User author, Service service) { |
| 52 | + super(title, description, author, service); |
| 53 | + setup(); |
| 54 | + } |
| 55 | + |
| 56 | + public FeatureProposal(Idea idea) { |
| 57 | + super(idea.getTitle(), idea.getDescription(), idea.getAuthor(), idea.getService()); |
| 58 | + setup(); |
| 59 | + addIdea(idea); |
| 60 | + } |
| 61 | + |
| 62 | + private void setup() { |
| 63 | + this.ideas = new ArrayList<Idea>(); |
| 64 | + this.voters = new ArrayList<User>(); |
| 65 | + } |
| 66 | + |
| 67 | + public List<Idea> getIdeas() { |
| 68 | + return ideas; |
| 69 | + } |
| 70 | + |
| 71 | + public void setIdeas(List<Idea> ideas) { |
| 72 | + this.ideas.forEach(idea -> { |
| 73 | + removeVoter(idea.getAuthor()); |
| 74 | + }); |
| 75 | + this.ideas = ideas; |
| 76 | + this.ideas.forEach(idea -> { |
| 77 | + addVoter(idea.getAuthor()); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + public void addIdea(Idea idea) { |
| 82 | + if (!this.ideas.contains(idea)) { |
| 83 | + this.ideas.add(idea); |
| 84 | + addVoter(idea.getAuthor()); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public void removeIdea(Idea idea) { |
| 89 | + this.ideas.remove(idea); |
| 90 | + removeVoter(idea.getAuthor()); |
| 91 | + } |
| 92 | + |
| 93 | + public List<User> getVoters() { |
| 94 | + return voters; |
| 95 | + } |
| 96 | + |
| 97 | + public void setVoters(List<User> voters) { |
| 98 | + this.voters = voters; |
| 99 | + } |
| 100 | + |
| 101 | + public void addVoter(User voter) { |
| 102 | + if (!this.voters.contains(voter)) { |
| 103 | + this.voters.add(voter); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public void removeVoter(User voter) { |
| 108 | + this.voters.remove(voter); |
| 109 | + } |
| 110 | + |
| 111 | + public int getVotes() { |
| 112 | + return this.voters.size(); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments