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

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;


public class UpdateDemo extends Demo {
    public static void main(String[] args) {
        UcmdbServiceStub serviceStub = getStub();
        getAddCIsAndRelationsDemo(serviceStub);
        getUpdateCIsAndRelationsDemo(serviceStub);
        getDeleteCIsAndRelationsDemo(serviceStub);
    }


    public static void getDeleteCIsAndRelationsDemo(UcmdbServiceStub serviceStub) {
        UcmdbServiceStub.DeleteCIsAndRelationsE requestE = new UcmdbServiceStub.DeleteCIsAndRelationsE();
        UcmdbServiceStub.DeleteCIsAndRelations request = new UcmdbServiceStub.DeleteCIsAndRelations();
        request.setCmdbContext(context);
        UcmdbServiceStub.CIsAndRelationsUpdates updates = new UcmdbServiceStub.CIsAndRelationsUpdates();
        UcmdbServiceStub.CIs cis = new UcmdbServiceStub.CIs();
        List<UcmdbServiceStub.CI> listCI = new ArrayList<UcmdbServiceStub.CI>();
        UcmdbServiceStub.CI ci = new UcmdbServiceStub.CI();
        UcmdbServiceStub.ID id = new UcmdbServiceStub.ID();

        id.setString("temp1");
        id.setTemp(true);
        ci.setID(id);
        ci.setType("node");
        UcmdbServiceStub.CIProperties props = new UcmdbServiceStub.CIProperties();
        UcmdbServiceStub.StrProps strProps = new UcmdbServiceStub.StrProps();
        UcmdbServiceStub.StrProp strProp1 = new UcmdbServiceStub.StrProp();
        strProp1.setName("name");
        String value1 = "blabla";
        strProp1.setValue(value1);
        strProps.addStrProp(strProp1);
        props.setStrProps(strProps);
        ci.setProps(props);
        listCI.add(ci);
        Object[] ciArray = listCI.toArray(new UcmdbServiceStub.CI[listCI.size()]);
        cis.setCI((UcmdbServiceStub.CI[]) ciArray);
        updates.setCIsForUpdate(cis);

        request.setCIsAndRelationsUpdates(updates);
        requestE.setDeleteCIsAndRelations(request);
        try {
            serviceStub.deleteCIsAndRelations(requestE);
        } catch (RemoteException e) {
            //handle exception
        } catch (UcmdbFault e) {
            //handle exception
        }
    }

    public static void getUpdateCIsAndRelationsDemo(UcmdbServiceStub serviceStub) {
        UcmdbServiceStub.UpdateCIsAndRelationsE requestE = new UcmdbServiceStub.UpdateCIsAndRelationsE();
        UcmdbServiceStub.UpdateCIsAndRelations request = new UcmdbServiceStub.UpdateCIsAndRelations();

        request.setCmdbContext(context);
        UcmdbServiceStub.CIsAndRelationsUpdates updates = new UcmdbServiceStub.CIsAndRelationsUpdates();
        UcmdbServiceStub.CIs cis = new UcmdbServiceStub.CIs();
        List<UcmdbServiceStub.CI> listCI = new ArrayList<UcmdbServiceStub.CI>();
        UcmdbServiceStub.CI ci = new UcmdbServiceStub.CI();
        UcmdbServiceStub.ID id = new UcmdbServiceStub.ID();
        //id of an existing CI
        id.setString("4337dd3bf548f766b1ddf4df7319b38d");
        id.setTemp(true);
        ci.setID(id);
        //here I updated the type
        ci.setType("router");
        UcmdbServiceStub.CIProperties props = new UcmdbServiceStub.CIProperties();
        UcmdbServiceStub.StrProps strProps = new UcmdbServiceStub.StrProps();
        //setting a property that CI must have, this property is used to identify the CI
        //here we set name to 'newName'
        //if there is an CI with the following attributes and the above id then its type will be updated to router
        UcmdbServiceStub.StrProp nameProp = new UcmdbServiceStub.StrProp();
        nameProp.setName("name");
        String hostKeyValue = "newName";
        nameProp.setValue(hostKeyValue);
        strProps.addStrProp(nameProp);
        props.setStrProps(strProps);
        ci.setProps(props);
        listCI.add(ci);
        Object[] ciArray = listCI.toArray(new UcmdbServiceStub.CI[listCI.size()]);
        cis.setCI((UcmdbServiceStub.CI[]) ciArray);
        updates.setCIsForUpdate(cis);
        request.setCIsAndRelationsUpdates(updates);
        requestE.setUpdateCIsAndRelations(request);
        try {
            UcmdbServiceStub.UpdateCIsAndRelationsResponseE responseE = serviceStub.updateCIsAndRelations(requestE);
            UcmdbServiceStub.UpdateCIsAndRelationsResponse response = responseE.getUpdateCIsAndRelationsResponse();
            UcmdbServiceStub.ClientIDToCmdbID[] ids = response.getCreatedIDsMap();
            for (int i = 0; i < ids.length; i++) {
                UcmdbServiceStub.ClientIDToCmdbID idMap = ids[i];
                System.out.println(idMap.getCmdbID().toString() + " is the id");
                //do something
            }
            System.out.println("end update CIS");
        } catch (RemoteException e) {

        } catch (UcmdbFault e) {
            System.out.println(e.toString());

        }
    }


