Understanding Data Buffer Classes Examples

This section discusses:

  • Employee Checklist page structure.

  • Object creation examples.

  • Data buffer hierarchy examples.

  • Rowset examples.

  • Hidden work scroll area example.

Most of the examples in this section use the Employee Checklist page.

Image: Employee Checklist page

The following image illustrates a structure in a Employee Checklist page.

Employee Checklist page

This page has the following record structure:

Scroll Level

Associated Primary Record

Rowset and Variable Name

Level zero

PERSONAL_DATA

Level zero rowset: &RS0

Level one scroll area

EMPL_CHECKLIST

Level one rowset: &RS1

Level one hidden work scroll area

CHECKLIST_ITEM

Level one rowset: &RS1H

Level two scroll area

EMPL_CHKLST_ITM

Level two rowset: &RS2

Another way of looking at the structure of a component is to use the Structure view. All the scroll areas are labeled, and the primary record is associated with each:

Image: EMPLOYEE_CHECKLIST structure

The following image explains EMPLOYEE_CHECKLIST structure of a component where in all the scroll areas are labeled, and the primary record is associated with each other.

EMPLOYEE_CHECKLIST structure

In the example, the visible level one scroll area also has only one row. That row is made up of the following records:

  • EMPL_CHECKLIST

  • DERIVED_HR

  • CHECKLIST_TBL

  • PERSONAL_DATA

You can see which records are associated with a scroll area by looking at the Order view for a page:

Image: EMPLOYEE_CHECKLIST page Order view showing records

The following image is an example of EMPLOYEE_CHECKLIST page. This page shows the Order view of the records associated with a scroll area.

EMPLOYEE_CHECKLIST page Order view showing records

Image: EMPLOYEE_CHECKLIST rowsets and rows

The level two rowset has three rows. Each row is made up of two records: the primary record, EMPL_CHKLST_ITM, and CHKLST_ITM_TBL, the record associated with the related display field DESCR. The following example shows the rowset:

EMPLOYEE_CHECKLIST rowsets and rows

Every record has fields associated with it, such as NAME, EMPLID and CHECKLIST_SEQ. These fields are associated with the record definitions; they are not the fields that appear on the page.

When declaring variables, use the class with the same name as the data buffer access data type (rowset objects should be declared as type Rowset, field objects as type Field, and so on). Data buffer variables can be of type Local, Global, or Component.

The following declarations are assumed throughout the examples that follow:

Local Rowset &LEVEL0, &ROWSET;
Local Row &ROW;
Local Record &REC;
Local Field &FIELD;

Level Zero Access

The following code instantiates a rowset object, from the Rowset class, that references the level zero rowset, containing all the page data. It stores the object in the &LEVEL0 variable.

&LEVEL0 = GetLevel0();

The level zero rowset contains all the rows, rowsets, records, and fields underneath it.

If the level zero rowset is formed from component buffer data, then the level zero rowset has one row of data and that row contains all the child rowsets, which in turn contain rows of data that contain other child rowsets.

If the level zero rowset is formed from buffer data, such as from an application message, then the level zero rowset may contain more than one row of data. Each row of the level zero rowset contains all the child rowsets associated with that row, which in turn contain rows of data that contain other child rowsets.

Use a level zero rowset when you want an absolute path to a lower-level object or to do some processing on the entire data buffer. For example, suppose you load all new data into the component buffers and want to redraw the page. You could use the following code:

/* Do processing to reload Component Buffers */
&LEVEL0 = GetLevel0();
&LEVEL0.Refresh();

Rowset Object

The following code instantiates a rowset object that references the rowset that contains the currently running PeopleCode program:

&ROWSET = GetRowset();

You might later use the &ROWSET variable and the ActiveRowCount property to iterate over all the rows of the rowset, to access a specific row (using the GetRow method), or to hide a child rowset (by setting the Visible property).

The level one rowset contains all the level two rowsets. However, the level two rowsets can only be accessed using the different rows of the level one rowset. From the level zero or level one rowset, you can only access a level two rowset by using the level one rowset and the appropriate row.

Image: Level two rowset from level one row

For example, suppose your program is running on some field of row five of a level two scroll area, which is on row three of its level one scroll area. The resulting rowset contains all the rows of the level two scroll area that are under the row three of the level one scroll area. The rowset does not contain any data that is under any other level two scroll areas. The following diagram illustrates these results:

