< Previous | Next >

List available user databases

In this lesson you learn how to use the Rational® ClearQuest® CM API to discover the Rational ClearQuest databases accessible to a given user. Such a list might be presented to the user before starting to work with database resources such as records, fields, and queries.

About this task

The following example uses the CqProvider.doGeDbSetList() method to obtain a list of all the Rational ClearQuest database sets known to the provider. (A database set is sometimes called a configuration or a schema repository.) By accessing each of those database sets, a complete list of databases accessible to the user is obtained.

In this example, each user database is identified by a combination of its database set name (CqDbSet.DISPLAY_NAME) and its user database name (CqUserDb.DISPLAY_NAME). This is the canonical way to identify a user database in the Rational ClearQuest CM API. The complete location syntax for a user database is cq.userdb:<db-set>/<user-db>.

public static void main(String[] args) throws Exception
    {
        try {
            CqProvider provider = Utilities.getProvider().cqProvider();
            
            System.out.println("CM API Library...\n" 
                                 + provider.stpProductInfo(null));
            System.out.println("ClearQuest Adaptor...\n" 
                                 + provider.stpProductInfo(Domain.CLEAR_QUEST));
            
            // Iterate over the database sets known to the provider
            for (CqDbSet set : provider.doGetDbSetList(DB_PROPS)) {
                // Skip database set if user doesn't have access
                if (set.getResourceError() != null)
                    continue;
                
                // Identify the user databases to which the user is subscribed
                for (CqUserDb userDb: set.getCurrentUser().getSubscribedDatabases()) {
                    CqProductInfo info = (CqProductInfo)userDb.getProductInfo();
                    System.out.println (userDb.getUserFriendlyLocation().getRepo()
                                        + ": " + info.getFullProductVersion() +
                                        " (" + info.getStageLabel()+ ", OMREV " 
                                        + (info.getObjectModelVersionMajor()*100
                                        + info.getObjectModelVersionMinor()) + ")");
                }
            }
        } catch(Throwable ex) {
            ex.printStackTrace();
        } finally {
            System.exit(0);  // to terminate Swing threads
        }
    }
    
    /** Properties to be displayed for subscribed user databases */
    static final PropertyRequest DB_PROPS =
                new PropertyRequest(CqDbSet.CURRENT_USER
                    .nest(CqUser.SUBSCRIBED_DATABASES
                          .nest(CqUserDb.USER_FRIENDLY_LOCATION,
                                CqUserDb.PRODUCT_INFO)));
}
For use in the rest of this tutorial, we define Utilities.getUserDbList. It uses the same technique as the previous example to compute the set of accessible user databases, where each user database proxy of the result is populated with a set of properties supplied as an argument to the method.
    static ResourceList<CqUserDb> getUserDbList(CqProvider provider, 
                                                PropertyRequest feedback)
    throws WvcmException
    {
        PropertyRequest wantedProps =
            new PropertyRequest(CqDbSet.CURRENT_USER
                .nest(CqUser.SUBSCRIBED_DATABASES.nest(feedback)));
        ResourceList<CqUserDb> result = provider.resourceList();

        for (CqDbSet set : provider.doGetDbSetList(wantedProps)) {
            if (set.getResourceError() == null)
                result.addAll(set.getCurrentUser().getSubscribedDatabases());
        }

        return result;
    }

The only interaction with the Rational ClearQuest application occurs during the invocation of the CqProvider.doGetDbSetList method at the start of the outer loop. The DB_PROPS nested property request used in that invocation forces Rational ClearQuest to log the user into each database set it knows about and obtain from that database set a list of the user databases to which that user is subscribed.

As this method executes, the Callback object given to the provider will be invoked for each database set to obtain the user identity and password for that database set. If the user cannot provide the proper credentials for a given database set, the failed-login exception is stored in the resource-error field of the returned proxy. A non-null value in this field is the standard way to inform the client that a resource in a list of resources could not be accessed to satisfy a property request.

Results

Now that you have a list of the available user databases, you can log in to a specific database and then begin working with resources in it.

Lesson checkpoint

You have now learned how to use the Rational ClearQuest CM API to list specific product repositories such as user databases.
In this lesson you learned the following:
  • How to use Rational ClearQuest CM API to get accessible user databases.
  • About CqUserDb objects.
< Previous | Next >