package ru.foror.articles.tapestry.blog.domain; import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.Lob; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import org.apache.tapestry5.beaneditor.NonVisual; import org.apache.tapestry5.beaneditor.Validate; import org.hibernate.search.annotations.DocumentId; import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.Index; import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.Store; import org.hibernate.validator.Length; import org.hibernate.validator.NotEmpty; import org.hibernate.validator.NotNull; import ru.foror.articles.common.domain.Category; import ru.foror.articles.common.domain.ISearchResult; import ru.foror.articles.common.domain.IWithTags; import ru.foror.articles.common.utils.Html; /** * Статья в блоге. * * @author Alexey Pomogaev foror@mail.ru */ @Indexed(index="article") @Entity @Table(name="article") @SequenceGenerator(name="default_seq", sequenceName="default_seq", allocationSize=1) public class Article implements Serializable, ISearchResult, IWithTags { private static final long serialVersionUID = 6774264953970869480L; @DocumentId @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="default_seq") @NonVisual private Long id; @NotEmpty @Length(max=100) @Validate("required,maxlength=100") private String title; @NotEmpty @Validate("required") @Lob private String body; @NotNull @ManyToOne @JoinColumn(name = "author_id") private Account author; @NotNull private Date created; @NotNull @ManyToOne @JoinColumn(name = "category_id") private Category category; @ManyToMany @JoinTable( name="articles_tags", joinColumns={@JoinColumn(name="article_id")}, inverseJoinColumns={@JoinColumn(name="tag_id")}) private List tags; @OneToMany(mappedBy="article", cascade=CascadeType.REMOVE) private List comments; public Article() { created = new Date(System.currentTimeMillis()); } public Article(String title, String body, Account author, Category category) { this(); this.title = title; this.body = body; this.author = author; this.category = category; } @Field(index=Index.TOKENIZED, store=Store.YES) public String getTitle() { return title; } @NonVisual @Field(index=Index.NO, store=Store.YES) public String getObjectOwner() { return author.getNickname(); } @NonVisual @Field(index=Index.TOKENIZED, store=Store.YES) public String getPlainText() { return Html.getText(body, true, false, false); } @NonVisual @Field(index=Index.NO, store=Store.YES) public Date getDate() { return created; } public void setTitle(String title) { this.title = title; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public Account getAuthor() { return author; } public void setAuthor(Account author) { this.author = author; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } public Long getId() { return id; } @SuppressWarnings("unchecked") public List getTags() { return (List) tags; } public void initTags(int size) { tags = new ArrayList(size); } public List getComments() { return comments; } public void addComment(Comment comment) { if (comments == null) { comments = new ArrayList(1); } if (comments.contains(comment)) { return; } else { comments.add(comment); comment.setArticle(this); } } @Override public int hashCode() { final int PRIME = 31; int result = 1; result = PRIME * result + ((created == null) ? 0 : created.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Article other = (Article) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } }