package tests.ProviderClientTest;

import java.io.*;
import org.snia.wbem.client.CIMClient;
import org.snia.wbem.cim.*;
import java.util.*;

public class WBEMValue
{

	Properties wbemProps = new Properties();
	DataInputStream in;


	public WBEMValue()
	{
	}


	public void setNamespace(String nameSpace)
	{
	}

	public String getPropertyValue(CIMClient cc, String nameSpace, String className, String propName)
	{
		CIMProperty valProp;
		CIMInstance currentInstance;
		CIMObjectPath currentInstancePath;
		CIMValue value = null;

		try
		{
			System.out.println("WBEMValue::getPropertyValue::"+className+"::"+propName);
			// 1) get the path
			CIMObjectPath pa1 = new CIMObjectPath(className, nameSpace);
			//
			// 2) enumerate the instances
			Enumeration instances =cc.enumInstances(new CIMObjectPath(className,
						nameSpace), CIMClient.DEEP);
			int instCount = 0;
			while (instances.hasMoreElements())
			{
				Object obj2=instances.nextElement();
				//
				// 3) get value of the property for this instance
				currentInstancePath=(CIMObjectPath)obj2;
				currentInstance=cc.getInstance(currentInstancePath,false);
				//System.out.println("WBEMValue::getPropertyValue::getting property: "+propName);
				valProp = currentInstance.getProperty(propName);
				try
				{
					value = valProp.getValue();

				// BAPU
				System.out.println("WBEMValue::getPropertyValue::value: "+value.toString());
					if (value != null && value.getValue() != null)
						return(value.toString());
					else
						return(null);
				}

				catch (NullPointerException nex)
				{
					return(null);
				}
			}
		}
		catch (CIMException e)
		{
			System.out.println("WBEMValue::getPropertyValue::Enum class exception: "+e);
			return(null);
		}
		catch (NullPointerException ne)
		{
			System.out.println("Null Pointer Encountered....proceed");
			return(null);
		}

		return(value.toString());

	}
	public Vector getPropertyValues(CIMClient cc, String nameSpace, String className, String propName)
	{
		CIMProperty valProp;
		CIMInstance currentInstance;
		CIMObjectPath currentInstancePath;
		CIMValue value = null;

		Vector properties = new Vector();

		try
		{
			System.out.println("WBEMValue::getPropertyValue::"+className+"::"+propName);
			// 1) get the path
			CIMObjectPath pa1 = new CIMObjectPath(className, nameSpace);
			//
			// 2) enumerate the instances
			Enumeration instances =cc.enumInstances(new CIMObjectPath(className,
						nameSpace), CIMClient.DEEP);
			int instCount = 0;
			while (instances.hasMoreElements())
			{
				Object obj2=instances.nextElement();
				//
				// 3) get value of the property for this instance
				currentInstancePath=(CIMObjectPath)obj2;
				currentInstance=cc.getInstance(currentInstancePath,false);
				//System.out.println("WBEMValue::getPropertyValue::getting property: "+propName);
				valProp = currentInstance.getProperty(propName);
				try
				{
					value = valProp.getValue();

					if (value != null && value.getValue() != null)
						properties.addElement(value.toString());
					else
						properties.addElement("null");
				}

				catch (NullPointerException nex)
				{
					return(properties);
				}
			}
		}
		catch (CIMException e)
		{
			System.out.println("WBEMValue::getPropertyValue::Enum class exception: "+e);
			return(properties);
		}
		catch (NullPointerException ne)
		{
			System.out.println("Null Pointer Encountered....proceed");
			return(properties);
		}

		return(properties);

	}

	public Vector getClassProperties(CIMClient cc, String nameSpace, String className)
	{
		Vector properties=null;

		try
		{
			// 1) get the path
			CIMObjectPath pa1 = new CIMObjectPath(className, nameSpace);
			//CIMObjectPath pa1 = new CIMObjectPath(className);
			//
			// 2) get the class
			CIMClass sysClass = cc.getClass(pa1, false);
			//System.out.println("System Class Properties: "+sysClass.numberOfProperties());
			//
			// 3) get all the properties
			properties = sysClass.getAllProperties();
			//
			// 4) enumerate the class instances
			Enumeration instances =cc.enumInstances(new CIMObjectPath(className,
						nameSpace), CIMClient.DEEP);

			while(instances.hasMoreElements())
			{
				int instCount = 0;
				Object obj2=instances.nextElement();
				//
				// 5) get value of the property for this instance
				CIMObjectPath currentInstancePath=(CIMObjectPath)obj2;
				//
				// 6) get the current instance to "unroll"
				CIMInstance currentInstance=cc.getInstance(currentInstancePath,false);

				for (int i = 0; i < properties.size(); i++)
				{
					CIMProperty cp = (CIMProperty)(properties.elementAt(i));
					String propName = cp.getName();
					//System.out.print("Property: "+propName+" ");


					CIMProperty valProp = currentInstance.getProperty(propName);
					try
					{
						CIMValue value = valProp.getValue();
						//if (value != null && value.getValue() != null)
						//	System.out.println("	Value: "+value.toString());
						//else
						//	System.out.println("	Value: NULL");
					}
					catch (NullPointerException nex)
					{
						System.out.println("	Value: NULL");
					}
				}
			}

		}
		catch (CIMException e)
		{
			System.out.println("WBEMValue::getClassProperties::Enum class exception: "+e);
		}
		catch (NullPointerException ne)
		{
			System.out.println("Null Pointer Encountered....proceed");
		}

		return(properties);

	}

	public void dumpProperties()
	{
		Enumeration enum = wbemProps.propertyNames();
		while(enum.hasMoreElements())
		{
			String object = (String)enum.nextElement();
			System.out.println("Object name: "+object);
			String property = (wbemProps.getProperty(object));
			System.out.println("Object: "+object+" Property: "+property);
		}
	}


}