Level two rowset from level one row

A further illustration uses an example from the Employee Checklist page.

Suppose that one employee was associated with three different checklists: Foreign Loan Departure, Foreign Loan Arrival, and Foreign Loan Host. The checklist code field (CHECKLIST_CD) on the first level of the page drives the entries on the second level. Each row in the level one rowset produces a different level two rowset.

Image: EMPLOYEE_CHECKLIST Foreign Loan Departure checklist

The Foreign Loan Departure checklist (000001) produces a checklist that contains such items as Briefing with Human Resources and Apply for Visas/Work permits, as shown in the following example:

EMPLOYEE_CHECKLIST Foreign Loan Departure checklist

Image: EMPLOYEE_CHECKLIST Foreign Load Arrival Checklist

The Foreign Loan Arrival checklist (0000004) produces a checklist that contains items such as Register at Consulate and Open New Foreign Bank Accounts, as shown in the following example:

EMPLOYEE_CHECKLIST Foreign Load Arrival Checklist

Row Object

When you create a page, you put fields from different records onto the page. You can think of this as creating a type of pseudo-SQL join. The row returned from this pseudo-join is a row object.

For example, the first level scroll area of the EMPLOYEE_CHECKLIST page contains the following fields, associated with these records:

Field

Record

CHECKLIST_DT

EMPL_CHECKLIST

CHECKLIST_CD

EMPL_CHECKLIST

COMMENTS

EMPL_CHECKLIST

DESCR

CHECKLIST_TBL

NAME

PERSONAL_DATA

RESPONSIBLE_ID

EMPL_CHECKLIST

The pseudo-SQL join might look like this:

JOIN A.CHECKLIST_DT, A.CHECKLIST_CD, A.COMMENTS, B.DESCR, C.NAME, A.RESPONSIBLE_ID
FROM PS_EMPL_CHECKLIST A, PS_CHECKLIST_TBL B, PS_PERSONAL_DATA C, WHERE. . . 

What goes into the Where clause is determined by the level zero of the page. For our example, the value is WHERE EMPLID=8001.

When the component is opened, data is loaded into the component buffers. Any row returned by the pseudo-SQL statement is a level one row object. The following table shows a returned row:

CHECKLIST_DT

CHECKLIST_CD

COMMENTS

DESCR

NAME

RESPONSIBLE_ID

12/03/98

000001

Foreign Loan Department Checklist

Peppen, Jacques

6602

Record Object

A record definition is a definition of what your underlying SQL database tables look like and how they process data. After you create record definitions, you build the underlying SQL tables that contain the application data that your users enter online in your production environment.

When you create a record object using the CreateRecord function, you are creating an area in the data buffers that has the same structure as the record definition, but no data.

When you instantiate a record object from the Record class using some variation of GetRecord, that record object references a single row of data in the SQL table.

Note: The data in the record that you retrieve is based on the row, which is analogous to setting keys to return a unique record.

The following code instantiates a record object for referencing the EMPL_CHECKLIST record of the specified row:

&REC = &ROW.GetRecord(RECORD.EMPL_CHECKLIST);

Using the short method, the following line of code is identical to the previous line:

&REC = &ROW.EMPL_CHECKLIST;

You might later use the &REC variable and the CopyFieldsTo property to copy all like-named fields from one record to another. In the following example, two row objects are created, the copy from row (COPYFRMROW) and the copy to row (COPYTROW). Using these rows, like-named fields are copied from CHECKLIST_ITEM to EMPL_CHKLST_ITM.

For &I = 1 To &ROWSET1.ActiveRowCount
   &COPYFRMROW = &ROWSET1.GetRow(&I);
   &COPYTROW = &RS2.GetRow(&I);
   &COPYFRMROW.CHECKLIST_ITEM.CopyFieldsTo(&COPYTROW.EMPL_CHKLST_ITM);
End-For;

A row may contain more than one record: in addition to the primary database record, you may have a related display record or a derived record. You can access these records as well. The level one rowset, &ROWSET1, is made up of many records. The following accesses two of them: EMPL_CHECKLIST and DERIVED_HR.

&REC1 = &ROW.EMPL_CHECKLIST;
&REC2 = &ROW.DERIVED_HR;

Field Object

