用于在文件存储库中扩展模式的样本代码

使用端到端样本代码和数据图,通过将新属性添加到文件存储库中的现有实体类型,来扩展模式。

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

  1. 将新扩展属性添加到文件存储库中的内置实体类型。
  2. 为扩展属性创建实体并附带值。
  3. 搜索具有扩展属性值的实体。

必备条件

确保您已阅读了“编程先决条件”主题(包括“扩展属性模式”部分,其中包含有关 propertySchema 和 extensionPropertySchema 数据对象的信息并列出了属性数据类型的有效语法)中的信息,并完成了其中描述的步骤。

样本代码

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

/**
 *  This class is used to demonstrate the use of VMM API to extend the schema.
 *  This example adds a new property to an existing entity type, in File repository.
 */
public class ExtendSchemaSample extends BaseApp
{
    /**
     *  A string constant for the new property name to be added
     */
    private static final String newPropName = "postOfficeBox";
    
    /**
     *  Application entry point
     *  @param args
     *  @throws WIMException 
     */
    public static void main(String[] args) throws WIMException
    {
        System.out.println("Starting .. ");
        // locate service
        service = locateService(null);
        System.out.println("Located service. Starting schema extension ... ");
        // Add a new property (runtime)
        runTimeAddNewProperty();
    }

    /**
     *  This method shows the use of the schema extension API
     *  This example does the following:
     *  Adds a new property, postOfficeBox, to an existing entity type
     *  Creates an entity with the value, 4-23 for extended property, postOfficeBox
     *  Searches for an entity with the value of the extended property
     */
    private static void runTimeAddNewProperty()
    {
        // Add the property "postOfficeBox" to "PersonAccount"
        addNewProperty();
        System.out.println("The property 'postOfficeBox' is added to 'PersonAccount'.");
        // Create a person with a property "postOfficeBox"
        System.out.println("\nCLIENT: Creating Person B with a defined property called 'postOfficeBox'.");
        createPerson();
        // Search on the property "postOfficeBox = 4-23" 
        searchPerson();
    }
	
