Mustercode für die Erweiterung des Schemas im Dateirepository

Mit dem umfassenden Mustercode und den Datengraphen können Sie das Schema erweitern, indem Sie ein neues Merkmal zu einem vorhandenen Entitätstyp im Dateirepository hinzufügen.

Dieses Beispielszenario deckt die folgenden Schritte ab:

  1. Es wird ein neues erweitertes Merkmal für einen integrierten Entitätstyp im Dateirepository hinzugefügt.
  2. Es wird eine Entität mit einem Wert für das erweiterte Merkmal erstellt.
  3. Es wird nach einer Entität mit dem Wert für das erweiterte Merkmal gesucht.

Voraussetzungen

Stellen Sie sicher, dass Sie die Informationen gelesen und die Schritte ausgeführt haben, die im Thema Voraussetzungen für die Programmierung beschrieben sind. Hierzu gehört auch der Abschnitt Merkmalschema erweitern, in dem Informationen zu Datenobjekten "propertySchema" und "extensionPropertySchema" enthalten sind und die gültige Syntax für Merkmalsdatentypen aufgeführt ist.

Mustercode

Fügen Sie den folgenden umfassenden Mustercode zu Ihrem Anwendungscode hinzu und ersetzen Sie die Variablen durch die tatsächlichen Werte, die Sie verwenden wollen.

/**
 *  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();
        }
    }
}

Eingabe- und Ausgabedatengraphen

Die Eingabedatengraphen und die resultierenden Ausgabedatengraphen für die einzelnen Schritte dieses Beispiels sind nachfolgend aufgeführt.

Eingabedatengraph für die Erstellung eines Merkmaltyps "postOfficeBox" und das Hinzufügen des Typs zum vorhandenen Entitätstyp
<?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>
Ausgabedatengraph nach der Erstellung eines Merkmaltyps "postOfficeBox" und dem Hinzufügen des Typs zum vorhandenen Entitätstyp:
<?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>
Eingabedatengraph für die Erstellung einer Entität "Person B" mit einem definierten Merkmal "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>
Ausgabedatengraph nach der Erstellung der Entität mit dem erweiterten Merkmal:
<?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>
Eingabedatengraph für die Suche nach der Entität mit dem erweiterten Merkmalswert (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>
Ausgabedatengraph nach der Suche nach der Entität mit dem erweiterten Merkmalswert (postOfficeBox='4-23') und gefundener Entität "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>


Rechtliche Hinweise | Feedback