package com.philips.ce.ecm2.web.encoding.ser;

import com.philips.ce.ecm2.web.ConceptPath;
import com.philips.ce.ecm2.web.ConceptPathVector;

import org.apache.axis.Constants;
import org.apache.axis.components.logger.LogFactory;
import org.apache.axis.encoding.SerializationContext;
import org.apache.axis.encoding.Serializer;
import org.apache.axis.utils.Messages;
import org.apache.axis.wsdl.fromJava.Types;
import org.apache.commons.logging.Log;
import org.w3c.dom.Element;
import org.xml.sax.Attributes;

import javax.xml.namespace.QName;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;
 

public class PathSerializer implements Serializer
{
    protected static Log log =
        LogFactory.getLog(PathSerializer.class.getName());

	public PathSerializer() {}
	
    /** Serialize a ConceptPath
     *
     * Walk the collection of name/id pairs, serializing each key/value pair
     * inside the previous element.
     *
     * @param name the desired QName for the element
     * @param attributes the desired attributes for the element
     * @param value the Object to serialize
     * @param context the SerializationContext in which to do all this
     * @exception IOException
     */
    public void serialize(QName name, Attributes attributes,
                          Object value, SerializationContext context)
        throws IOException
    {
    	System.out.println("Start serialization: " + name.toString());
        if (!(value instanceof ConceptPathVector))
            throw new IOException(
                Messages.getMessage("noConceptPathVector00", "ConceptPathSerializer", 
                                     value.getClass().getName()));

        ConceptPathVector path = (ConceptPathVector)value;
                
        context.startElement(name, attributes);
        for (Iterator i = path.iterator(); i.hasNext(); )
        {
        	// start with <ConceptPath>
            ConceptPath item = (ConceptPath)i.next();
        	if (!(item instanceof ConceptPath))
            throw new IOException(
                Messages.getMessage("noConceptPath00", "ConceptPathSerializer", 
                                     value.getClass().getName()));
            context.startElement(new QName("Concept"),null);
            context.serialize(new QName("name"),  null, item.getName());
            context.serialize(new QName("conceptId"),  null, item.getId());
        }
        for (int i = 0; i< path.size() + 1 ; i++ ) {
        	context.endElement();
        }
    }


    public String getMechanismType() { return Constants.AXIS_SAX; }

	/*
	 * The XML schema shows nested conceptIds.
	 */
	 
    public boolean writeSchema(Types types) throws Exception {
    	System.out.println("PathSerializer: Writing WSDL stuff:" + types.toString() );

        Element complexType = types.createElement("complexType");
        complexType.setAttribute("name", "path");

        types.writeSchemaElement(new QName("getSquareWS"), complexType);
        Element seq = types.createElement("sequence");
        complexType.appendChild(seq);

        Element element = types.createElement("element");
        element.setAttribute("name", "name");
        element.setAttribute("minOccurs", "1");
        element.setAttribute("maxOccurs", "1");
        element.setAttribute("type", "xsd:string");
        seq.appendChild(element);
        
        Element element2 = types.createElement("element");
        element2.setAttribute("name", "conceptId");
        element2.setAttribute("minOccurs", "1");
        element2.setAttribute("maxOccurs", "1");
        element2.setAttribute("type", "xsd:string");
        seq.appendChild(element2);

        Element element3 = types.createElement("element");
        element3.setAttribute("name", "concept");
        element3.setAttribute("minOccurs", "1");
        element3.setAttribute("maxOccurs", "1");
        element3.setAttribute("type", "urn:getSquare:path");
        seq.appendChild(element3);

        return true;
  
    }
}
