/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 *
 * ManufacturerSetupBasicServiceBean.java
 */

package org.eu.adapt.demonstrator.bs.manufacturer;

import java.util.Iterator;
import java.rmi.RemoteException;
import javax.ejb.SessionContext;
import javax.ejb.SessionBean;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.eu.adapt.demonstrator.web.demo.ManufacturerItemsDefs;
import org.eu.adapt.demonstrator.eb.manufacturer.CustomerHomeLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.CustomerLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.FinishedGoodsHomeLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.FinishedGoodsLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.InventoryItemHomeLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.InventoryItemLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.ItemHomeLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.ItemLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.ProductionLineHomeLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.ProductionLineLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.PurchaseOrderHomeLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.PurchaseOrderLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.StorehouseHomeLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.StorehouseLocal;
import org.eu.adapt.services.demo_1_1.manufacturer.ManufacturerSetupBasicService_wsdl.ManufacturerSetupBasicServicePortType;

public class ManufacturerSetupBasicServiceBean implements SessionBean, ManufacturerSetupBasicServicePortType
{
    public boolean setup()
       throws RemoteException
    {
        boolean successful = false;

        try
        {
            Iterator customersIterator = _customerHome.findAll().iterator();
            while (customersIterator.hasNext())
            {
                CustomerLocal customer = (CustomerLocal) customersIterator.next();
                _customerHome.remove(customer.getPrimaryKey());
            }

            Iterator purchaseOrdersIterator = _purchaseOrderHome.findAll().iterator();
            while (purchaseOrdersIterator.hasNext())
            {
                PurchaseOrderLocal purchaseOrder = (PurchaseOrderLocal) purchaseOrdersIterator.next();
                _purchaseOrderHome.remove(purchaseOrder.getPrimaryKey());
            }

            Iterator finishedGoodsIterator = _finishedGoodsHome.findAll().iterator();
            while (finishedGoodsIterator.hasNext())
            {
                FinishedGoodsLocal finishedGoods = (FinishedGoodsLocal) finishedGoodsIterator.next();
                _finishedGoodsHome.remove(finishedGoods.getPrimaryKey());
            }

            Iterator itemsIterator = _itemHome.findAll().iterator();
            while (itemsIterator.hasNext())
            {
                ItemLocal item = (ItemLocal) itemsIterator.next();
                _itemHome.remove(item.getPrimaryKey());
            }

            Iterator inventoryItemIterator = _inventoryItemHome.findAll().iterator();
            while (inventoryItemIterator.hasNext())
            {
                InventoryItemLocal inventoryItem = (InventoryItemLocal) inventoryItemIterator.next();
                _inventoryItemHome.remove(inventoryItem.getPrimaryKey());
            }

            Iterator productionLinesIterator = _productionLineHome.findAll().iterator();
            while (productionLinesIterator.hasNext())
            {
                ProductionLineLocal productionLine = (ProductionLineLocal) productionLinesIterator.next();
                _productionLineHome.remove(productionLine.getPrimaryKey());
            }

            Iterator storehouseIterator = _storehouseHome.findAll().iterator();
            while (storehouseIterator.hasNext())
            {
                StorehouseLocal storehouse = (StorehouseLocal) storehouseIterator.next();
                _storehouseHome.remove(storehouse.getPrimaryKey());
            }

            CustomerLocal customer0 = _customerHome.create(ManufacturerItemsDefs.CUSTOMER_0, ManufacturerItemsDefs.CUSTOMER_NAME_0); 
            CustomerLocal customer1 = _customerHome.create(ManufacturerItemsDefs.CUSTOMER_1, ManufacturerItemsDefs.CUSTOMER_NAME_1); 

            FinishedGoodsLocal finishedGoods0 = _finishedGoodsHome.create(ManufacturerItemsDefs.FINISHED_GOODS_0, ManufacturerItemsDefs.FINISHED_GOODS_NAME_0);
            FinishedGoodsLocal finishedGoods1 = _finishedGoodsHome.create(ManufacturerItemsDefs.FINISHED_GOODS_1, ManufacturerItemsDefs.FINISHED_GOODS_NAME_1);
            FinishedGoodsLocal finishedGoods2 = _finishedGoodsHome.create(ManufacturerItemsDefs.FINISHED_GOODS_2, ManufacturerItemsDefs.FINISHED_GOODS_NAME_2);
            FinishedGoodsLocal finishedGoods3 = _finishedGoodsHome.create(ManufacturerItemsDefs.FINISHED_GOODS_3, ManufacturerItemsDefs.FINISHED_GOODS_NAME_3);
            FinishedGoodsLocal finishedGoods4 = _finishedGoodsHome.create(ManufacturerItemsDefs.FINISHED_GOODS_4, ManufacturerItemsDefs.FINISHED_GOODS_NAME_4);
            FinishedGoodsLocal finishedGoods5 = _finishedGoodsHome.create(ManufacturerItemsDefs.FINISHED_GOODS_5, ManufacturerItemsDefs.FINISHED_GOODS_NAME_5);

            StorehouseLocal storehouse0 = _storehouseHome.create(ManufacturerItemsDefs.STOREHOUSE_0);
            StorehouseLocal storehouse1 = _storehouseHome.create(ManufacturerItemsDefs.STOREHOUSE_1);

            ProductionLineLocal productionLine0 = _productionLineHome.create(ManufacturerItemsDefs.PRODUCTION_LINE_0, storehouse0);
            ProductionLineLocal productionLine1 = _productionLineHome.create(ManufacturerItemsDefs.PRODUCTION_LINE_1, storehouse1);
            ProductionLineLocal productionLine2 = _productionLineHome.create(ManufacturerItemsDefs.PRODUCTION_LINE_2, storehouse1);

            InventoryItemLocal inventoryItem0 = _inventoryItemHome.create(storehouse0, finishedGoods0, 0, 1000000);
            InventoryItemLocal inventoryItem1 = _inventoryItemHome.create(storehouse0, finishedGoods1, 0, 1000000);
            InventoryItemLocal inventoryItem2 = _inventoryItemHome.create(storehouse1, finishedGoods2, 0, 1000000);
            InventoryItemLocal inventoryItem3 = _inventoryItemHome.create(storehouse1, finishedGoods3, 0, 1000000);
            InventoryItemLocal inventoryItem4 = _inventoryItemHome.create(storehouse1, finishedGoods4, 0, 1000000);
            InventoryItemLocal inventoryItem5 = _inventoryItemHome.create(storehouse1, finishedGoods5, 0, 1000000);

            productionLine0.getFinishedGoods().add(finishedGoods0);
            productionLine0.getFinishedGoods().add(finishedGoods1);
            productionLine1.getFinishedGoods().add(finishedGoods2);
            productionLine2.getFinishedGoods().add(finishedGoods3);
            productionLine2.getFinishedGoods().add(finishedGoods4);
            productionLine2.getFinishedGoods().add(finishedGoods5);

            successful = true;
        }        
        catch (Exception exception)
        {
            System.out.println("Exception: \"" + exception + "\"");
        }
        catch (Error error)
        {
            System.out.println("Error: \"" + error + "\"");
        }

        return successful;
    }

