001/*
002 * Copyright 2006-2011 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.core.job.builder;
017
018import org.springframework.batch.core.Job;
019import org.springframework.batch.core.Step;
020import org.springframework.batch.core.job.flow.Flow;
021import org.springframework.batch.core.job.flow.FlowJob;
022import org.springframework.batch.core.step.builder.StepBuilderException;
023
024/**
025 * A job builder for {@link FlowJob} instances. A flow job delegates processing to a nested flow composed of steps and
026 * conditional transitions between steps.
027 * 
028 * @author Dave Syer
029 * 
030 * @since 2.2
031 */
032public class FlowJobBuilder extends JobBuilderHelper<FlowJobBuilder> {
033
034        private Flow flow;
035
036        /**
037         * Create a new builder initialized with any properties in the parent. The parent is copied, so it can be re-used.
038         * 
039         * @param parent a parent helper containing common job properties
040         */
041        public FlowJobBuilder(JobBuilderHelper<?> parent) {
042                super(parent);
043        }
044
045        /**
046         * Start a job with this flow, but expect to transition from there to other flows or steps.
047         * 
048         * @param flow the flow to start with
049         * @return a builder to enable fluent chaining
050         */
051        public JobFlowBuilder start(Flow flow) {
052                return new JobFlowBuilder(this, flow);
053        }
054
055        /**
056         * Start a job with this step, but expect to transition from there to other flows or steps.
057         * 
058         * @param step the step to start with
059         * @return a builder to enable fluent chaining
060         */
061        public JobFlowBuilder start(Step step) {
062                return new JobFlowBuilder(this, step);
063        }
064
065        /**
066         * Provide a single flow to execute as the job.
067         * 
068         * @param flow the flow to execute
069         * @return this for fluent chaining
070         */
071        protected FlowJobBuilder flow(Flow flow) {
072                this.flow = flow;
073                return this;
074        }
075
076        /**
077         * Build a job that executes the flow provided, normally composed of other steps.
078         * 
079         * @return a flow job
080         */
081        public Job build() {
082                FlowJob job = new FlowJob();
083                job.setName(getName());
084                job.setFlow(flow);
085                super.enhance(job);
086                try {
087                        job.afterPropertiesSet();
088                }
089                catch (Exception e) {
090                        throw new StepBuilderException(e);
091                }
092                return job;
093        }
094
095}