Dynamic ADF Train: Adding train stops programmatically

I’m going to show how to add train stops to ADF train programmatically “on-the-fly”. In my use-case I have some ticket-booking application. It has a bounded task flow with train model. At the first stop of the train users input number of passengers and at the following stops they input some passengers’ info. The number of stops with passengers’ info has to be changed dynamically depending on the value submitted at the first train stop. So, the result of described behaviour should look like this:

The bounded task flow has the following structure:

StartView activity is a page fragment where we input number of passengers and DynamicView activity provides a page fragment to input passenger’s info. At the moment we have only one activity for passenger’s info and I will add extra activities if the number of passengers is greater than one.
The inputNumberSpinbox in StartView page fragment submits its value to passengersNumber property of some PageFlowScope backing bean and action for the Submit button is a method of the same bean:

public class MainTrain {
 //Extra added train stops
 private List<ActivityId> dynamicStops = new ArrayList<ActivityId>();
 
 //Value of inputNumberSpinbox
 private int passengersNumber = 1;
 
 public String buttonPress(){
 //The number of extra added train stops is greater than needed
 if (passengersNumber <= dynamicStops.size())
 clearExtraStops();
 else //The number of extra added train stops is less than needed 
 if (passengersNumber-1 > dynamicStops.size()) 
 addDynamicStops(); 
 return null;
 }

So, by pressing on Submit button we either add some train stops or clear extra stops depending on the value of inputNumberSpinbox. We save all added dynamic stops in dynamicStops list. Let’s have a look at the clearExtraStops() method:

 private void clearExtraStops() {
 for (int i = dynamicStops.size(); i >= passengersNumber; i--) {
 //Get ActivityId to be removed
 ActivityId removeActivityId = dynamicStops.get(i-1);

 //Get current train model and remove train stop
 TrainModel trainModel = TrainUtils.findCurrentTrainModel();
 trainModel.getTrainStops().remove(removeActivityId);
 
 //Remove activity from task flow definition
 getTaskFlowDefinition().getActivities().remove(removeActivityId);
 dynamicStops.remove(i-1);
 } 
 }

The method removes two things: the train stop from the train model and the activity from the task flow definition. The addDynamicStops() method is going to be much more interesting:

private void addDynamicStops() { 
 for (int i = dynamicStops.size(); i < passengersNumber - 1; i++) {
 //Creating new ActivityId
 ActivityId activityId = 
 new ActivityId(getTaskFlowId(), new StringBuilder("DynamicView").append(i).toString()); 

 //The main trick of the post.
 //We consider DynamicView activity as a base for new train stop and new activity
 
 //Get base activity (DynamicView) and its train stop
 Activity baseActivity = getBaseDynamicActivity();
 TrainStopContainer stopContainer = (TrainStopContainer)baseActivity.getMetadataObject();
 TrainStop baseTrainStop = stopContainer.getTrainStop();

 //Create new Activity based on DynamicView but with new ActivityId 
 ActivityImpl activityImpl = new ActivityImpl(baseActivity, activityId); 
 //Add created activity to the task flow definition
 getTaskFlowDefinition().getActivities().put(activityId, activityImpl);

 //Create new train stop based on the DynamicView's train stop
 TrainStopModel trainStopModel = new TrainStopModel(
 new TrainStopImpl(baseTrainStop, i+2), activityId);
 //Add created train stop to the train stop model
 TrainModel trainModel = TrainUtils.findCurrentTrainModel();
 trainModel.getTrainStops().put(activityId, trainStopModel); 
 //Add created activity to our list
 dynamicStops.add(activityId); 
 }
}
 
private Activity getBaseDynamicActivity() {
 ActivityId baseActivityId = new ActivityId(getTaskFlowId(), "DynamicView"); 
 MetadataService metadataService = MetadataService.getInstance();
 return metadataService.getActivity(baseActivityId); 
}

private TaskFlowDefinition getTaskFlowDefinition() {
 MetadataService metadataService = MetadataService.getInstance();
 return metadataService.getTaskFlowDefinition(getTaskFlowId()); 
}


private TaskFlowId getTaskFlowId() {
 ControllerContext controllerContext = ControllerContext.getInstance(); 
 ViewPortContext currentViewPortCtx = controllerContext.getCurrentViewPort(); 
 TaskFlowContext taskFlowCtx = currentViewPortCtx.getTaskFlowContext(); 
 return taskFlowCtx.getTaskFlowId();
}

So, the principal trick of this post is to create new activity and train stops basing on existing ones for DynamicView. In order to implement the idea I created two classes: ActivityImpl and TrainStopImpl. The classes are nothing else than just proxy classes implementing Activity and TrainStop interfaces correspondently. They delegates interface implementation to the base instances except some specific methods like getters for Id and DisplayName:

public class TrainStopImpl implements TrainStop { 
 //Base instance 
 private TrainStop baseTrainStop;
 
 private int mpassNo;
 private static final String PASSANGER_FORM = "Passenger's data: ";
 
 public TrainStopImpl(TrainStop trainStop, int passNo) {
 baseTrainStop = trainStop; 
 mpassNo = passNo;
 }

 //Specific implementation
 public String getDisplayName() {
 return new StringBuilder(PASSANGER_FORM).append(mpassNo).toString();
 }

 public String getOutcome() {
 return baseTrainStop.getOutcome();
 }

 public String getSequential() {
 return baseTrainStop.getSequential();
 }

...
public class ActivityImpl implements Activity {
 private Activity baseActivity;
 private ActivityId mid;
 
 public ActivityImpl(Activity activity, ActivityId id) {
 baseActivity = activity;
 mid = id;
 }

 //Specific implementation
 public ActivityId getId() {
 return mid;
 }

 public String getType() {
 return baseActivity.getType();
 }

 public Object getMetadataObject() {
 return baseActivity.getMetadataObject();
 }
...

And one more picture for this post, just to show it’s working:

That’s all! You can download sample application for JDeveloper 11.1.1.2.0.

Reference: Dynamic ADF Train. Adding train stops programmatically from our JCG partner Eugene Fedorenko at the ADF Practice blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

One Response to "Dynamic ADF Train: Adding train stops programmatically"

  1. The source code does not compile with JDev ADF 11.1.2

Leave a Reply


+ 7 = ten



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books