Difference between revisions of "EDT:Tutorial: RUI With DataBase Lesson 7"

From Eclipsepedia

Jump to: navigation, search
(New page: Access a database with EGL Rich UI {| style="float: right" |< Previous | [[EDT:Tu...)
 
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
 
 
 
 
[[EDT:Tutorial: Access a database with EGL Rich UI|Access a database with EGL Rich UI]]
 
[[EDT:Tutorial: Access a database with EGL Rich UI|Access a database with EGL Rich UI]]
  
Line 9: Line 5:
 
|[[EDT:Tutorial: RUI With DataBase Lesson 6|&lt; Previous]] | [[EDT:Tutorial: RUI With DataBase Lesson 8|Next >]]
 
|[[EDT:Tutorial: RUI With DataBase Lesson 6|&lt; Previous]] | [[EDT:Tutorial: RUI With DataBase Lesson 8|Next >]]
 
|}
 
|}
= Lesson 7: Create a library of reusable functions =
+
= Lesson 7: Create a library of reusable functions =
  
Create a library to format money values and to associate
+
Create a library to format money values and to associate category numbers with descriptions.Libraries contain functions, constants, and variables that you can use in multiple locations.  
category numbers with descriptions.Libraries contain functions, constants, and variables that
+
you can use in multiple locations.  
+
  
When you reference a declaration
+
When you reference a declaration in the library from other logic such as a service or handler, you can include the library name as a prefix. For example, '''MyLibrary.myLibraryVariable''' is appropriate if the library name is '''MyLibrary''' and the library includes the '''myLibraryVariable''' variable. Alternatively, you can include the library name in a '''use''' statement in the other logic and avoid the need to qualify every reference. In that case, '''myLibraryVariable''' is sufficient to reference that variable.  
in the library from other logic such as a service or handler, you
+
can include the library name as a prefix. For example, <tt>MyLibrary.myLibraryVariable</tt> is
+
appropriate if the library name is <tt>MyLibrary</tt> and
+
the library includes the <tt>myLibraryVariable</tt> variable.
+
Alternatively, you can include the library name in a '''use''' statement
+
in the other logic and avoid the need to qualify every reference.
+
In that case, <tt>myLibraryVariable</tt> is sufficient to
+
reference that variable.
+
  
== Create a Library part ==
+
== Create a Library&nbsp;  ==
  
 +
To create a Library:
  
 +
*Right-click the PaymentClient folder, then click '''New''' &gt; '''Library'''.
 +
*In the New EGL Library window, enter the following information:
 +
**In the '''EGL source file name''' field, enter the name '''PaymentLib'''.
 +
**In the '''Package''' field, enter the name '''libraries'''.
 +
**Under '''EGL Library Type''', leave the default value of '''Basic''' selected.
  
To create a Library part:
+
:The new Library&nbsp;opens in the EGL editor.
  
<ol><li>Right-click the PaymentClient folder,
+
*Replace the boilerplate code in the Library&nbsp;with the following lines:
then click '''New''' &gt; '''Library'''.
+
<li>In the New EGL Library window, enter
+
the following information:  
+
  
<ul><li>In the '''EGL source file name''' field, enter
+
  package libraries;
the following name:
+
 
 +
  library PaymentLib {}
 +
   
 +
  end
  
PaymentLib
+
*Save the file.
  
<li>In the '''Package''' field, enter the following
+
== Create the categories array  ==
name:
+
  
libraries
+
Add the following code before the final '''end''' statement: <code></code>The value is an array, and as is true of all arrays in EGL, the index of the first element is 1, not 0.
  
<li>Under '''EGL Library Type''', leave the default
 
value of '''Basic''' selected.
 
</ul>
 
The new Library part opens in the EGL editor.
 
<li>Replace the boilerplate code in the Library part with the
 
following lines:
 
 
package libraries;
 
 
library PaymentLib type BasicLibrary {}
 
 
end
 
 
<li>Save the file.
 
</ol>
 
 
== Create the categories array ==
 
 
Add the following code before the final '''end''' statement:
 
<code>
 
 
       categories string[] =[
 
       categories string[] =[
 
    
 
    
Line 76: Line 47:
 
                       "Other"          // 7
 
                       "Other"          // 7
 
               ];
 
               ];
</code>
 
The value is an array, and as is true of all arrays
 
in EGL, the index of the first element is 1, not 0.
 
  
The array
 
is used in logic that acts as follows:
 
  
<ul><li>Places an expense category into the database in integer form,
 
to save space.
 
<li>Places the expense category onto the web page in string form,
 
for clarity.
 
</ul>
 
  
== Create the get functions for categories ==
+
The array is used in logic that acts as follows:
  
The next functions convert between the following two formats
+
*Places an expense category into the database in integer form, to save space.
for expense categories: integer and string.
+
*Places the expense category onto the web page in string form, for clarity.
 +
 
 +
== Create the get functions for categories  ==
 +
 
 +
The next functions convert between the following two formats for expense categories: integer and string.
 +
 
 +
Add the following code before the final '''end''' statement: <code></code>The function receives the integer format of an expense category and returns the related array element. If the input value is 0, the function returns an empty string.  
  
<ol><li>Add the following code before the final '''end''' statement:
 
<code>
 
 
       function getCategoryDesc(cat int in) returns(string)
 
       function getCategoryDesc(cat int in) returns(string)
           if(cat != 0)      // the integer is not 0
+
           if(cat&nbsp;!= 0)      // the integer is not 0
 
               return(categories[cat]);
 
               return(categories[cat]);
 
           else
 
           else
Line 103: Line 68:
 
           end
 
           end
 
       end
 
       end
</code>
 
The function receives the integer format of an expense
 
category and returns the related array element. If the input value
 
is 0, the function returns an empty string.
 
  
<li>Add the following code before the final '''end''' statement:  
+
 
<code>
+
 
 +
Add the following code before the final '''end''' statement: <code></code>This function receives the string format of an expense category and returns the integer format, if possible. If no match is found for the received string, the function returns 0.
 +
 
 
       function getCategoryNum(desc string in) returns(int)
 
       function getCategoryNum(desc string in) returns(int)
 
           for(i int from 1 to categories.getSize())
 
           for(i int from 1 to categories.getSize())
Line 118: Line 81:
 
           return(0); // no match
 
           return(0); // no match
 
       end
 
       end
</code>
 
This function receives the string format of an expense
 
category and returns the integer format, if possible. If no match
 
is found for the received string, the function returns 0.
 
  
<li>Format the file.
 
<li>Save and close the <tt>PaymentLib</tt> Library. If you see errors in your source file, compare your code
 
to the file contents in
 
[[EDT:Tutorial: RUI With DataBase Lesson 7 Code|Code for PaymentLib.egl after lesson 7]].
 
</ol>
 
  
''' Related reference '''<br>
 
  
[../../com.ibm.egl.lr.doc/topics/regl_core_array.html Arrays]
+
== Save your changes  ==
  
== Lesson checkpoint ==
+
The complete your coding of the payment library:
  
You learned how to complete the following tasks:
+
#Format the file.
 +
#Save and close the '''PaymentLib''' Library. If you see errors in your source file, compare your code to the file contents in [[EDT:Tutorial: RUI With DataBase Lesson 7 Code|Code for PaymentLib.egl after lesson 7]].
  
<ul><li>Create a Library part.
+
== Lesson checkpoint  ==
<li>Add functions and a variable to a library.
+
</ul>
+
  
{| style="float: right"
+
You learned how to complete the following tasks:
|[[EDT:Tutorial: RUI With DataBase Lesson 6|&lt; Previous]] | [[EDT:Tutorial: RUI With DataBase Lesson 8|Next >]]
+
 
 +
*Create a Library type.
 +
*Add functions and a variable to a library.
 +
 
 +
{| style="float: right" class="FCK__ShowTableBorders"
 +
|-
 +
| [[EDT:Tutorial: RUI With DataBase Lesson 6|&lt; Previous]] &#124; [[EDT:Tutorial: RUI With DataBase Lesson 8|Next &gt;]]
 
|}
 
|}
  
 
[[Category:EDT]]
 
[[Category:EDT]]

Latest revision as of 18:02, 6 December 2011

Access a database with EGL Rich UI


< Previous | Next >

Contents

[edit] Lesson 7: Create a library of reusable functions

Create a library to format money values and to associate category numbers with descriptions.Libraries contain functions, constants, and variables that you can use in multiple locations.

When you reference a declaration in the library from other logic such as a service or handler, you can include the library name as a prefix. For example, MyLibrary.myLibraryVariable is appropriate if the library name is MyLibrary and the library includes the myLibraryVariable variable. Alternatively, you can include the library name in a use statement in the other logic and avoid the need to qualify every reference. In that case, myLibraryVariable is sufficient to reference that variable.

[edit] Create a Library 

To create a Library:

  • Right-click the PaymentClient folder, then click New > Library.
  • In the New EGL Library window, enter the following information:
    • In the EGL source file name field, enter the name PaymentLib.
    • In the Package field, enter the name libraries.
    • Under EGL Library Type, leave the default value of Basic selected.
The new Library opens in the EGL editor.
  • Replace the boilerplate code in the Library with the following lines:
  package libraries;
  
  library PaymentLib {}
    
  end
  • Save the file.

[edit] Create the categories array

Add the following code before the final end statement: The value is an array, and as is true of all arrays in EGL, the index of the first element is 1, not 0.

      categories string[] =[
  
                      "Rent",          // 1
                      "Food",          // 2
                      "Entertainment", // 3
                      "Automotive",    // 4
                      "Utilities",     // 5
                      "Clothes",       // 6
                      "Other"          // 7
              ];


The array is used in logic that acts as follows:

  • Places an expense category into the database in integer form, to save space.
  • Places the expense category onto the web page in string form, for clarity.

[edit] Create the get functions for categories

The next functions convert between the following two formats for expense categories: integer and string.

Add the following code before the final end statement: The function receives the integer format of an expense category and returns the related array element. If the input value is 0, the function returns an empty string.

      function getCategoryDesc(cat int in) returns(string)
          if(cat != 0)       // the integer is not 0
              return(categories[cat]);
          else
              return("");
          end
      end


Add the following code before the final end statement: This function receives the string format of an expense category and returns the integer format, if possible. If no match is found for the received string, the function returns 0.

      function getCategoryNum(desc string in) returns(int)
          for(i int from 1 to categories.getSize())
              if(categories[i] == desc)
                  return(i);
              end
          end
          return(0);	// no match
      end


[edit] Save your changes

The complete your coding of the payment library:

  1. Format the file.
  2. Save and close the PaymentLib Library. If you see errors in your source file, compare your code to the file contents in Code for PaymentLib.egl after lesson 7.

[edit] Lesson checkpoint

You learned how to complete the following tasks:

  • Create a Library type.
  • Add functions and a variable to a library.
< Previous | Next >