使用样本代码片段和数据图来搜索已更改和已删除实体。
此样本方案中涵盖以下步骤:
请将以下样本代码添加到您的应用程序代码,并将变量替换为您要使用的实际值。
/**
* 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;
}
<?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>59</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: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>
<?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>
<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>