/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * RetailerPurchaseOrderList.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 RetailerPurchaseOrderList
{
    public static void generateRetailerPurchaseOrderList(String rootPath, PrintWriter writer)
        throws IOException
    {
        synchronized (_retailerPurchaseOrderList)
        {
            ADAPTLookAndFeel.generateTableHeader("Retailer Purchase Orders", null, writer);
            ADAPTLookAndFeel.generateTableSubheader("Number", null, "Status", null, writer);
            Iterator retailerPurchaseOrderListIterator = _retailerPurchaseOrderList.iterator();
            while (retailerPurchaseOrderListIterator.hasNext())
            {
                RetailerPurchaseOrder retailerPurchaseOrder = (RetailerPurchaseOrder) retailerPurchaseOrderListIterator.next();
                ADAPTLookAndFeel.generateTableEntry(retailerPurchaseOrder.getRetailerPurchaseOrderNumber(), "../retailer/PurchaseOrder.html?name=" + URLEncoder.encode(retailerPurchaseOrder.getRetailerPurchaseOrderNumber(), "UTF-8"), retailerPurchaseOrder.getStatusAsString(), null, writer);
            }
            ADAPTLookAndFeel.generateTableFooter(writer);
        }
    }

    public static void addRetailerPurchaseOrder(String orderNumber)
    {
        synchronized (_retailerPurchaseOrderList)
        {
            RetailerPurchaseOrder retailerPurchaseOrder = new RetailerPurchaseOrder(orderNumber, RetailerPurchaseOrder.STATUS_UNKNOWN);
            
            _retailerPurchaseOrderList.add(0, retailerPurchaseOrder);
        }
    }

    public static void setRetailerPurchaseOrderStatus(String orderNumber, int status)
    {
        synchronized (_retailerPurchaseOrderList)
        {
            boolean  done                                  = false;
            Iterator retailerPurchaseOrderListIterator = _retailerPurchaseOrderList.iterator();
            while ((! done) && retailerPurchaseOrderListIterator.hasNext())
            {
                RetailerPurchaseOrder retailerPurchaseOrder = (RetailerPurchaseOrder) retailerPurchaseOrderListIterator.next();

                if (retailerPurchaseOrder.getRetailerPurchaseOrderNumber().equals(orderNumber))
                {
                    retailerPurchaseOrder.setStatus(status);
                    done = true;
                }
            }
        }
    }

    private static List _retailerPurchaseOrderList = new LinkedList();
}