/*
 * This class is autogenerated if it does not exists. It may be modified.
 * Copyright: (c) Reinders Posters
 * Modified:  $Date: 2007/07/17 14:03:53 $
 */

package nl.reinders.bm;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.Table;

/**
 * A JPA entity class for stand.
 * @version    $Revision: 1.13 $
 * @author     $Author: toeukpap $
 */
@Entity
@Table(name="stand")
public class Stand extends nl.reinders.bm.generated.Stand
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(Stand.class.getName());
	
	// =========================================================================================
	// CONSTRUCTORS

	/**
	 * 
	 */
	public Stand()
	{
		construct();
	}
	
	/**
	 * 
	 *
	 */
	private void construct()
	{
		// defaults
		setStandid("");
		setPendingChanges(false);
	}

	
	// ========================================================================================================
	// PROPERTIES
	
	/** determine the highest version in all the standversion of this stand or 0 */
	public int determineHighestVersionOfAllMyStandversions()
	{
    	int lHighestVersion = 0;
		for (Standversion lStandversion : getStandversionsWhereIAmStand())
		{
			if (lStandversion.getVersion() != null && lStandversion.getVersion() > lHighestVersion) lHighestVersion = lStandversion.getVersion(); 
		}
		return lHighestVersion;
	}

	/** determine the standversion with the highest version in this stand or null */
	public Standversion determineLatestStandversion()
	{
    	Standversion lLatestStandversion = null;
		for (Standversion lStandversion : getStandversionsWhereIAmStand())
		{
			if (lLatestStandversion == null || lStandversion.getVersion() > lLatestStandversion.getVersion()) lLatestStandversion = lStandversion; 
		}
		return lLatestStandversion;
	}

	
	// ========================================================================================================
	// DOA
	
	/**
	 * Find all stands that are candidate for the DerrivedFrom property
	 */
	static public List<Stand> findAllForDerrivedFrom()
	{
		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 Stand t order by t.iStandid");
		return (java.util.List<nl.reinders.bm.Stand>)lQuery.getResultList();
	}

	/**
	 * fetch all Relationstand where Relation is prefetched for performance
	 */
	public List<nl.reinders.bm.Relationstand> getRelationstandsWhereIAmStandPrefetchRelation()
	{
		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 Relationstand t join fetch t.iRelation where t.iStand = :iStand");
		lQuery.setParameter("iStand", this);
		return (java.util.List<nl.reinders.bm.Relationstand>)lQuery.getResultList();
	}

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

	public String createStringForDisplay()
	{
		return getStandid();
	}
	
}
