package org.ambraproject.rhino.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;

@Entity
@Table(name = "articleRevision")
public class ArticleRevision implements Timestamped {

  @Id
  @GeneratedValue
  @Column
  private long revisionId;

  @JoinColumn(name = "ingestionId", nullable = false)
  @OneToOne
  private ArticleIngestion ingestion;

  @Column
  private int revisionNumber;

  @Column
  private Date created;

  public long getRevisionId() {
    return revisionId;
  }

  public void setRevisionId(long revisionId) {
    this.revisionId = revisionId;
  }

  public ArticleIngestion getIngestion() {
    return ingestion;
  }

  public void setIngestion(ArticleIngestion ingestion) {
    this.ingestion = ingestion;
  }

  public int getRevisionNumber() {
    return revisionNumber;
  }

  public void setRevisionNumber(int revisionNumber) {
    this.revisionNumber = revisionNumber;
  }

  public Date getCreated() {
    return created;
  }

  public void setCreated(Date created) {
    this.created = created;
  }

  @Transient
  @Override
  public Date getLastModified() {
    return getCreated();
  }


  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    ArticleRevision that = (ArticleRevision) o;

    if (revisionId != that.revisionId) return false;
    if (revisionNumber != that.revisionNumber) return false;
    return ingestion != null ? ingestion.equals(that.ingestion) : that.ingestion == null;

  }

  @Override
  public int hashCode() {
    int result = (int) (revisionId ^ (revisionId >>> 32));
    result = 31 * result + (ingestion != null ? ingestion.hashCode() : 0);
    result = 31 * result + revisionNumber;
    return result;
  }

  @Override
  public String toString() {
    return "ArticleRevision{" +
        "revisionId=" + revisionId +
        ", ingestion=" + ingestion +
        ", revisionNumber=" + revisionNumber +
        '}';
  }
}

@@ -32,16 +32,110 @@
 model;%0A%0A
+import org.hibernate.annotations.Generated;%0Aimport org.hibernate.annotations.GenerationTime;%0A%0A
 import j
@@ -151,32 +151,32 @@
 istence.Column;%0A
-
 import javax.per
@@ -366,16 +366,51 @@
 .Table;%0A
+import javax.persistence.Temporal;%0A
 import j
@@ -764,30 +764,218 @@
 Number;%0A%0A  @
-Column
+Generated(value= GenerationTime.INSERT)%0A  @Temporal(javax.persistence.TemporalType.TIMESTAMP)%0A  @Column(insertable=false, updatable=false, columnDefinition=%22timestamp default current_timestamp%22)
 %0A  private D

add timestamp annotations to articleRevision created member