# How to Use the SAS Java Metadata Interface

- Package Description
- Example 1: Server Connection
- Example 2: Get Repositories
- Example 3: Get Metadata Types
- Example 4: Create Objects
- Example 5: Read Objects
- Example 6: Delete Objects
- Example 7: Get all objects of a particular type
- Example 8: Get an existing object
- Sample Program

## Example 5: Read Created Objects

This is an example of how to read objects that exist in the SAS Metadata Repository. This methods retrieves the physical table, columns, and note created in the previous example.

       /**
        * This method reads the newly created objects back from the server.
        * @param repository identifies the repository from which to read our objects.
        */
       public void readTable(CMetadata repository)
       {
          if(repository != null)
          {
             try
             {
                System.out.println("\nReading objects from the server...");

                // First we create an MdObjectStore as a container for all of the objects
                // we will create/read/persist to the server as one collection.
                MdObjectStore store = _factory.createObjectStore();

                // The following statements define GetMetadataObjectsSubset options strings.
                // These XML strings are used in conjunction with SAS Open Metadata Interface
                // flags. The <XMLSELECT> element specifies filter criteria. The
                // element specifies the metadata properties to be returned for each object
                // from the server.
                String xmlSelect = "<XMLSELECT Search=\"@Name='TableTest'\"/>";
                String template =
                   "<Templates>" +
                      "<PhysicalTable Id=\"\" Name=\"\" Desc=\"\">" +
                        "<Columns/>" +
                      "</PhysicalTable>" +
                      "<Column Id=\"\" Name=\"\" Desc=\"\" ColumnName=\"\">" +
                         "<Notes/>" +
                      "</Column>" +
                      "<TextStore Id=\"\" Name=\"\" Desc=\"\" StoredText=\"\"/>" +
                    "</Templates>";

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

                // The following statements go to the server with a fully-qualified
                // repository ID, specify the type of object we are searching for
                // (MdObjectFactory.PHYSICALTABLE), utilizing the OMI_XMLSELECT, OMI_TEMPLATE,
                // and OMI_GET_METADATA flags. OMI_GET_METADATA tells the server to get all of
                // the attributes specified in the template for each object that is returned.
                //
                // The table, column, and note will be read from the server and created within
                // the specified object store.
                int flags = MdOMIUtil.OMI_XMLSELECT | MdOMIUtil.OMI_TEMPLATE |
                               MdOMIUtil.OMI_GET_METADATA;
                List tableList = _factory.getOMIUtil().getMetadataObjectsSubset(store,
                                                           repository.getFQID(),
                                                           MetadataObjects.PHYSICALTABLE,
                                                           flags,
                                                           sOptions);
                Iterator iter = tableList.iterator();
                while (iter.hasNext())
                {
                   // The table returned from the server
                   PhysicalTable table = (PhysicalTable) iter.next();
                   System.out.println("Found table: " + table.getName() + " (" +
                                        table.getId() + ")");

                   // Get the list of columns for this table.
                   AssociationList columns = table.getColumns();
                   for (int i = 0; i < columns.size(); i++)
                   {
                      // The column associated to the table
                      Column column = (Column) columns.get(i);
                      System.out.println("Found column: " + column.getName() + " (" +
                                           column.getId() + ")");
                      System.out.println("\tDescription: " + column.getDesc());
                      System.out.println("\tColumnName: " + column.getColumnName());

                      // We now have a column, and request to get its notes.
                      AssociationList notes = column.getNotes();
                      for (int j = 0; j < notes.size(); j++)
                      {
                         // The note associated to the column
                         TextStore note = (TextStore) notes.get(j);
                         System.out.println("Found textstore: " + note.getName() + " (" +
                                              note.getId() + ")");
                         System.out.println("\tStoredText: " + note.getStoredText());
                      }
                   }
                }

                // 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();
             }
          }
       }