이 레슨에서는 레코드를 새로 작성하고 레코드의 필드 값을
지정하는 방법을 학습합니다.
이 레슨의 코드 예제는 사용자가 레코드를 작성할 수 있도록 하기 위한
예제를 제공합니다. 초기 대화 상자에서는 사용자가 레코드를 작성할 데이터베이스와
작성할 레코드의 유형을 선택할 수 있습니다. 레코드가 작성되면
사용자에게 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)));
// Read the list of all record types from the selected database and
// remove from that list those record types that are not submittable.
ResourceList<CqRecordType> rTypes =
setUserFriendlyLocation(userDb.getRecordTypeSet());
Iterator<CqRecordType> types = rTypes.iterator();
while (types.hasNext()) {
if (!types.next().getIsSubmittable())
types.remove();
}
// Present the list of submittable record types to the user for
// selection
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);
// The actual name for the new record is determined by the
// schema. All that is needed here is a "suggested" location
// that makes the record a member of the specified record type.
CqRecord record = cqRecordType.cqProvider().cqRecord((StpLocation) recordType
.getUserFriendlyLocation().child("new"));
// Create the record. Don't try to deliver it yet since mandatory
// fields may need to be set by the user before delivery will
// succeed.
record = record.doCreateRecord(RECORD_PROPERTIES, CqProvider.HOLD);
/*
* After delivering the created record to its database, the
* EditRecord dialog will want to redisplay it in its own viewer.
* We need to create this "original" proxy after the fact
* because we don't have a valid location for the new record until
* after it has been created. Need to use the stable location
* because, in some cases, the user-friendly location can change
* when field values are changed.
*/
CqRecord selected = recordType.cqProvider()
.cqRecord(record.getStableLocation());
// With the new record created in the change context, the process
// proceeds in the same fashion as editing a record. Mandatory
// fields must be supplied by the user and then it can be delivered.
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);
}
}
/** The record type properties read prior to creating a record */
final static PropertyRequest RECORD_TYPE_PROPERTIES =
new PropertyRequest(CqRecordType.USER_FRIENDLY_LOCATION,
CqRecordType.IS_SUBMITTABLE,
CqRecordType.DISPLAY_NAME);
레코드를 새로 작성하면, Rational® CM
API를 사용하여 레코드에 대해 다른 오퍼레이션을 수행할 수 있습니다.
예를 들어, 결과 세트에 새 레코드를 리턴하는 조회를 실행하여 새 레코드가
사용자 데이터베이스에 있는지 검증할 수 있습니다.