| HRTree - organize an heirarchical reporting structure |
HRTree - organize an heirarchical reporting structure
See below.
At the moment, in its alpha stages, HRTree is designed primarily as a means to allow the user to create or format an heirarchical reporting structure.
In the future, I hope to expand HRTree's capabilities to include any heirarchical structure, such that HRTree will go from ''Human Resources Tree'' to ''Heirarchical Tree''.
An empty HRTree object is created with:
my $structure = new HRTree;
which has the underlying structure:
{"email_id" => $email_id,
"full_name" => $full_name,
"department" => $department,
"position" => $position,
"pos_title" => $pos_title,
"reports_to" => $reports_to,
"subords" => [],
"emp_list" => undef}
(Don't worry about the variables in the above anonymous hash for now, they will be explained later.)
The object $structure can be filled in two ways, either from a formatted data file that has been
prepared in advance (see
Preparing the Data File, below) or by creating each data entry manually (see Employees Class, below).
Let's assume, for the time being, that you have a prepared data file,
called $dat_file. To load the HRTree object from this file, use:
$structure->get_list_from($dat_file);
This will put all the data from the data file into the pre-defined
structure of the object, $structure.
The data file is a simple, flat, tab-delimieted file with one record per line. The structure of the file is:
$email_id $full_name $department $position $pos_title $reports_to ;
The first field, $email_id, is the index for the database, and the value that is provided on STDIN
for $emp (more below).
Note that the ending tab and semi-colon are necessary (although I'm not sure why, and maybe this will disappear). Blank lines and comments (on separate lines, preceded by #) are welcome.
(Eventually, I hope to make it possible to use HRTree in conjunction with Perl's database interfaces.)
To get all the details on a particular employee, whom we shall call
$emp, simply call the get_details_for method:
my $employee = $structure->get_details_for($emp);
The returned variable, $employee, is an HRTree object that contains all the particulars for $emp. Each of these particulars can be retrieved as follows:
my $email_id = $employee->email_id;
my $full_name = $employee->full_name;
my $department = $employee->department;
my $position = $employee->position;
my $pos_title = $employee->pos_title;
my $reports_to = $employee->reports_to;
In the same way, an existing value can be changed or assigned by using the same methods in reverse:
$employee->email_id = $email_id;
$employee->full_name = $full_name;
$employee->department = $department;
$employee->position = $position;
$employee->pos_title = $pos_title;
$employee->reports_to = $reports_to;
The heirarchy above or below $emp can be returned in a number of ways, depending on how you want to see the
data. If you want to print a simple listing in plain text, use the list_table
method; if you want to print an HTML table, use the html_table
method; or, if you are feeling particularly adventurous, you can use the whois method to return a reference to an array (if the request is to see the
heirarchy below $emp) or a reference to another HRTree object (if the request is to see the
heirarchy below
$emp) containing the whole of the reporting heirarchy under $emp.
$structure->list_table_above($emp); # will print:
Reporting Sturcture above Jill Munroe:
John Bosley
Charlie
$structure->html_table_above($emp); # still has to be written
Let's change $emp to King Lear.
$structure->list_table_below($emp); # will print:
Duke of Albany
Goneril
Duke of Cornwall
Regan
Earl of Gloucester
Edgar
Edmund
Captain
Earl of Kent
$structure->html_table_below($emp); # will print:
<TABLE BORDER="1">
<TR><TD>Lear</TD>
<TD><TABLE BORDER="1">
<TR><TD>Duke of Albany</TD>
<TD><TABLE BORDER="1">
<TR><TD>Gonreil</TD></TR>
</TABLE>
</TD>
</TR>
<TR><TD>Duke of Cornwall</TD>
<TD><TABLE BORDER="1">
<TR><TD>Regan</TD></TR>
</TABLE>
</TD>
</TR>
<TR><TD>Earl of Gloucester</TD>
<TD><TABLE BORDER="1">
<TR><TD>Edgar</TD>
<TD>Edmund</TD>
<TD><TABLE BORDER="1">
<TR><TD>Captain</TD></TR>
</TABLE>
</TD>
</TR>
</TABLE>
</TD>
</TR>
<TR><TD>Earl of Kent</TD></TR>
</TABLE>
</TD>
</TR>
</TABLE>
(Note: I am thinking of modifying the code so that, rather than
printing the data to STDOUT, these methods return variables
over which the user has more control, albeit more responsibility.)
The Employees class, which is a subclass of HRTree, is used to create and manipulate data elements for an individual employee.
my @details = ( # particulars for new employee
"fredf",
"Fred Flintstone",
"Quarry",
"Breaker",
"Limestone Breaker",
"His Boss"
);
Assign details to new employee:
my $fred = Employees->new(@details);
Details can also be assigned individually:
my $barney = Employees->new;
$barney->full_name("Barney Rubble");
Now, print $barney->full_name; will print Barney Rubble.
To add Fred to our data file, use:
Employees->add(\@details, $dat_file);
(It may be more intuitive to call something like
Employees->add($fred, $dat_file), but this would
require that the user create the $fred object
first, which isn't currently necessary.)
Or, to fire him (sonewhat prematurely, perhaps):
Employees->delete($fred->email_id, $dat_file);
A more usual implementation would be to grab $emp
from STDIN, and then to call the same method on $emp:
Employees->delete($emp, $dat_file);
Robert Watkins - email: rwatkins@foo-bar.org