Express (Distributed operating systems), v8.0 > Secure applications and their environment > Authenticate users > Select a registry or repository > Manage realms in a federated repository > Virtual member manager > Develop with virtual member manager > Integrate virtual member manager into the application > Sample code


Sample code for extending schema in the file repository

Use the end-to-end sample code and data graphs to extend the schema by adding a new property to an existing entity type in file repository.

The following steps are covered in this sample scenario:

  1. Add a new extended property to a built-in entity type in file repository.

  2. Create an entity with value for extended property.
  3. Search for an entity with the value of the extended property.


Prerequisites

Ensure that we have read the information and completed the steps described in the topic, Program prerequisites, including the section, Extend property schema, which has information about propertySchema and extensionPropertySchema data objects and lists the valid syntax for property data types.


Sample code

Add the following end-to-end sample code to the application code and replace the variables with the actual values to use.

/**
 *  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 above
            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, "//publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/  ://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 above
            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();
        }
    }
}


Input and output data graphs

The input data graphs and the resulting output data graphs for each step of this example are provided next.

Input data graph for creating a property type postOfficeBox and adding it to existing entity type

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:sdo="commonj.sdo"
    xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:schema>
<wim:propertySchema nsPrefix="yourprefix" nsURI="//publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/  ://www.yourco.com/wim/yourext"
          dataType="String" multiValued="false" propertyName="postOfficeBox">  
<wim:applicableEntityTypeNames>PersonAccount
</wim:applicableEntityTypeNames>
</wim:propertySchema>
</wim:schema>
</wim:Root>
</sdo:datagraph> 

Output data graph after creating a property type postOfficeBox and adding it to existing entity type:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:sdo="commonj.sdo"
    xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:schema>
<wim:propertySchema nsPrefix="yourprefix" nsURI="//publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/  ://www.yourco.com/wim/yourext"
          dataType="String" multiValued="false" propertyName="postOfficeBox">  
<wim:applicableEntityTypeNames>PersonAccount
</wim:applicableEntityTypeNames>
</wim:propertySchema>
</wim:schema>
</wim:Root>
</sdo:datagraph> 

Input data graph for creating an entity, Person B, with a defined property, postOfficeBox:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim"
    xmlns:yourprefix="//publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/  ://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> 

Output data graph after creating entity with the extended property:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//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> 

Input data graph for searching for entity with extended property value, postOfficeBox='4-23':

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//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> 

Output data graph after searching for entity with extended property value, postOfficeBox='4-23' (the entity, Person B found):

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//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> 

Parent topic: Sample code



+

Search Tips   |   Advanced Search