Example usage for org.springframework.statemachine.action Action Action

List of usage examples for org.springframework.statemachine.action Action Action

Introduction

In this page you can find the example usage for org.springframework.statemachine.action Action Action.

Prototype

Action

Source Link

Usage

From source file:org.springframework.statemachine.recipes.tasks.TasksHandler.java

/**
 * {@link Action} which simply sends an event of continue
 * tasks into a state machine.//from  ww  w  . j  a  va 2s .co  m
 *
 * @return the action
 */
private Action<String, String> continueAction() {
    return new Action<String, String>() {

        @Override
        public void execute(StateContext<String, String> context) {
            listener.onTasksContinue();
        }
    };
}

From source file:org.springframework.statemachine.recipes.tasks.TasksHandler.java

/**
 * {@link Action} calls {@link TasksListener#onTasksAutomaticFix(StateContext)}
 * before checking status of extended state variables related to tasks. If all
 * variables are ok, event {@code EVENT_CONTINUE} is sent, otherwise event
 * {@code EVENT_FALLBACK} is send which takes state machine into a manual handling.
 *
 * @return the action/*from   w  w w  .  j  av a 2s.  c o m*/
 */
private Action<String, String> automaticAction() {
    return new Action<String, String>() {

        @Override
        public void execute(StateContext<String, String> context) {

            listener.onTasksAutomaticFix(TasksHandler.this, context);

            boolean hasErrors = false;
            Map<Object, Object> variables = context.getExtendedState().getVariables();
            for (Entry<Object, Object> entry : variables.entrySet()) {
                if (entry.getKey() instanceof String
                        && ((String) entry.getKey()).startsWith(STATE_TASKS_PREFIX)) {
                    if (entry.getValue() instanceof Integer) {
                        Integer value = (Integer) entry.getValue();
                        if (value < 0) {
                            hasErrors = true;
                            break;
                        }
                    }
                }
            }
            if (hasErrors) {
                context.getStateMachine().sendEvent(EVENT_FALLBACK);
            } else {
                context.getStateMachine().sendEvent(EVENT_CONTINUE);
            }
        }
    };
}

From source file:org.springframework.statemachine.recipes.tasks.TasksHandler.java

/**
 * {@link Action} which resets related extended state variables
 * to zero for tasks order to indicate a fixed tasks.
 *
 * @return the action/*  w  w w  .  j a  v  a2s . c  o  m*/
 */
private Action<String, String> fixAction() {
    return new Action<String, String>() {

        @Override
        public void execute(StateContext<String, String> context) {
            Map<Object, Object> variables = context.getExtendedState().getVariables();
            for (Entry<Object, Object> entry : variables.entrySet()) {
                if (entry.getKey() instanceof String
                        && ((String) entry.getKey()).startsWith(STATE_TASKS_PREFIX)) {
                    if (entry.getValue() instanceof Integer) {
                        Integer value = (Integer) entry.getValue();
                        if (value < 0) {
                            variables.put(entry.getKey(), 0);
                        }
                    }
                }
            }
        }
    };
}