//
// XMLReferences.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 XMLReferences.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: XMLReferences.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: XMLReferences.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:59  karl
// import of SNIA 0_7
//
// Revision 1.9  2001/06/23 13:11:33  davidsi
// - Check for null keys before looking at size() (Jean Lamontagne)
//
// Revision 1.8  2000/11/06 19:17:55  davidsi
// - Reconciled error ids.
//
// Revision 1.7  2000/08/16 06:00:00  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.6.8.1  2000/08/10 00:03:36  shawn
// head merge
//
// Revision 1.6  2000/05/22 17:36:36  davidsi
// - Fixed handling of object paths.
//
// Revision 1.5  2000/05/11 10:11:25  davidsi
// - Fixed tabs.
//
// Revision 1.4  2000/05/06 17:20:05  davidsi
// - Brought in line with SUN WBEM APIs 2.2
//
// Revision 1.3  2000/04/09 14:46:15  davidsi
// - Changed "Name" attribute to "NAME".
//
// Revision 1.2  2000/02/13 15:10:08  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.client.CIMOMHandle;
import org.snia.wbem.cim.CIMInstance;
import org.snia.wbemcmd.utils.SimpleEnum;

public class XMLReferences extends XMLClass {
    static String commandName="References";
    static Vector allowedAttrs;
    
    
    static {
	//
	// Set up valid parameter names
	//
	allowedAttrs=new Vector();
	allowedAttrs.addElement(new XMLAttribute(paramName,XMLP_RESULTROLE));
	allowedAttrs.addElement(new XMLAttribute(paramName,XMLP_ROLE));
    }
    
    
    public void execute(HTTPWriter pw,XMLNode node,CIMObjectPath path,CIMOMHandle hdl) 
	    throws CIMException,MalformedURLException {
	String className;
	Enumeration enum;
	String[] propertyList;
	boolean includeQualifiers;
	boolean includeClassOrigin;

	String role=node.extractParameterValue(XMLP_ROLE,(String)null);
	propertyList=node.extractParameterStringArray(XMLP_PROPERTYLIST);
	includeQualifiers=node.extractParameterValue(XMLP_QUAL,true);
	includeClassOrigin=node.extractParameterValue(XMLP_ORIGIN,false);

		
	//
	// Look for ObjectName
	//
	XMLNode tmpNode=node;
	for(;tmpNode!=null;tmpNode=tmpNode.getNext()) {
	    if (tmpNode.getToken()!=XMLNode.XML_ELEMENT_IPARAMVALUE)
		continue;
	    String attr=tmpNode.getAttribute(paramName);
	    if (attr==null || !attr.equalsIgnoreCase(XMLP_OBJECTNAME))
		continue;
	    tmpNode=tmpNode.getChild();
	    if (tmpNode==null)
		throw new CIMException(CIMException.CIM_ERR_FAILED,
				"Needed a child for <IPARAMVALUE> when handling AssociatorNames");
	    int token=tmpNode.getToken();
	    if (token==XMLNode.XML_ELEMENT_CLASSNAME)
		path=new CIMObjectPath(tmpNode.mustGetAttribute("NAME"),
				       path.getNameSpace());
	    else if (token==XMLNode.XML_ELEMENT_INSTANCENAME)
		getInstanceName(tmpNode, path);
	    break;  
	}
	String resultClass=node.extractParameterValueAttr(XMLP_RESULTCLASS,XMLNode.XML_ELEMENT_CLASSNAME,
						   paramName);
	
	enum=hdl.references(path,resultClass,role,includeQualifiers,includeClassOrigin,propertyList);
	if (enum!=null) {
	    while (enum.hasMoreElements()) {
		CIMInstance ci=(CIMInstance)(enum.nextElement());
		pw.println("<VALUE.OBJECTWITHPATH>");
		CIMObjectPath cop=new CIMObjectPath(ci.getClassName(),ci.getKeyValuePairs());
		cop.setNameSpace(path.getNameSpace());
		pw.println(ci.toXml(cop,false,includeQualifiers,includeClassOrigin,propertyList));
		pw.println("</VALUE.OBJECTWITHPATH>");
	    }
	}
	return;
    }
    

    
    //
    //
    //
    public static Enumeration getNames(HTTPOutput output,
					CIMObjectPath path,
					String resultClass,
					String role,
					boolean includeQualifiers,
					boolean includeClassOrigin,
					String propertyList[]) throws CIMException {
	XMLNode result;
	Vector params=new Vector();
	String extra="";
	
	output.setMethod(commandName);
	
	if (role!=null)
	    params.addElement(new Param(XMLP_ROLE,role));
	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>"); 	    
	}
	
	if( (path.getKeys() != null) && (path.getKeys().size()>0) ) {
	    //
	    // Been passed an instance path
	    //
	    try {
		extra=extra.concat("<IPARAMVALUE NAME=\""+XMLP_OBJECTNAME+"\">"+
		    path.toXml()+
		    "</IPARAMVALUE>");
	    } catch (MalformedURLException mue) {
		throw new CIMException(CIMException.CIM_ERR_INVALID_PARAMETER,
				    mue);
	    }
	} else {
	    //
	    // Assume a classname
	    //
	    extra="<IPARAMVALUE NAME=\""+XMLP_OBJECTNAME+"\">"+
		"<CLASSNAME NAME=\""+path.getObjectName()+
		"\"></CLASSNAME></IPARAMVALUE>";
	}
	
	if (resultClass!=null) {
	    extra=extra.concat("<IPARAMVALUE NAME=\""+XMLP_RESULTCLASS+"\">"+
		    "<CLASSNAME NAME=\""+resultClass+
		    "\"></CLASSNAME></IPARAMVALUE>");
	}
	
	result=intrinsicMethod(output,path,commandName,params,extra);

	if (result==null) {
	    return(null);
	}
	
	//
	// Build enumeration
	//
	SimpleEnum se=new SimpleEnum();
	XMLNode tmp;
	Vector v=new Vector();
	for(result=result;result!=null;result=result.getNext()) {
	    v.removeAllElements();  
	    CIMObjectPath cop=getObjectWithPath(result,v);

	    //
	    // Size should always be 1..
	    //
	    if (cop!=null && v.size()>0) {
		if (v.size()>1) {
		    throw new CIMException(CIMException.CIM_ERR_FAILED,
			"getObjectWithPath returned multiple values");
		}
		Object obj=v.elementAt(0);
		if (obj instanceof CIMInstance) {// only care about instances
		    se.addElement((CIMInstance)obj);
		}
	    }
	}
	return(se);
	
    }	    
    
}
