In this lesson, you learn how to create a new record and
specify field values of the record.
About this task
The code example in this lesson provides an example for allowing
a user to create a record. Initial dialogs allow the user to select
the database in which to create the record and the type of record
to create. After the record is created, the user is presented with
the EditRecord dialog. In that dialog, the user can set mandatory
or optional fields and then deliver the new record to the database.
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);