All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.data.jpa.domain.GenericAuditable Maven / Gradle / Ivy

package org.springframework.data.jpa.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.Optional;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.joda.time.DateTime;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.domain.Auditable;

/**
 * 
  @Override
  public Optional getCreatedBy() {
    return Optional.ofNullable(createdBy);
  }

  @Override
  public void setCreatedBy(U createdBy) {
    this.createdBy = createdBy;
  }

  @Override
  public Optional getCreatedDate() {
    return null == createdDate ? Optional.empty() : Optional.of(LocalDateTime.ofInstant(createdDate.toInstant(), ZoneId.systemDefault()));
  }

  @Override
  public void setCreatedDate(LocalDateTime createdDate) {
    this.createdDate = Date.from(createdDate.atZone(ZoneId.systemDefault()).toInstant());
    this.createdAddress = getRemoteAddress(null);
  }

  @Override
  public Optional getLastModifiedBy() {
    return Optional.ofNullable(lastModifiedBy);
  }

  @Override
  public void setLastModifiedBy(U lastModifiedBy) {
    this.lastModifiedBy = lastModifiedBy;
  }

  @Override
  public Optional getLastModifiedDate() {
    return null == lastModifiedDate ? Optional.empty() : Optional.of(LocalDateTime.ofInstant(lastModifiedDate.toInstant(), ZoneId.systemDefault()));
  }

  @Override
  public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
    this.lastModifiedDate = Date.from(lastModifiedDate.atZone(ZoneId.systemDefault()).toInstant());
    this.lastModifiedAddress = getRemoteAddress(null);
  }
 * 
 */
@Access(AccessType.FIELD)
@MappedSuperclass
public abstract class GenericAuditable extends GenericPersistable implements Auditable {
  private static final long serialVersionUID = -1564212293184319064L;

  /**
   * 
   * @CreatedBy
   * @Column(length = COLUMN_LENGTH_ID, updatable = false)
   * private String createdBy;
   * 
*/ @ManyToOne(fetch = FetchType.LAZY) @CreatedBy @JoinColumn(name = "createdBy", updatable = false, foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT)) protected U createdBy; @CreatedDate @Temporal(value = TemporalType.TIMESTAMP) @Column(updatable = false) protected Date createdDate; @Column(length = COLUMN_LENGTH_ADDRESS, updatable = false) protected String createdAddress; /** *
   * @LastModifiedBy
   * @Column(length = COLUMN_LENGTH_ID)
   * private String lastModifiedBy;
   * 
*/ @ManyToOne(fetch = FetchType.LAZY) @LastModifiedBy @JoinColumn(name = "lastModifiedBy", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT)) protected U lastModifiedBy; @LastModifiedDate @Temporal(value = TemporalType.TIMESTAMP) @Column protected Date lastModifiedDate; @Column(length = COLUMN_LENGTH_ADDRESS) protected String lastModifiedAddress; @Override public U getCreatedBy() { return Optional.ofNullable(createdBy).get(); } @Override public void setCreatedBy(U createdBy) { this.createdBy = createdBy; } @Override public DateTime getCreatedDate() { return null == createdDate ? null : new DateTime(createdDate); } @Override public void setCreatedDate(DateTime createdDate) { this.createdDate = null == createdDate ? null : createdDate.toDate(); this.createdAddress = getRemoteAddress(null); } public String getCreatedAddress() { return createdAddress; } @Override public U getLastModifiedBy() { return Optional.ofNullable(lastModifiedBy).get(); } @Override public void setLastModifiedBy(U lastModifiedBy) { this.lastModifiedBy = lastModifiedBy; } @Override public DateTime getLastModifiedDate() { return null == lastModifiedDate ? null : new DateTime(lastModifiedDate); } @Override public void setLastModifiedDate(DateTime lastModifiedDate) { this.lastModifiedDate = null == lastModifiedDate ? null : lastModifiedDate.toDate(); this.lastModifiedAddress = getRemoteAddress(null); } public String getLastModifiedAddress() { return lastModifiedAddress; } public void setCreatedAddress(String createdAddress) { this.createdAddress = createdAddress; } public void setLastModifiedAddress(String lastModifiedAddress) { this.lastModifiedAddress = lastModifiedAddress; } }