package info.bukova.isspst.data;

import info.bukova.isspst.Constants;
import info.bukova.isspst.StringUtils;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class MUnitEmb
{
	@Column(name = "MUNIT_ID")
	private Integer id;

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

	@Column(name = "MUNIT_DESCRIPTION", length = Constants.LEN_DESCRIPTION)
	private String description;

	public MUnitEmb()
	{
		// Embeddable object is loaded by hibernate like NULL, if all properties
		// are NULL. So, we set ID.
		this.id = 0;
	}

	public MUnitEmb(MUnit munit)
	{
		this.setId(munit.getId());
		this.setDescription(munit.getDescription());
		this.setName(munit.getName());
	}

	public Integer getId()
	{
		return this.id;
	}

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

	public String getName()
	{
		return StringUtils.decodeSpecialChars(this.name);
	}

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

	public String getDescription()
	{
		return StringUtils.decodeSpecialChars(this.description);
	}

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

	public static boolean isEqualMUnitEmbForFilter(MUnitEmb value, MUnitEmb search)
	{
		if ((search == null) || (search.getName() == null) || search.getName().isEmpty())
		{
			return true;
		}
		else if (value != null)
		{
			return StringUtils.isEqualForFilter(value.getName(), search.getName());
		}

		return false;
	}

	public boolean eqWith(MUnit munit)
	{
		if (munit == null)
		{
			return false;
		}

		return this.getId() == munit.getId() && this.getName().equals(munit.getName()) && this.getDescription().equals(munit.getDescription());
	}

	@Override
	public boolean equals(Object munit)
	{
		return munit != null
				&& (munit instanceof MUnitEmb)
				&& this.getId() == ((MUnitEmb) munit).getId()
				&& (this.getName() == ((MUnitEmb) munit).getName() || this.getName().equals(((MUnitEmb) munit).getName()))
				&& (this.getDescription() == ((MUnitEmb) munit).getDescription() || this.getDescription().equals(((MUnitEmb) munit).getDescription()));
	}

	@Override
	public String toString()
	{
		return this.getName();
	}
}
