Previous Page | Next Page

Using the SAS Java Metadata Interface

Creating Objects

You can create objects by using the methods in the MdFactory interface. You must create a Java object instance for every new and existing metadata object that you want to update or delete in a SAS Metadata Repository. You must also create an object store in which to hold the metadata objects. The object store maintains a list of the objects that need to be persisted to the SAS Metadata Server with a single request.

The following code creates a new PhysicalTable object, a new Column object, and a new TextStore object. The code then creates associations between these objects. After the metadata objects are created, they are persisted to the SAS Metadata Server.

Notes:

   /**
    * This example creates a table, column, and a note on the column using
    * the store methods. 
    * @param Repository CMetadata object with id of form:  A0000001.A5KHUI98
    */
   public void createTable(CMetadata repository)
   {
      if (repository != null)
      {
         try
         {
            System.out.println("\nCreating objects on the server...");
            
            // We have a repository object.
            // We use the reposFQID method to get its fully qualified ID.
            String reposFQID = repository.getFQID();

            // We need the short repository ID to create an object.
            String shortReposID = reposFQID.substring(reposFQID.indexOf(".") + 1, 
                                    reposFQID.length());

            // Now we create an object store to hold all our objects.  
            // This will be used to maintain a list of objects to persist
            // to the SAS Metadata Server.
            MdObjectStore store = _factory.createObjectStore();
            // Create a PhysicalTable object named "TableTest".
            PhysicalTable table=(PhysicalTable) _factory.createComplexMetadataObject(
                    store,
                    null,
                    "TableTest",
                    MetadataObjects.PHYSICALTABLE,
                    shortReposID
                   );

            // Create a Column named "ColumnTest".
            Column column = (Column) _factory.createComplexMetadataObject(
                   store,
                   null,
                   "ColumnTest",
                   MetadataObjects.COLUMN,
                   shortReposID
                   );

            // Set the attributes of the column.
            column.setColumnName("MyTestColumnName");
            column.setSASColumnName("MyTestSASColumnName");
            column.setDesc("This is a description of a column");

            // Use the get"AssociationName"() method to associate the column with  
            // the table. This method creates an AssociationList object for the table
            // object. The inverse association will be created automatically.
            // The add(MetadataObject) method adds myColumn to the AssociationList.
            table.getColumns().add(column);

            // Create a note for the column named "NoteTest".
            TextStore note = (TextStore) _factory.createComplexMetadataObject(
                    store,
                    null,
                    "NoteTest",
                    MetadataObjects.TEXTSTORE,
                    shortReposID
                   );

            // Set the StoredText= attribute for the note
            note.setStoredText("Information about the note");
            
            // Associate the note with the column.
            column.getNotes().add(note);

            // Now, persist all of these changes to the 
            // SAS Metadata Server
            table.updateMetadataAll();

            // When finished, clean up the objects in the store if they 
            // are no longer being used.
            store.dispose();
         }
         catch (MdException e)
         {
            e.printStackTrace();
         }
         catch (RemoteException e)
         {
            e.printStackTrace();
         }      
      }
   }

For more information about object stores and AssociationList objects, see SAS Java Metadata Interface Overview.

Previous Page | Next Page | Top of Page