package it.enea.cruise.model.user;



import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.PreRemove;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 
 * @author Gianluca D'Agosta
 * Questa classe gestisce la tabella "users_roles" del database 
 * con i dati di consumo delle Comunita' energetiche.
 * Essa permette di estrarre il campo "communityID"
 * collegato ad un utente ed un ruolo, che non sarebbe raggiungibile altrimenti.
 *
 */

@Entity(name = "userRole")
@Table(name = "users_roles")
public class UserRole {
	
	/*
	 * Properties
	 */
	
	@EmbeddedId
    private UserRoleId id = new UserRoleId();
	
	@ManyToOne
	@JoinColumn(name="userid")
	@MapsId("userid")
	//@JsonBackReference(value = "user-userRoles")
	private User user;
 
    @ManyToOne
    @JoinColumn(name="roleid")
    @MapsId("rolesid")
    private Role role;
    
    @ManyToOne
	@JoinColumn(name="communityid")
 	@MapsId("communityid")
    //@JsonBackReference(value = "community-userRoles")
	private Community community;
    
    @JsonIgnore
    @Transient
    private int communityid;
    
    /*
     * Generic Methods
     */
    
    public UserRole() {}
    
    public UserRole(User user, Role role, Community community) {
        this.user = user;
        this.role = role;
        this.community = community;
        this.id = new UserRoleId(user.getUserid(), role.getId(), community.getCommunityid());
    }
    
//    public UserRole(User user, Role role, Long communityid) {
//        this.user = user;
//        this.role = role;
//        this.communityid = communityid;
//        this.id = new UserRoleId(user.getUserid(), role.getId(), communityid);
//        System.out.println(this);
//    }

    /*
     * Getters and setters
     */
    
	public Long getCommunityid() {
		return community.getCommunityid();
	}

	public UserRoleId getId() {
		return id;
	}

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

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		id.setUserId(user.getUserid());
		this.user = user;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		id.setRoleId(role.getId());
		this.role = role;
	}

	public Community getCommunity() {
		return this.community;
	}
	
	public void setCommunity(Community community) {
		id.setCommunityId(community.getCommunityid());
		this.community = community;
	}
	
	public void clear() {
		this.user = null;
		this.community = null;
		this.role = null;
	}
	
	
	@PreRemove
	public void removeLinks() {
		/*
		 * Devo rimuovere i link agli oggetti user e community e scollegare anche
		 * dall'altro lato della relazione
		 */
		this.user.getUserroles().remove(this);
		//this.community.getUserRoles().remove(this);
		//this.role.getUserRoles.remove(this);
		this.community = new Community();
		this.user = new User();
		this.role = new Role();

	}
}
