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

package org.eu.adapt.demonstrator.web.demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import org.eu.adapt.demonstrator.web.ADAPTLookAndFeel;

public class ManufacturerPurchaseOrderList
{
    public static void generateManufacturerPurchaseOrderList(String rootPath, PrintWriter writer)
        throws IOException
    {
        synchronized (_manufacturerPurchaseOrderList)
        {
            ADAPTLookAndFeel.generateTableHeader("Manufacturer Purchase Orders", null, writer);
            ADAPTLookAndFeel.generateTableSubheader("Number", null, "Status", null, writer);
            Iterator manufacturerPurchaseOrderListIterator = _manufacturerPurchaseOrderList.iterator();
            while (manufacturerPurchaseOrderListIterator.hasNext())
            {
                ManufacturerPurchaseOrder manufacturerPurchaseOrder = (ManufacturerPurchaseOrder) manufacturerPurchaseOrderListIterator.next();
                ADAPTLookAndFeel.generateTableEntry(manufacturerPurchaseOrder.getManufacturerPurchaseOrderNumber(), "../manufacturer/PurchaseOrder.html?name=" + URLEncoder.encode(manufacturerPurchaseOrder.getManufacturerPurchaseOrderNumber(), "UTF-8"), manufacturerPurchaseOrder.getStatusAsString(), null, writer);
            }
            ADAPTLookAndFeel.generateTableFooter(writer);
        }
    }

    public static void addManufacturerPurchaseOrder(String orderNumber)
    {
        synchronized (_manufacturerPurchaseOrderList)
        {
            ManufacturerPurchaseOrder manufacturerPurchaseOrder = new ManufacturerPurchaseOrder(orderNumber, ManufacturerPurchaseOrder.STATUS_UNKNOWN);
            
            _manufacturerPurchaseOrderList.add(0, manufacturerPurchaseOrder);
        }
    }

    public static void setManufacturerPurchaseOrderStatus(String orderNumber, int status)
    {
        synchronized (_manufacturerPurchaseOrderList)
        {
            boolean  done                                  = false;
            Iterator manufacturerPurchaseOrderListIterator = _manufacturerPurchaseOrderList.iterator();
            while ((! done) && manufacturerPurchaseOrderListIterator.hasNext())
            {
                ManufacturerPurchaseOrder manufacturerPurchaseOrder = (ManufacturerPurchaseOrder) manufacturerPurchaseOrderListIterator.next();

                if (manufacturerPurchaseOrder.getManufacturerPurchaseOrderNumber().equals(orderNumber))
                {
                    manufacturerPurchaseOrder.setStatus(status);
                    done = true;
                }
            }
        }
    }

    private static List _manufacturerPurchaseOrderList = new LinkedList();
}