這一課的程式碼範例提供可讓使用者建立記錄的範例。起始對話框容許使用者選取要在其中建立記錄的資料庫及要建立的記錄類型。建立記錄之後,使用者會看到 EditRecord 對話框。在該對話框中,使用者可以設定必要欄位或選用欄位,然後將新記錄傳遞至資料庫。
public static void main(String[] args)
{
try {
CqProvider provider = Utilities.getProvider().cqProvider();
Viewer viewer = new Viewer(provider);
ResourceList<CqUserDb> databases = Utilities.getUserDbList(provider, null);
CqUserDb userDb = (CqUserDb) JOptionPane
.showInputDialog(null,
"Choose a Database for the New Record",
"Create Record",
JOptionPane.INFORMATION_MESSAGE,
null,
databases.toArray(new Object[] {}),
databases.get(0));
if (userDb == null) System.exit(0);
userDb = (CqUserDb) userDb
.doReadProperties(new PropertyRequest(CqUserDb.RECORD_TYPE_SET
.nest(RECORD_TYPE_PROPERTIES)));
// 從所選取資料庫讀取所有記錄類型清單,
// 並從該清單移除無法提交的那些記錄類型。
ResourceList<CqRecordType> rTypes =
setUserFriendlyLocation(userDb.getRecordTypeSet());
Iterator<CqRecordType> types = rTypes.iterator();
while (types.hasNext()) {
if (!types.next().getIsSubmittable())
types.remove();
}
// 將可提交的記錄類型清單呈現給使用者
// 選擇
CqRecordType recordType = (CqRecordType) JOptionPane
.showInputDialog(null,
"Choose the type of record to create",
"All Record Types in "
+ userDb.location().string(),
JOptionPane.INFORMATION_MESSAGE,
null,
rTypes.toArray(new CqRecordType[] {}),
rTypes.get(0));
if (recordType == null) System.exit(0);
// 新記錄的實際名稱由
// 綱目決定。此處只需要「建議的」位置,
// 該位置使記錄成為指定的記錄類型之成員。
CqRecord record = cqRecordType.cqProvider().cqRecord((StpLocation) recordType
.getUserFriendlyLocation().child("new"));
// 建立記錄。目前請勿傳送它,因為使用者
// 使用者需要先設定欄位,傳遞才會
// 成功。
record = record.doCreateRecord(RECORD_PROPERTIES, CqProvider.HOLD);
/*
* 在將已建立的記錄傳給其資料庫之後,
* EditRecord 對話框需要在自己的檢視器中重新顯示該記錄。
* 事後我們需要建立這個「原始」Proxy,
* 因為要等到建立這個 Proxy 之後,我們才會有新記錄的有效位置。
* 需要使用可靠的位置,
* 因為有時候如果欄位值變更,
* 則對使用者友善的位置會變更。
*/
CqRecord selected = recordType.cqProvider()
.cqRecord(record.getStableLocation());
// 在變更環境定義中建立新記錄之後,
// 處理程序的進行方式與編輯記錄一樣。必要
// 欄位必須由使用者提供,然後才能傳送該欄位。
viewer.editRecord("Create Record ", record, selected)
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (Throwable ex) {
ex.printStackTrace();
Utilities.exception(null, "Create Record", ex);
System.exit(0);
}
}
/** 在建立記錄之前讀取記錄類型內容 */
final static PropertyRequest RECORD_TYPE_PROPERTIES =
new PropertyRequest(CqRecordType.USER_FRIENDLY_LOCATION,
CqRecordType.IS_SUBMITTABLE,
CqRecordType.DISPLAY_NAME);
建立新記錄之後,您就可以使用 ClearQuest® CM API 來對該記錄執行其他作業。例如,您可以執行查詢並在結果集中傳回新記錄,來驗證新記錄是否在使用者資料庫中。