import com.hp.ucmdb.generated.UcmdbFault;
import com.hp.ucmdb.generated.UcmdbServiceStub;

import java.net.MalformedURLException;
import java.rmi.RemoteException;


public class ClassModelDemo extends  Demo{
    public static void main(String[] args) throws RemoteException, UcmdbFault, MalformedURLException {
        UcmdbServiceStub serviceStub = getStub();
        getClassAncestorsDemo(serviceStub);
        getCmdbClassDefinitionDemo(serviceStub);
        getAllClassesHierarchyDemo(serviceStub);

    }

    public static void getAllClassesHierarchyDemo(UcmdbServiceStub serviceStub) {
        System.out.println("start get all classes hierarchy");
        UcmdbServiceStub.GetAllClassesHierarchyE requestE = new UcmdbServiceStub.GetAllClassesHierarchyE();
        UcmdbServiceStub.GetAllClassesHierarchy request =
                new UcmdbServiceStub.GetAllClassesHierarchy();
        request.setCmdbContext(context);
        requestE.setGetAllClassesHierarchy(request);
        try {
            UcmdbServiceStub.GetAllClassesHierarchyResponseE responseE =
                    serviceStub.getAllClassesHierarchy(requestE);
            UcmdbServiceStub.GetAllClassesHierarchyResponse response = responseE.getGetAllClassesHierarchyResponse();
            UcmdbServiceStub.UcmdbClassModelHierarchy hierarchy =
                    response.getClassesHierarchy();
            for(UcmdbServiceStub.UcmdbClassHierarchyNode node:hierarchy.getClassHierarchyNode())
            { System.out.println("CI type:" + node.getClassNames().getClassName() + " with parent "+ node.getClassParentName());}
            System.out.println("end get all classes hierarchy");
        } catch (RemoteException e) {
//handle exception
        } catch (UcmdbFault e) {
//handle exception
        }
    }
    public static void getClassAncestorsDemo(UcmdbServiceStub serviceStub) {
        UcmdbServiceStub.GetClassAncestorsE getClassAncestorsE = new UcmdbServiceStub.GetClassAncestorsE();
        UcmdbServiceStub.GetClassAncestors getClassAncestors = new UcmdbServiceStub.GetClassAncestors();

        getClassAncestors.setCmdbContext(context);
        getClassAncestors.setClassName("unix");
        getClassAncestorsE.setGetClassAncestors(getClassAncestors);
        try {
            UcmdbServiceStub.GetClassAncestorsResponseE responseE =
                    serviceStub.getClassAncestors(getClassAncestorsE);
            UcmdbServiceStub.GetClassAncestorsResponse response = responseE.getGetClassAncestorsResponse();

            UcmdbServiceStub.UcmdbClassModelHierarchy hierarchy = response.getClassHierarchy();

            for (int i = 0; i < hierarchy.getClassHierarchyNode().length; i++) {
                System.out.println("### Level  " + i + " class is " + hierarchy.getClassHierarchyNode()[i].getClassParentName());

            }

        } catch (RemoteException e) {
            //handle exception
        } catch (UcmdbFault e) {
            //handle exception
        }

    }
    public static void getCmdbClassDefinitionDemo(UcmdbServiceStub serviceStub) {
        UcmdbServiceStub.GetCmdbClassDefinitionE getCmdbClassDefinitionE =
                new UcmdbServiceStub.GetCmdbClassDefinitionE();
        UcmdbServiceStub.GetCmdbClassDefinition getCmdbClassDefinition =
                new UcmdbServiceStub.GetCmdbClassDefinition();
        getCmdbClassDefinition.setCmdbContext(context);
        getCmdbClassDefinition.setClassName("node");
        getCmdbClassDefinitionE.setGetCmdbClassDefinition(getCmdbClassDefinition);
        try {
            UcmdbServiceStub.GetCmdbClassDefinitionResponseE responseE =
                    serviceStub.getCmdbClassDefinition(getCmdbClassDefinitionE);
            UcmdbServiceStub.GetCmdbClassDefinitionResponse response = responseE.getGetCmdbClassDefinitionResponse();

            UcmdbServiceStub.UcmdbClass ucmdbClass = response.getUcmdbClass();
            System.out.println("$$$ Class  " + ucmdbClass.getDisplayLabel() + " has description " + ucmdbClass.getDescription());
        } catch (RemoteException e) {
            System.out.println(e.toString());
        } catch (UcmdbFault e) {
            System.out.println(e.toString());
            //handle exception
        }
    }
}