package pl.edu.icm.saos.persistence.model;
import java.util.List;
import javax.persistence.Cacheable;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import pl.edu.icm.saos.persistence.common.DataObject;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
/**
* pl. odrębna opinia
*
* @author madryk
*/
@Entity(name = "ct_judgment_opinion")
@Cacheable(true)
@SequenceGenerator(name = "seq_ctj_opinion", allocationSize = 1, sequenceName = "seq_ctj_opinion")
public class ConstitutionalTribunalJudgmentDissentingOpinion extends DataObject {
private Judgment judgment;
private String textContent;
private List<String> authors = Lists.newArrayList();
//------------------------ GETTERS --------------------------
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_ctj_opinion")
@Override
public long getId() {
return id;
}
@ManyToOne(fetch=FetchType.LAZY)
public Judgment getJudgment() {
return judgment;
}
public String getTextContent() {
return textContent;
}
@ElementCollection
@CollectionTable(name="ct_judgment_opinion_author",
joinColumns = {@JoinColumn(name = "fk_ct_judgment_opinion", nullable = false)},
uniqueConstraints={@UniqueConstraint(name="ct_judgment_opinion_author_unique", columnNames={"fk_ct_judgment_opinion", "author"})})
@Column(name="author", nullable=false)
private List<String> getAuthors_() {
return authors;
}
@Transient
public List<String> getAuthors() {
return ImmutableList.copyOf(getAuthors_());
}
//------------------------ LOGIC --------------------------
public void addAuthor(String author) {
Preconditions.checkArgument(!containsAuthor(author));
this.authors.add(author);
}
public boolean containsAuthor(String author) {
return this.authors.contains(author);
}
public void removeAuthor(String author) {
this.authors.remove(author);
}
//------------------------ SETTERS --------------------------
public void setJudgment(Judgment judgment) {
this.judgment = judgment;
}
public void setTextContent(String textContent) {
this.textContent = textContent;
}
@SuppressWarnings("unused") // for hibernate
private void setAuthors_(List<String> authors) {
this.authors = authors;
}
//------------------------ HashCode & Equals --------------------------
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((judgment == null) ? 0 : judgment.hashCode());
result = prime * result
+ ((textContent == null) ? 0 : textContent.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof ConstitutionalTribunalJudgmentDissentingOpinion)) {
return false;
}
ConstitutionalTribunalJudgmentDissentingOpinion other = (ConstitutionalTribunalJudgmentDissentingOpinion) obj;
if (judgment == null) {
if (other.judgment != null) {
return false;
}
} else if (!judgment.equals(other.judgment)) {
return false;
}
if (textContent == null) {
if (other.textContent != null) {
return false;
}
} else if (!textContent.equals(other.textContent)) {
return false;
}
return true;
}
//------------------------ toString --------------------------
@Override
public String toString() {
return "ConstitutionalTribunalJudgmentDissentingOpinion [id="
+ id + ", textContent=" + textContent +"]";
}
}