/*
 * This class is autogenerated if it does not exists. It may be modified.
 * Copyright: (c) Reinders Posters
 * Modified:  $Date: 2007/08/06 14:51:30 $
 */

package nl.reinders.bm;

import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.Table;

import nl.reinders.bm.AbstractBean.ProgressListener;

import org.tbee.util.EAN13Util;
import org.tbee.util.jpa.EntityManagerChangeCount;
import org.tbee.util.jpa.EntityManagerFinder;

/**
 * A JPA entity class for standassign.
 * @version    $Revision: 1.13 $
 * @author     $Author: toeukpap $
 */
@Entity
@Table(name="standassign")
public class Standassign extends nl.reinders.bm.generated.Standassign
implements java.io.Serializable
{
	static final long serialVersionUID = 0;
	static final String SOURCECODE_VERSION = "$Revision: 1.13 $";
	static org.apache.log4j.Logger log4j = org.apache.log4j.Logger.getLogger(Standassign.class.getName());
	
	
	// ===============================================================================================
	// CONSTRUCTOR
	
	public Standassign() { construct(); }
	public Standassign(Standversion standversion) { construct(); setStandversion(standversion); }
	
	private void construct()
	{
		// defaults
		setCode(0);
		setChangeIndicator(false);
		setEnding(false);
		setEnding2(false);
		setPendingChanges(false);
	}
	
	
	// ===============================================================================================
	// PROPERTIES
	
	// ChangeIndicator
	public void setChangeIndicator(Boolean value) { super.setChangeIndicator( value.booleanValue() ? 1 : 0); }
	public Boolean hasChangeIndicator() { return (super.getChangeIndicator() != 0); }
	final static public Class CHANGEINDICATOR_PROPERTY_CLASS = Boolean.class;
	
	// Ending
	public void setEnding(Boolean value) { super.setEnding( value.booleanValue() ? 1 : 0); }
	public Boolean hasEnding() { return (super.getEnding() != 0); }
	final static public Class ENDING_PROPERTY_CLASS = Boolean.class;
	
	// Ending2
	public void setEnding2(Boolean value) { super.setEnding2( value.booleanValue() ? 1 : 0); }
	public Boolean hasEnding2() { return (super.getEnding2() != 0); }
	final static public Class ENDING2_PROPERTY_CLASS = Boolean.class;
	
	/** check the EAN for validity */
	public void setAssignean(java.math.BigDecimal value)
	{
		if (value != null && !value.equals(BigDecimal.ZERO)) // we allow 0 as an EAN
		{
			String lEanString = "" + value.longValue();
			if (lEanString.length() != 13) throw new IllegalArgumentException("EAN moet 13 tekens bevatten: " + lEanString);
			if (!EAN13Util.validateCheckDigit(lEanString))  throw new IllegalArgumentException("Check digit klopt niet: " + lEanString);
		}
		super.setAssignean(value);
	}
	
	/**
	 * Check for duplicates
	 */
	public void setCode(Integer value)
	{
		// check for code duplicates
		if (value != null && getStandversion() != null)
		{
			for (Standassign lOtherStandassign : getStandversion().getStandassignsWhereIAmStandversion())
			{
				if ( !lOtherStandassign.equals(this) && value.equals(lOtherStandassign.getCode()) )
				{
					throw new IllegalArgumentException("Code " + value + " komt reeds voor");
				}
			}
		}
		
		// accept value
		super.setCode(value);
	}

	/**
	 * Check for duplicates
	 */
	public void setArticle(Article value)
	{
		// check for code duplicates
		if (value != null && getStandversion() != null)
		{
			for (Standassign lOtherStandassign : getStandversion().getStandassignsWhereIAmStandversion())
			{
				if ( !lOtherStandassign.equals(this) && value.equals(lOtherStandassign.getArticle()) )
				{
					throw new IllegalArgumentException("Artikel " + value.getArticlenr() + " komt reeds voor");
				}
			}
		}
		
		// accept value
		super.setArticle(value);
	}

	/** 
	 * Check for duplicate code
	 */
	public void setStandversion(nl.reinders.bm.Standversion value)
	{
		// check for code duplicates
		if (value != null && getCode() != null)
		{
			for (Standassign lOtherStandassign : value.getStandassignsWhereIAmStandversion())
			{
				if ( !lOtherStandassign.equals(this) && getCode().equals(lOtherStandassign.getCode()) )
				{
					throw new IllegalArgumentException("Code " + getCode() + " komt reeds voor in standversion " + value);
				}
			}
		}

		// check for article duplicates
		if (value != null && getArticle() != null)
		{
			for (Standassign lOtherStandassign : value.getStandassignsWhereIAmStandversion())
			{
				if ( !lOtherStandassign.equals(this) && getArticle().equals(lOtherStandassign.getArticle()) )
				{
					throw new IllegalArgumentException("Artikel " + getArticle().getArticlenr() + " komt reeds voor in standversion " + value);
				}
			}
		}

		// allow setup
		super.setStandversion(value);
	}
	


	// =========================================================================================
	// Storage
	
    /**
     * Create storages for this standversion (if not exists) and assign the batchtype and articles
     * 
     */
    public void createStorage()
    {
// at this time the toplink library does not support cascade update and insert, so we need to hand code that		
if (EntityManagerChangeCount.hasPendingChanges()) throw new IllegalStateException("Entity manager has pending changes");		
EntityManagerFinder.find().getTransaction().begin(); 		
try
{
		// check to see if there already is a storage
     	String lStorageId = getStandversion().getStand().getStandid() + "/" + getCode();
     	Storage lStorage = Storage.findByDescription(lStorageId);
     	if (lStorage == null)
     	{
     		// add storage location
     		lStorage = new Storage(); 
     		lStorage.setDescription(lStorageId);
EntityManagerFinder.find().persist(lStorage);         		
     	}
     	else
     	{    
     		// empty the storage
     		lStorage.removeAllArticle2Storage();
EntityManagerFinder.find().merge(lStorage);         		
     	}
     	
		// set batchtype
     	lStorage.setBatchtype( getBatchtype() );
     	
		// fill storage with article
     	Article2Storage lArticle2Storage = new Article2Storage(getArticle(), lStorage);
EntityManagerFinder.find().persist(lArticle2Storage);         		
EntityManagerFinder.find().getTransaction().commit();     	
}
finally {
	if (EntityManagerFinder.find().getTransaction().isActive()) EntityManagerFinder.find().getTransaction().rollback();  
	EntityManagerChangeCount.clearPendingChanges();
}
    }

    
	// =========================================================================================
	// Display

    /**
     * Versionless
     */
	public String createSimpleStringForDisplay()
	{
		
    	StringBuilder lStringBuilder = new StringBuilder();
    	if (getStandversion() != null && getStandversion().getStand() != null) lStringBuilder.append(getStandversion().getStand().getStandid());
    	lStringBuilder.append("/");
    	lStringBuilder.append(getCode());
    	return lStringBuilder.toString();
		
	}
	
	/**
	 * With version
	 * @return
	 */
	public String createStringForDisplay()
	{
		
    	StringBuilder lStringBuilder = new StringBuilder();
    	if (getStandversion() != null) lStringBuilder.append(getStandversion().createStringForDisplay());
    	lStringBuilder.append("/");
    	lStringBuilder.append(getCode());
    	return lStringBuilder.toString();
		
	}
	
	// ===============================================================================================
	// toString
	
	/**
	 *
	 */
	public String toString()
	{
		StringBuffer lStringBuffer = new StringBuffer();
		lStringBuffer.append(super.toString());
		lStringBuffer.append(", code="); lStringBuffer.append(getCode());
		return lStringBuffer.toString();
	}

}
