/*
 * Created on 27 nov. 03
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.gerling.inventaire.ejb;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;

import com.gerling.inventaire.dto.PC;
import com.gerling.inventaire.dto.Printer;
import com.gerling.inventaire.dto.Screen;
import com.gerling.inventaire.dto.SupportPack;


/**
 * @ejb.bean name="SupportPackCMP"
 *  jndi-name="com.gerling.inventaire.ejb.SupportPackCMPBean"
 *	local-jndi-name="com.gerling.inventaire.ejb.SupportPackCMPBean"
 *	type="CMP"
 *  primkey-field="supportPackId"
 *  schema="supportPackSchema" 
 *  cmp-version="2.x"
 * 
 * @ejb.persistence 
 *   table-name="SUPPORT_PACK" 
 * 
 * @ejb.finder 
 *    query="SELECT OBJECT(a) FROM supportPackSchema as a"  
 *    signature="java.util.Collection findAll()"
 * 
 * @ejb.finder 
 *    query = "SELECT OBJECT(a) FROM supportPackSchema as a where a.reseller = ?1"
 *    signature = "java.util.Collection findByReseller(java.lang.String reseller)"
 * 
 * @ejb.finder 
 *     query = "SELECT OBJECT(a) FROM supportPackSchema as a where a.purchaser = ?1"
 *     signature = "java.util.Collection findByPurchaser (java.lang.String purchaser)"
 * 
 * @jboss.persistence 
 *    datasource = "java:/InventoryDB"
 *    datasource-mapping = "Oracle8"
 *    create-table = "true"
 *    remove-table = "false"
 *    pk-constraint = "true"
 *  
 **/

public abstract class SupportPackCMPBean implements EntityBean
{
	EntityContext entityContext;


	/**
	 * The  ejbCreate method.
	 * 
	 * @ejb.create-method 
	 */
	public java.lang.Integer ejbCreate(SupportPack supportPack) throws javax.ejb.CreateException
	{
		setSupportPack(supportPack); 
		return null;
	}

	/**
	 * The container invokes this method immediately after it calls ejbCreate.
	 * 
	 */
	public void ejbPostCreate(SupportPack supportPack) throws javax.ejb.CreateException
	{
	}

	/**
	* Returns the supportPackId
	* @return the supportPackId
	* 
	* @ejb.persistent-field 
	* @ejb.persistence
	*    column-name="SP_ID"
	*     sql-type="NUMBER"
	* @ejb.pk-field 
	* @ejb.interface-method
	*/
	public abstract java.lang.Integer getSupportPackId();

	/**
	* Sets the supportPackId
	* 
	* @param java.lang.Integer the new supportPackId value
	* 
	* @ejb.interface-method
	*/
	public abstract void setSupportPackId(java.lang.Integer supportPackId);

	/**
	* Returns the contractEffectDate
	* @return the contractEffectDate
	* 
	* @ejb.persistent-field 
	* @ejb.persistence
	*    column-name="C_EFFECT_DATE"
	*     sql-type="DATE"
	*  
	* @ejb.interface-method
	*/
	public abstract java.util.Date getContractEffectDate();

	/**
	* Sets the contractEffectDate
	* 
	* @param java.util.Date the new contractEffectDate value
	* 
	* @ejb.interface-method
	*/
	public abstract void setContractEffectDate(
		java.util.Date contractEffectDate);

	/**
	* Returns the contractPurchaseDate
	* @return the contractPurchaseDate
	* 
	* @ejb.persistent-field 
	* @ejb.persistence
	*    column-name="C_PURCHASE_DATE"
	*     sql-type="DATE"
	*  
	* @ejb.interface-method
	*/
	public abstract java.util.Date getContractPurchaseDate();

	/**
	* Sets the contractPurchaseDate
	* 
	* @param java.util.Date the new contractPurchaseDate value
	* 
	* @ejb.interface-method
	*/
	public abstract void setContractPurchaseDate(
		java.util.Date contractPurchaseDate);

