< 上一個課程 | 下一個課程 >

列示可用的使用者資料庫

在這一課,您學到如何使用 Rational® CM API,來探索可讓特定使用者存取的 Rational ClearQuest® 資料庫。在開始使用資料庫資源(例如記錄、欄位及查詢)之前,使用者可能會看到這類清單。

下列範例使用 CqProvider.doGeDbSetList() 方法,取得提供者已知的所有 Rational ClearQuest 資料庫集清單。(資料庫集有時稱為配置或綱目儲存庫。)藉由存取每一個資料庫集,可取得使用者可存取的完整資料庫清單。

在這個範例中,以每一個使用者資料庫的資料庫集名稱 (CqDbSet.DISPLAY_NAME) 及其使用者資料庫名稱 (CqUserDb.DISPLAY_NAME) 之組合來識別該使用者資料庫。以 Rational CM API 來說,這是識別使用者資料庫的標準方法。使用者資料庫的完整位置語法是 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));
            
            // 反覆使用提供者已知的資料庫集
            for (CqDbSet set : provider.doGetDbSetList(DB_PROPS)) {
                // 如果使用者沒有存取權,則跳過資料庫集
                if (set.getResourceError() != null)
                    continue;
                
                // 識別使用者訂閱的使用者資料庫
                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);  // 終止 Swing 執行緒
        }
    }
    
    /** 針對已訂閱的使用者資料庫顯示的內容 */
    static final PropertyRequest DB_PROPS =
                new PropertyRequest(CqDbSet.CURRENT_USER
                    .nest(CqUser.SUBSCRIBED_DATABASES
                          .nest(CqUserDb.USER_FRIENDLY_LOCATION,
                                CqUserDb.PRODUCT_INFO)));
}
為了在本指導教學的其餘課程中使用,我們定義了 Utilities.getUserDbList。它採用與前一個範例相同的技術來計算可存取的使用者資料庫集,其中,該結果的各個使用者資料庫 Proxy 中會移入一組內容,該組內容是作為此方法的引數。
    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;
    }

與 ClearQuest 應用程式的唯一互動,是發生於開始外部迴圈之際的 CqProvider.doGetDbSetList 方法呼叫期間。在該呼叫中使用的 DB_PROPS 巢狀內容要求會強迫 ClearQuest 將使用者記載至它已知的每一個資料庫集,並從該資料庫集取得該使用者訂閱的使用者資料庫清單。

當這個方法執行時,會為每一個資料庫集呼叫給予提供者的 Callback 物件,以取得該資料庫集的使用者身分和密碼。如果使用者無法為給予的資料庫集提供適當認證,則失敗登入異常會儲存於傳回的 Proxy 的資源錯誤欄位中。這個欄位中的非空值是通知用戶端,無法存取資源清單中的某個資源以滿足內容要求的標準方法。

既然您有了可用的使用者資料庫清單,您就可以登入特定資料庫,然後開始使用該資料庫中的資源。

課程檢查點

您目前已瞭解如何使用 Rational CM API,列示諸如使用者資料庫之類的特定產品儲存庫。
在這一課,您學到下列各項:
  • 如何使用 Rational CM API 以取得可存取的使用者資料庫。
  • 關於 CqUserDb 物件。
< 上一個課程 | 下一個課程 >

意見反應