    /**
     *  This method creates a person with the following attributes
     *    uid: personb
     *    cn: Person B
     *    sn: PersonBLastName
     *    postOfficeBox: 4-23
     */
    private static void createPerson()
    {
        //Create Person B ...
        try
        {
            // Get a root data object. The rest of the object tree would be built under it
            DataObject root = SDOHelper.createRootDataObject();            // Create a "PersonAccount" entity type data object under root
            // The object is in the default WIM namespace
            DataObject person = root.createDataObject(SchemaConstants.DO_ENTITIES, Service.WIM_NS_URI, 
                    SchemaConstants.DO_PERSON_ACCOUNT);
            // Set the values for required attributes, uid, cn, sn
            // Also set the value for the newly added "postOfficeBox" attribute
            person.set("uid", "personb");
            person.set("cn", "Person B");
            person.set("sn", "PersonBLastName");
            person.set(newPropName, "4-23");
            System.out.println("Input datagraph for create person  -> " + printDO(root));
            // Invoke the create API to create the person account entity defined previously
            root = service.create(root); 
            System.out.println("Output datagraph for create person  -> " + printDO(root);
            System.out.println("CLIENT: Person B has been created\n\n");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     *  This method adds the "postOfficeBox" property to "PersonAccount" entity type.
     */
    @SuppressWarnings("unchecked")
    private static void addNewProperty() 
    {
        try
        {
            System.out.println("\nCLIENT: Creating new property type postOfficeBox 
                    and adding it to existing entity type PersonAccount");
            // Get a root data object. The rest of the object tree would be built under it
            DataObject root = SDOHelper.createRootDataObject();            // Create a new "schema" object under root. 
            // This object will contain the details of the schema modifications that need to be made
            DataObject dynaSchemaDO = root.createDataObject(SchemaConstants.DO_SCHEMA);
            /***************************************/
            /** Code to define the new property   **/
            /***************************************/
            // Create a property schema data object under the schema object created earlier
            DataObject propSchemaDO = dynaSchemaDO.createDataObject(SchemaConstants.DO_PROPERTY_SCHEMA);
            // Set the values for the property, such as, namespace URI, namespace prefix, property name
            propSchemaDO.set(SchemaConstants.PROP_NS_URI, "http://www.yourco.com/wim/yourext");
            propSchemaDO.set(SchemaConstants.PROP_NS_PREFIX, "yourprefix");
            propSchemaDO.set(SchemaConstants.PROP_PROPERTY_NAME, newPropName);
            // Specify if the property is multi-valued
            // If it is multi-valued, its type must be List
            propSchemaDO.setBoolean(SchemaConstants.PROP_MULTI_VALUED, false);
            // Specify the property's data type
            // Data types can be simple like boolean, int, float, double, String 
            // or special like Address, Person, Group, etc.
            propSchemaDO.set(SchemaConstants.PROP_DATA_TYPE, SchemaConstants.DATA_TYPE_STRING);
            // Get the list of entity types to which this property needs to be added
            List applicableEntityTypes = propSchemaDO.getList(SchemaConstants.PROP_APPLICABLE_ENTITY_TYPE_NAMES);
            // Add the applicable entity type names to the list fetched previously
            applicableEntityTypes.add(Service.DO_PERSON_ACCOUNT);
            /***************************************************/
            /** The new property definition is done           **/
            /** Now the createSchema API needs to be called   **/
            /***************************************************/
            System.out.println("Input datagraph for create property -> " + printDO(root));
            // Invoke the create schema API to actually add the property definition to the schema model
            root = service.createSchema(root);
            System.out.println("Output datagraph for create property -> " + printDO(root));
            System.out.println("\nCLIENT: New property type is created.");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     *  Search for person with postOfficeBox = 4-23.
     */
    @SuppressWarnings("unchecked")
    private static void searchPerson()
    {
        try
        {
            //Search Person B
            // Create a search control data object
            DataObject root = SDOHelper.createRootDataObject();            DataObject searchCtrl = SDOHelper.createControlDataObject(root, null, 
                    SchemaConstants.DO_SEARCH_CONTROL);
            // Specify the properties that need to be fetched for all matching entries
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn");
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn");
            // Specify the matching criteria as the search expression
            // The expression here specifies the entity type as "PersonAccount" 
            // and value of "postOfficeBox" as '4-23'
            // Executing the search with this expression will return all "PersonAccount" objects 
                    that have a value of "postOfficeBox" as '4-23'
            // The properties 'uid', 'sn' and 'cn' would be fetched for all objects
            searchCtrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, "@xsi:type='" 
                    + Service.DO_PERSON_ACCOUNT + "' and " + newPropName + "='4-23'");
            System.out.println("Searching for " + searchCtrl.getString(SchemaConstants.PROP_SEARCH_EXPRESSION));
            System.out.println("Input datagraph for search -> " + printDO(root));
            // Invoke the search API with the search control
            root = service.search(root);            System.out.println("Output datagraph for search -> " + printDO(root));
            // Iterate over the search results
            // The results are contained as a List in property "entities" 
            // of the data object returned by the Search API
            // A convenience constant "DO_ENTITIES" is defined in SchemaConstants to refer to this property
            printIdentifiers(root);
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

输入和输出数据图

接下来提供此示例的每个步骤的输入数据图以及产生的输出数据图。

用于创建属性类型 postOfficeBox 并将其添加到现有实体类型的输入数据图
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:sdo="commonj.sdo"
    xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:schema>
      <wim:propertySchema nsPrefix="yourprefix" nsURI="http://www.yourco.com/wim/yourext"
          dataType="String" multiValued="false" propertyName="postOfficeBox">
        <wim:applicableEntityTypeNames>PersonAccount</wim:applicableEntityTypeNames>
      </wim:propertySchema>
    </wim:schema>
  </wim:Root>
</sdo:datagraph>
创建属性类型 postOfficeBox 并将其添加到现有实体类型之后的输出数据图:
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:sdo="commonj.sdo"
    xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:schema>
      <wim:propertySchema nsPrefix="yourprefix" nsURI="http://www.yourco.com/wim/yourext"
          dataType="String" multiValued="false" propertyName="postOfficeBox">
        <wim:applicableEntityTypeNames>PersonAccount</wim:applicableEntityTypeNames>
      </wim:propertySchema>
    </wim:schema>
  </wim:Root>
</sdo:datagraph>
用于创建实体 Person B(具有已定义属性 postOfficeBox)的输入数据图:
<?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" 
    xmlns:yourprefix="http://www.yourco.com/wim/yourext">
  <wim:Root>
    <wim:entities xsi:type="wim:PersonAccount">
      <wim:uid>personb</wim:uid>
      <wim:cn>Person B</wim:cn>
      <wim:sn>PersonBLastName</wim:sn>
      <yourprefix:postOfficeBox>4-23</yourprefix:postOfficeBox>
    </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=personb,o=defaultWIMFileBasedRealm" 
          repositoryId="InternalFileRepository"
          uniqueId="fc050249-d245-498f-a3f3-1a3d7566f971" 
          uniqueName="uid=personb,o=defaultWIMFileBasedRealm"/>
    </wim:entities>
  </wim:Root>
</sdo:datagraph>
用于搜索具有扩展属性值 postOfficeBox='4-23' 的实体的输入数据图:
<?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:SearchControl" 
          expression="@xsi:type='PersonAccount' and postOfficeBox='4-23'">
      <wim:properties>uid</wim:properties>
      <wim:properties>sn</wim:properties>
      <wim:properties>cn</wim:properties>
    </wim:controls>
  </wim:Root>
</sdo:datagraph>
搜索具有扩展属性值 postOfficeBox='4-23'(找到实体 Person B)之后的输出数据图:
<?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="fc050249-d245-498f-a3f3-1a3d7566f971" 
          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>


使用条款 | 反馈