Creating Objects

You can create metadata 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 read, update, or delete in a SAS Metadata Repository. You must create an object store in which to hold the metadata objects. The object store maintains a list of the metadata 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 Keyword object. The code creates associations between these objects. After the metadata objects are created, they are persisted to the SAS Metadata Server.
Notes:
  • To create and persist a new metadata object, you must specify a metadata repository in which to store the object. You can specify a repository identifier directly in the createComplexMetadataObject method.
  • Because these are new metadata objects, they are assigned permanent metadata object identifiers when they are persisted to the SAS Metadata Server. A request that creates Java object instances to represent existing metadata objects needs to determine their metadata object identifiers before persisting changes to the SAS Metadata Server. For more information, see Getting and Updating Existing Objects.
  • When creating a new metadata object with createComplexMetadataObject, a valid metadata type must be passed in. For a list of all valid metadata types, see com.sas.metadata.remote.MetadataObjects.
  • The example defines PublicType and UsageVersion attributes for the table and column objects, and an association to a folder. As a result, the objects are visible in the SAS Folders tree, and they can be accessed by the utilities that are available to objects in that tree. Later, these attribute values can be used to simplify metadata queries. For more information, see Using Interfaces that Read and Write Metadata in SAS 9.3.
    A metadata object must be unique within a folder. The example includes a private method that verifies that no other tables with the same Name and PublicType attribute values exist in the folder.
/**
    * The following statements create a table, column, and keyword on the column.
    * The objects are created in the user's My Folder folder.
    * @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 getFQID 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 our objects.  
            // This will be used to maintain a list of objects to persist
            // to the SAS Metadata Server.
            MdObjectStore store = _factory.createObjectStore();
            String tableName = "TableTest";
            String tableType = "Table";
            
            // The getUserHomeFolder method retrieves (or creates, if necessary)
            // the user's My Folder folder.  If the folder does not 
            // exist, the method automatically creates it.
            // This is the folder in which we will create the table.
            Tree myFolder = _factory.getOMIUtil().getUserHomeFolder(store, "", 
                  MdOMIUtil.FOLDERTYPE_MYFOLDER, "", 0, true);
            
            // Before creating any objects, we must verify that the Table does 
            // not already exist within the parent folder.  The table cannot be
            // created if it is not unique within the folder.
            if (!isUnique(myFolder, tableName, tableType))
            {
               // Create a PhysicalTable object named "TableTest".
               PhysicalTable table = (PhysicalTable) _factory.createComplexMetadataObject
                  (store,
                        null,
                        "TableTest",
                        MetadataObjects.PHYSICALTABLE,
                        shortReposID);
   
               // Set the PublicType and UsageVersion attributes for the table.
               table.setPublicType("Table");
               table.setUsageVersion(1000000.0);
               
               // Add the table to the user's "My Folder" location.
               table.getTrees().add(myFolder);
              
               // Create a Column named "ColumnTest".
               Column column = (Column) _factory.createComplexMetadataObject
                  (store,
                        null,
                        "ColumnTest",
                        MetadataObjects.COLUMN,
                        shortReposID);
   
               // Set the attributes of the column, including PublicType and
               // UsageVersion.
               column.setPublicType("Column");
               column.setUsageVersion(1000000.0);
               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 keyword for the column named "KeywordTest".
               Keyword keyword = (Keyword) _factory.createComplexMetadataObject
                  (store,
                        null,
                        "KeywordTest",
                        MetadataObjects.KEYWORD,
                        shortReposID);
   
               // Associate the keyword with the column.
               column.getKeywords().add(keyword);
   
               // Now, persist all of these changes to the 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();
         }      
      }
   }

   /**
    * isUnique() is a private method that determines whether an object is unique 
    * within a given folder.
    * @param folder is the name of the parent folder
    * @param name is the Name value of the object
    * @param type is the PublicType value of the object
    * @return true if an object with the specified name and type exists within 
    * the folder.
    */
   private boolean isUnique(Tree folder, String name, String type) 
      throws RemoteException, MdException
   {
      // Now, retrieve the objects in the folder and make sure that the folder doesn't 
      // already contain this table. The object's Name and PublicType attribute values
      //  are used to determine if it is unique.
      List members = folder.getMembers();
      for (Iterator iter = members.iterator(); iter.hasNext(); )
      {
         CMetadata meta = (CMetadata) iter.next();
         if (meta instanceof PrimaryType)
         {
            // Verify that the types and object names match
            // A case-insensitive match should be used when comparing the names.
            if (type.equals(((PrimaryType) meta).getPublicType()) &&
                name.equals(meta.getName()))
            {
               // We found a match.
               return true;
            }
         }         
      }
      return false;
   }
For more information about object stores and AssociationList objects, see SAS Java Metadata Interface Overview.