    public static void getAddCIsAndRelationsDemo(UcmdbServiceStub serviceStub) {
        UcmdbServiceStub.AddCIsAndRelationsE requestE = new UcmdbServiceStub.AddCIsAndRelationsE();
        UcmdbServiceStub.AddCIsAndRelations request = new UcmdbServiceStub.AddCIsAndRelations();
        request.setCmdbContext(context);
        request.setUpdateExisting(true);
        UcmdbServiceStub.CIsAndRelationsUpdates updates = new UcmdbServiceStub.CIsAndRelationsUpdates();
        UcmdbServiceStub.CIs cis = new UcmdbServiceStub.CIs();
        List<UcmdbServiceStub.CI> listCI = new ArrayList<UcmdbServiceStub.CI>();
        UcmdbServiceStub.CI ci = new UcmdbServiceStub.CI();
        UcmdbServiceStub.ID id = new UcmdbServiceStub.ID();
        id.setString("temp1");
        id.setTemp(true);
        ci.setID(id);
        ci.setType("node");
        UcmdbServiceStub.CIProperties props = new UcmdbServiceStub.CIProperties();
        UcmdbServiceStub.StrProps strProps = new UcmdbServiceStub.StrProps();
        UcmdbServiceStub.StrProp strProp = new UcmdbServiceStub.StrProp();
        strProp.setName("name");
        String value = "newCI";
        strProp.setValue(value);
        strProps.addStrProp(strProp);
        props.setStrProps(strProps);
        ci.setProps(props);
        listCI.add(ci);
        Object[] ciArray = listCI.toArray(new UcmdbServiceStub.CI[listCI.size()]);
        cis.setCI((UcmdbServiceStub.CI[]) ciArray);
        updates.setCIsForUpdate(cis);
        request.setCIsAndRelationsUpdates(updates);
        requestE.setAddCIsAndRelations(request);
        try {
            UcmdbServiceStub.AddCIsAndRelationsResponseE responseE = serviceStub.addCIsAndRelations(requestE);
            UcmdbServiceStub.AddCIsAndRelationsResponse response = responseE.getAddCIsAndRelationsResponse();
            UcmdbServiceStub.ClientIDToCmdbID[] ids = response.getCreatedIDsMap();
            for (int i = 0; i < ids.length; i++) {
                UcmdbServiceStub.ClientIDToCmdbID idMap = ids[i];
                System.out.println(idMap.getCmdbID().toString());
                //do something
            }
        } catch (RemoteException e) {
            //handle exception
        } catch (UcmdbFault e) {
            //handle exception
        }
    }
}