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 entities

Use the end-to-end sample code and data graphs for search operations on changed entities.

The following steps are covered in this sample scenario:

  1. Create two entities.
  2. 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 5), 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.

  3. Update one of the entities.
  4. Get properties of the updated entity.
  5. Search for changed entities by using the ChangeControl and ChangeResponseControl data object.


Prerequisites

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


Sample code

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

public class SearchSample extends BaseApp
{
    private static String ejbJndiName = "ejb/com/ibm/websphere/wim/ejb/WIMServiceHome";

    /**
     *  testScenario
     *  This test scenario does the following:
     *  Creates two entities
     *  Gets the current checkpoint for the repository      *  Updates one of the entities
     *  Gets the properties of the updated entity
     *  Searches for changed entities
     */
    public static void main(String[] args) throws Exception
    {
        // Locate the service
service = locateService(ejbJndiName);
        // Create two users with uid user1 and user2 respectively
        DataObject root1 = addPersonAccount("user1","Admin","AdminSn");
        DataObject root2 = addPersonAccount("user2","Operator","OperatorSn");
        // Get the current checkpoint for the repository         // This is required before we do any updates on the entities
        // so that it can be passed back when we do a search
        // to retrieve entities changed since this checkpoint
        DataObject prevRoot = getCurrentCheckPoint();
        // Update entity user1
        DataObject updatedRoot = updatePersonAccount(root1, "Manager");
        // Get the properties of the updated entity
        DataObject getRoot = getPersonAccount(updatedRoot);
        // Search for the changed entity
        DataObject searchRoot = searchChangedEntities(prevRoot);
    }

    /**
     *  addPersonAccount
     *  Adds an entity of PersonAccount entity type
      * @param uid value to be set
      * @param cn value to be set
      * @param sn value to be set
      * @return DataObject
     *  @throws Exception
     */
    public static DataObject addPersonAccount(String uid, String cn, String sn) throws Exception
    {
        DataObject root = SDOHelper.createRootDataObject();
        DataObject entity = SDOHelper.createEntityDataObject(root, null,
                SchemaConstants.DO_PERSON_ACCOUNT);
        // Set the properties that the newly created entity will have
entity.set("uid", uid);
        entity.set("cn", cn);
        entity.set("sn", sn);
        System.out.println("Input datagraph before creating user"+ printDO(root));
        root = service.create(root);
        System.out.println("Output datagraph after creating user"+ printDO(root));
        return root;
    }

