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

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

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.RetailerItemsDefs;
import org.eu.adapt.demonstrator.eb.retailer.CustomerReferenceHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.PurchaseOrderHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.PurchaseOrderItemHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.ProductHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.CatalogHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.CatalogItemHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.ManufacturerHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.InventoryHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.WarehouseHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.CustomerReferenceLocal;
import org.eu.adapt.demonstrator.eb.retailer.PurchaseOrderLocal;
import org.eu.adapt.demonstrator.eb.retailer.PurchaseOrderItemLocal;
import org.eu.adapt.demonstrator.eb.retailer.ProductLocal;
import org.eu.adapt.demonstrator.eb.retailer.CatalogLocal;
import org.eu.adapt.demonstrator.eb.retailer.CatalogItemLocal;
import org.eu.adapt.demonstrator.eb.retailer.ManufacturerLocal;
import org.eu.adapt.demonstrator.eb.retailer.InventoryLocal;
import org.eu.adapt.demonstrator.eb.retailer.WarehouseLocal;
import org.eu.adapt.services.demo_1_1.retailer.RetailerSetupBasicService_wsdl.RetailerSetupBasicServicePortType;

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

        try
        {
            Iterator customerReferencesIterator = _customerReferenceHome.findAll().iterator();
            while (customerReferencesIterator.hasNext())
            {
                CustomerReferenceLocal customerReference = (CustomerReferenceLocal) customerReferencesIterator.next();
                _customerReferenceHome.remove(customerReference.getPrimaryKey());
            }

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

            Iterator purchaseOrderItemsIterator = _purchaseOrderItemHome.findAll().iterator();
            while (purchaseOrderItemsIterator.hasNext())
            {
                PurchaseOrderItemLocal purchaseOrderItem = (PurchaseOrderItemLocal) purchaseOrderItemsIterator.next();
                _purchaseOrderItemHome.remove(purchaseOrderItem.getPrimaryKey());
            }

            Iterator productsIterator = _productHome.findAll().iterator();
            while (productsIterator.hasNext())
            {
                ProductLocal product = (ProductLocal) productsIterator.next();
                _productHome.remove(product.getPrimaryKey());
            }

            Iterator catalogsIterator = _catalogHome.findAll().iterator();
            while (catalogsIterator.hasNext())
            {
                CatalogLocal catalog = (CatalogLocal) catalogsIterator.next();
                _catalogHome.remove(catalog.getPrimaryKey());
            }

            Iterator catalogItemsIterator = _catalogItemHome.findAll().iterator();
            while (catalogItemsIterator.hasNext())
            {
                CatalogItemLocal catalogItem = (CatalogItemLocal) catalogItemsIterator.next();
                _catalogItemHome.remove(catalogItem.getPrimaryKey());
            }

            Iterator manufacturersIterator = _manufacturerHome.findAll().iterator();
            while (manufacturersIterator.hasNext())
            {
                ManufacturerLocal manufacturer = (ManufacturerLocal) manufacturersIterator.next();
                _manufacturerHome.remove(manufacturer.getPrimaryKey());
            }

            Iterator inventoryIterator = _inventoryHome.findAll().iterator();
            while (inventoryIterator.hasNext())
            {
                InventoryLocal inventory = (InventoryLocal) inventoryIterator.next();
                _inventoryHome.remove(inventory.getPrimaryKey());
            }

            Iterator warehouseIterator = _warehouseHome.findAll().iterator();
            while (warehouseIterator.hasNext())
            {
                WarehouseLocal warehouse = (WarehouseLocal) warehouseIterator.next();
                _warehouseHome.remove(warehouse.getPrimaryKey());
            }

            CustomerReferenceLocal customerReference0 = _customerReferenceHome.create(RetailerItemsDefs.CUSTOMERREFERENCE_0, "1 The Street", "The North End", "A City", "East", "XY1 2AB", "EU"); 
            CustomerReferenceLocal customerReference1 = _customerReferenceHome.create(RetailerItemsDefs.CUSTOMERREFERENCE_1, "1 A Road", "The South Side", "A Town", "West", "AB1 2XY", "EU"); 

            CatalogLocal catalog0 = _catalogHome.create(RetailerItemsDefs.CATALOG_0);
            CatalogLocal catalog1 = _catalogHome.create(RetailerItemsDefs.CATALOG_1);

            WarehouseLocal warehouse0 = _warehouseHome.create(RetailerItemsDefs.WAREHOUSE_0);
            WarehouseLocal warehouse1 = _warehouseHome.create(RetailerItemsDefs.WAREHOUSE_1);

            ProductLocal product0 = _productHome.create(RetailerItemsDefs.CATALOGITEM_NAME_0);
            ProductLocal product1 = _productHome.create(RetailerItemsDefs.CATALOGITEM_NAME_1);
            ProductLocal product2 = _productHome.create(RetailerItemsDefs.CATALOGITEM_NAME_2);
            ProductLocal product3 = _productHome.create(RetailerItemsDefs.CATALOGITEM_NAME_3);
            ProductLocal product4 = _productHome.create(RetailerItemsDefs.CATALOGITEM_NAME_4);
            ProductLocal product5 = _productHome.create(RetailerItemsDefs.CATALOGITEM_NAME_5);

            CatalogItemLocal catalogItem0 = _catalogItemHome.create(catalog0, product0, RetailerItemsDefs.CATALOGITEM_NAME_0, RetailerItemsDefs.CATALOGITEM_DESCRIPTION_0, "Standard", 1000.0f);
            CatalogItemLocal catalogItem1 = _catalogItemHome.create(catalog0, product1, RetailerItemsDefs.CATALOGITEM_NAME_1, RetailerItemsDefs.CATALOGITEM_DESCRIPTION_1, "Standard", 1000.0f);
            CatalogItemLocal catalogItem2 = _catalogItemHome.create(catalog1, product2, RetailerItemsDefs.CATALOGITEM_NAME_2, RetailerItemsDefs.CATALOGITEM_DESCRIPTION_2, "Standard", 1000.0f);
            CatalogItemLocal catalogItem3 = _catalogItemHome.create(catalog1, product3, RetailerItemsDefs.CATALOGITEM_NAME_3, RetailerItemsDefs.CATALOGITEM_DESCRIPTION_3, "Standard", 1000.0f);
            CatalogItemLocal catalogItem4 = _catalogItemHome.create(catalog1, product4, RetailerItemsDefs.CATALOGITEM_NAME_4, RetailerItemsDefs.CATALOGITEM_DESCRIPTION_4, "Standard", 1000.0f);
            CatalogItemLocal catalogItem5 = _catalogItemHome.create(catalog1, product5, RetailerItemsDefs.CATALOGITEM_NAME_5, RetailerItemsDefs.CATALOGITEM_DESCRIPTION_5, "Standard", 1000.0f);

            ManufacturerLocal manufacturer0 = _manufacturerHome.create(RetailerItemsDefs.MANUFACTURER_0);
            ManufacturerLocal manufacturer1 = _manufacturerHome.create(RetailerItemsDefs.MANUFACTURER_1);
            ManufacturerLocal manufacturer2 = _manufacturerHome.create(RetailerItemsDefs.MANUFACTURER_2);

            InventoryLocal inventory0 = _inventoryHome.create(warehouse0, product0, 0, 1000000);
            InventoryLocal inventory1 = _inventoryHome.create(warehouse0, product1, 0, 1000000);
            InventoryLocal inventory2 = _inventoryHome.create(warehouse1, product2, 0, 1000000);
            InventoryLocal inventory3 = _inventoryHome.create(warehouse1, product3, 0, 1000000);
            InventoryLocal inventory4 = _inventoryHome.create(warehouse1, product4, 0, 1000000);
            InventoryLocal inventory5 = _inventoryHome.create(warehouse1, product5, 0, 1000000);

            product0.getWarehouses().add(warehouse0);
            product1.getWarehouses().add(warehouse0);
            product2.getWarehouses().add(warehouse1);
            product3.getWarehouses().add(warehouse1);
            product4.getWarehouses().add(warehouse1);
            product5.getWarehouses().add(warehouse1);

            manufacturer0.getProducts().add(product0);
            manufacturer0.getProducts().add(product1);
            manufacturer1.getProducts().add(product2);
            manufacturer1.getProducts().add(product3);
            manufacturer1.getProducts().add(product4);
            manufacturer1.getProducts().add(product5);
            manufacturer2.getProducts().add(product0);
            manufacturer2.getProducts().add(product1);
            manufacturer2.getProducts().add(product2);
            manufacturer2.getProducts().add(product3);
            manufacturer2.getProducts().add(product4);
            manufacturer2.getProducts().add(product5);
            
            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();

            _customerReferenceHome = (CustomerReferenceHomeLocal) initialContext.lookup("local/Retailer_CustomerReferenceEB");
            _purchaseOrderHome     = (PurchaseOrderHomeLocal) initialContext.lookup("local/Retailer_PurchaseOrderEB");
            _purchaseOrderItemHome = (PurchaseOrderItemHomeLocal) initialContext.lookup("local/Retailer_PurchaseOrderItemEB");
            _productHome           = (ProductHomeLocal) initialContext.lookup("local/Retailer_ProductEB");
            _catalogHome           = (CatalogHomeLocal) initialContext.lookup("local/Retailer_CatalogEB");
            _catalogItemHome       = (CatalogItemHomeLocal) initialContext.lookup("local/Retailer_CatalogItemEB");
            _manufacturerHome      = (ManufacturerHomeLocal) initialContext.lookup("local/Retailer_ManufacturerEB");
            _inventoryHome         = (InventoryHomeLocal) initialContext.lookup("local/Retailer_InventoryEB");
            _warehouseHome         = (WarehouseHomeLocal) initialContext.lookup("local/Retailer_WarehouseEB");

            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()
    {
        _customerReferenceHome = null;
        _purchaseOrderHome     = null;
        _purchaseOrderItemHome = null;
        _productHome           = null;
        _catalogHome           = null;
        _catalogItemHome       = null;
        _manufacturerHome      = null;
        _inventoryHome         = null;
        _warehouseHome         = null;
    }

    private SessionContext _sessionContext = null;
    
    private transient CustomerReferenceHomeLocal _customerReferenceHome = null;
    private transient PurchaseOrderHomeLocal     _purchaseOrderHome     = null;
    private transient PurchaseOrderItemHomeLocal _purchaseOrderItemHome = null;
    private transient ProductHomeLocal           _productHome           = null;
    private transient CatalogHomeLocal           _catalogHome           = null;
    private transient CatalogItemHomeLocal       _catalogItemHome       = null;
    private transient ManufacturerHomeLocal      _manufacturerHome      = null;
    private transient InventoryHomeLocal         _inventoryHome         = null;
    private transient WarehouseHomeLocal         _warehouseHome         = null;
}
