Moving a Resource to a Different LDAP Container — Service Connector API Example (Java)

Example code is provided that illustrates moving a resource to a different LDAP container using method calls available in the Service Connector API.

The step numbers in the comments correspond to the steps in the illustration, Moving a Resource to a Different LDAP Container.

public XmlResourceDetail moveResourceToAnotherContainer(int aModelVersion)
                    throws DirectoryEngineFault, SecurityFault
    {
        // Step 1: perform some search on resources
        FindResources findResources = FindResources.Factory.newInstance();
        findResources.setModelVersion(aModelVersion);
        findResources.addResourceName("Clint Hill");
        FindResourcesResponse resources = getOrgResourceService().findResources(findResources);
        // select a resource from list
        XmlModelEntity resource = resources.getResourceArray(0);
        // Step 2: get details of the selected resource
        String[] guids = { resource.getGuid() };
        XmlResourceDetail resourceDetail = getOrgResourceService().getResource(aModelVersion, guids)[0];
        // Step 3: list configured LDAP Containers for selection of destination
        XmlContainer[] ldapContainers = getDirectoryService().listContainers();
        // select an LDAP Container
        XmlContainer ldapContainer = ldapContainers[0];
        // Step 4: get list of non-existing candidates from selected LDAP Container
        ListCandidateResourcesResponse candidates =
            getDirectoryService().listCandidateResources(ldapContainer.getId(), XmlCandidateChoice.NON_EXISTING);
        // select a candidate as final destination
        XmlResourceCandidate candidate = candidates.getCandidateArray(0);
        // Step 5: update selected resource with new LDAP properties
        XmlUpdateResourceType[] update = { XmlUpdateResourceType.Factory.newInstance() };
        update[0].setGuid(resourceDetail.getGuid());
        // copy details of LDAP candidate
        LdapReference ldapRef = update[0].addNewLdapReference();
        ldapRef.setContainerId(ldapContainer.getId());
        ldapRef.setLdapAlias(candidate.getLdapAlias());
        ldapRef.setLdapDn(candidate.getLdapDn());
        ldapRef.setSecondaryRefArray(candidate.getSecondaryRefArray());
        XmlResourceDetail[] response = getOrgResourceService().updateResource(update);
        return(response[0]);
    }
}