The HR Director Needs an Upgrade
We just built the basic program that all users have. Remember, we want to see the role-based upgrade in action. So I want the HR director to get new classes that allow access to salary and hourly rate details for staff and contractors, respectively.
To demonstrate this, the code is arranged into two folders called old and upgrade. The files in the old folder are non-upgraded versions as per Figure 3. The files in the upgrade folder represent the modified files that produce the output illustrated in Figure 2.
I now want to upgrade the HR director client. To produce the display in Figure 2, I make the following changes to the files:
- ContractStaff.java
- PermanentStaff.java
- rules.xml
- hrstaffdetails.xml
The changes apply just to the addition of a new data member, an associated setter method, and an overloaded toString() method in each of the subclasses ContractStaff and PermanentStaff.
public class ContractStaff extends Employee { private String hourlyRate; public ContractStaff() { super("Contractor"); } public void setHourlyRate(String newHourlyRate) { hourlyRate = newHourlyRate; } public String toString() { String newline = System.getProperty("line.separator"); return super.toString() + "Hourly rate: " + hourlyRate + newline; } } public class PermanentStaff extends Employee { private String salary; public PermanentStaff() { super("Permanent"); } public void setSalary(String newSalary) { salary = newSalary; } public String toString() { String newline = System.getProperty("line.separator"); return super.toString() + "Salary: " + salary + newline; } }
The above class modifications are reflected in the following changes to the two rules files.
<?xml version="1.0"?> <digester-rules> <object-create-rule pattern="hrstaffdetails" classname="HRStaffDetails" /> <set-properties-rule pattern="hrstaffdetails" < <alias attr-name="company" prop-name="company" /> </set-properties-rule> <pattern value="hrstaffdetails/permanentstaff"> <object-create-rule classname="PermanentStaff" /> <call-method-rule pattern="personName" methodname="setPersonName" paramcount="0" /> <call-method-rule pattern="socialSecurityNumber" methodname="setSocialSecurityNumber" paramcount="0" /> <call-method-rule pattern="salary" methodname="setSalary" paramcount="0" /> <set-next-rule methodname="addPermanentStaff" /> </pattern> <pattern value="hrstaffdetails/contractstaff"> <object-create-rule classname="ContractStaff" /> <call-method-rule pattern="personName" methodname="setPersonName" paramcount="0" /> <call-method-rule pattern="socialSecurityNumber" methodname="setSocialSecurityNumber" paramcount="0" /> <call-method-rule pattern="hourlyRate" methodname="setHourlyRate" paramcount="0" /> <set-next-rule methodname="addContractor" /> </pattern> </digester-rules> <?xml version="1.0"?> <hrstaffdetails company="company"> <permanentstaff> personName>Francis Bacon</personName> <socialSecurityNumber>1111111</socialSecurityNumber> <salary>30000</salary> </permanentstaff> <permanentstaff> <personName>Francesca Bacon</personName> <socialSecurityNumber>2222222</socialSecurityNumber> <salary>30000</salary> </permanentstaff> <contractstaff> <personName>Francisco Bacon</personName> <socialSecurityNumber>3333333</socialSecurityNumber> <hourlyRate>300</hourlyRate> </contractstaff> </hrstaffdetails>
With these changes in place, the Java files can be recompiled and the program can be rerun. This will produce the program output displayed in Figure 2.
If you don’t want to recompile the Java classes on the client machine, the compiled Java files can be copied, overwriting the old versions. Then, when the Digester is created, the new code is used. The rules in this case are represented by the two XML files.
The easiest way to test the supplied code is to copy it into a folder. Within this folder you will see two subfolders called upgrade and old.
From the main folder, compile and run the program as described earlier. This will give you the Figure 3 program output.
To see the upgraded code, copy the contents from the upgrade folder into the main folder. Then recompile and run the program to see the upgraded view.