Cloned Source sample (console) (Delphi)
The ClonedSource sample demonstrates using the clone source feature to create different presentations/views of a table's data. Each presentation can have its own filter or sort order but making modifications to the data table in any one of the clones, like updating a record or inserting/deleting a record, will also update any/all other views.
Getting Started
The sample is located in C:\Users\Public\Documents\RemObjects Samples\Data Abstract for Delphi\ClonedSource (Console)
.
To build it, you will need Delphi Rad Studio, and like all the samples provided with Data Abstract for Delphi you will need to be running Relativity Server with the DASamples
Domain and the Simple
Schema available.
Included with the sample is a uLogSupport
class which has a LogTable
procedure that takes an instance of TDADataTable and creates a nicely formatted string representation of the table that it then prints to the Console which can be useful for debugging purposes. (An example of its output can be seen at the end of this document)
Running the Sample
The sample retrieves the ProductGroups
table and prints the up to the first 25 rows of the table to the console (Step 2). It then creates another table object, setting the previously retrieved table as the CloneSource
, sets a filter and a new sorting order, and then prints the table to the console again to show that the ordering is different and also prints the original table again to show its untouched (Step 3). Finally a new record is inserted into the cloned table and the contents of the cloned table and the original table are printed to show that the record appears in both tables (Step 4).
Examining the Code
All of the code can be found in the uClonedSource
source file. The ProductGroups
table used in the sample has only 3 fields and 20 records.
Creating a Clone Source & filtering a table
Creating a table that will act as a clone of the original source table is quick and easy. First create a new table as normal. Then assign the table that will act as the source to the CloneSource property of a subclass of TDADataTable, here the TDAMemDataTable class is used.
To set a filter pass a string containing the filtering condition to the Filter property of TDADataTable (or a subclass). Once the filter is active, only those records that match the condition will be visible. To enable the filter, pass True
to the Filtered property.
The IndexFieldNames property allows you to define which of the table fields should be used as the index or sort field. To use it pass the field name as a string to the property.
The final step to make the clone table active is to call the Open procedure.
lClonedTable:= TDAMemDataTable.Create(nil);
... snipped code ...
lClonedTable.CloneSource := lTable;
lClonedTable.Filter := 'Parent=1';
lClonedTable.Filtered := True;
lClonedTable.IndexFieldNames := 'Name';
lClonedTable.Open;
Example Output
This is an example of the output you will see when the sample is run. In Step 2 you can see the full contents of the ProductGroups
table. In Step 3 the cloned table is displayed showing that the table is filtered to only show those records where the Parent
field is equal to 1
and the table is sorted based on the values in the Name
field. In Step 4 you can see that the table contains new record.
ClonedSource
:Cloned Source sample has been started.
Target URL is http://localhost:7099/bin
RO DataAbstract layer is configured.
STEP 1: Login to DataService
Connecting to Relativity server: http://localhost:7099/bin
Login string is :
User Id="simple";Password="simple";Domain="DASamples";Schema="Simple"
Login has been successfull. Going further...
STEP 2. Open ProductGroups table ...
Table: ProductGroups (20 rows from 20)
---------------------------------------------------------
| Id | Parent | Name |
---------------------------------------------------------
| 1 | 0 | Computer Parts |
| 2 | 1 | Motherboards |
| 3 | 1 | Hard-Disk Drives |
| 4 | 1 | Solid State Disks |
| 5 | 1 | RAM |
| 6 | 1 | CPU |
| 7 | 1 | Video |
| 8 | 1 | Cases |
| 9 | 1 | Keyboards |
| 10 | 1 | Mouses |
| 11 | 1 | Monitors |
| 12 | 0 | Computers |
| 13 | 0 | Notebooks |
| 14 | 13 | Netbooks |
| 15 | 0 | Tablets |
| 16 | 0 | Players |
| 17 | 0 | Printers |
| 18 | 17 | Dot Printers |
| 19 | 17 | Laser Printers |
| 20 | 17 | Inc Printers |
---------------------------------------------------------
STEP 3. Open cloned table and apply conditions ...
Filter: Parent=1
Sorting: Name
Table: ProductGroups (10 rows from 10)
---------------------------------------------------------
| Id | Parent | Name |
---------------------------------------------------------
| 8 | 1 | Cases |
| 6 | 1 | CPU |
| 3 | 1 | Hard-Disk Drives |
| 9 | 1 | Keyboards |
| 11 | 1 | Monitors |
| 2 | 1 | Motherboards |
| 10 | 1 | Mouses |
| 5 | 1 | RAM |
| 4 | 1 | Solid State Disks |
| 7 | 1 | Video |
---------------------------------------------------------
Original table wasn't affected to above conditional:
Table: ProductGroups (20 rows from 20)
---------------------------------------------------------
| Id | Parent | Name |
---------------------------------------------------------
| 1 | 0 | Computer Parts |
| 2 | 1 | Motherboards |
| 3 | 1 | Hard-Disk Drives |
| 4 | 1 | Solid State Disks |
| 5 | 1 | RAM |
| 6 | 1 | CPU |
| 7 | 1 | Video |
| 8 | 1 | Cases |
| 9 | 1 | Keyboards |
| 10 | 1 | Mouses |
| 11 | 1 | Monitors |
| 12 | 0 | Computers |
| 13 | 0 | Notebooks |
| 14 | 13 | Netbooks |
| 15 | 0 | Tablets |
| 16 | 0 | Players |
| 17 | 0 | Printers |
| 18 | 17 | Dot Printers |
| 19 | 17 | Laser Printers |
| 20 | 17 | Inc Printers |
---------------------------------------------------------
STEP 3. Insert new record into cloned table ...
Table: ProductGroups (10 rows from 10)
---------------------------------------------------------
| Id | Parent | Name |
---------------------------------------------------------
| 8 | 1 | Cases |
| 6 | 1 | CPU |
| 3 | 1 | Hard-Disk Drives |
| 9 | 1 | Keyboards |
| 11 | 1 | Monitors |
| 2 | 1 | Motherboards |
| 10 | 1 | Mouses |
| 5 | 1 | RAM |
| 4 | 1 | Solid State Disks |
| 999 | 1 | === New Group === |
---------------------------------------------------------
Record is also appeared in main table:
Table: ProductGroups (20 rows from 20)
---------------------------------------------------------
| Id | Parent | Name |
---------------------------------------------------------
| 1 | 0 | Computer Parts |
| 2 | 1 | Motherboards |
| 3 | 1 | Hard-Disk Drives |
| 4 | 1 | Solid State Disks |
| 5 | 1 | RAM |
| 6 | 1 | CPU |
| 999 | 1 | === New Group === |
| 8 | 1 | Cases |
| 9 | 1 | Keyboards |
| 10 | 1 | Mouses |
| 11 | 1 | Monitors |
| 12 | 0 | Computers |
| 13 | 0 | Notebooks |
| 14 | 13 | Netbooks |
| 15 | 0 | Tablets |
| 16 | 0 | Players |
| 17 | 0 | Printers |
| 18 | 17 | Dot Printers |
| 19 | 17 | Laser Printers |
| 20 | 17 | Inc Printers |
---------------------------------------------------------
Done!
Cleanup!
Press Return to exit...