User.java

/**
 * TFG 75.678 - TFG Desarrollo web 2020 e-Learning for Schools
 * Copyright (C) 2020  Eduardo Rodriguez Carro
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.uoc.tfg.sel.repository.model;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * Modelo de usuario.
 *
 * @author Eduardo Rodriguez carro
 */
@JsonInclude(value = Include.NON_NULL)
@Table(name="user")
@Entity
@DynamicUpdate
@DynamicInsert
public class User implements Serializable {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -2610710175510124607L;

	/** The id. */
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "id")
	private Integer id;
	
	/** The login. */
	@Size(min = 1,max = 50)
	@Column(name = "loginname")
	private String login;
	
	/** The name. */
	@NotNull
	@Size(min = 1,max = 100)
	@Column(name = "name")
	private String name;
	
	/** The surname. */
	@NotNull
	@Size(min = 1,max = 100)
	@Column(name = "surname")
	private String surname;
	
	/** The email. */
	@Email
	@Size(max = 300)
	@Column(name = "email")
	private String email;
	
	/** The active. */
	@NotNull
	@Type(type = "org.hibernate.type.NumericBooleanType")
	@Column(name = "active",nullable = false, columnDefinition = "TINYINT(1)")
	private Boolean active;
	
	/** The type. */
	@ManyToOne(optional = false,cascade = CascadeType.DETACH,fetch = FetchType.EAGER)
	@JoinColumn(name="usertype_id", nullable=false)
	private UserType type;
	
	/** The password. */
	@Size(min = 0,max = 512)
	@Column(name = "password")
	private String password;
	
	
	/** The type. */
	@ManyToOne(optional = true,cascade = CascadeType.DETACH,fetch = FetchType.LAZY)
	@JoinColumn(name="legaltutor_id", nullable=true)
	private User legalTutor;
	
	
	/**
	 * Gets the password.
	 *
	 * @return the password
	 */
	@JsonIgnore
	public String getPassword() {
		return password;
	}

	/**
	 * Sets the password.
	 *
	 * @param password the new password
	 */
	@JsonProperty("password")
	public void setPassword(String password) {
		this.password = password;
	}
	
	/**
	 * Gets the id.
	 *
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * Sets the id.
	 *
	 * @param id the new id
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * Gets the login.
	 *
	 * @return the login
	 */
	public String getLogin() {
		return login;
	}

	/**
	 * Sets the login.
	 *
	 * @param login the new login
	 */
	public void setLogin(String login) {
		this.login = login;
	}

	/**
	 * Gets the name.
	 *
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name.
	 *
	 * @param name the new name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Gets the surname.
	 *
	 * @return the surname
	 */
	public String getSurname() {
		return surname;
	}

	/**
	 * Sets the surname.
	 *
	 * @param surname the new surname
	 */
	public void setSurname(String surname) {
		this.surname = surname;
	}

	/**
	 * Gets the email.
	 *
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * Sets the email.
	 *
	 * @param email the new email
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * Gets the type.
	 *
	 * @return the type
	 */
	public UserType getType() {
		return type;
	}

	/**
	 * Sets the type.
	 *
	 * @param type the new type
	 */
	public void setType(UserType type) {
		this.type = type;
	}

	/**
	 * Sets the active.
	 *
	 * @param active the new active
	 */
	public void setActive(Boolean active) {
		this.active = active;
	}

	/**
	 * Gets the active.
	 *
	 * @return the active
	 */
	public Boolean getActive() {
		return active;
	}



	/**
	 * Gets the legal tutor.
	 *
	 * @return the legal tutor
	 */
	public User getLegalTutor() {
		return legalTutor;
	}

	/**
	 * Sets the legal tutor.
	 *
	 * @param legalTutor the new legal tutor
	 */
	public void setLegalTutor(User legalTutor) {
		this.legalTutor = legalTutor;
	}

	/**
	 * Hash code.
	 *
	 * @return the int
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((active == null) ? 0 : active.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		return result;
	}

	/**
	 * Equals.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (active == null) {
			if (other.active != null)
				return false;
		} else if (!active.equals(other.active))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		return true;
	}

	/**
	 * To string.
	 *
	 * @return the string
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("User [id=").append(id).append(", login=").append(login)
				.append(", name=").append(name).append(", surname=").append(surname).append(", email=").append(email)
				.append(", active=").append(active).append(", type=").append(type).append("]");
		return builder.toString();
	}
}