Express (Distributed operating systems), v8.0 > Secure applications and their environment > Authenticate users > Select a registry or repository > Manage realms in a federated repository > Virtual member manager > Develop with virtual member manager > Integrate virtual member manager into the application > Sample code


Sample code for searching for changed and deleted entities

Use the sample code snippet and data graphs to search for changed and deleted entities.

The following steps are covered in this sample scenario:

  1. Get the current checkpoint for the repository by using the ChangeResponseControl data object. This checkpoint is required because pass this checkpoint when searching for changed entities by using the ChangeControl data object (step 3), so that only entities changed since this checkpoint are returned.

    Important: There are two prerequisites when you use the ChangeControl and ChangeResponseControl data objects: We must set the value of supportChangeLog property for the repository to native, and enable the change tracking feature in the underlying repository. For more information see the topic, Search for changed entities.

  2. Delete an entity.
  3. Search for changed and deleted entities by specifying CHANGETYPE_ALL as the value for the CHANGETYPES property. In this scenario, the deleted entity is returned. For more information about changed entities, see the topic Search for changed entities. For end-to-end sample code for searching for changed entities, see the topic Sample code for searching for changed entities.


Prerequisites

Ensure that we have read the information and completed the steps described in the topic, Program prerequisites.

Entities must exist in the repository before running the methods given in the following sample code.


Sample code

Add the following sample code to the application code and replace the variables with the actual values to use.

    /**
     *  This test deletes an entity and then
     *  does a search for changed entities
     */
    public static void testScenario() throws Exception
    {
        // Get the current checkpoint for the repository         // This is required before we do any updates on the entities
        // or delete any entities, so that it can be passed back when
        // we do a search to retrieve entities changed or deleted since this checkpoint
        DataObject prevRoot = getCurrentCheckPoint();
        //Delete the entity
        deleteEntity("uid=SalesManager2,o=SalesManager,dc=yourco,dc=com");
        // Search for the changed entity
        DataObject searchRoot = searchChangedEntities(prevRoot);
    }

     /**
     * deleteEntity to perform a delete operation
      *@param uniqueName uniqueName of the entity that needs to be deleted
     * @return DataObject after delete operation
      *@throws Exception
     */
    public static DataObject deleteEntity(String uniqueName) throws Exception
    {
        DataObject root = SDOHelper.createRootDataObject();
        DataObject entity = SDOHelper.createEntityDataObject(root, null, SchemaConstants.DO_ENTITY);
        DataObject ctrl = SDOHelper.createControlDataObject(root, null, SchemaConstants.DO_DELETE_CONTROL);
        // Set the properties on Delete Control dataObject
        ctrl.setBoolean(SchemaConstants.PROP_DELETE_DESCENDANTS, true);
        ctrl.setBoolean(SchemaConstants.PROP_RETURN_DELETED, true);
        entity.createDataObject(SchemaConstants.DO_IDENTIFIER).set(SchemaConstants.PROP_UNIQUE_NAME,
                uniqueName);
        root  = service.delete(root);
        return root;
    }

    /**
     * Get the current check point
     */
    public static DataObject getCurrentCheckPoint() throws Exception
    {
        DataObject root = SDOHelper.createRootDataObject();
        DataObject ctrl = SDOHelper.createControlDataObject(root, SchemaConstants.WIM_NS_URI,
                SchemaConstants.DO_CHANGE_CONTROL);   
        DataObject result = service.search(root);
        System.out.println("Check Point: "+ printDO(result));
        return result;
    }

    /**
     *  searchChangedEntities search the entities that have changed
     *  @param prevRoot DataObject the represents the previous checkpoint
     *  @return DataObject with search results
     *  @throws Exception
     */
    public static DataObject searchChangedEntities(DataObject prevRoot) throws Exception
    {
        DataObject root = SDOHelper.createRootDataObject();
        DataObject ctrl = SDOHelper.createControlDataObject(root, SchemaConstants.WIM_NS_URI,
                SchemaConstants.DO_CHANGE_CONTROL);
        // Set the search expression on the control data object
ctrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, "@xsi:type='PersonAccount'");
        // Set the properties that have to be retrieved in search results
        ctrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
        ctrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn");
        ctrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn");
        // Set the CHANGETYPE_ALL to search for changed as well as deleted entities
        ctrl.getList(SchemaConstants.PROP_CHANGETYPES).add(SchemaConstants.CHANGETYPE_ALL);
        SDOHelper.createChangeCtrlFromChangeRespCtrl(root, prevRoot);
        System.out.println("Input datagraph before searching for changed entities"+ printDO(root));
        DataObject results = service.search(root);
        System.out.println("output datagraph after searching changed entities"+ printDO(results));
        return results;
    }


Input and output data graphs

Output data graph after retrieving the checkpoint:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:controls xsi:type="wim:ChangeResponseControl">
<wim:checkPoint>  
<wim:repositoryId>ldapRepo
</wim:repositoryId>  
<wim:repositoryCheckPoint>59
</wim:repositoryCheckPoint>
</wim:checkPoint>
</wim:controls>
</wim:Root>
</sdo:datagraph> 

Input data graph for searching for changed and deleted entities with * (CHANGETYPE_ALL) as the value for the CHANGETYPES property:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:controls xsi:type="wim:ChangeControl" expression="@xsi:type='PersonAccount'">
<wim:properties>uid
</wim:properties>
<wim:properties>cn
</wim:properties>
<wim:properties>sn
</wim:properties>
<wim:checkPoint>  
<wim:repositoryId>ldapRepo
</wim:repositoryId>  
<wim:repositoryCheckPoint>59
</wim:repositoryCheckPoint>
</wim:checkPoint>
<wim:changeTypes>*
</wim:changeTypes>
</wim:controls>
</wim:Root>
</sdo:datagraph> 

Output data graph after searching for changed and deleted entities:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities>
<wim:identifier externalName="uid=SalesManager2,o=SalesManager,dc=yourco,dc=com"
          repositoryId="ldapRepo" uniqueId="de158d76-f710-44cc-9e21-747baf2f8944"/>
<wim:changeType>delete
</wim:changeType>
</wim:entities>
<wim:controls xsi:type="wim:ChangeResponseControl">
<wim:checkPoint>  
<wim:repositoryId>ldapRepo
</wim:repositoryId>  
<wim:repositoryCheckPoint>60
</wim:repositoryCheckPoint>
</wim:checkPoint>
</wim:controls>
</wim:Root>
</sdo:datagraph> 

Parent topic: Sample code



+

Search Tips   |   Advanced Search