	/**
	* Returns the contractDuration
	* @return the contractDuration
	* 
	* @ejb.persistent-field 
	* @ejb.persistence
	*    column-name="C_DURATION"
	*     sql-type="NUMBER"
	*  
	* @ejb.interface-method
	*/
	public abstract java.lang.Integer getContractDuration();

	/**
	* Sets the contractDuration
	* 
	* @param java.lang.Integer the new contractDuration value
	* 
	* @ejb.interface-method
	*/
	public abstract void setContractDuration(
		java.lang.Integer contractDuration);

	/**
	* Returns the callNumber
	* @return the callNumber
	* 
	* @ejb.persistent-field 
	* @ejb.persistence
	*    column-name="CALL_NUMBER"
	*     sql-type="VARCHAR"
	*  
	* @ejb.interface-method
	*/
	public abstract java.lang.String getCallNumber();

	/**
	* Sets the callNumber
	* 
	* @param java.lang.String the new callNumber value
	* 
	* @ejb.interface-method
	*/
	public abstract void setCallNumber(java.lang.String callNumber);

	/**
	* Returns the contractSpecification
	* @return the contractSpecification
	* 
	* @ejb.persistent-field 
	* @ejb.persistence
	*    column-name="C_SPECIFICATION"
	*     sql-type="VARCHAR"
	*  
	* @ejb.interface-method
	*/
	public abstract java.lang.String getContractSpecification();

	/**
	* Sets the contractSpecification
	* 
	* @param java.lang.String the new contractSpecification value
	* 
	* @ejb.interface-method
	*/
	public abstract void setContractSpecification(
		java.lang.String contractSpecification);

	/**
	* Returns the reseller
	* @return the reseller
	* 
	* @ejb.persistent-field 
	* @ejb.persistence
	*    column-name="RESELLER"
	*     sql-type="VARCHAR"
	*  
	* @ejb.interface-method
	*/
	public abstract java.lang.String getReseller();

	/**
	* Sets the reseller
	* 
	* @param java.lang.String the new reseller value
	* 
	* @ejb.interface-method
	*/
	public abstract void setReseller(java.lang.String reseller);

	/**
	* Returns the maintenanceCompany
	* @return the maintenanceCompany
	* 
	* @ejb.persistent-field 
	* @ejb.persistence
	*    column-name="M_COMPANY"
	*     sql-type="VARCHAR"
	*  
	* @ejb.interface-method
	*/
	public abstract java.lang.String getMaintenanceCompany();

	/**
	* Sets the maintenanceCompany
	* 
	* @param java.lang.String the new maintenanceCompany value
	* 
	* @ejb.interface-method
	*/
	public abstract void setMaintenanceCompany(
		java.lang.String maintenanceCompany);

	/**
	* Returns the purchaser
	* @return the purchaser
	* 
	* @ejb.persistent-field 
	* @ejb.persistence
	*    column-name="PURCHASER"
	*     sql-type="VARCHAR"
	*  
	* @ejb.interface-method
	*/
	public abstract java.lang.String getPurchaser();

	/**
	* Sets the purchaser
	* 
	* @param java.lang.String the new purchaser value
	* 
	* @ejb.interface-method
	*/
	public abstract void setPurchaser(java.lang.String purchaser);

	/**
	* Returns the contractRecordingDate
	* @return the contractRecordingDate
	* 
	* @ejb.persistent-field 
	* @ejb.persistence
	*    column-name="C_RECORD_DATE"
	*     sql-type="DATE"
	*  
	* @ejb.interface-method
	*/
	public abstract java.util.Date getContractRecordingDate();

	/**
	* Sets the contractRecordingDate
	* 
	* @param java.util.Date the new contractRecordingDate value
	* 
	* @ejb.interface-method
	*/
	public abstract void setContractRecordingDate(
		java.util.Date contractRecordingDate);

	//////////////////////
	// Business Methods //
	//////////////////////
	
