package eu.bcvsolutions.idm.acc.entity;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ForeignKey;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Size;

import org.hibernate.envers.Audited;
import org.hibernate.envers.RelationTargetAuditMode;
import org.hibernate.validator.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sun.istack.NotNull;

import eu.bcvsolutions.idm.acc.domain.AccountType;
import eu.bcvsolutions.idm.core.api.domain.DefaultFieldLengths;
import eu.bcvsolutions.idm.core.api.entity.AbstractEntity;

/**
 * Account on target system
 * 
 * @author Radek Tomiška
 *
 */
@Entity
@Table(name = "acc_account", indexes = { 
		@Index(name = "ux_acc_account_sys_entity", columnList = "system_entity_id", unique = true),
		@Index(name = "ux_account_uid", columnList = "uid,system_id", unique = true),
		@Index(name = "idx_acc_account_sys_id", columnList = "system_id"),
		@Index(name = "idx_acc_account_sys_entity", columnList = "system_entity_id"),
		@Index(name = "idx_acc_account_role_sys_id", columnList = "role_system_id")
		})
public class AccAccount extends AbstractEntity {
	
	private static final long serialVersionUID = -565558977675057360L;

	@NotEmpty
	@Size(min = 1, max = DefaultFieldLengths.UID)
	@Column(name = "uid", length = DefaultFieldLengths.UID, nullable = false)
	private String uid;
	
	@Audited(withModifiedFlag=true)
	@NotNull
	@Enumerated(EnumType.STRING)
	@Column(name = "account_type", nullable = false)
	private AccountType accountType;
	
	@Audited(withModifiedFlag=true)
	@NotNull
	@ManyToOne(optional = false)
	@JoinColumn(name = "system_id", referencedColumnName = "id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
	@SuppressWarnings("deprecation") // jpa FK constraint does not work in hibernate 4
	@org.hibernate.annotations.ForeignKey( name = "none" )
	private SysSystem system;
	
	@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED, withModifiedFlag=true)
	@ManyToOne(optional = true)
	@JoinColumn(name = "system_entity_id", referencedColumnName = "id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
	@SuppressWarnings("deprecation") // jpa FK constraint does not work in hibernate 4
	@org.hibernate.annotations.ForeignKey( name = "none" )
	private SysSystemEntity systemEntity;
	
	@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED, withModifiedFlag=true)
	@ManyToOne(optional = true)
	@JoinColumn(name = "role_system_id", referencedColumnName = "id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
	@SuppressWarnings("deprecation") // jpa FK constraint does not work in hibernate 4
	@org.hibernate.annotations.ForeignKey( name = "none" )
	private SysRoleSystem roleSystem;
	
	@JsonIgnore
	@OneToMany(mappedBy = "account")
	private List<AccIdentityAccount> identityAccounts;

	public void setAccountType(AccountType accountType) {
		this.accountType = accountType;
	}
	
	public AccountType getAccountType() {
		return accountType;
	}

	public SysSystem getSystem() {
		return system;
	}

	public void setSystem(SysSystem system) {
		this.system = system;
	}

	public SysSystemEntity getSystemEntity() {
		return systemEntity;
	}

	public void setSystemEntity(SysSystemEntity systemEntity) {
		this.systemEntity = systemEntity;
	}
	
	public void setIdentityAccounts(List<AccIdentityAccount> identityAccounts) {
		this.identityAccounts = identityAccounts;
	}
	
	public List<AccIdentityAccount> getIdentityAccounts() {
		return identityAccounts;
	}
	
	public void setUid(String uid) {
		this.uid = uid;
	}
	
	public String getUid() {
		return uid;
	}
	
	public void setRoleSystem(SysRoleSystem roleSystem) {
		this.roleSystem = roleSystem;
	}
	
	public SysRoleSystem getRoleSystem() {
		return roleSystem;
	}
	
}

@@ -1336,88 +1336,9 @@
 id%22)
-,%0A%09%09@Index(name = %22idx_acc_account_role_sys_id%22, columnList = %22role_system_id%22)
 %0A
+
 %09%09%7D)
@@ -2576,433 +2576,8 @@
 ;%0A%09%0A
-%09@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED, withModifiedFlag=true)%0A%09@ManyToOne(optional = true)%0A%09@JoinColumn(name = %22role_system_id%22, referencedColumnName = %22id%22, foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))%0A%09@SuppressWarnings(%22deprecation%22) // jpa FK constraint does not work in hibernate 4%0A%09@org.hibernate.annotations.ForeignKey( name = %22none%22 )%0A%09private SysRoleSystem roleSystem;%0A%09%0A
 %09@Js
@@ -3445,169 +3445,9 @@
 id;%0A
+
 %09%7D%0A
-%09%0A%09public void setRoleSystem(SysRoleSystem roleSystem) %7B%0A%09%09this.roleSystem = roleSystem;%0A%09%7D%0A%09%0A%09public SysRoleSystem getRoleSystem() %7B%0A%09%09return roleSystem;%0A%09%7D%0A%09%0A
 %7D%0A

Remove relation on RoleSystem - we need many to many relation