使用样本代码片段和数据图,根据扩展属性的值来执行搜索。
以下样本代码片段和数据图显示如何使用搜索条件来搜索以其扩展属性 age 的值作为搜索标准的实体。
确保您已阅读了“编程先决条件”主题中的信息(其中列出了属性数据类型的有效语法)并完成了其中描述的步骤。
您把必须在不同搜索条件中填充了具有扩展属性的实体。有关如何创建扩展属性的样本代码,请参阅以下主题
请将以下代码片段添加到您的应用程序代码,并将变量替换为您要使用的实际值。
private static void searchWithSearchBase()
{
try
{
//Search Person B
DataObject root = SDOHelper.createRootDataObject(); DataObject searchCtrl = SDOHelper.createControlDataObject(root, null,
SchemaConstants.DO_SEARCH_CONTROL);
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("age");
/**
* You can add search bases to the search as shown here:
* o=defaultWIMFileBasedRealm for default file repository search
* or
* dc=yourco,dc=com for the LDAP repository search.
* If no search base is provided, the search runs across all the base entries configured.
*/
searchCtrl.getList(SchemaConstants.PROP_SEARCH_BASES).add("o=defaultWIMFileBasedRealm");
searchCtrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION,
"@xsi:type='PersonAccount' and age > 2");
root = service.search(root); System.out.println("Output data graph" + printDO(root));
printIdentifiers(root);
}
catch (Exception e) {
e.printStackTrace();
}
}
您可以通过如下方式来验证搜索结果:验证实体是否属于指定的搜索条件,如 o=defaultWIMFileBasedRealm(对于缺省文件存储库搜索)或 dc=yourco,dc=com(对于 LDAP 存储库搜索)。
使用搜索条件来搜索实体(基于扩展属性值)的输出数据图:
<?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=personb,o=defaultWIMFileBasedRealm"
repositoryId="InternalFileRepository"
uniqueId="9e27cd3b-6300-4264-a4ac-768dd1092d11"
uniqueName="uid=personb,o=defaultWIMFileBasedRealm"/>
<wim:uid>personb</wim:uid>
<wim:cn>Person B</wim:cn>
<wim:sn>PersonBLastName</wim:sn>
</wim:entities>
</wim:Root>
</sdo:datagraph>