	/**
	 * @param supportPack the support pack
	 * @ejb.interface-method 
	 */
	public void setSupportPack (SupportPack supportPack)
	{
		setSupportPackId(supportPack.getSupportPackId());
		setContractEffectDate(supportPack.getContractEffectDate());
		setContractPurchaseDate(supportPack.getContractPurchaseDate());
		setContractDuration(supportPack.getContractDuration());
		setCallNumber(supportPack.getCallNumber());
		setContractSpecification(supportPack.getContractSpecification());
		setReseller(supportPack.getReseller());
		setMaintenanceCompany(supportPack.getMaintenanceCompany());
		setPurchaser(supportPack.getPurchaser());
		setContractRecordingDate(supportPack.getContractRecordingDate());	
	}
	
	/**
	 * 
	 * @return the support pack bound to this ejb
	 * @ejb.interface-method 
	 */
	public SupportPack getSupportPack()
	{
		return new SupportPack(getSupportPackId(), getContractEffectDate(), getContractPurchaseDate(), getContractDuration(), getCallNumber(), getContractSpecification(), getReseller(), getMaintenanceCompany(), getPurchaser(), getContractRecordingDate());
	}
	
	/**
	 * 
	 * @param pcList (com.gerling.inventaire.dto.PC) Collection of PCs covered by this support pack
	 * @ejb.interface-method 
	 */
	public void addPCs (List pcList)
	{
		try
		{
			Collection  usedPCs = new LinkedList();
			//First remove all PCs belonging to this support pack
			Iterator iterator = getPCs().iterator();
			while (iterator.hasNext())
			{
				 ((PCCMPLocal)iterator.next()).remove();
			}
			//now add the PCs taken out of the list
			Context context = new InitialContext();
			PCCMPLocalHome pcCMPLocalHome  = (PCCMPLocalHome)context.lookup("com.gerling.inventaire.ejb.PCCMPBean");
			
			iterator = pcList.iterator();
			while (iterator.hasNext())
			{
				
				usedPCs.add(pcCMPLocalHome.create((PC)iterator.next()));				
			}
			
			//set the PC List
			setPCs(usedPCs);
		}
		catch (Exception e)
		{
			throw new EJBException(e);
		}
	}


	/**
	 * 
	 * @param screensList (com.gerling.inventaire.dto.Screen) Collection of Screens used by this support pack
	 * @ejb.interface-method 
	 */
	public void addScreens (List screensList)
	{
		try
		{
			Collection usedScreens = new LinkedList();
			// First remove all screens belonging to this support pack
			Iterator iterator = getScreens().iterator();
			while (iterator.hasNext())
			{
				((ScreenCMPLocal)iterator.next()).remove();
			}
			//now add the screens taken out of the list
			Context context = new InitialContext();
			ScreenCMPLocalHome screenCMPLocalHome = (ScreenCMPLocalHome)context.lookup("com.gerling.inventaire.ejb.ScreenCMPBean");
			iterator = screensList.iterator();
			while (iterator.hasNext())
			{
				usedScreens.add(screenCMPLocalHome.create((Screen)iterator.next()));
			}
			//set the Screens List
			setScreens(usedScreens);
		}
		catch (Exception e)
		{
			throw new EJBException(e);
		}
	}


	/**
	 * 
	 * @param printersList (com.gerling.inventaire.dto.Printer) Collection of Printers used by this support pack
	 * @ejb.interface-method 
	 */
	public void addPrinters (List printersList)
	{
		try
		{
			Collection usedPrinters = new LinkedList();
			//First remove all printers belonging to this support pack
			Iterator iterator = getPrinters().iterator();
			while (iterator.hasNext())
			{
				((PrinterCMPLocal)iterator.next()).remove();
			}
			//now add the printers taken out of the list
			Context context = new InitialContext();
			PrinterCMPLocalHome printerCMPLocalHome = (PrinterCMPLocalHome)context.lookup("com.gerling.inventaire.ejb.PrinterCMPBean");
			iterator = printersList.iterator();
			while (iterator.hasNext())
			{
				usedPrinters.add(printerCMPLocalHome.create((Printer)iterator.next()));
			}
			//set the Printers list
			setPrinters(printersList);
		}
		catch (Exception e)
		{
			throw new EJBException(e);
		}
	}

	


