Created
December 2, 2015 12:36
-
-
Save jabu10245/0c66e018fae053376231 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import static javax.persistence.CascadeType.ALL; | |
import static javax.persistence.FetchType.EAGER; | |
import static javax.persistence.GenerationType.SEQUENCE; | |
import java.io.Serializable; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.Id; | |
import javax.persistence.JoinColumn; | |
import javax.persistence.OneToMany; | |
import javax.persistence.OneToOne; | |
import javax.persistence.SequenceGenerator; | |
import javax.persistence.Transient; | |
import javax.persistence.Version; | |
import javax.validation.constraints.AssertTrue; | |
import org.apache.commons.lang.StringUtils; | |
@Entity | |
public class Content implements Serializable { | |
private static final long serialVersionUID = 1L; | |
/* Members ------------------------------------------------------------- */ | |
@Id @GeneratedValue(strategy = SEQUENCE, generator = "content_seq") | |
@SequenceGenerator(name = "content_seq", sequenceName = "content_seq", initialValue = 1, allocationSize = 1) | |
private Long id; | |
@OneToOne(cascade = ALL) @JoinColumn(name = "current_content_revision_id") | |
private ContentRevision current; | |
@OneToMany(mappedBy = "content", cascade = ALL, orphanRemoval = true, fetch = EAGER) | |
private List<ContentRevision> revisionList; | |
@Version | |
private int version; | |
/* Properties ---------------------------------------------------------- */ | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
public ContentRevision getCurrent() { | |
return current; | |
} | |
public void setCurrent(ContentRevision current) { | |
this.current = current; | |
} | |
/** | |
* Checks whether the current revision is in the list of revisions. | |
* @return {@code true} if valid, {@code false} otherwise | |
*/ | |
@Transient // ignored in JPA | |
@AssertTrue // javax.validation | |
public boolean isCurrentRevisionInList() { | |
return current != null && getRevisionList().contains(current); | |
} | |
public List<ContentRevision> getRevisionList() { | |
if (revisionList == null) | |
revisionList = new ArrayList<ContentRevision>(); | |
return revisionList; | |
} | |
public void setRevisionList(List<ContentRevision> revisionList) { | |
this.revisionList = revisionList; | |
} | |
/* Methods ------------------------------------------------------------- */ | |
@Override | |
public int hashCode() { | |
// using "id", since we don't have any other identifying property | |
return id == null ? 0 : id.intValue(); | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (obj == this) | |
return true; | |
else if (!(obj instanceof Content)) | |
return false; | |
// compare using "id" | |
Content that = (Content) obj; | |
if (this.id == null || that.id == null) | |
return false; | |
else | |
return this.id.equals(that.id); | |
} | |
/** | |
* Adds the given revision. | |
* @param revision revision to add | |
* @return {@code true} if added, {@code false} otherwise | |
*/ | |
public boolean addRevision(ContentRevision revision) { | |
if (revision != null) { | |
revision.setContent(this); | |
return getRevisionList().add(revision); | |
} | |
return false; | |
} | |
/** | |
* Adds a new revision with the given text. | |
* @param text text data | |
* @return the new revision, or {@code null} if text was empty | |
*/ | |
public ContentRevision addRevision(String text) { | |
if (!StringUtils.isBlank(text)) { | |
ContentRevision revision = new ContentRevision(text); | |
if (addRevision(revision)) | |
return revision; | |
} | |
return null; | |
} | |
/** | |
* Factory method to create a content with an initial revision. | |
* @param text text data | |
* @return the new content, or {@code null} if text was empty | |
*/ | |
public static Content create(String text) { | |
Content content = new Content(); | |
ContentRevision initial = content.addRevision(text); | |
if (initial != null) { | |
content.setCurrent(initial); | |
return content; | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import static javax.persistence.GenerationType.SEQUENCE; | |
import java.io.Serializable; | |
import java.util.Date; | |
import javax.persistence.Basic; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.Id; | |
import javax.persistence.JoinColumn; | |
import javax.persistence.ManyToOne; | |
import javax.persistence.SequenceGenerator; | |
import javax.persistence.Temporal; | |
import javax.persistence.TemporalType; | |
import javax.persistence.Version; | |
import javax.validation.constraints.Min; | |
import javax.validation.constraints.NotNull; | |
import org.apache.commons.lang.StringUtils; | |
import org.apache.commons.lang.builder.EqualsBuilder; | |
import org.apache.commons.lang.builder.HashCodeBuilder; | |
@Entity | |
public class ContentRevision implements Serializable { | |
private static final long serialVersionUID = 1L; | |
/* Members ------------------------------------------------------------- */ | |
@Id @GeneratedValue(strategy = SEQUENCE, generator = "content_revision_seq") | |
@SequenceGenerator(name = "content_revision_seq", sequenceName = "content_revision_seq", initialValue = 1, allocationSize = 1) | |
private Long id; | |
@ManyToOne @JoinColumn(name = "content_id", updatable = false) | |
private Content content; | |
@Basic | |
private String textData; | |
@Temporal(TemporalType.TIMESTAMP) @Column(name = "reg_date") | |
private Date registrationDate; | |
@Version | |
private int version; | |
/* Constructors -------------------------------------------------------- */ | |
public ContentRevision(String text) { | |
this.textData = StringUtils.trimToNull(text); | |
this.registrationDate = new Date(); | |
} | |
public ContentRevision() { | |
this(null); | |
} | |
/* Properties ---------------------------------------------------------- */ | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
@NotNull | |
public Content getContent() { | |
return content; | |
} | |
public void setContent(Content content) { | |
this.content = content; | |
} | |
@NotNull | |
public String getTextData() { | |
return textData; | |
} | |
public void setTextData(String textData) { | |
this.textData = StringUtils.trimToNull(textData); | |
} | |
public Date getRegistrationDate() { | |
return registrationDate; | |
} | |
public void setRegistrationDate(Date registrationDate) { | |
this.registrationDate = registrationDate; | |
} | |
@Min(0) | |
public int getVersion() { | |
return version; | |
} | |
public void setVersion(int version) { | |
this.version = version; | |
} | |
/* Methods ------------------------------------------------------------- */ | |
@Override | |
public int hashCode() { | |
return new HashCodeBuilder() | |
.append(content) | |
.append(textData) | |
.append(registrationDate) | |
.toHashCode(); | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (obj == this) | |
return true; | |
else if (!(obj instanceof ContentRevision)) | |
return false; | |
ContentRevision that = (ContentRevision) obj; | |
return new EqualsBuilder() | |
.append(this.content, that.content) | |
.append(this.textData, that.textData) | |
.append(this.registrationDate, that.registrationDate) | |
.isEquals(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment