< Anterior | Siguiente >

Creación de un registro

En esta lección, aprenderá a crear un registro nuevo y especificar los valores de campo del registro.
El ejemplo de código que se muestra en esta lección ofrece un ejemplo para permitir que un usuario cree un registro. Los diálogos iniciales permiten que el usuario seleccione la base de datos en la que se va a crear el registro y el tipo de registro que se va a crear. Una vez creado dicho registro, al usuario se le muestra el diálogo EditRecord. En este diálogo, el usuario puede establecer campos obligatorios u opcionales y, a continuación entregar el nuevo registro a la base de datos.
    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);
Cuando haya creado un registro nuevo, podrá realizar otras operaciones sobre él utilizando ClearQuest CM API. Por ejemplo, puede validar que el nuevo registro esté en la base de datos de usuarios ejecutando una consulta que devuelva el nuevo registro en el conjunto de resultados.

Punto de comprobación de la lección

Ahora ha aprendido a utilizar ClearQuest CM API para desarrollar acciones de aplicaciones cliente que realizan operaciones de creación en una base de datos de usuarios.
En esta lección, ha aprendido lo siguiente:
  • Cómo utilizar ClearQuest CM API para realizar operaciones con el fin de crear nuevos recursos y valores de la propiedad, como por ejemplo, un registro y los valores de campo en una base de datos de usuarios desde una aplicación cliente.
< Anterior | Siguiente >

Comentarios