	//////////////////////
	// Callback methods //
	//////////////////////

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbActivate()
	 */
	public void ejbActivate()
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbLoad()
	 */
	public void ejbLoad()
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbPassivate()
	 */
	public void ejbPassivate()
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbRemove()
	 */
	public void ejbRemove()
		throws RemoveException
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbStore()
	 */
	public void ejbStore()
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
	 */
	public void setEntityContext(EntityContext arg0)
	{
		// TODO Auto-generated method stub
		entityContext = arg0;

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#unsetEntityContext()
	 */
	public void unsetEntityContext()
	{
		// TODO Auto-generated method stub
		entityContext = null;

	}

	////////////////
	// CMR Fields //
	////////////////
	
	
	/**
	 * @ejb.interface-method 
	 * 
	 * @ejb.relation 
	 *    name = "supportpack-PC"
	 *    role-name = "supportpackToPC"
	 *    target-ejb = "PCCMP"
	 *    target-multiple = "no"
	 *    target-role-name = "PCTosupportpack"
	 * 
	 * @jboss.target-relation 
	 *    related-pk-field = "SP_ID"
	 *    fk-column = "sp_idfk"
	 *    fk-constraint = "true"
	 *    
	 */
	public abstract Collection getPCs();
	
	/**
	 * 
	 * @param pcsCollection the PC collection to set for the 1-n possible relationship
	 * @ejb.interface-method 
	 */
	public abstract void setPCs (java.util.Collection pcsCollection);	
	
	
	
	/**
	 * @ejb.interface-method 
	 * 
	 * @ejb.relation 
	 *    name = "supportpack-Screen"
	 *    role-name = "supportpackToScreen"
	 *    target-ejb = "ScreenCMP"
	 *    target-multiple = "yes"
	 *    target-role-name = "ScreenTosupportpack"
	 * 
	 * @jboss.target-relation 
	 *    related-pk-field = "SP_ID"
	 *    fk-column = "sp_idfk"
	 *    fk-constraint = "true"
	 * 
	 */
	public abstract Collection getScreens();
	
	
	/**
	 * 
	 * @param screensCollection the Screens collection to set for the 1-n possible relationship
	 * @ejb.interface-method 
	 */
	public abstract void setScreens(java.util.Collection screensCollection);

	/**
	 * @ejb.interface-method 
	 * 
	 * @ejb.relation 
	 *    name = "supportpack-Printer"
	 *    role-name = "supportpackToPrinter"
	 *    target-ejb = "PrinterCMP"
	 *    target-multiple = "no"
	 *    target-role-name = "PrinterTosupportpack"
	 * 
	 * @jboss.target-relation 
	 *    related-pk-field = "SP_ID"
	 *    fk-column = "sp_idfk"
	 *    fk-constraint = "true"
	 * 
	 */
	public abstract Collection getPrinters();
	
	/**
	 * 
	 * @param printersCollection the Printers collection to set for the 1-n possible relationship
	 * @ejb.interface-method 
	 */
	public abstract void setPrinters(java.util.Collection printersCollection);


	/**
	 * @ejb.interface-method 
	 * 
	 * @ejb.relation
	 *    name = "supportpack-Peripheral"
	 *    role-name = "supportpackToPeripheral"
	 *    target-ejb = "PeripheralCMP"
	 *    target-multiple = "no"
	 *    target-role-name = "PeripheralToSupportpack"
	 *

	 *  
	 * @jboss.target-relation 
	 *    related-pk-field = "SP_ID"
	 *    fk-column = "sp_idfk"
	 *    fk-constraint = "true"
	 *    
	 * 
	 */	
	public abstract Collection getPeripherals();
	
	/**
	 * 
	 * @param PeripheralsCollection the Peripherals collection to set for the 1-n possible relationship
	 * @ejb.interface-method 
	 */
	public abstract void setPeripherals(java.util.Collection PeripheralsCollection);



}
