How to Use the SAS Java Metadata Interface


Example 6: Delete Objects

This is an example of how to delete objects in the SAS Metadata Repository. This example deletes the objects created in a prior example.

   /**
    * This method deletes the objects we created in repository
    * @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 containing all of the objects that need to be deleted
            // from the server.
            List deleteList = new ArrayList();

            // Query for the table again
            String xmlSelect = "<XMLSELECT Search=\"@Name='TableTest'\"/>";
            String template = "<Templates>" +
                                 "<PhysicalTable>" +
                                   "<Columns/>" +
                                 "</PhysicalTable>" +
                                 "<Column>" +
                                    "<Notes/>" +
                                 "</Column>" +
                               "</Templates>";

            // Add the xml select and template strings together
            String sOptions = xmlSelect + template;

            int flags = MdOMIUtil.OMI_XMLSELECT | MdOMIUtil.OMI_TEMPLATE |
                           MdOMIUtil.OMI_GET_METADATA;
            List tableList = _factory.getOMIUtil().getMetadataObjectsSubset(store,
                                                       repository.getFQID(),
                                                       MetadataObjects.PHYSICALTABLE,
                                                       flags,
                                                       sOptions);


            // Now that we have the objects, we need to add them to the delete list
            Iterator iter = tableList.iterator();
            while (iter.hasNext())
            {
               PhysicalTable table = (PhysicalTable) iter.next();
               deleteList.add(table);

               // Get the columns
               AssociationList columns = table.getColumns();
               for (int i = 0; i < columns.size(); i++)
               {
                  Column column = (Column) columns.get(i);
                  deleteList.add(column);

                  // Get the notes
                  AssociationList notes = column.getNotes();
                  for (int j = 0; j < notes.size(); j++)
                  {
                     TextStore note = (TextStore) notes.get(j);
                     deleteList.add(note);
                  }
               }
            }

            // Delete everything added to 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();
         }
      }
   }