How to Use the SAS Java Metadata Interface


Example 4: Create Objects

This is an example of how to create new objects in the SAS Metadata Repository. This example creates a physical table, column, and a note on that column.

   /**
    * This method creates a table, column, and note on the column, using
    * the store methods. It is a good example of how a wizard-style user interface
    * would utilize the MdFactory class.
    * @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.
            // we use the ReposId method to get the short ID.
            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 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 stored text attribute for the note
            note.setStoredText("Information about the note");

            // Associate the note with the column.
            column.getNotes().add(note);

            // Now, perist all of these changes to the server
            table.updateMetadataAll();

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