package net.yadaframework.security.persistence.entity;
import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Consente di creare un'url da cui accedere direttamente a una pagina con login automatico
 *
 */
@Entity
public class YadaAutoLoginToken implements Serializable {
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unused")
	private final transient Logger log = LoggerFactory.getLogger(getClass());
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long id;

	private long token;
	
	private Date timestamp; // data di creazione
	private Date expiration; // Scadenza. Null = never expire

	@OneToOne
	private YadaUserCredentials yadaUserCredentials;
	
	@PrePersist
	void setDefaults() {
		timestamp = new Date();
		token = (long) (Math.random()*Long.MAX_VALUE);
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public long getToken() {
		return token;
	}

	public void setToken(long token) {
		this.token = token;
	}

	public Date getExpiration() {
		return expiration;
	}

	public void setExpiration(Date expiration) {
		this.expiration = expiration;
	}

	public YadaUserCredentials getYadaUserCredentials() {
		return yadaUserCredentials;
	}

	public void setYadaUserCredentials(YadaUserCredentials yadaUserCredentials) {
		this.yadaUserCredentials = yadaUserCredentials;
	}

	public Date getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(Date timestamp) {
		this.timestamp = timestamp;
	}

	

}

@@ -101,24 +101,58 @@
 il.Date;%0D%0A%0D%0A
+import javax.persistence.Column;%0D%0A
 import javax
@@ -365,66 +365,276 @@
 t;%0D%0A
-%0D%0Aimport org.slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory
+import javax.persistence.Temporal;%0D%0Aimport javax.persistence.TemporalType;%0D%0Aimport javax.persistence.Version;%0D%0A%0D%0Aimport org.slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory;%0D%0A%0D%0Aimport com.fasterxml.jackson.annotation.JsonView;%0D%0A%0D%0Aimport net.yadaframework.web.YadaJsonView
 ;%0D%0A%0D
@@ -971,16 +971,344 @@
 ));%0D%0A%09%0D%0A
+%09// For synchronization with external databases%0D%0A%09@JsonView(YadaJsonView.WithEagerAttributes.class)%0D%0A%09@Column(columnDefinition=%22DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP%22)%0D%0A%09@Temporal(TemporalType.TIMESTAMP)%0D%0A%09protected Date modified;%0D%0A%09%0D%0A%09// For optimistic locking%0D%0A%09@Version%0D%0A%09protected long version;%0D%0A%09%0D%0A
 %09@Id%0D%0A%09@
@@ -2428,16 +2428,278 @@
 %0D%0A%09%7D%0D%0A%0D%0A
+%09public Date getModified() %7B%0D%0A%09%09return modified;%0D%0A%09%7D%0D%0A%0D%0A%09public void setModified(Date modified) %7B%0D%0A%09%09this.modified = modified;%0D%0A%09%7D%0D%0A%0D%0A%09public long getVersion() %7B%0D%0A%09%09return version;%0D%0A%09%7D%0D%0A%0D%0A%09public void setVersion(long version) %7B%0D%0A%09%09this.version = version;%0D%0A%09%7D%0D%0A%0D%0A
 %09%0D%0A%0D%0A%7D%0D%0A

Add fields modified and version