Deleting Objects

This section provides an example of how to delete metadata objects. Deleting objects is similar to updating objects. You must create a Java object instance that represents the server metadata object on the client before you can delete it.
In this example, the getMetadataObjectsSubset method gets the PhysicalTable object that was created and updated in Creating Objects, and in Getting and Updating Existing Objects. The deleteMetadataObjects method deletes the objects.
Because PhysicalTable is a PrimaryType subtype in the SAS Metadata Model, and the object instance stores a valid TypeName value in the PublicType attribute, there is no need to specify a template to get the associated Column and Keyword objects, and there is no need to specifically delete the objects. When you delete an object that specifies the name of a type definition from the SAS type dictionary in the PublicType attribute, the SAS Metadata Server uses the template that is internalized in the type definition to identify the associated objects in its logical metadata definition. The metadata server automatically deletes these associated objects as well. The getMetadataObjectsSubset method is in the MdOMIUtil interface. The deleteMetadataObjects method is in the MdFactory interface.
/**
    * This example deletes the objects that we created.
    * @param repository
    */
   public void deleteTable(CMetadata repository)
   {
      if(repository != null)
      {
         try
         {
            System.out.println("\nDeleting the objects from the server...");
            MdObjectStore store = _factory.createObjectStore();
            
            // Create a list of the objects that need to be deleted
            // from the server.
            List<CMetadata> deleteList = new ArrayList<CMetadata>();
            
            // Query for the table again.
            String xmlSelect = "<XMLSELECT Search=\"*[@PublicType='Table' and " + 
                                 "@Name='TableTest']\"/>";

               // Note: Since the object has a valid PublicType value, the SAS 9.3 
               // Metadata Server automatically deletes all objects associated
               // with the table, such as its Column and Keyword objects, when the table 
               // is deleted. There is no need to specify a template to delete
               // the associated objects.
            
            int flags = MdOMIUtil.OMI_XMLSELECT;
            List tableList = _factory.getOMIUtil().getMetadataObjectsSubset(store, 
                                                       repository.getFQID(),
                                                       MetadataObjects.PHYSICALTABLE,
                                                       flags,
                                                       xmlSelect);
            
            
            // Add the found objects to the delete list.
            Iterator iter = tableList.iterator();
            while (iter.hasNext())
            {
               PhysicalTable table = (PhysicalTable) iter.next();
               deleteList.add(table);
            }

            // Delete everything that is in the delete list.
            if (deleteList.size() > 0)
            {
               System.out.println("Deleting " + deleteList.size() + " objects");
               _factory.deleteMetadataObjects(deleteList);
            }
            
            // 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();
         }
      }
   }
For an executable version of this example and the examples in “Creating Objects,” “Getting and Updating Existing Objects,” and a few additional examples, see Sample Program.