Wednesday, March 18, 2015

ADF: Show / Hide Train ignored node programatically

Brief 

To create a train model for bounded task flows in ADF with train model option, set its Train property in the Behavior section of the Property Inspector to true. 
shows in the task flow visual diagrammer as shown below.
Now we have an ADF train with three node view1, view2 and view3.jsff
 By setting the TrainStop  "ignore" property of a view3 activity to true, or any expression evaluate to true in real practice the node will not be rendered.


Requirement

Actually the expression in ignore property will evaluate once by render train node and even if expression evaluates to false in any node step the ignored node render state will not changes  ,Now its required to show the ignored node "view3" without reenter the train

Environment

JDeveloper/ADF 11.1.1.7

Solution

Create two buttons for the show / hide functionality. Which viewable property depend on a variable in pageFlowScope


 import java.util.HashMap;  
 import java.util.Iterator;  
 import oracle.adf.controller.ControllerContext;  
 import oracle.adf.controller.TaskFlowContext;  
 import oracle.adf.controller.TaskFlowId;  
 import oracle.adf.controller.ViewPortContext;  
 import oracle.adf.controller.metadata.ActivityId;  
 import oracle.adf.controller.metadata.MetadataService;  
 import oracle.adf.controller.metadata.model.Activity;  
 import oracle.adf.controller.metadata.model.TaskFlowDefinition;  
 import oracle.adf.controller.metadata.model.TrainStop;  
 import oracle.adf.controller.metadata.model.TrainStopContainer;  
 import oracle.adfinternal.controller.train.TrainModel;  
 import oracle.adfinternal.controller.train.TrainStopModel;  
 import oracle.adfinternal.controller.train.TrainUtils;  
 public class View2Bean {  
   public View2Bean() {  
   }  
   public String showView3Node() {  
     // Add event code here...  
     TrainModel trainModel = TrainUtils.findCurrentTrainModel();  
     ActivityId nodeActivityId = null;  
     ActivityId activityId;  
     Activity act;  
     for (Iterator it =  
        getTaskFlowDefinition(getTaskFlowId()).getActivities().values().iterator();  
        it.hasNext(); ) {  
       act = (Activity)it.next();  
       activityId = act.getId();  
       if (act.getIdAttribute().equals("view3")) {  
         System.out.println("Node");  
         System.out.println(act.getId());  
         nodeActivityId = activityId;  
       }  
     }  
     MetadataService metadataService = MetadataService.getInstance();  
     //Get activity and its train stop definition  
     Activity activity =  
       metadataService.getActivity((ActivityId)nodeActivityId);  
     TrainStopContainer stopContainer =  
       (TrainStopContainer)activity.getMetadataObject();  
     TrainStop ts = stopContainer.getTrainStop();  
     trainModel.getTrainStops().put((ActivityId)nodeActivityId,  
                     new TrainStopModel(ts,  
                              (ActivityId)nodeActivityId));  
     return null;  
   }  
   public String hideView3Node() {  
     // Add event code here...  
     TrainModel trainModel = TrainUtils.findCurrentTrainModel();  
     ActivityId nodeActivityId = null;  
     ActivityId activityId;  
     Activity act;  
     for (Iterator it =  
        getTaskFlowDefinition(getTaskFlowId()).getActivities().values().iterator();  
        it.hasNext(); ) {  
       act = (Activity)it.next();  
       activityId = act.getId();  
       if (act.getIdAttribute().equals("view3")) {  
         System.out.println("Node");  
         System.out.println(act.getId());  
         nodeActivityId = activityId;  
       }  
     }  
     MetadataService metadataService = MetadataService.getInstance();  
     //Get activity and its train stop definition  
     Activity activity =  
       metadataService.getActivity((ActivityId)nodeActivityId);  
     TrainStopContainer stopContainer =  
       (TrainStopContainer)activity.getMetadataObject();  
     TrainStop ts = stopContainer.getTrainStop();  
     trainModel.getTrainStops().remove((ActivityId)nodeActivityId);  
     return null;  
   }  
   public TaskFlowId getTaskFlowId() {  
     ControllerContext controllerContext = ControllerContext.getInstance();  
     ViewPortContext currentViewPort =  
       controllerContext.getCurrentViewPort();  
     TaskFlowContext taskFlowContext = currentViewPort.getTaskFlowContext();  
     TaskFlowId taskFlowId = taskFlowContext.getTaskFlowId();  
     return taskFlowId;  
   }  
   public TaskFlowDefinition getTaskFlowDefinition(TaskFlowId taskFlowId) {  
     assert taskFlowId != null;  
     MetadataService metadataService = MetadataService.getInstance();  
     TaskFlowDefinition taskFlowDefinition =  
       metadataService.getTaskFlowDefinition(taskFlowId);  
     return taskFlowDefinition;  
   }  
 }  

 the sample application can be downloaded from here