The following instantiates a field object, from the Field class, that is used to access a specific field in the record:

&FIELD = &REC.GetField(FIELD.CHECKLIST_CD);

You might later use the &FIELD variable as a condition:

If ALL(&FIELD) Then

Here is another example:

If &FIELD.Value = "N" Then

Note: The data in the field that you retrieve is based on the record, which is in turn based on the row.

You can also set the value of a field. Using the GetField function does not create a copy of the data from the component buffer. Setting the value or a property of the field object sets the actual component buffer field or property.

See Assigning Objects.

In the following example, the type of field is verified, and the value is replaced with the tangent of that value if it is a number

If &FIELD.Type <> "NUMBER" Then
   /* do error recording */
Else
   &FIELD.Value = Tan(&FIELD.Value);
End-If;

Image: EMPLOYEE_CHECKLIST repatriation checklist

The following EMPLOYEE_CHECKLIST image is an example of data buffer hierarchy.

EMPLOYEE_CHECKLIST repatriation checklist

Suppose you want to access the BRIEFING_STATUS field at level two of the following page:

First, determine where your code is running. For this example, the code is starting at a field on a record at level zero. However, you do not always have to start at level zero.

If you start with level zero, you must traverse the data hierarchy, through the level one rowset to the level two rowset, before you can access the record that contains the field.

Obtaining the Rowset

You first obtain the level zero rowset, which is the PERSONAL_DATA rowset. You do not need to know the name of the level zero rowset to access it:

&LEVEL0 = GetLevel0();

Obtaining Rows

The next object to get is a row. As the following code is working with data that is loaded from a page, only one row is at level zero. However, if you have rowsets that are populated with data that is not based on component buffers (for example, an application message), you may have more than one row at level zero.

&LEVEL0_ROW = &LEVEL0(1);

Obtaining Child Rowsets

To obtain the level two rowset, traverse through the level one rowset first. Therefore, the next object to get is the level one rowset, as shown in the following example:

&LEVEL1 = &LEVEL0_ROW.GetRowset(SCROLL.EMPL_CHECKLIST);

Obtaining Subsequent Rows

If you are traversing a page, obtain the appropriate row after you get a rowset. To process all the rows of the rowset, set this functionality up in a loop, as shown in the following example:

For &I = 1 to &LEVEL1.ActiveRowCount
   &LEVEL1_ROW = &LEVEL1(&I);
   . . .
End-For;

Obtaining Subsequent Rowsets and Rows

Traverse another level in the page structure to access the second level rowset, and then use a loop to access the rows in the level two rowset.

Because we are processing all the rows at level one, we are just adding code to the previous For loop. As we process through all the rows at level two, we are adding a second For loop. The new code is in bold in the following example:

For &I = 1 to &LEVEL1.ActiveRowCount
   &LEVEL1_ROW = &LEVEL1(&I);
   &LEVEL2 = &LEVEL1_ROW.GetRowset(SCROLL.
EMPL_CHKLST_ITM);
   For &J = 1 to &LEVEL2.ActiveRowCount
      &LEVEL2_ROW = &LEVEL2(&J);
   . . .
   End-For;
End-For;

Obtaining Records

A row always contains a record, and it may contain only a child rowset, depending on how your page is set up. GetRecord is the default method for a row, so all you have to specify is the record name.

Because we are processing all the rows at level two, we just add code to the For loops of the previous example. The new code is in bold:

For &I = 1 to &LEVEL1.ActiveRowCount
   &LEVEL1_ROW = &LEVEL1(&I);
   &LEVEL2 = &LEVEL1_ROW.GetRowset(SCROLL.EMPL_CHKLST_ITM);
   For &J = 1 to &LEVEL2.ActiveRowCount
      &LEVEL2_ROW = &LEVEL2(&J);
      &RECORD = &LEVEL2_ROW.EMPL_CHKLST_ITM;
   . . .
   End-For;
End-For;

Obtaining Fields

Records are made up of fields. GetField is the default method for a record, so all you have to specify is the field name.

Because we are processing all the rows at the level one, we are just adding code to the For loops of the previous example. The new code is in bold:

For &I = 1 to &LEVEL1.ActiveRowCount
   &LEVEL1_ROW = &LEVEL1(&I);
   &LEVEL2 = &LEVEL1_ROW.GetRowset(SCROLL.EMPL_CHKLST_ITM);
   For &J = 1 to &LEVEL2.ActiveRowCount
      &LEVEL2_ROW = &LEVEL2(&J);
      &RECORD = &LEVEL2_ROW.EMPL_CHKLST_ITM;
      &FIELD = &RECORD.BRIEFING_STATUS;
      /* Do processing */
   End-For;
End-For;

Using Shortcuts

Image: Rowset example

The previous code is the long way of accessing this field. The following example uses shortcuts to access the field in one line of code. The following code assumes all rows are level one:

Rowset example

Here’s another method of expressing the code:

Object Type

Code

Rowset
&LEVEL0 = GetLevel0();
Row
&LEVEL0_ROW = &LEVEL0(1);
Rowset
&LEVEL1 = &LEVEL0_ROW.GetRowset(SCROLL.EMPL_CHECKLIST);
For &I = 1 to &LEVEL1.ActiveRowCount
Row
&LEVEL1_ROW = &LEVEL1(&I);
Rowset
&LEVEL2 = &LEVEL1_ROW.GetRowset(SCROLL.EMPL_CHKLST_ITM);
    For &J = 1 to &LEVEL2.ActiveRowCount
Row
       &LEVEL2_ROW = &LEVEL2(&J);
Record
       &RECORD = &LEVEL2_ROW.EMPL_CHKLST_ITM;
Field
       &FIELD = &RECORD.BRIEFING_STATUS;
            /* Do processing */
    End-For;
End-For;

The following code example traverses up to four levels of rowsets and could easily be modified to do more. This example only processes the first record in every rowset. To process every record, set up another For loop (For &R = 1 to &LEVELX.RECORDCOUNT, and so on). Notice the use of the ChildCount function (to process all children rowsets within a rowset), ActiveRowCount, IsChanged, and dot notation.

In the following example, ellipses indicate where application-specific code should go.

&Level0_ROWSET = GetLevel0();
For &A0 = 1 To &Level0_ROWSET.ActiveRowCount

    ... 

/***************************/
/* Process Level 1 Records */
/*-------------------------*/
   If &Level0_ROWSET(&A0).ChildCount > 0 Then
   For &B1 = 1 To &Level0_ROWSET(&A0).ChildCount
      &LEVEL1_ROWSET = &Level0_ROWSET(&A0).GetRowset(&B1);
      For &A1 = 1 To &LEVEL1_ROWSET.ActiveRowCount
      If &LEVEL1_ROWSET(&A1).GetRecord(1).IsChanged Then

          ... 

 /***************************/
 /* Process Level 2 Records */
 /*-------------------------*/
         If &LEVEL1_ROWSET(&A1).ChildCount > 0 Then
         For &B2 = 1 To &LEVEL1_ROWSET(&A1).ChildCount
            &LEVEL2_ROWSET = &LEVEL1_ROWSET(&A1).GetRowset(&B2);
            For &A2 = 1 To &LEVEL2_ROWSET.ActiveRowCount
            If &LEVEL2_ROWSET(&A2).GetRecord(1).IsChanged Then

               ... 

/***************************/
/* Process Level 3 Records */
/*-------------------------*/
               If &LEVEL2_ROWSET(&A2).ChildCount > 0 Then
               For &B3 = 1 To &LEVEL1_ROWSET(&A2).ChildCount
                  &LEVEL3_ROWSET = &LEVEL2_ROWSET(&A2).GetRowset(&B3);
                  For &A3 = 1 To &LEVEL3_ROWSET.ActiveRowCount
                  If &LEVEL3_ROWSET(&A3).GetRecord(1).IsChanged Then

                      ...
   
                  End-If; /* A3 - IsChanged */
                  End-For; /* A3 - Loop */
               End-For; /* B3 - Loop */
               End-If; /* A2 - ChildCount > 0 */
/*--------------------------------*/
/* End of Process Level 3 Records */
/**********************************/

            End-If; /* A2 - IsChanged */
            End-For; /* A2 - Loop */
            End-For; /* B2 - Loop */
            End-If; /* A1 - ChildCount > 0 */
/*--------------------------------*/
/* End of Process Level 2 Records */
/**********************************/

      End-If; /* A1 - IsChanged */
      End-For; /* A1 - Loop */
   End-For; /* B1 - Loop */
   End-If; /* A0 - ChildCount > 0 */