    /**
     * getCurrentCheckPoint Retrieves the current checkpoint from the repository      * @return DataObject
     * @throws Exception
     */
    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("Checkpoint: "+ printDO(result));
        return result;
    }

    /**
     *  updatePersonAccount
     *  Updates an already existing entity of PersonAccount entity type
      * @param entity Input DataObject that needs to be modified
     *  @param newCn value of property that needs to be updated
     *  @return Updated DataObject
     *  @throws Exception
     */
    public static DataObject updatePersonAccount(DataObject entity, String newCn) throws Exception
    {
        DataObject user = (DataObject) entity.getList(SchemaConstants.DO_ENTITIES).get(0);
        // Set the new value for cn to update the entity
        user.set("cn", newCn);
        System.out.println("Input datagraph before updating user"+ printDO(entity));
        DataObject root = service.update(entity);
        System.out.println("Output datagraph after updating user"+ printDO(root));
        return root;
    }

    /**
     *  getPersonAccount
     *  Gets the properties of an existing entity of PersonAccount entity type
      * @param entity Input DataObject that needs to be retrieved with the properties
      * @return DataObject with the properties
      * @throws Exception
     */
    public static DataObject getPersonAccount(DataObject entity) throws Exception
    {
        DataObject propCtrl = SDOHelper.createControlDataObject(entity, null,
                SchemaConstants.DO_PROPERTY_CONTROL);    
        // Set the properties that have to be retrieved for the entity
        propCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn");
        propCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
        propCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn");
        System.out.println("Input datagraph before getting user properties"+ printDO(entity));
        // Perform get operation
DataObject root = service.get(entity);
        System.out.println("Output datagraph after getting user properties"+ printDO(root));
        return root;
    }

    /**
     * searchChangedEntities Searches for 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, null,
                SchemaConstants.DO_CHANGE_CONTROL);
        // Set the search expression
        ctrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, "@xsi:type='PersonAccount'");         
        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 changed 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

The input data graphs and the resulting output data graphs for each step of this example are provided next.

Input data graph for creating a user (user1):

<?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 xsi:type="wim:PersonAccount">
<wim:uid>user1
</wim:uid>
<wim:cn>Admin
</wim:cn>
<wim:sn>AdminSn
</wim:sn>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Output data graph after creating a user (user1):

<?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 xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=user1,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="45258d76-82b4-4a44-9c3a-077f5a82f15e" uniqueName="uid=user1,dc=yourco,dc=com"/>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Input data graph for creating another user (user2):

<?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 xsi:type="wim:PersonAccount">
<wim:uid>user2
</wim:uid>
<wim:cn>Operator
</wim:cn>
<wim:sn>OperatorSn
</wim:sn>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Output data graph after creating another user (user2):

<?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 xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=user2,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="6ca02580-d6d1-4131-91ed-2329ad6599eb" uniqueName="uid=user2,dc=yourco,dc=com"/>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Output data graph with current 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>28
</wim:repositoryCheckPoint>
</wim:checkPoint>
</wim:controls>
</wim:Root>
</sdo:datagraph> 

Input data graph for updating user1:

<?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 xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=user1,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="45258d76-82b4-4a44-9c3a-077f5a82f15e" uniqueName="uid=user1,dc=yourco,dc=com"/>
<wim:cn>Manager
</wim:cn>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Output data graph after updating user1:

<?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 xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=user1,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="45258d76-82b4-4a44-9c3a-077f5a82f15e" uniqueName="uid=user1,dc=yourco,dc=com"/>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Input data graph for getting properties of user1:

<?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 xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=user1,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="45258d76-82b4-4a44-9c3a-077f5a82f15e" uniqueName="uid=user1,dc=yourco,dc=com"/>
<wim:cn>Manager
</wim:cn>
</wim:entities>
<wim:controls xsi:type="wim:PropertyControl">
<wim:properties>sn
</wim:properties>
<wim:properties>uid
</wim:properties>
<wim:properties>cn
</wim:properties>
</wim:controls>
</wim:Root>
</sdo:datagraph> 

Output data graph after getting properties of user1:

<?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 xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=user1,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="45258d76-82b4-4a44-9c3a-077f5a82f15e" uniqueName="uid=user1,dc=yourco,dc=com"/>
<wim:uid>user1
</wim:uid>
<wim:cn>Manager
</wim:cn>
<wim:sn>AdminSn
</wim:sn>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Input data graph for searching for changed 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: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>28
</wim:repositoryCheckPoint>
</wim:checkPoint>
<wim:changeTypes>*
</wim:changeTypes>
</wim:controls>
</wim:Root>
</sdo:datagraph> 

Output data graph after searching for changed 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 xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=user1,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="45258d76-82b4-4a44-9c3a-077f5a82f15e" uniqueName="uid=user1,dc=yourco,dc=com"/>
<wim:changeType>modify
</wim:changeType>
<wim:uid>user1
</wim:uid>
<wim:cn>Manager
</wim:cn>
<wim:sn>AdminSn
</wim:sn>
</wim:entities>
<wim:controls xsi:type="wim:ChangeResponseControl">
<wim:checkPoint>  
<wim:repositoryId>ldapRepo
</wim:repositoryId>  
<wim:repositoryCheckPoint>29
</wim:repositoryCheckPoint>
</wim:checkPoint>
</wim:controls>
</wim:Root>
</sdo:datagraph> 

Parent topic: Sample code



+

Search Tips   |   Advanced Search