package info.bukova.isspst.data;

import info.bukova.isspst.Constants;

import java.util.Date;

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.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class RequirementSubject implements OwnedDataModel {

	//OwnedDataModel
	@Id
	@Column(name = "ID")
	@GeneratedValue(strategy = GenerationType.TABLE)
	private int id;
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="OWNED_BY_ID")
	private User ownedBy;
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="MODIFIED_BY_ID")
	private User modifiedBy;
	@Column(name = "CREATED")
	private Date created;
	@Column(name = "MODIFIED")
	private Date modified;
	@Transient
	private boolean valid;
	//RequirementSubject
	@Column(name = "CODE", unique = true)
	private String code;
	@Column(name = "NAME")
	private String name;
	@Column(name = "DESCRIPTION", length = Constants.LEN_DESCRIPTION)
	private String description;
	
	@NotEmpty(message = "{MaterialFormCodeConstr}")
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public int getId() {
		return id;
	}

	@Override
	public void setId(int id) {
		this.id = id;
	}

	@Override
	public User getOwnedBy() {
		return ownedBy;
	}

	@Override
	public void setOwnedBy(User ownedBy) {
		this.ownedBy = ownedBy;
	}

	@Override
	public User getModifiedBy() {
		return modifiedBy;
	}

	@Override
	public void setModifiedBy(User modifiedBy) {
		this.modifiedBy = modifiedBy;
	}

	@Override
	public Date getCreated() {
		return created;
	}

	@Override
	public void setCreated(Date created) {
		this.created = created;
	}

	@Override
	public Date getModified() {
		return modified;
	}

	@Override
	public void setModified(Date modified) {
		this.modified = modified;
	}

	@Override
	public boolean isValid() {
		return valid;
	}

	@Override
	public void setValid(boolean valid) {
		this.valid = valid;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (obj != null)
		{
			if (obj instanceof RequirementSubject)
			{
				RequirementSubject item = (RequirementSubject) obj;

				int thisId = this.getId();
				int itemId = item.getId();
				boolean equalsId = (thisId == itemId);

				if (equalsId && (thisId == 0))
				{
					return super.equals(obj);
				}

				return equalsId;
			}
		}

		return false;
	}

}
