*/
@SuppressWarnings("unchecked")
public ILink doEnterFlow(FlowLauncher flowLauncher, String finishFlowId, Iterable<String> initialValues) {
String pageName = null;
// grab FlowState now because the flow may be finished and no longer be available.
FlowState currentFlowState = getFlowManagement().getCurrentFlowState();
FlowState flowState = null;
boolean success = false;
try {
// should this be a force situation and ignore any errors? seems unlikely as Amplafi would want user to be able to save any half-finished changes.
if (finishFlowId != null ) {
flowState = getFlowManagement().getFlowState(finishFlowId);
if ( flowState != null && !flowState.isCompleted()) {
pageName = flowState.finishFlow();
}
}
if ( flowLauncher != null) {
flowLauncher.setFlowManagement(getFlowManagement());
if (flowLauncher instanceof StartFromDefinitionFlowLauncher) {
// if currentFlowState was just finished, we want the final flow state.
if(currentFlowState != null){
Map<String, String> initialFlowState = currentFlowState.getExportedValuesMap().getAsFlattenedStringMap();
flowLauncher.putAll(initialFlowState);
}
if ( initialValues != null) {
((StartFromDefinitionFlowLauncher)flowLauncher).setPropertyRoot(getContainer());
((StartFromDefinitionFlowLauncher)flowLauncher).addInitialValues(initialValues);
}
}
}
success = true;
} catch (FlowValidationException e) {
getFlowResultHandler().handleValidationTrackings(e.getTrackings(), this);
}
if (success) {
try {
if ( flowLauncher != null ) {
flowState = flowLauncher.call();
pageName = (flowState != null) ? flowState.getCurrentPage() : null;
}
if ( isBlank(pageName) ) {
// stay on current page if the finishFlow failed.
pageName = getPageName();
}
} catch (FlowValidationException e) {
getFlowResultHandler().handleValidationTrackings(e.getTrackings(), this);
// HACK: cleanup - this seems really wrong - we should only clean up flow if was the flow started
// not just any random flow. - we are relying on luck that the failed flow is the current flow.
// the FlowValidationException should have a reference to the flowState.
FlowState current = getFlowManagement().getCurrentFlowState();
if (current!=null) {
getFlowManagement().dropFlowState(current);
}
pageName = null;
} finally {
if ( pageName == null || pageName.equals(this.getPage().getPageName()) || isDynamic()) {
List<String> findComponentsToUpdate = findComponentsToUpdate(getUpdateComponents());
this.updateComponents(findComponentsToUpdate);
}
}
}
FlowState newCurrentFlow = getFlowManagement().getCurrentFlowState();
if ( newCurrentFlow != null && newCurrentFlow != flowState ) {
pageName = newCurrentFlow.getCurrentPage();
} else {
newCurrentFlow = flowState;
}
FlowWebUtils.activatePageIfNotNull(getRequestCycle(), pageName, newCurrentFlow);
return null;