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

import java.rmi.RemoteException;


public class ImpactDemo extends Demo {
    public static void main(String[] args) {
        UcmdbServiceStub serviceStub = getStub();
        calculateImpactAndGetImpactPathDemo(serviceStub);
        getImpactRulesByGroupName(serviceStub);
        getImpactRulesByNamePrefix(serviceStub);
    }

    public static void calculateImpactAndGetImpactPathDemo(UcmdbServiceStub serviceStub) {
        UcmdbServiceStub.CalculateImpactE requestE = new UcmdbServiceStub.CalculateImpactE();
        UcmdbServiceStub.CalculateImpact request = new UcmdbServiceStub.CalculateImpact();
        request.setCmdbContext(context);
        //set root cause ids
        UcmdbServiceStub.IDs ids = new UcmdbServiceStub.IDs();
        UcmdbServiceStub.ID id = new UcmdbServiceStub.ID();
        id.setString("4083c2037b6d7bd58e048f1117833309");
        id.setTemp(true);

        UcmdbServiceStub.ID[] idArray = new UcmdbServiceStub.ID[1];
        idArray[0] = id;
        ids.setID(idArray);
        request.setIDs(ids);
        //set impact category
        request.setImpactCategory("change");
        //set rule Names
        UcmdbServiceStub.ImpactRuleNames_type0 impactRuleNames = new UcmdbServiceStub.ImpactRuleNames_type0();
        UcmdbServiceStub.ImpactRuleName impactRuleName = new UcmdbServiceStub.ImpactRuleName();
        impactRuleName.setString("test");
        UcmdbServiceStub.ImpactRuleName[] impactRules = new UcmdbServiceStub.ImpactRuleName[1];
        impactRules[0] = impactRuleName;
        impactRuleNames.setImpactRuleName(impactRules);
        request.setImpactRuleNames(impactRuleNames);
        //set severity
        request.setSeverity(0);
        requestE.setCalculateImpact(request);
        try {
            UcmdbServiceStub.CalculateImpactResponseE responseE = serviceStub.calculateImpact(requestE);
            UcmdbServiceStub.CalculateImpactResponse response = responseE.getCalculateImpactResponse();
            UcmdbServiceStub.Topology topology = response.getImpactTopology();

            UcmdbServiceStub.CIs ci = topology.getCIs();
            UcmdbServiceStub.CI[] cis = ci.getCI();
            for (UcmdbServiceStub.CI confItem : cis) {
                System.out.println("CI type is " + confItem.getType() + " ,id is " + confItem.getID());
            }

            UcmdbServiceStub.Relations relations = topology.getRelations();
            UcmdbServiceStub.Relation[] rels = relations.getRelation();
            for (UcmdbServiceStub.Relation rel : rels) {
                System.out.println("Relation type is " + rel.getType() + " ,id is " + rel.getID());
            }


            System.out.println("End impact analysis");

            UcmdbServiceStub.Identifier identifier = response.getIdentifier();
            UcmdbServiceStub.Topology topology2 = response.getImpactTopology();
            UcmdbServiceStub.Relation relation = topology.getRelations().getRelation()[0];
            UcmdbServiceStub.GetImpactPathE request2E = new UcmdbServiceStub.GetImpactPathE();
            UcmdbServiceStub.GetImpactPath request2 = new UcmdbServiceStub.GetImpactPath();
            //set cmdb context
            request2.setCmdbContext(context);
            //set impact identifier
            request2.setIdentifier(identifier);
            //set shallowRelation
            UcmdbServiceStub.ShallowRelation shallowRelation = new UcmdbServiceStub.ShallowRelation();
            shallowRelation.setID(relation.getID());

            shallowRelation.setEnd1ID(relation.getEnd1ID());
            shallowRelation.setEnd2ID(relation.getEnd2ID());
            shallowRelation.setType(relation.getType());
            request2.setRelation(shallowRelation);
            request2E.setGetImpactPath(request2);

            UcmdbServiceStub.GetImpactPathResponseE response2E =
                    getStub().getImpactPath(request2E);
            UcmdbServiceStub.GetImpactPathResponse response2 = response2E.getGetImpactPathResponse();
            UcmdbServiceStub.ImpactTopology impactTopology = response2.getImpactPathTopology();
        } catch (RemoteException e) {
            //To change body of catch statement
            // use File | Settings | File Templates.
            e.printStackTrace();
        } catch (UcmdbFault e) {
            //To change body of catch statement
            // use File | Settings | File Templates.
            e.printStackTrace();
        }

    }

    public static void getImpactRulesByGroupName(UcmdbServiceStub serviceStub) {
        System.out.println("Start getImpactRulesByGroupName");
        UcmdbServiceStub.GetImpactRulesByGroupNameE requestE = new UcmdbServiceStub.GetImpactRulesByGroupNameE();
        UcmdbServiceStub.GetImpactRulesByGroupName request =
                new UcmdbServiceStub.GetImpactRulesByGroupName();
        //set cmdb context
        request.setCmdbContext(context);
        //set group names list
        String[] arrayList = new String[2];
        arrayList[0] = "other";
        arrayList[1] = "mui";
        request.setRuleGroupNameFilter(arrayList);
        requestE.setGetImpactRulesByGroupName(request);
        try {
            UcmdbServiceStub.GetImpactRulesByGroupNameResponseE responseE =
                    serviceStub.getImpactRulesByGroupName(requestE);

            UcmdbServiceStub.ImpactRules rules = responseE.getGetImpactRulesByGroupNameResponse().getImpactRules();
            UcmdbServiceStub.ImpactRule[] impacts = rules.getImpactRule();
            for (UcmdbServiceStub.ImpactRule impactRule : impacts) {
                System.out.println(impactRule.getRuleName());
            }
            System.out.println("End getImpactRulesByGroupName");
        } catch (RemoteException e) {
            //handle exception
        } catch (UcmdbFault e) {
            //handle exception
        }
    }

    public static void getImpactRulesByNamePrefix(UcmdbServiceStub serviceStub) {
        System.out.println("Start getImpactRulesByNamePrefix");
        UcmdbServiceStub.GetImpactRulesByNamePrefixE requestE = new UcmdbServiceStub.GetImpactRulesByNamePrefixE();
        UcmdbServiceStub.GetImpactRulesByNamePrefix request =
                new UcmdbServiceStub.GetImpactRulesByNamePrefix();
        //set cmdb context
        request.setCmdbContext(context);
        String[] arrayList = new String[1];
        arrayList[0] = "te";
        request.setRuleNamePrefixFilter(arrayList);
        requestE.setGetImpactRulesByNamePrefix(request);
        try {
            UcmdbServiceStub.GetImpactRulesByNamePrefixResponseE responseE =
                    getStub().getImpactRulesByNamePrefix(requestE);
            UcmdbServiceStub.ImpactRules rules = responseE.getGetImpactRulesByNamePrefixResponse().getImpactRules();
            UcmdbServiceStub.ImpactRule[] impacts = rules.getImpactRule();
            for (UcmdbServiceStub.ImpactRule impactRule : impacts) {
                System.out.println(impactRule.getRuleName());
            }
            System.out.println("End getImpactRulesByNamePrefix");
        } catch (RemoteException e) {
            //handle exception
        } catch (UcmdbFault e) {
            //handle exception
        }
    }
}