    public void setSessionContext(SessionContext sessionContext)
    {
        _sessionContext = sessionContext;
    }

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

    public void ejbCreate()
    {
        try
        {
            InitialContext initialContext = new InitialContext();

            _customerHome       = (CustomerHomeLocal) initialContext.lookup("local/Manufacturer_CustomerEB");
            _purchaseOrderHome  = (PurchaseOrderHomeLocal) initialContext.lookup("local/Manufacturer_PurchaseOrderEB");
            _itemHome           = (ItemHomeLocal) initialContext.lookup("local/Manufacturer_ItemEB");
            _finishedGoodsHome  = (FinishedGoodsHomeLocal) initialContext.lookup("local/Manufacturer_FinishedGoodsEB");
            _productionLineHome = (ProductionLineHomeLocal) initialContext.lookup("local/Manufacturer_ProductionLineEB");
            _inventoryItemHome  = (InventoryItemHomeLocal) initialContext.lookup("local/Manufacturer_InventoryItemEB");
            _storehouseHome     = (StorehouseHomeLocal) initialContext.lookup("local/Manufacturer_StorehouseEB");

            initialContext.close();
        }
        catch (NamingException namingException)
        {
            System.out.println("Name lookup problem: \"" + namingException + "\"");
            namingException.printStackTrace(System.out);
        }
        catch (Exception exception)
        {
            System.out.println("Problem: \"" + exception + "\"");
            exception.printStackTrace(System.out);
        }
        catch (Error error)
        {
            System.out.println("Problem: \"" + error + "\"");
            error.printStackTrace(System.out);
        }
    }

    public void ejbActivate()
    {
    }

    public void ejbPassivate()
    {
    }

    public void ejbLoad()
    {
    }

    public void ejbStore()
    {
    }    

    public void ejbRemove()
    {
        _customerHome       = null;
        _purchaseOrderHome  = null;
        _itemHome           = null;
        _finishedGoodsHome  = null;
        _productionLineHome = null;
        _inventoryItemHome  = null;
        _storehouseHome     = null;
    }

    private SessionContext _sessionContext = null;
    
    private transient CustomerHomeLocal       _customerHome       = null;
    private transient PurchaseOrderHomeLocal  _purchaseOrderHome  = null;
    private transient ItemHomeLocal           _itemHome           = null;
    private transient FinishedGoodsHomeLocal  _finishedGoodsHome  = null;
    private transient ProductionLineHomeLocal _productionLineHome = null;
    private transient InventoryItemHomeLocal  _inventoryItemHome  = null;
    private transient StorehouseHomeLocal     _storehouseHome     = null;
}