用于搜索已更改和已删除实体的样本代码

使用样本代码片段和数据图来搜索已更改和已删除实体。

此样本方案中涵盖以下步骤:

  1. 通过使用 ChangeResponseControl 数据对象来获取存储库的当前检查点。此检查点是必需的,因为在通过使用 ChangeControl 数据对象搜索已更改实体时(步骤 3),您必须传递此检查点,以便仅返回在此检查点之后更改的实体。
    要点: 使用 ChangeControl 和 ChangeResponseControl 数据对象时有两个先决条件:一是必须将存储库的 supportChangeLog 属性的值设置为 native,二是启用并更改底层存储库中的跟踪功能。有关更多信息,请参阅“搜索更改的实体”主题。
  2. 删除实体。
  3. 通过将 CHANGETYPE_ALL 指定为 CHANGETYPES 属性的值,搜索已更改和已删除实体。在此方案中,将返回已删除实体。有关已更改实体的更多信息,请参阅“搜索更改的实体”主题。有关用于搜索已更改实体的端到端样本代码,请参阅“搜索已更改实体的样本代码”主题。

必备条件

确保您已阅读了“编程先决条件”主题中的信息并完成了其中描述的步骤。

在运行以下样本代码中给定的方法之前,实体必须存在于存储库中。

样本代码

请将以下样本代码添加到您的应用程序代码,并将变量替换为您要使用的实际值。

    /**
     *  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>
用于搜索已更改和已删除实体(以 * (CHANGETYPE_ALL) 作为 CHANGETYPES 属性的值)的输入数据图:
<?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>


使用条款 | 反馈