搜索已更改实体的样本代码

使用端到端样本代码和数据图对更改的实体执行搜索操作。

此样本场景中包含以下步骤:

  1. 创建两个实体。
  2. 使用 ChangeResponseControl 数据对象获取存储库的当前检查点。此检查点是必需的,因为使用 ChangeControl 数据对象(步骤 5)搜索更改的实例时必须经过此检查点,这样就只会返回在此检查点之后更改的实体。
    要点: 使用 ChangeControl 和 ChangeResponseControl 数据对象时有两个先决条件:一是必须将存储库的 supportChangeLog 属性的值设置为 native,二是启用并更改底层存储库中的跟踪功能。有关更多信息,请参阅主题搜索更改的实体
  3. 更新其中一个实体。
  4. 获取已更新实体的属性。
  5. 使用 ChangeControl 和 ChangeResponseControl 数据对象搜索更改的实体。

必备条件

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

样本代码

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

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;
    }
}

输入和输出数据图

下面提供了此示例中每个步骤的输入数据图和生成的输出数据图。

创建用户 (user1) 的输入数据图:
<?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>
创建用户 (user1) 后的输出数据图:
<?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>
创建另一个用户 (user2) 的输入数据图:
<?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>
创建另一个用户 (user2) 后的输出数据图:
<?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>
更新 user1 的输入数据图:
<?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>
更新 user1 后的输出数据图:
<?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>
获取 user1 的属性的输入数据图:
<?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>
获取 user1 的属性后的输出数据图:
<?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>


使用条款 | 反馈