|
|
Sage-ST
ä
Tutorial
|
1. Overview.
In this lesson you will break up your code into three separate packages and one main driver procedure.
2. What You'll Use.
You will use your text editor and the Ada compiler.
3. Assignment.
Up to this point, all your code has been lumped into one big file. The file for this system is relatively small even with everything in it, but good programming techniques recommend that the contents be divided into several packages, each package containing procedures that are related to a single purpose. Which procedures belong in which packages is somewhat subjective and should be discussed among your programming team.
For this class we will break the code up into a reports package, an editing package, a utility package, and a main driver/procedure. Each package will have a specification (spec) file with an extension of ADS and a body file with an extension of ADB. The main driver file will have an extension of ADA. Thus, instead of a single file (HRS.GPL) in your directory, you will now have seven files: UtilPack.ADS, UtilPack.ADB, ReptPack.ADS, ReptPack.ADB, EditPack.ADS, EditPack.ADB, and HRS.ADA. Remember, any variables that were declared globally must now be declared in each package where they are used. The variable "option" comes to mind.
When you go to compile the system, remember that a specification must be compiled before its associated body, and that all specifications must be compiled prior to the main driver.
4. Our Solution.
(UtilPack.ADS)
package UtilPack is
procedure Load_Employee;
procedure Unload_Employee;
procedure Rebuild_All_Relations;
end UtilPack;
(UtilPack.ADB)
with Display;
with ModSys ;
with Sage ;
package body UtilPack is
procedure Load_Employee is
begin
Display.DisplayMessage ("Working on Load Employee", false);
end Load_Employee;
procedure Unload_Employee is
begin
Display.DisplayMessage ("Working on Unload Employee", false);
end Unload_Employee;
procedure Rebuild_All_Relations is
--*
-- Rebuild_All_Relations
--
-- This procedure will rebuild the indexes of all the relations
-- in the HRS database.
--
--
emp_Rebuilt : boolean := false;
org_Rebuilt : boolean := false;
num_Of_Duplicates : ModSys.S_long_integer := 0;
begin
Sage.OpenRelation ("Employee", true);
Display.DisplayMessage ("Rebuilding employee index", false);
Sage.RebuildIndex ("Employee", true, num_Of_Duplicates);
emp_rebuilt := (Sage.SageError = 0);
Sage.CloseRelation ("Employee");
Sage.OpenRelation ("Org", true);
Display.DisplayMessage ("Rebuilding Organization Index", false);
Sage.ReBuildIndex ("Org", false, num_Of_Duplicates);
org_Rebuilt := (Sage.SageError = 0);
Sage.CloseRelation ("Org");
if emp_Rebuilt and org_Rebuilt then
Display.DisplayMessage ("Rebuild of HRS data base complete", false);
else
Display.DisplayMessage ("Portions of the rebuild were not successful.",
true);
end if;
end Rebuild_All_Relations;
end UtilPack;
(ReptPack.ADS)
package ReptPack is
procedure Employee_Report;
end ReptPack;
(ReptPack.ADB)
with Display ;
with Files ;
with ModSys ;
with Reports ;
with Sage ;
with ThorPort;
package body ReptPack is
procedure Get_File_Name (fName : in out string) is
begin
Sage.PutFieldA ("Utility", "FileName", "CON");
Display.DisplayForm ("RptInfo", "Utility", "FileName", FALSE);
Sage.GetFieldA ("Utility", "FileName", fName);
end Get_File_Name;
procedure Employee_Report is
--*
-- Employee_Report - Generate simple employee report.
--
-- KABHaar, 7 November 1990
--
-- The Sage-ST reports package can be used to fulfill the reporting
-- requirements for most applications. The format and actual field contents
-- of the report are separated from the application code by using THOR forms.
-- Once the logic is in place to assemble the desired records, changes in
-- report format and content can often be made without code changes. This
-- example prints the Employee records in SSN order.
--
-- CALLING SEQUENCE -
--
-- Employee_Report;
--
-- ENTRY -
--
-- EXIT -
--
success : boolean := false;
newPage : boolean := false;
state : Files.FileState;
ftbl : Reports.FileRP;
error : ModSys.S_natural := 0;
lines_Per_Page : ModSys.S_natural := 55;
file_Name : string (1..30) := (others => ascii.nul);
begin
Sage.OpenRelation ("Employee", false);
Get_File_Name (file_Name);
ThorPort.ClearScreen;
Reports.OpenReport (ftbl, file_Name, state);
if file_Name (file_Name'first..file_Name'first+2) = "CON" then
lines_Per_Page := 24;
end if;
Reports.DefineReport (ftbl, true, false, lines_Per_Page, false, false, 1);
Reports.WrForm (ftbl, "RptEmpH", "", "", true, error);
Sage.ReadRecord ("Employee", "SSN", Sage.First);
while Sage.SageError = 0 loop
Reports.WrForm (ftbl, "RptEmpB", "RptEmpH", "", true, error);
Sage.ReadRecord ("Employee", "SSN", Sage.Next);
if Sage.SageError /= 0 then
while (Reports.RemainingLines (ftbl) >= 1) and (not newPage) loop
Reports.WrLn (ftbl, newPage);
end loop;
exit;
end if;
exit when Reports.ConsoleTerminate;
end loop;
Reports.CloseReport (ftbl, state);
Sage.CloseRelation ("Employee");
end Employee_Report;
end ReptPack;
(EditPack.ADS)
package EditPack is
procedure Edit_Employee;
procedure Edit_Organization;
procedure Browse_Employee;
end EditPack;
(EditPack.ADB)
with Display ;
with ModSys ;
with Sage ;
with SageLib ;
with Strings ;
package body EditPack is
option : string (1..1) := (others => ascii.nul);
procedure Display_Edit_Message (operation : in string) is
--*
-- Display_Edit_Message - notify user of success or failure of operation
--
-- Author - M.L. Taylor 5 Feb 1990
-- modified KABHaar 7 Nov 1990
--
-- The user passes in a word describing the operation being attempted,
-- usually "added", "modified", "deleted", or "found". If the operation
-- was successful and Sage.SageError is zero, this word will be used in
-- the phrase "Record <string>". If the operation was not successful,
-- Display.DisplayError is used to display an error message.
--
--
-- CALLING SEQUENCE -
--
-- Display_Edit_Message ("added"); OR
-- Display_Edit_Message ("modified"); OR
-- Display_Edit_Message ("deleted");
--
-- ENTRY -
--
-- operation : string defining the attempted operation.
--
-- EXIT -
--
begin
if Sage.SageError = 0 THEN
Display.DisplayMessage ("Record " & operation, false);
else
Display.DisplayError (Sage.SageError, true);
end if;
end Display_Edit_Message;
procedure Add_Employee is
--*
-- Add_Employee
--
-- Author - M.L. Taylor 5 Feb 1990
-- modified KABHaar 7 Nov 1990
-- modified KABHaar 5 Feb 1991
--
--
-- Requires that the user enter an organization number for each employee
-- and that the number be that of a previously-entered organization.
--
--
-- CALLING SEQUENCE -
--
-- Add_Employee;
--
-- ENTRY -
--
-- EXIT -
--
begin
Sage.ReadRecordR ("Org", "OrgNum", Sage.EQ, "Employee", "OrgNum");
if Sage.SageError = 0 then
Sage.WriteRecord ("Employee");
Display_Edit_Message ("added");
else
Display.DisplayMessage ("Bad organization # - record not added", true);
end if;
end Add_Employee;
procedure Modify_Employee is
--*
-- Modify_Employee
--
-- Author - M.L. Taylor 5 Feb 1990
-- modified KABHaar 7 Nov 1990
-- modified KABHaar 5 Feb 1991
--
--
-- Requires that the user enter an organization number for each employee
-- and that the number be that of a previously-entered organization.
--
--
-- CALLING SEQUENCE -
--
-- Modify_Employee;
--
-- ENTRY -
--
-- EXIT -
--
begin
Sage.ReadRecordR ("Org", "OrgNum", Sage.EQ, "Employee", "OrgNum");
if Sage.SageError = 0 then
Sage.ReWriteRecord ("Employee");
Display_Edit_Message ("modified");
else
Display.DisplayMessage ("Bad organization - record not modified", true);
end if;
end Modify_Employee;
procedure Delete_Employee is
begin
Sage.DeleteRecord ("Employee");
Display_Edit_Message ("deleted");
end Delete_Employee;
procedure Get_Next_Employee is
begin
Sage.ReadRecord ("Employee", "SSN", Sage.Next);
if Sage.SageError /= 0 then
Sage.ReadRecord ("Employee", "SSN", Sage.First);
Display.DisplayMessage ("First record", false);
end if;
end Get_Next_Employee;
procedure Get_Previous_Employee is
begin
Sage.ReadRecord ("Employee", "SSN", Sage.Previous);
if Sage.SageError /= 0 then
Sage.ReadRecord ("Employee", "SSN", Sage.Last);
Display.DisplayMessage ("Last record", false);
end if;
end Get_Previous_Employee;
procedure Locate_Employee is
begin
Sage.ReadRecord ("Employee", "SSN", Sage.GE);
Display_Edit_Message ("found");
end Locate_Employee;
procedure Edit_Employee is
--*
-- Edit_Employee - Maintain the Employee relation
--
-- This procedure is used to perform all maintenance functions for the
-- Employee relation. Allowed maintenance functions are
--
-- Exit - close Employee relation and return to MainMenu
-- Add - add the currently displayed record
-- Modify - change the currently displayed record
-- Delete - delete the currently displayed record
-- Next - display the next employee in key sequence
-- Previous - display the previous employee in key sequence
-- Locate - display the employee with key GE on form
--
begin
Sage.OpenRelation ("Employee", true);
Sage.OpenRelation ("Org", false);
-- This call added so that the edit form does not come up blank initially
Sage.ReadRecord ("Employee", "SSN", Sage.First);
Sage.PutFieldA ("Utility", "A1", "E");
loop
Display.DisplayForm ("EmpEd", "Utility", "A1", false);
Sage.GetFieldA ("Utility", "A1", option);
case option (1) is
when 'E' => exit;
when 'A' => Add_Employee;
when 'M' => Modify_Employee;
when 'D' => Delete_Employee;
when 'N' => Get_Next_Employee;
when 'P' => Get_Previous_Employee;
when 'L' => Locate_Employee;
when others => Display.DisplayMessage ("Selected option unavailable",
true);
end case;
end loop;
Sage.CloseRelation ("Employee");
Sage.CloseRelation ("Org");
end Edit_Employee;
procedure Edit_Organization is
--*
-- Edit_Organization - Maintain the Org relation
--
-- This procedure is used to perform all maintenance functions for the
-- Organization relation. RecordEdit provides a complete editor. Allowed
-- maintenance functions are
--
-- Exit - close Org relation and return to MainMenu
-- Add - add the currently displayed record
-- Modify - change the currently displayed record
-- Delete - delete the currently displayed record
-- Next - display the next employee in key sequence
-- Previous - display the previous employee in key sequence
-- Locate - display the employee with key GE on form
--
begin
Sage.PutFieldA ("Utility", "A1", "E");
SageLib.RecordEdit ("OrgEd", "Org", "OrgNum", "Utility", "A1");
end Edit_Organization;
procedure Browse_Employee is
begin
Display.DisplayMessage ("Working on Browse Employee File", false);
end Browse_Employee;
end EditPack;
(HRS.ADA)
with Display ;
with EditPack;
with ReptPack;
with Sage ;
with UtilPack;
procedure HRS is
--*
-- HRS- Sage-ST training class Prototype 14 (GRAPL)
--
-- Author - M.L. Taylor 5 Feb 1990
-- modified KABHaar 7 Nov 1990
--
-- The goal of Prototype 14 is to break up HRS.ADA into packages.
--
-- What was previously one large file (HRS.ADA) is now seven files:
-- EDITPACK.ADS, EDITPACK.ADB, REPTPACK.ADS, REPTPACK.ADB, UTILPACK.ADS
-- UTILPACK.ADB, and HRS.ADA.
--
--
-- CALLING SEQUENCE -
--
-- GRAPL HRS
--
-- ENTRY -
--
-- EXIT -
--
option : string (1..1) := (others => ascii.nul);
begin
Sage.OpenSystem ("HRS.DFL", 5, 3, 5000);
loop
Display.DisplayForm ("MainMenu", "", "", false);
Sage.GetFieldA ("Utility", "A1", option);
case option (1) is
when 'E' => exit;
when 'M' => EditPack.Edit_Employee;
when 'O' => EditPack.Edit_Organization;
when 'R' => UtilPack.Rebuild_All_Relations;
when 'B' => EditPack.Browse_Employee;
when 'P' => ReptPack.Employee_Report;
when 'L' => UtilPack.Load_Employee;
when 'U' => UtilPack.Unload_Employee;
when others => Display.DisplayMessage ("Selected option unavailable",
true);
end case;
end loop;
Sage.CloseSystem;
end HRS;
Go Back To
Tutorial Table of Contents