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:
Ensure that you have read the information and completed the steps described in the topic, Programming prerequisites.
Add the following end-to-end sample code to your application code and replace the variables with the actual values that you want 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;
}
}
The input data graphs and the resulting output data graphs for each step of this example are provided next.
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sdo="commonj.sdo" xmlns:wim="http://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>
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sdo="commonj.sdo" xmlns:wim="http://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>
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sdo="commonj.sdo" xmlns:wim="http://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>
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sdo="commonj.sdo" xmlns:wim="http://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>
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sdo="commonj.sdo" xmlns:wim="http://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>
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sdo="commonj.sdo" xmlns:wim="http://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>
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sdo="commonj.sdo" xmlns:wim="http://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>
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sdo="commonj.sdo" xmlns:wim="http://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>
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sdo="commonj.sdo" xmlns:wim="http://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>
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sdo="commonj.sdo" xmlns:wim="http://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>
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sdo="commonj.sdo" xmlns:wim="http://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>