4D S.A. Copyright 2001

When you create a selection of records, you create a list of references to the selected records in the server machine memory. To work with the data contained in the selected records, you need to retrieve a local copy of the records. These records are returned on the client machine in data buffers that you can parse using 4D Open for Java methods.

You can add, modify, or delete the retrieved data. You can then return your changes to the server in a buffer that you created and filled with your data.

As in any multi-user system, 4D Server provides you with a scheme for locking data when you want to perform data modifications. In 4th Dimension, locks are record-based: a record is either locked or unlocked. In order to minimize network traffic for locking and unlocking requests, 4D Server allows you set the access mode for each table in each process to read-write or read only.

In addition, 4D Open offers you 4th Dimension transactions to maintain data integrity during multiple updates. This is discussed in the next section.

Working with the Current Selection


In many cases, you may want to retrieve several records at once. For instance, you might want to display a selection of records in a list. To do this, you would create a selection of records on the server and then retrieve the selection of records locally.

Once you have retrieved a selection of records, you can allow users to modify or add to the data in the selection. The modified data can then be returned to the server.

Retrieving Data from the Current Selection

To retrieve the current selection of records from the server, you call the LoadFields method. This method returns the records in an opSelection. This method returns the datas in an array, which is filled according to a format declared in an opFieldArray.

The diagram below depicts a table in which records are rows and fields are columns. The current selection of records in this table can be thought of as a discontinuous group of rows. When you retrieve a selection of records by calling LoadFields, you can specify the fields of data that you want from this selection of records. In this case, 4D Server returns a discontinuous group of columns from this group of rows:

Updating the Selected Records

The ArrayToSelection method is the counterpart to the SelectionToArray method. This method allows you to update an entire selection in one request. The contents of the current selection are overwritten. If there are more records in the array than in the current selection, new records will be added in the 4D Server database. If the current selection is empty, new records will be created for the entire array. Using this method, you can quickly and easily create a large quantity of records.

Note: When data is overwritten, only the fields specified are modified. The other fields are left unchanged.

To send the contents of the array to the server, you call the ArrayToSelection method. When you call this routine, the contents of the buffer overwrite the contents of the current selection on the server. The diagram below depicts how ArrayToSelection can be used.

Retrieving Distinct Values from the Current Selection

In some cases, you may want to view all the unique values in a particular field. For instance, you might want to view the distinct values for a Title field in a personnel database. By setting the current selection to all records in the table, you could use the DistinctValues method to get a list of all the job titles used in the company.

Deleting the Current Selection

4D Open provides routines for deleting records. You can delete the current selection of records by using DeleteSelection method.

Working with the Current Record


In some cases, you may want to work with a single record at a time. Using 4D Open for Java, you can specify the current record and then work with the data in that record or the data in the records related to it. There are a number of operations that you can perform on the current record, including:

Retrieving data from the fields in the current record or the record(s) related to it,

Modifying the current record,

Deleting the current record.

Specifying the Current Record

You can specify the record to be made the current record by calling the GotoSelectedRecord method. The GotoSelectedRecord method does not modify the current selection of records. If you want to change the current record and make it the only record in the selection, you can use the GotoRecord method.

Retrieving Data from the Current Record

After you specify the current record, you need to specify the fields that you want to retrieve from the current record. You can do this by using the LoadFields method. Upon execution of this routine, the fields for the current record are returned in an array:

   resultDataArray = currentProcess.LoadFields(fieldArray); 

where fieldArray is an opFieldArray that specifies what field we want to retrieve.

Modifying Data in the Current Record

To modify data on the server, you create a new instance of opDataArray to hold the modified data, fill it with new values, and then request that 4D Open for Java modify the current record using the contents of the array.

Creating a new Record

To create a new record on the server, you create a new instance of opDataArray to hold the new data, fill it with new values, and then request that 4D Open for Java create the current record using the contents of the array.

Generating a Sequence Number for a New Record: In some databases, it is important that each record store a unique, non-repeating number in one of its fields. In an invoicing database, for instance, each customer should have a unique ID number. For the first customer, the customer ID number would start at 1. For each subsequent customer, the customer ID number would be incremented.

In 4D Server, a unique, non-repeating number is called a sequence number. Sequence numbers can automatically be generated for a field in a file. You can generate a sequence number from 4D Open For Java by calling the SequenceNumber method.

Note: 4D Server guarantees that you will never get the same sequence number twice. However, 4D Server does not guarantee a pure sequential numbering because you can have concurrent processes incrementing the same sequence number. In such a case, if a process cancels its request, for instance while adding a new record, 4D Server cannot "reuse" the number if the sequence number has been previously incremented by another process.

Deleting the Current Record

4D Open For Java provides routines for deleting records. You can delete the current record by using the DeleteRecord method.

Setting a Table to Read-Only or Read-Write


Each table in a database is in either read-write or read-only mode for each process of the database. Read-only means that records in the table can be loaded but not modified. Read-write means that records in the table can be loaded and modified if no other user has locked the record first.

Using the 4D Open For Java routines ReadOnly or ReadWrite, you can set the mode of a table for the current process. To change the status of all the tables, you can pass -1 as the target table to ReadOnly or ReadWrite.

Note: If you change the mode of a table, the change takes effect for the next record loaded. If there is a record currently loaded when you change the table's mode, that record is not affected by the change.

Read-Only Mode

While a table is in read-only mode, you will be able to display or print records in the table, but not modify them. If you want only to display records, you should set the table to read-only mode. This will ensure that other users will be able to access the records in the table in read-write mode.

Note that although you cannot modify or delete records in a read-only table, you can add new records.

Read-Write Mode

If you want to modify records in a table, the table must be in read-write mode. By default, all tables are in read-write mode when you connect to a database. In read-write mode, you can modify a record as long as no other user or process has locked the record first. If the record is locked, you will be able to view the record but not modify it.


4D S.A. Copyright 2001