IDfCollections, Part II

In the previous post, we looked at the basics of processing the contents of an IDfCollection object. In this post I will show how to process a generic IDfCollection, and how to determine if an IDfCollection is empty.

First, the code for processing a generic IDfCollection. The following method will take as input, an IDfCollection object of which it knows nothing about. It will “explore” the collection and retrieve the attribute values it contains according to the column types. You might embed code like this in your application as a generic way to extract IDfCollection content. In this example, I just print the content as it is extracted. This limits the method’s usefulness as a generic process, but it works well as an illustration.  You can change it to do something more meaningful.

private void processGenericCollection(IDfCollection col) {

    // #6 check for empty collections
    boolean isEmpty = true;

        try {
            // #1 print column names
            int colNum = col.getAttrCount();
            for (int i=0; i
               System.out.print(col.getAttr(i).getName() + "\t");
            }
            System.out.println();

            // #2 print contents of each row
            while (col.next()) {

                // #7 collection is not empty
                isEmpty = false;

                // #3 process each column individually according to its data type
                for (int j=0; j<colNum; j++) {

                // #4 get col data type
                int colType = col.getAttr(j).getDataType();

                // get col value based on type
                String colValue = "";
                if (colType == IDfType.DF_BOOLEAN)
                   colValue = Boolean.toString(col.getBoolean(col.getAttr(j).getName()));
                else if (colType == IDfType.DF_DOUBLE)
                   colValue = Double.toString(col.getDouble(col.getAttr(j).getName()));
                else if (colType == IDfType.DF_ID)
                   colValue = col.getId(col.getAttr(j).getName()).toString();
                else if (colType == IDfType.DF_INTEGER)
                   colValue = Integer.toString(col.getInt(col.getAttr(j).getName()));
                else if (colType == IDfType.DF_STRING)
                   colValue = col.getString(col.getAttr(j).getName());
                else if (colType == IDfType.DF_TIME)
                   colValue = col.getTime(col.getAttr(j).getName()).toString();

                // #5 print value  System.out.print(colValue + "\t");
                }
                System.out.println();
            }
        } catch (DfException e) {
            e.printStackTrace();
        }
        col.close();

        // #8 process empty collection
        if (isEmpty)
          System.out.println("The IDfCollection contains no results (zero rows)");
     }
  1. Before executing the first next() method, you can “explore” the IDfCollection and retrieve information about the columns.  Unfortunately, you can’t retrieve information about the rows, like, how many there are (We’ll look at that next week).  In this example, I just print the names of the columns in the IDfCollection (these will correspond to the values in the SELECT statement).
  2. Now execute the next() method to advance the row pointer in the IDfCollection.  The col object now represents the first row in the IDfCollection.
  3. Process each column of the current row according to its type.  To do this, iterate over the columns again.  The columns are represented by IDfAttr objects.
  4. Simply get the type of each IDfAttr represents and use an if statement to extract the value using the proper get method (Types are represented as integer constants.  Constants are discussed here.).
  5. Print the value.  Obviously, this is the step in the code where something more interesting needs to happen to make this method worthwhile.  I will expand this code in future posts to do something more useful here.

As I mentioned in #1, you can’t determine how many rows an IDfCollection has by interrogating it — or if it has any at all. Unfortunately, an empty collection is not returned as null from the IDfQuery.execute() method because it contains column definitions.  Therefore, testing for a null IDfCollection object will not indicate an empty collection.   The next few bullets present a simple flag that can be set to determine if a collection is empty (i.e., the query returned no results), as opposed to some other error that returned an empty IDfCollection object.

  1. Define a boolean variable to act as the flag for an empty IDfCollection object and set it to true (assume the collection is empty until content is actually retrieved).
  2. Once the next() method executes for the first time, set the flag to false.  If the collection is in fact empty, the code in the while loop will not execute.
  3. Simply test the value of the flag variable and act accordingly.

This code is a direct expansion of the basic processing loop presented in the previous post.  It has been enhanced  to accommodate a generic collection (i.e., not knowing what the column names or types are), and to handle empty collections.  Next week I’ll show you two methods for determining the size of an IDfCollection object.

Advertisements

About Scott
I have been implementing Documentum solutions since 1997. In 2005, I published a book about developing Documentum solutions for the Documentum Desktop Client (ISBN 0595339689). In 2010, I began this blog as a record of interesting and (hopefully) helpful bits of information related to Documentum, and as a creative outlet.

One Response to IDfCollections, Part II

  1. ufo bobot says:

    Thanks, it was useful

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: