有界任务流具有以下结构:
StartView活动是一个页面片段,我们在其中输入乘客人数,而DynamicView活动提供了一个页面片段来输入乘客的信息。 目前,我们只有一个活动来提供乘客的信息,如果乘客人数大于一个,我将添加其他活动。
在StartView页面片段的inputNumberSpinbox提交其值设置为某些PageFlowScope支持bean的passengersNumber财产和行动的提交按钮是同一个bean的方法:
public class MainTrain {//Extra added train stopsprivate List<ActivityId> dynamicStops = new ArrayList<ActivityId>();//Value of inputNumberSpinboxprivate int passengersNumber = 1;public String buttonPress(){//The number of extra added train stops is greater than neededif (passengersNumber <= dynamicStops.size())clearExtraStops();else //The number of extra added train stops is less than needed if (passengersNumber-1 > dynamicStops.size()) addDynamicStops(); return null;}
因此,通过按Submit按钮,我们可以根据inputNumberSpinbox的值添加一些火车停靠点或清除额外的停靠点 。 我们将所有添加的动态停靠点保存在dynamicStops列表中。 让我们看一下clearExtraStops()方法:
private void clearExtraStops() {for (int i = dynamicStops.size(); i >= passengersNumber; i--) {//Get ActivityId to be removedActivityId removeActivityId = dynamicStops.get(i-1);//Get current train model and remove train stopTrainModel trainModel = TrainUtils.findCurrentTrainModel();trainModel.getTrainStops().remove(removeActivityId);//Remove activity from task flow definitiongetTaskFlowDefinition().getActivities().remove(removeActivityId);dynamicStops.remove(i-1);} }
该方法删除了两件事:火车模型中的火车停靠点和任务流定义中的活动。 addDynamicStops()方法将变得更加有趣:
private void addDynamicStops() { for (int i = dynamicStops.size(); i < passengersNumber - 1; i++) {//Creating new ActivityIdActivityId 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 stopActivity 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 definitiongetTaskFlowDefinition().getActivities().put(activityId, activityImpl);//Create new train stop based on the DynamicView's train stopTrainStopModel trainStopModel = new TrainStopModel(new TrainStopImpl(baseTrainStop, i+2), activityId);//Add created train stop to the train stop modelTrainModel trainModel = TrainUtils.findCurrentTrainModel();trainModel.getTrainStops().put(activityId, trainStopModel); //Add created activity to our listdynamicStops.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();
}
因此,本文的主要技巧是创建新的活动,并基于DynamicView的现有活动来停止站点。 为了实现这个想法,我创建了两个类: ActivityImpl和TrainStopImpl 。 这些类只不过是相应地实现Activity和TrainStop接口的代理类。 它们将接口实现委派给基本实例,除了一些特定的方法(例如,用于ID和DisplayName的getter)之外:
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 implementationpublic 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 implementationpublic ActivityId getId() {return mid;}public String getType() {return baseActivity.getType();}public Object getMetadataObject() {return baseActivity.getMetadataObject();}
...
这篇文章的另一张图片只是为了展示它的工作原理:
就这样! 您可以下载JDeveloper 11.1.1.2.0的示例应用程序 。
参考: 动态ADF列车。 通过ADF实践博客中的JCG合作伙伴 Eugene Fedorenko以编程方式添加火车停靠站 。
翻译自: https://www.javacodegeeks.com/2012/05/dynamic-adf-train-adding-train-stops.html