Previous Topic: Screen Field Mapping Parameters Panel

Next Topic: Example: Defining a Real Percentage Field (PCX)

Example: Defining a Currency Field Type (CUR)

User-defined field types are useful where data requires some conversion before being displayed, or where a standard conversion is required prior to storage.

The conversion process does not have to be two-way. For example, a user-defined type could be used to allow an automobile registration number to be processed to convert all zeros to the letter O and all ones to the letter I to ensure against confusion on inquiry screens. The original data could be stored in another field if necessary.

In the automobile registration number example there is no conversion from internal to external format since they are both the same. The only parameter to the external to internal conversion function is the field to be converted.

The following example is somewhat more complicated to illustrate the power of user-defined field types. In it we will define a currency field type. A company dealing with international customers who are charged and who pay in their own currency would want to define a value field only once for a particular data item, rather than various fields or even separate files to store the data.

The solution is to store the data in a neutral format and then convert it before display with the correct number of decimal places and any other desired editing. Rather than require the developers to remember to call these conversion routines, a user-defined data type allows the definition to be made once and is then automatically generated by CA 2E when required.

The following panels describe the definition process step by step.

  1. First define the functions used for the field type on the *Field attribute types file. From the Edit Database Relations panel, enter *F in the object positioner field to position on the CA 2E *Field attribute types file. Enter F next to the *Field attribute types file name to display the functions that have been defined for this file.

  2. Create the four following mapping functions that will be used by the currency field type as shown below. The access path for each function type should be *NONE.

  3. For the purposes of this example, the following new fields are required. You can create them as part of the function creation process.

  4. Press Enter to define the fields and return to the Display Fields panel. Zoom into each field to display the Edit Field Details panel.

    Enter details for the *external currency field as follows.

    Enter details for the *internal currency field as follows.

    Enter details for the no. of decimals field as follows.

    Note the assigned DDS field names for the fields you just defined. In this case, XBCD, ZZNB, and QHNB. You will need these when you define the user source for the ext –> int src and the int –> ext src functions.

  5. Define parameters to the Currency ext –> int EXCINTFUN function as shown below using the fields you just defined. Be sure to define the *external currency field first. You can use a sequence number to ensure this.

    Zoom into each parameter to display the Edit Function Parameter Details panel. Assign the usage and role for the Currency ext –> int function parameters as follows.

Parameter

Usage

Role

*external currency

I

MAP

*internal currency

O

MAP

  1. Define parameters to the Currency ext –> int src EXCUSRSRC function as shown below. Be sure to define the *external currency field first. You can use a sequence number to ensure this.

    Zoom into each parameter to display the Edit Function Parameter Details panel. Assign the usage and role for the Currency ext –> int src function parameters as follows.

Parameter

Usage

Role

*external currency

I

MAP

*internal currency

O

MAP

  1. Define parameters to the Currency int –> ext EXCINTFUN function as shown below. Be sure to define the *external currency field first. You can use a sequence number to ensure this.

    Zoom into each parameter to display the Edit Function Parameter Details panel. Assign the usage and role for the Currency int –> ext function parameters as follows.

Parameter

Usage

Role

*external currency

O

MAP

*internal currency

I

MAP

no. of decimals

I

MAP

  1. Define parameters to the Currency int –> ext src EXCUSRSRC function as shown. Be sure to define the *external currency field first. You can use a sequence number to ensure this.

    Zoom into each parameter to display the Edit Function Parameter Details panel. Assign the usage and role for the Currency int –> ext src function parameters as follows.

Parameter

Usage

Role

*external currency

O

MAP

*internal currency

I

MAP

no. of decimals

I

MAP

  1. Edit the action diagram for the Currency ext –> int EXCINTFUN to call the EXCUSRSRC function for the external to internal conversion.

  2. Specify the details of the parameter interface.

  3. The RPG source for the Currency ext –> int src EXCUSRSRC function is as follows. The process involves the removal of any decimal point found in the data. Note that to reduce the complexity of the example no validation has been included.

*CURRENCY EXT –> INT SRC

*CONVERT TO INTERNAL FORMAT

 

C

C

C

*

*

Z-ADD15

Z-ADD14

MOVEA

#IXBCD

X

Y

OUT

20

20

 

 

*STRIP OUT DECIMAL POINT

 

 

 

 

 

C

C

C

C

C

C

C

C

C

  X

  OUT,X

*

DOUEQ1

IFNE

MOVE

SUB

END

SUB

END

MOVEA

MOV

’.’

OUT,X

1

1

INP

WRK14

INP,Y

Y

X

WRK14

#OZZNB

14

  1. Edit the action diagram for the Currency int –> ext EXCINTFUN to call the EXCUSRSRC function for the internal to external conversion.

  2. Specify the details of the parameter interface.

  3. Following is the RPG source for the Currency int –> ext src EXCUSRSRC function.

*CURRENCY INT –> EXT SRC

 

E

E

C

C

C

C

C

C

C

C

C

C

C

C

C

C

*

*

  #IQHNB

*

  #IQHNB

INP

OUT

MOVE

MOVE

Z-ADD

MOVEA

MOVEA

IFEQ

MOVE

MOVEA

ELSE

IFEQ

MOVE

MOVEA

MOVEA

ELSE

   14

   15

’ ’

#IZZNB

#IQHNB

WRK14

INP

1

’.’

INP,14

2

’.’

INP,13

INP,14

1

1

OUT

WRK14

WRK2N

INP

OUT

OUT,14

OUT,15

OUT,13

OUT,14

OUT,15

14

20

 

C

C

C

C

C

C

C

C

C

C

C

C

C

C

C

C

  #IQHNB

  #IQHNB

IFEQ

MOVE

MOVEA

MOVEA

MOVEA

ELSE

IFEQ

MOVE

MOVEA

MOVEA

MOVEA

MOVEA

END

END

END

END

3

’.’

INP,12

INP,13

INP,14

4

’.’

INP,11

INP,12

INP,13

INP,14

OUT,12

OUT,13

OUT,14

OUT,15

OUT,11

OUT,12

OUT,13

OUT,14

OUT,15

 

 

 

*CHANGE LEADING ZEROES TO BLANKS

 

 

 

 

 

C

C

C

C

C

C

  OUT,X

*

*

Z-ADD1

DOUNE

MOVE

ADD

END

MOVEA

’0’

’ ’

1

OUT

X

OUT,X

X

#OXBCD

20

  1. Zoom into the *Field attribute types file from the Edit Database Relations panel to display a list of the currently defined field types.

  2. Press F9 from the Display Field Types panel to add the new field type CUR. Specify attributes for the CUR field type on the Edit Field Type panel as follows. Be sure to enter Y for the initial Allow value mapping value.

  3. Press Enter to confirm the values you entered. Press F3 to specify additional attributes for the CUR field type on the Edit Field Type Defaults panel as follows.

  4. In order to use the CUR field type, you need a Currency file as shown below. The records on this file will reflect the number of decimal places required by the various currency types.

    Any files that actually use the CUR field type must reference the Currency file. The no. of decimals field must be virtualized across the Refers to relation.

  5. In order to map the value of the no. of decimals to the field type, display the entries on the file by entering E on the Edit Database Relations panel as shown above. Enter M against the entry defined using the currency field type to define the additional parameter to the mapped user-defined field type function.

  6. This panel allows the designer to define from which context the actual parameters may be selected.

For more information on this process, refer to this topic, the Supplying Parameters to Mapping Functions subtopic, the Mapping Function Parameters: Panel/Report Entry Level subtopic, and the online help.