//
// XMLGetInstance.java
//
// The contents of this file are subject to the SNIA Public License Version 1.0
// (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
//    http://www.snia.org/resources/openSource.html
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the 
// License for the specific language governing rights and limitations
// under the License.
//
// The Original Code is XMLGetInstance.java
// 
// The Initial Developer of the Original Code is David Simons.
//
// Contributor(s):
//
// This source uses the Solaris WBEM APIs version 2.2. 
// Synchronization will be maintained with the Solaris WBEM APIS,
// which are being standardized through the Java Community Process.
//
//
// $Id: XMLGetInstance.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: XMLGetInstance.java,v $
// Revision 1.1  2003/07/17 18:32:55  kumpf
// HP-BAPU: PEP-64 CIM Client and CIM Listener Java Interface
//
// Revision 1.1.1.1  2001/10/31 18:17:45  karl
// import of SNIA 0_7
//
// Revision 1.8  2000/11/12 14:53:14  davidsi
// - No longer output instancename - its not needed.
// - Added support for <VALUE.OBJECT>.
//
// Revision 1.7  2000/11/06 19:32:12  davidsi
// - Added support for <VALUE_NAMEDOBJECT>.
//
// Revision 1.6  2000/08/16 05:59:59  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.5.8.1  2000/08/10 00:03:36  shawn
// head merge
//
// Revision 1.5  2000/05/11 10:11:24  davidsi
// - Fixed tabs.
//
// Revision 1.4  2000/05/06 17:20:03  davidsi
// - Brought in line with SUN WBEM APIs 2.2
//
// Revision 1.3  2000/04/09 14:41:07  davidsi
// - Removed VALUE.NAMEDOBJECT elements.
//
// Revision 1.2  2000/02/13 15:10:06  davidsi
// - Added text to cover re-use of Solaris WBEM APIs.
//
// Revision 1.1.1.1  2000/02/06 16:41:02  davidsi
// Initial version.
//
//
//
package org.snia.wbemcmd.xml;

import java.util.Enumeration;
import java.util.Vector;
import java.net.MalformedURLException;
import org.snia.wbem.cim.CIMException;
import org.snia.wbem.cim.CIMObjectPath;
import org.snia.wbem.cim.CIMInstance;
import org.snia.wbem.client.CIMOMHandle;



public class XMLGetInstance extends XMLClass 
{
    static String commandName="GetInstance";

    
    public void execute(HTTPWriter pw, XMLNode node, CIMObjectPath path,
        CIMOMHandle hdl) throws CIMException, MalformedURLException 
    {
        String[] propertyList;
        boolean localOnly;
        boolean includeQualifiers;
        boolean includeClassOrigin;
        CIMInstance cimInstance;
        
        
        propertyList=node.extractParameterStringArray(XMLP_PROPERTYLIST);
        localOnly=node.extractParameterValue(XMLP_LOCAL,true);
        includeQualifiers=node.extractParameterValue(XMLP_QUAL,true);
        includeClassOrigin=node.extractParameterValue(XMLP_ORIGIN,false);

        for(;;) 
        {
            if (node==null)
                throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
                    "Could not find a parameter named "+XMLP_INSTANCENAME);
        
            node=node.mustFindElement(XMLNode.XML_ELEMENT_IPARAMVALUE);
            String name=node.getAttribute(paramName);
            if (name!=null && name.equalsIgnoreCase(XMLP_INSTANCENAME) )
            break;
            node=node.getNext();
        }
        node=node.getChild();
        if (node==null) 
            throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
                    "Could not find an element with <IPARAMVALUE>");
               
        getInstanceName(node, path);

        //cimInstance=hdl.getInstance(path,localOnly);
        cimInstance = hdl.getInstance(path, localOnly, includeQualifiers, 
            includeClassOrigin, propertyList);

        if (cimInstance==null)
            throw new CIMException(CIMException.CIM_ERR_NOT_FOUND,
            "Path=" + path);
        
    //
    //      Only need an instance - the name is not necessary 
    //    CIMObjectPath cop=new CIMObjectPath(cimInstance.getClassName(),cimInstance.getKeyValuePairs());
    //    cop.setNameSpace(path.getNameSpace());
    //    pw.println(cop.toXml());

        pw.println(cimInstance.toXml(null, localOnly, 
            includeQualifiers, includeClassOrigin, propertyList));
        return;
    }
    
    
    public static CIMInstance getInstance(HTTPOutput output,
        CIMObjectPath path, boolean localOnly) throws CIMException 
    {
        return(XMLGetInstance.getInstance(output, path, localOnly, false, false, null));
    }

    public static CIMInstance getInstance(
        HTTPOutput output,
        CIMObjectPath path,
        boolean localOnly,
        boolean includeQualifiers,
        boolean includeClassOrigin,
        String propertyList[]) throws CIMException
    {
        XMLNode result;
        Vector params=new Vector();
        XMLAttribute attr;
        String extra="";

        output.setMethod(commandName);
        //
        // Always request qualifiers
        //
        params.addElement(new Param(XMLP_LOCAL,localOnly));
        params.addElement(new Param(XMLP_QUAL, includeQualifiers));
        params.addElement(new Param(XMLP_ORIGIN, includeClassOrigin));

        if (propertyList != null) 
        {
            extra="<IPARAMVALUE NAME=\""+XMLP_PROPERTYLIST+"\"><VALUE.ARRAY>";
            for(int i=0;i<propertyList.length;i++)
                extra=extra.concat("<VALUE>"+propertyList[i]+"</VALUE>");
            extra=extra.concat("</VALUE.ARRAY></IPARAMVALUE>");         
        }

        //result=intrinsicMethod(output,path,commandName,params,
        //               instanceNameToKey(path,"InstanceName"));
        result = intrinsicMethod(output, path, commandName, params,
                       instanceNameToKey(path, "InstanceName") + extra);
            
        if (result==null) 
        {
            return(null);
        }
        
        // Now find XML_ELEMENT_VALUE_NAMEDOBJECT element which
        // shouldn't be comming back but some implementations
        // insist on using it
        if ((result.getToken()==XMLNode.XML_ELEMENT_VALUE_NAMEDOBJECT)
            || (result.getToken()==XMLNode.XML_ELEMENT_VALUE_NAMEDINSTANCE)) {
            XMLNode lresult=result.getChild();
            if ( lresult==null 
             || lresult.getToken()!=XMLNode.XML_ELEMENT_INSTANCENAME) {
            throw new CIMException(CIMException.XMLERROR,
                            "XMLGetInstance.getInstance",
                            "Need <INSTANCENAME>");
            }
            // getInstanceName(tmpNode, path);
            lresult=lresult.getNext();
            if (lresult==null)        
            throw new CIMException(CIMException.XMLERROR,
                        "XMLGetInstance.getInstance",
                        "Need <INSTANCE> - no node");

            lresult=lresult.findElement(XMLNode.XML_ELEMENT_INSTANCE);
            if (lresult==null) {
            throw new CIMException(CIMException.XMLERROR,
                    "XMLGetInstance.getInstance",
                    "Need <INSTANCE> - no node found");
            }  
            return(new CIMInstance(lresult));
        }
        
        if (result==null) {
            return(null);
        }
        if (result.getToken()==XMLNode.XML_ELEMENT_VALUE_OBJECT)
            result=result.getChild();
        result=result.mustFindElement(XMLNode.XML_ELEMENT_INSTANCE);    
        CIMInstance cimInstance=new CIMInstance(result);
        return(cimInstance);
    }
}
