package com.Test.LiveGrid.server;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;



public class ItemSupplyXmlServletNew extends HttpServlet  {
	
	int i =10;
	long skuNum =12345;
	long unitCost= 100;
	@SuppressWarnings("unchecked")
	public void service(HttpServletRequest request,
            HttpServletResponse response)
	throws IOException, ServletException
	{
		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		XMLSerializer xmlWriter = new XMLSerializer(out, new OutputFormat("xml", "UTF-8", true));
		
		try{
			xmlWriter.startDocument();
			
			AttributesImpl attrs = new AttributesImpl();
			attrs.addAttribute("", "", "created_by", "", "OSA");
			attrs.addAttribute("", "", "created_at", "", new java.util.Date().toString());
			ArrayList unitsList = new ArrayList();
			unitsList.add("EA");
			unitsList.add("Roll");
			unitsList.add("pkt");
			unitsList.add("set");
			unitsList.add("Bag");
			unitsList.add("Box");
			unitsList.add("Ctn");
			
			Iterator it1 = unitsList.iterator();
			
			xmlWriter.startElement("", "", "supplyItem", attrs);
			
			for(int j =0 ; j<= 50000; j++){
			
				
				
				attrs = new AttributesImpl();
				
				attrs.addAttribute("", "", "SKU", "", ""+skuNum+j );
				if(it1.hasNext()){
					attrs.addAttribute("", "", "units", "", ""+ it1.next());
				}else{
					 it1 = unitsList.iterator();
					 attrs.addAttribute("", "", "units", "", ""+ it1.next());
				}
				attrs.addAttribute("", "", "category", "", "Category="+j );
				attrs.addAttribute("", "", "itemName", "", "itemName="+j );
				attrs.addAttribute("", "", "unitCost", "", "unitCost="+unitCost+j );
				attrs.addAttribute("", "", "description", "", "cost of Item Number="+j+ " is "+  unitCost+j);
				
				
				
				 
				xmlWriter.startElement("", "", "rows", attrs);
				   
				xmlWriter.endElement("", "", "rows");
			}
			xmlWriter.endElement("", "", "supplyItem");
			xmlWriter.endDocument();
		}catch(SAXException e){
			e.printStackTrace();
		}
		   
		    	
		    
		
	}

}
