/*
 * This is autogenerated and should not be modified.
 * Any changes should be made to the super class.
 * Copyright: (c) Reinders Posters
 * Modified:  $Date$
 */

package nl.reinders.bm.generated;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;

import javax.persistence.EntityManager;
import javax.persistence.MappedSuperclass;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.SequenceGenerator;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.FetchType;
import javax.persistence.CascadeType;
import javax.persistence.JoinColumn;
import javax.persistence.Query;
import javax.persistence.Transient;

/**
 * A JPA entity class for batch.
 * @version    $Revision$
 * @author     $Author$
 */
@MappedSuperclass
abstract public class Batch extends nl.reinders.bm.AbstractBean
implements java.io.Serializable
         , java.lang.Cloneable
         , Comparable<nl.reinders.bm.Batch>
{
	static final long serialVersionUID = 0;
	static final String SOURCECODE_VERSION = "$Revision$";
	static org.apache.log4j.Logger log4j = org.apache.log4j.Logger.getLogger(Batch.class.getName());

	// ===============================================================================================
	// RELATIONS OneToMany

	/** BatchbuyorderlinesWhereIAmBatch*/
	public void addBatchbuyorderlinesWhereIAmBatch(nl.reinders.bm.Batchbuyorderline value)
	{
		if (value != null && !iBatchbuyorderlinesWhereIAmBatch.contains(value))
		{
			iBatchbuyorderlinesWhereIAmBatch.add(value);
			value.setBatch( (nl.reinders.bm.Batch)this);
		}
	}
	public void removeBatchbuyorderlinesWhereIAmBatch(nl.reinders.bm.Batchbuyorderline value)
	{
		if (value != null && iBatchbuyorderlinesWhereIAmBatch.contains(value))
		{
			iBatchbuyorderlinesWhereIAmBatch.remove(value);
			value.setBatch( (nl.reinders.bm.Batch)null );
		}
	}
	public java.util.List<nl.reinders.bm.Batchbuyorderline> getBatchbuyorderlinesWhereIAmBatch() { return iBatchbuyorderlinesWhereIAmBatch; }
	@OneToMany(mappedBy = "iBatch", fetch = FetchType.LAZY, targetEntity = nl.reinders.bm.Batchbuyorderline.class, cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE} )
	private java.util.List<nl.reinders.bm.Batchbuyorderline> iBatchbuyorderlinesWhereIAmBatch = new java.util.ArrayList<nl.reinders.bm.Batchbuyorderline>();

	/** BatchcountsWhereIAmBatch*/
	public void addBatchcountsWhereIAmBatch(nl.reinders.bm.Batchcount value)
	{
		if (value != null && !iBatchcountsWhereIAmBatch.contains(value))
		{
			iBatchcountsWhereIAmBatch.add(value);
			value.setBatch( (nl.reinders.bm.Batch)this);
		}
	}
	public void removeBatchcountsWhereIAmBatch(nl.reinders.bm.Batchcount value)
	{
		if (value != null && iBatchcountsWhereIAmBatch.contains(value))
		{
			iBatchcountsWhereIAmBatch.remove(value);
			value.setBatch( (nl.reinders.bm.Batch)null );
		}
	}
	public java.util.List<nl.reinders.bm.Batchcount> getBatchcountsWhereIAmBatch() { return iBatchcountsWhereIAmBatch; }
	@OneToMany(mappedBy = "iBatch", fetch = FetchType.LAZY, targetEntity = nl.reinders.bm.Batchcount.class, cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE} )
	private java.util.List<nl.reinders.bm.Batchcount> iBatchcountsWhereIAmBatch = new java.util.ArrayList<nl.reinders.bm.Batchcount>();

	/** BatchcreditsWhereIAmBatch*/
	public void addBatchcreditsWhereIAmBatch(nl.reinders.bm.Batchcredit value)
	{
		if (value != null && !iBatchcreditsWhereIAmBatch.contains(value))
		{
			iBatchcreditsWhereIAmBatch.add(value);
			value.setBatch( (nl.reinders.bm.Batch)this);
		}
	}
	public void removeBatchcreditsWhereIAmBatch(nl.reinders.bm.Batchcredit value)
	{
		if (value != null && iBatchcreditsWhereIAmBatch.contains(value))
		{
			iBatchcreditsWhereIAmBatch.remove(value);
			value.setBatch( (nl.reinders.bm.Batch)null );
		}
	}
	public java.util.List<nl.reinders.bm.Batchcredit> getBatchcreditsWhereIAmBatch() { return iBatchcreditsWhereIAmBatch; }
	@OneToMany(mappedBy = "iBatch", fetch = FetchType.LAZY, targetEntity = nl.reinders.bm.Batchcredit.class, cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE} )
	private java.util.List<nl.reinders.bm.Batchcredit> iBatchcreditsWhereIAmBatch = new java.util.ArrayList<nl.reinders.bm.Batchcredit>();

	/** BatchsellorderlinesWhereIAmBatch*/
	public void addBatchsellorderlinesWhereIAmBatch(nl.reinders.bm.Batchsellorderline value)
	{
		if (value != null && !iBatchsellorderlinesWhereIAmBatch.contains(value))
		{
			iBatchsellorderlinesWhereIAmBatch.add(value);
			value.setBatch( (nl.reinders.bm.Batch)this);
		}
	}
	public void removeBatchsellorderlinesWhereIAmBatch(nl.reinders.bm.Batchsellorderline value)
	{
		if (value != null && iBatchsellorderlinesWhereIAmBatch.contains(value))
		{
			iBatchsellorderlinesWhereIAmBatch.remove(value);
			value.setBatch( (nl.reinders.bm.Batch)null );
		}
	}
	public java.util.List<nl.reinders.bm.Batchsellorderline> getBatchsellorderlinesWhereIAmBatch() { return iBatchsellorderlinesWhereIAmBatch; }
	@OneToMany(mappedBy = "iBatch", fetch = FetchType.LAZY, targetEntity = nl.reinders.bm.Batchsellorderline.class, cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE} )
	private java.util.List<nl.reinders.bm.Batchsellorderline> iBatchsellorderlinesWhereIAmBatch = new java.util.ArrayList<nl.reinders.bm.Batchsellorderline>();


	// ===============================================================================================
	// RELATIONS ManyToOne

	/** Article */
	public nl.reinders.bm.Article getArticle() { return iArticle; }
	public void setArticle(nl.reinders.bm.Article value)
	{
		if (value == iArticle) return; // optimalisation and prevent looping
		try
		{
			nl.reinders.bm.Article lValue = iArticle;
			fireVetoableChange(ARTICLE_PROPERTY_ID, lValue, value);
			if (lValue != null) lValue.removeBatchsWhereIAmArticle( (nl.reinders.bm.Batch)this );
			iArticle = value;
			if (value != null) value.addBatchsWhereIAmArticle( (nl.reinders.bm.Batch)this );
			firePropertyChange(ARTICLE_PROPERTY_ID, lValue, value);
		}
		catch (java.beans.PropertyVetoException e) { throw new java.lang.IllegalArgumentException("Value vetoed: " + e.getMessage()); }
	}
	@ManyToOne(fetch = FetchType.LAZY, targetEntity = nl.reinders.bm.Article.class, cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE} ) @JoinColumn(name="articlenr") 
	private nl.reinders.bm.Article iArticle;
	final static public String ARTICLE_PROPERTY_ID = "article";
	final static public Class ARTICLE_PROPERTY_CLASS = nl.reinders.bm.Article.class;
	final static public String ARTICLE_NAME = "Article"; // with this ID you can access the internationalization
	// to make IN queryies possible
	@Column(name="articlenr", insertable=false, updatable=false)
	private java.math.BigDecimal iArticlenr = null;

	/** Batchtype */
	public nl.reinders.bm.Batchtype getBatchtype() { return iBatchtype; }
	public void setBatchtype(nl.reinders.bm.Batchtype value)
	{
		if (value == iBatchtype) return; // optimalisation and prevent looping
		try
		{
			nl.reinders.bm.Batchtype lValue = iBatchtype;
			fireVetoableChange(BATCHTYPE_PROPERTY_ID, lValue, value);
			if (lValue != null) lValue.removeBatchsWhereIAmBatchtype( (nl.reinders.bm.Batch)this );
			iBatchtype = value;
			if (value != null) value.addBatchsWhereIAmBatchtype( (nl.reinders.bm.Batch)this );
			firePropertyChange(BATCHTYPE_PROPERTY_ID, lValue, value);
		}
		catch (java.beans.PropertyVetoException e) { throw new java.lang.IllegalArgumentException("Value vetoed: " + e.getMessage()); }
	}
	@ManyToOne(fetch = FetchType.LAZY, targetEntity = nl.reinders.bm.Batchtype.class, cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE} ) @JoinColumn(name="batchtype") 
	private nl.reinders.bm.Batchtype iBatchtype;
	final static public String BATCHTYPE_PROPERTY_ID = "batchtype";
	final static public Class BATCHTYPE_PROPERTY_CLASS = nl.reinders.bm.Batchtype.class;
	final static public String BATCHTYPE_NAME = "Batchtype"; // with this ID you can access the internationalization
	// to make IN queryies possible
	@Column(name="batchtype", insertable=false, updatable=false)
	private java.math.BigDecimal iBatchtypenr = null;



	// ===============================================================================================
	// PROPERTIES

	/** Batchnr */
	public Integer getBatchnr() { return iBatchnr; }
	public void setBatchnr(Integer value)
	{
		try
		{
			Integer lValue = iBatchnr;
			if (log4j.isDebugEnabled()) log4j.debug("setBatchnr: " + lValue + " -> " + value);
			fireVetoableChange(BATCHNR_PROPERTY_ID, lValue, value);
			iBatchnr = value;
			firePropertyChange(BATCHNR_PROPERTY_ID, lValue, value);
		}
		catch (java.beans.PropertyVetoException e) { throw new java.lang.IllegalArgumentException("Value vetoed: " + e.getMessage()); }
	}
	@Id @GeneratedValue(generator="batch.batchnr", strategy=GenerationType.TABLE) @TableGenerator( name="batch.batchnr", pkColumnValue="batchnr", table="sequence", pkColumnName="seq_name", valueColumnName="seq_count", initialValue=1, allocationSize=1) @Column(name="batchnr")
	private Integer iBatchnr = null;
	final static public String BATCHNR_PROPERTY_ID = "batchnr";
	final static public Class BATCHNR_PROPERTY_CLASS = Integer.class;
	final static public String BATCHNR_NAME = "Batchnr"; // with this ID you can access the internationalization

	/** Costprice */
	public java.math.BigDecimal getCostprice() { return iCostprice; }
	public void setCostprice(java.math.BigDecimal value)
	{
		try
		{
			java.math.BigDecimal lValue = iCostprice;
			if (log4j.isDebugEnabled()) log4j.debug("setCostprice: " + lValue + " -> " + value);
			fireVetoableChange(COSTPRICE_PROPERTY_ID, lValue, value);
			iCostprice = value;
			firePropertyChange(COSTPRICE_PROPERTY_ID, lValue, value);
		}
		catch (java.beans.PropertyVetoException e) { throw new java.lang.IllegalArgumentException("Value vetoed: " + e.getMessage()); }
	}
	@Column(name="costprice")
	private java.math.BigDecimal iCostprice = null;
	final static public String COSTPRICE_PROPERTY_ID = "costprice";
	final static public Class COSTPRICE_PROPERTY_CLASS = java.math.BigDecimal.class;
	final static public String COSTPRICE_NAME = "Costprice"; // with this ID you can access the internationalization

	/** Creationdate */
	public java.util.Calendar getCreationdate() { return iCreationdate; }
	public void setCreationdate(java.util.Calendar value)
	{
		try
		{
			java.util.Calendar lValue = iCreationdate;
			if (log4j.isDebugEnabled()) log4j.debug("setCreationdate: " + lValue + " -> " + value);
			fireVetoableChange(CREATIONDATE_PROPERTY_ID, lValue, value);
			iCreationdate = value;
			firePropertyChange(CREATIONDATE_PROPERTY_ID, lValue, value);
		}
		catch (java.beans.PropertyVetoException e) { throw new java.lang.IllegalArgumentException("Value vetoed: " + e.getMessage()); }
	}
	@Temporal(TemporalType.TIMESTAMP) @Column(name="creationdate")
	private java.util.Calendar iCreationdate = null;
	final static public String CREATIONDATE_PROPERTY_ID = "creationdate";
	final static public Class CREATIONDATE_PROPERTY_CLASS = java.util.Calendar.class;
	final static public String CREATIONDATE_NAME = "Creationdate"; // with this ID you can access the internationalization

	/** Lazylock */
	public Integer getLazylock() { return iLazylock; }
	public void setLazylock(Integer value)
	{
		try
		{
			Integer lValue = iLazylock;
			if (log4j.isDebugEnabled()) log4j.debug("setLazylock: " + lValue + " -> " + value);
			fireVetoableChange(LAZYLOCK_PROPERTY_ID, lValue, value);
			iLazylock = value;
			firePropertyChange(LAZYLOCK_PROPERTY_ID, lValue, value);
		}
		catch (java.beans.PropertyVetoException e) { throw new java.lang.IllegalArgumentException("Value vetoed: " + e.getMessage()); }
	}
	@Version @Column(name="lazylock")
	private Integer iLazylock = 0;
	final static public String LAZYLOCK_PROPERTY_ID = "lazylock";
	final static public Class LAZYLOCK_PROPERTY_CLASS = Integer.class;
	final static public String LAZYLOCK_NAME = "Lazylock"; // with this ID you can access the internationalization

	/** Amount */
	public Integer getAmount() { return iAmount; }
	public void setAmount(Integer value)
	{
		try
		{
			Integer lValue = iAmount;
			if (log4j.isDebugEnabled()) log4j.debug("setAmount: " + lValue + " -> " + value);
			fireVetoableChange(AMOUNT_PROPERTY_ID, lValue, value);
			iAmount = value;
			firePropertyChange(AMOUNT_PROPERTY_ID, lValue, value);
		}
		catch (java.beans.PropertyVetoException e) { throw new java.lang.IllegalArgumentException("Value vetoed: " + e.getMessage()); }
	}
	@Column(name="_amount")
	private Integer iAmount = 0;
	final static public String AMOUNT_PROPERTY_ID = "amount";
	final static public Class AMOUNT_PROPERTY_CLASS = Integer.class;
	final static public String AMOUNT_NAME = "Amount"; // with this ID you can access the internationalization

	/** Dwhmodified */
	public java.util.Calendar getDwhmodified() { return iDwhmodified; }
	public void setDwhmodified(java.util.Calendar value)
	{
		try
		{
			java.util.Calendar lValue = iDwhmodified;
			if (log4j.isDebugEnabled()) log4j.debug("setDwhmodified: " + lValue + " -> " + value);
			fireVetoableChange(DWHMODIFIED_PROPERTY_ID, lValue, value);
			iDwhmodified = value;
			firePropertyChange(DWHMODIFIED_PROPERTY_ID, lValue, value);
		}
		catch (java.beans.PropertyVetoException e) { throw new java.lang.IllegalArgumentException("Value vetoed: " + e.getMessage()); }
	}
	@Temporal(TemporalType.TIMESTAMP) @Column(name="dwhmodified")
	private java.util.Calendar iDwhmodified = new java.util.GregorianCalendar();
	final static public String DWHMODIFIED_PROPERTY_ID = "dwhmodified";
	final static public Class DWHMODIFIED_PROPERTY_CLASS = java.util.Calendar.class;
	final static public String DWHMODIFIED_NAME = "Dwhmodified"; // with this ID you can access the internationalization

	/** Dwhby */
	public Integer getDwhby() { return iDwhby; }
	public void setDwhby(Integer value)
	{
		try
		{
			Integer lValue = iDwhby;
			if (log4j.isDebugEnabled()) log4j.debug("setDwhby: " + lValue + " -> " + value);
			fireVetoableChange(DWHBY_PROPERTY_ID, lValue, value);
			iDwhby = value;
			firePropertyChange(DWHBY_PROPERTY_ID, lValue, value);
		}
		catch (java.beans.PropertyVetoException e) { throw new java.lang.IllegalArgumentException("Value vetoed: " + e.getMessage()); }
	}
	@Column(name="dwhby")
	private Integer iDwhby = null;
	final static public String DWHBY_PROPERTY_ID = "dwhby";
	final static public Class DWHBY_PROPERTY_CLASS = Integer.class;
	final static public String DWHBY_NAME = "Dwhby"; // with this ID you can access the internationalization

	/** Inserted */
	public java.util.Calendar getInserted() { return iInserted; }
	public void setInserted(java.util.Calendar value)
	{
		try
		{
			java.util.Calendar lValue = iInserted;
			if (log4j.isDebugEnabled()) log4j.debug("setInserted: " + lValue + " -> " + value);
			fireVetoableChange(INSERTED_PROPERTY_ID, lValue, value);
			iInserted = value;
			firePropertyChange(INSERTED_PROPERTY_ID, lValue, value);
		}
		catch (java.beans.PropertyVetoException e) { throw new java.lang.IllegalArgumentException("Value vetoed: " + e.getMessage()); }
	}
	@Temporal(TemporalType.TIMESTAMP) @Column(name="inserted")
	private java.util.Calendar iInserted = new java.util.GregorianCalendar();
	final static public String INSERTED_PROPERTY_ID = "inserted";
	final static public Class INSERTED_PROPERTY_CLASS = java.util.Calendar.class;
	final static public String INSERTED_NAME = "Inserted"; // with this ID you can access the internationalization


	// ===============================================================================================
	// Clone

	/** clone */
	public Object clone()
	{ 
		nl.reinders.bm.Batch lNew = new nl.reinders.bm.Batch();
		lNew.setArticle( getArticle() ); 
		lNew.setBatchtype( getBatchtype() ); 
		lNew.setCostprice( getCostprice() ); 
		lNew.setCreationdate( getCreationdate() ); 
		lNew.setAmount( getAmount() ); 
		// not cloning this column: lNew.setDwhmodified( getDwhmodified() ); 
		// not cloning this column: lNew.setDwhby( getDwhby() ); 
		lNew.setInserted( getInserted() ); 
		return lNew;
	}
	public nl.reinders.bm.Batch cloneShallow() { return (nl.reinders.bm.Batch)clone();}


	// =========================================================================================
	// Comparable

	/**
	 * default compare on primary key
	 */
	public int compareTo(nl.reinders.bm.Batch o)
	{
		if (this.getBatchnr() == null) return -1;
		if (o == null) return 1;
		return this.getBatchnr().compareTo( o.getBatchnr() );
	}


	// =========================================================================================
	// DAO

	/**
	 * @param batchnr
	 */
	static public nl.reinders.bm.Batch findByPK(Integer batchnr)
	{
		EntityManager lEntityManager = org.tbee.util.jpa.EntityManagerFinder.findEntityManager();
		if (log4j.isDebugEnabled()) log4j.debug("Using EM #" + Integer.toHexString( lEntityManager.hashCode() ) );
		Query lQuery = lEntityManager.createQuery("select t from Batch t where t.iBatchnr=:batchnr");
		lQuery.setParameter("batchnr", batchnr);
		java.util.List<nl.reinders.bm.Batch> lList = lQuery.getResultList();
	    if (lList.size() == 0) return null;
	    return lList.get(0);
	}

	/**
	 * find all entities
	 */
	static public java.util.List<nl.reinders.bm.Batch> findAll()
	{
		return findAllOrderedBy(null);
	}

	/**
	 * find all entities ordered by
	 */
	static public java.util.List<nl.reinders.bm.Batch> findAllOrderedBy(String orderedBy)
	{
		EntityManager lEntityManager = org.tbee.util.jpa.EntityManagerFinder.findEntityManager();
		if (log4j.isDebugEnabled()) log4j.debug("Using EM #" + Integer.toHexString( lEntityManager.hashCode() ) );
		Query lQuery = lEntityManager.createQuery("select t from Batch t" + (orderedBy == null ? "" : " order by t." + orderedBy) + "");
		java.util.List<nl.reinders.bm.Batch> lList = lQuery.getResultList();
	    return lList;
	}


	// =========================================================================================
	// EQUALS

	/**
	 *
	 */
	public boolean equals(Object o)
	{
		if (o == null) return false;
		if (!(o instanceof nl.reinders.bm.Batch)) return false;
		nl.reinders.bm.Batch lOther = (nl.reinders.bm.Batch)o;
		if (
			this.getBatchnr() != null && lOther.getBatchnr() != null
		)
		{
			return
				   this.getBatchnr().equals( lOther.getBatchnr() )
			;
		}
		else
			return super.equals(o);
	}



	// =========================================================================================
	// CONVIENIENCE

	/**
	 *
	 */
	public String toString()
	{
		StringBuffer lStringBuffer = new StringBuffer();
		lStringBuffer.append(super.toString());
		lStringBuffer.append(", Batchnr="); lStringBuffer.append(getBatchnr());
		return lStringBuffer.toString();
	}


	/**
	 * Using the Internationalization class
	 */
	static public String translate(String id)
	{
		return org.tbee.util.Internationalization.getInternationalization().translate(nl.reinders.bm.Batch.class, id);
	}
}
