package info.bukova.isspst.data;

import info.bukova.isspst.Constants;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.validator.constraints.NotBlank;

@Entity
@Table(name = "BUILDING")
public class Building extends BaseData implements DataModel {

	@Column(name = "CODE", unique = true)
	private String code;

	@Column(name = "NAME")
	private String name;

	@Column(name = "DESCRIPTION", length = Constants.LEN_DESCRIPTION)
	private String description;
	
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "building", orphanRemoval = true)
	@LazyCollection(LazyCollectionOption.FALSE)
	private List<Room> rooms;
	
	public Building() {
		rooms = new ArrayList<Room>();
	}

	/**
	 * @return the code
	 */
	@NotBlank(message = "{BuildingsFormCodeConstr}")
	public String getCode() {
		return code;
	}

	/**
	 * @param code
	 *            the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

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

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

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	public List<Room> getRooms() {
		return rooms;
	}

	public void setRooms(List<Room> rooms) {
		this.rooms = rooms;
	}
	
	public void addRoom(Room room) {
		room.setBuilding(this);
		rooms.add(room);
	}
	
	public void removeRoom(Room room) {
		rooms.remove(room);
	}
	
}
