//
// XMLCreateInstance.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 XMLCreateInstance.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: XMLCreateInstance.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: XMLCreateInstance.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:37  karl
// import of SNIA 0_7
//
// Revision 1.10  2001/03/19 17:41:08  davidsi
// - Don't let instances be created unless the object path has at least
//   one key specified.
//
// Revision 1.9  2001/01/12 17:54:08  davidsi
// - Fixed bug whereby the namespace was not getting set in the path
//   being returned to the caller.
//
// Revision 1.8  2000/11/06 18:57:05  davidsi
// - From Michael Leizin (michael@epitera.com). Check that the instance
//   passed over the wire has all the keys correctly specified.
//
// Revision 1.7  2000/09/15 17:12:57  davidsi
// - createInstance() now returns a CIMObjectPath.
//
// 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:36:56  davidsi
// - Fixed parameter name for creation (was InstanceName rather than
//   NewInstance.
//
// Revision 1.2  2000/02/13 15:10:05  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;
import org.snia.wbem.cim.CIMProperty;
import org.snia.wbem.cim.CIMInstanceException;

public class XMLCreateInstance extends XMLClass {
    static String commandName="CreateInstance";


    public void execute(HTTPWriter pw,XMLNode node,CIMObjectPath path,CIMOMHandle hdl)
	    throws CIMException, MalformedURLException {
	String className;
	CIMInstance cimInstance;
    

	node.mustGetAttribute(paramName);
	
	node=node.getChild();
	if (node==null ) 
	    throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
				    "Could not find child on <IPARAMVALUE> element");
	cimInstance=new CIMInstance(node);
	// Check keys
        Vector keys=cimInstance.getKeyValuePairs();
	for(int i=0;i<keys.size();i++) {  //pick current instance
	    CIMProperty key=(CIMProperty)keys.elementAt(i);
	    if(key.getValue().getValue()==null){
		throw new CIMInstanceException(CIMInstanceException.CIM_ERR_MISSING_KEY,"Key must be provided!");
	    }
	}

	CIMObjectPath realPath=new CIMObjectPath(cimInstance.getClassName(),
						cimInstance.getKeyValuePairs());
	realPath.setNameSpace(path.getNameSpace());
	hdl.createInstance(realPath,cimInstance);
	//
	CIMObjectPath newpath=new CIMObjectPath(cimInstance.getClassName(),
						cimInstance.getKeyValuePairs());
	newpath.setNameSpace(path.getNameSpace());
	pw.println(newpath.toXml());
    }
    
    
    
    //
    //
    //
    public static CIMObjectPath createInstance(HTTPOutput output,
		   CIMObjectPath path,
		   CIMInstance cimInstance) throws CIMException {
	XMLNode result;
	String instanceDefn;
	String pathName=path.getObjectName();

	output.setMethod(commandName);
	
	//
	// Check that the path has specified at least one key (can't have
	// an instance without such keys)
	//
	Vector validKeys=path.getKeys();
	if (validKeys == null || validKeys.size()==0)
	    throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
			"The path (" + path +
			") for the instance does not specify any keys");
	
	try {
	    instanceDefn=cimInstance.toXml(null);
	} catch (MalformedURLException url) {
	    throw new CIMException(CIMException.CIM_ERR_FAILED,url);
	}
	
		
	result=intrinsicMethod(output,path,commandName,
		    "<IPARAMVALUE NAME=\"NewInstance\">"
		    +instanceDefn
		    +"</IPARAMVALUE>");
	if (result!=null) {
	    CIMObjectPath cimPath=new CIMObjectPath();
         
            try
            {
	        getInstanceName(result, cimPath);

	        // 
	        // We have to set the namespace on the basis of the path that
	        // was originally passed since it didn't come back over
	        // the wire.
	        //
	        cimPath.setNameSpace(path.getNameSpace());
	        return(cimPath);
            }
            catch (java.lang.NullPointerException npe)
            {
                // Attn: 09/16/02 - (Jenny) KeyBinding is missing.  Ignore
                //                  for now until we can return an appropriate
                //                  error.
	        // throw new CIMException(CIMException.CIM_ERR_FAILED,
		//	               "Bad XML response.");
	        cimPath.setNameSpace(path.getNameSpace());
	        return(cimPath);
            }
            catch (Exception e)
            {
	        throw new CIMException(CIMException.CIM_ERR_FAILED,
			               "Failed to create instance.");
            }
            
	} else
	    throw new CIMException(CIMException.CIM_ERR_FAILED,
			"Failed to create instance -  Empty response.");
    }	


}
