< 上一课 | 下一课 >

创建记录

在本课程中,您将了解如何创建新记录并指定记录的字段值。
在本课程的下面这段示例代码中,描述了如何让用户创建记录。最初的对话框让用户选择一个数据库,在这个数据库中将要创建记录和记录类型。创建记录后,会向用户呈示一个 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);
创建一条新记录后,可以利用 ClearQuest® CM API 对其执行其他操作。例如,可以执行查询,看结果集中是否返回了这条新记录,从而验证这条新记录是否已在用户数据库中。

课程检查点

现在,您已经了解如何利用 ClearQuest CM API 来开发可在用户数据库中执行创建操作的客户机应用程序操作。
在本课程中,您学习到了以下内容:
  • 利用 ClearQuest CM API 执行操作,这些操作可通过客户机应用程序在用户数据库中创建新资源和属性值,例如记录及其字段值。
< 上一课 | 下一课 >

反馈