Sunday, May 26, 2013

Add new department into lookup during adding an employee

While adding a new employee you can select a department for that employee from a List Of Value ( LOV ).
The case is that, the LOV displays all existed departments, But you may want to add a new department not existed in the list for the employee.
a: At the model layer :
1- Create DepartmentsView , EmployeesView then add them at application module.
2- Go to DepartmentsView and write a default expression for the DepartmentId primary key to get a positive number directly when creating a new Department:

The written expression is:
 (new oracle.jbo.server.SequenceImpl("DEPARTMENTS_SEQ",adf.object.getDBTransaction())).getSequenceNumber()  
3- Go to EmployeesView creating a new View Accessor to DepartmentView at EMPLOYEE table.








4- Go to application module to create a method and add it to the client interface by
                                                 Create the Java Class AppModuleImpl:

then write a method that define a single formal parameter           
                                             
      The written code is :
   public void RefreshAndSelect(Number Id){  
     if(Id!=null){  
       EmployeesViewImpl employeesViewImpl= getEmployees1();  
       EmployeesViewRowImpl employeesViewRowImpl = (EmployeesViewRowImpl)employeesViewImpl.getCurrentRow();  
       RowSet rowSetDep= employeesViewRowImpl.getDepartmentsView1();  
       employeesViewRowImpl.getDepartmentsView1().executeQuery();        
       employeesViewRowImpl.setDepartmentId(Id);  
     }  

  Add the created method  RefreshAndSelect  to the application module Client Interface


b: At the ViewController Layer 
We will need to make two Task Flows:
1 - ManageEmployees-Task
2 - Department-Task.
  • We will need two task flows to have the ability to commit each task separately,
Which means that if we created a new employee (Inside ManageEmployees-Task ) then created a new Department inside ( Department-Task ) and click a save button at Department-Task, it will save only the department not the employee and vice versa.

1- First Task Flow (ManageEmployees-Task) :

This task contains a default activity Fragment Page called  EmployeesFrag , this fragment page shows all employees as a table and has a button AddEmp  to add a new Employee.
AddEmp  button has ( CreateInsert ) as ( ActionListener ) and ( addEmp ) as ( Action ) to navigate to the ( EmployeesFormFrag ).


The  ( EmloyeesFormFrag ) contains a form represent employee:  
EmloyeesFormFrag has a button ( AddDept ) that only has Action  ( addDept ) to  navigate to the Department-Task

The Department-Task has a default activity CreateInsert to  create a new record then goes to DepartmentFrag 

DepartmentFrag  contains a form and two buttons that have SetActionListener to set a sesionScope parameter,

OK button set the Id of the new created Department  to a sessionScope parameter (DeptId)

 Cancel button set  the DeptId to null 


Pressing OK will goes to taskFlowReturn1 to Commit the transaction . .

 Pressing Cancel will goes to taskFlowReturn2 to Rollback the transaction.

What makes taskFlowReturn1 commit only the department is the selection of " Always Begin New Transaction " and unchecking of " Share data controls with calling task flow ":



The method RefreshAndSelect will be called if the Ok button is pressed because it sets the the sessionScope parameter to non null value:


The RefreshAndSelect method will be executed with parameter equal to the sessionScope Parameter:


Notes:
  1.  At application module don't make a relation between DepartmentView and EmployeesView.            If you related the EmployeesView with DepartmentsView,  the Employees Page which contain employees table will show the employees of the first department only.
  2. If you used pageflowScope instead of sessionScope the return value would be null.
Download the sample application from here


No comments:

Post a Comment

java - fill distinct objects in ArrayList

If you have a list that contains some objects that it is considered as duplication when two or three fields of these objects are equal. How ...