Using the SAS Java Metadata Interface |
This section provides an example of how to delete metadata objects. Similar to updating objects, you must create Java objects that represent the server metadata objects on the client before you can delete them. When you delete an object, all of its dependent objects are automatically deleted as well. A dependent object is an object that has a 1:1 cardinality with the specified object and cannot exist independently of the object. An example of a dependent object is a Column object. A Column object cannot exist on the SAS Metadata Server independently of some type of table object. See the DataTable metadata type in SAS 9.2 Metadata Model: Reference for a listing of the supported table subtypes.
In this example, we use the getMetadataObjectsSubset method to get the objects that we created and updated in Creating Objects, and in Getting and Updating Existing Objects. We use the deleteMetadataObjects method to delete them. 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 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 xmlSelect 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 ); // Add the found objects 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 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 they // are 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.
Copyright © 2010 by SAS Institute Inc., Cary, NC, USA. All rights reserved.