/*--------------------------------*/
/* End of Process Level 1 Records */
/**********************************/
    
End-For; /* A0 - Loop */

In the FieldChange event for the CHECKLIST_CD field on the EMPL_CHECKLIST record, a PeopleCode program does the following:

  1. Flushes the rowset and hidden work scroll area.

  2. Performs a Select statement on the hidden work scroll area based on the value of the CHECKLIST_CD field and the effective date.

  3. Clears the level two scroll area.

  4. Copies like-named fields from the hidden work scroll area to the level two scroll area.

The following example shows how to do this using built-in functions.

&CURRENT_ROW_L1 = CurrentRowNumber(1);

&ACTIVE_ROW_L2 = ActiveRowCount(RECORD.EMPL_CHECKLIST, 
&CURRENT_ROW_L1, RECORD.EMPL_CHKLST_ITM);

If All(CHECKLIST_CD) Then
   ScrollFlush(RECORD.CHECKLIST_ITEM);
   ScrollSelect(1, RECORD.CHECKLIST_ITEM, RECORD.CHECKLIST_ITEM, 
"Where Checklist_Cd = :1 and EffDt = (Select Max(EffDt) From 
PS_Checklist_Item Where Checklist_Cd = :2)", 
CHECKLIST_CD, CHECKLIST_CD);

   &FOUNDDOC = FetchValue(CHECKLIST_ITEM.CHKLST_ITEM_CD, 1);
   &SELECT_ROW = ActiveRowCount(RECORD.CHECKLIST_ITEM);

   For &I = 1 To &ACTIVE_ROW_L2
      DeleteRow(RECORD.EMPL_CHECKLIST, &CURRENT_ROW_L1, RECORD.EMPL_CHKLST_ITM, 1);
   End-For;

   If All(&FOUNDDOC) Then
      For &I = 1 To &SELECT_ROW
         CopyFields(1, RECORD.CHECKLIST_ITEM, &I, 2, 
RECORD.EMPL_CHECKLIST, &CURRENT_ROW_L1, RECORD.EMPL_CHKLST_ITM, &I);
         If &I <> &SELECT_ROW Then
            InsertRow(RECORD.EMPL_CHECKLIST, &CURRENT_ROW_L1, 
RECORD.EMPL_CHKLST_ITM, &I);
         End-If;
      End-For;
   End-If;
End-If;

The following example performs the same function as the previous code, only it uses the data buffer classes:

  1. Flushes the rowset and hidden work scroll area (&RS1H).

  2. Performs a Select statement on &RS1H based on the value of the CHECKLIST_CD field and the effective date.

  3. Clears the level two rowset (&RS2).

  4. Copies like-named fields from &RS1H to &RS1.

    Local Rowset &RS0, &RS1, &RS2, &RS1H;
    
    &RS0 = GetLevel0();
    &RS1 = GetRowset();
    &RS2 = GetRowset(SCROLL.EMPL_CHKLST_ITM);
    &RS1H = &RS0.GetRow(1).GetRowset(SCROLL.CHECKLIST_ITEM);
    
    &MYFIELD = CHECKLIST_CD;
    
    If All(&MYFIELD) Then
       &RS1H.Flush();
       &RS1H.Select(RECORD.CHECKLIST_ITEM, "where Checklist_CD = :1 
    and EffDt = (Select Max(EffDt) from PS_CHECKLIST_ITEM 
    Where CheckList_CD = :2)", CHECKLIST_CD, CHECKLIST_CD);
    
       For &I = 1 To &RS2.ActiveRowCount
          &RS2.DeleteRow(1);
       End-For;
    
    &FOUND = &RS1H.GetCurrEffRow().CHECKLIST_ITEM. CHKLST_ITEM_CD.Value;
    
       If All(&FOUND) Then
          For &I = 1 To &RS1H.ActiveRowCount
             &COPYFRMROW = &RS1H.getrow(&I);
             &COPYTROW = &RS2.getrow(&I);        
             &COPYFRMROW.CHECKLIST_ITEM.CopyFieldsTo(&COPYTROW.EMPL_CHKLST_ITM);
             If &I <> &RS1H.ActiveRowCount Then
                &RS2.InsertRow(&I);
             End-If;
          End-For;
       End-If;
    End-If;