001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.command;
016    
017    import org.apache.oozie.client.Job;
018    
019    /**
020     * Base class for submit transition command. The derived class has to override these following functions:
021     * <p/>
022     * loadState() : load the job's and/or actions' state
023     * submit() : submit the job
024     * notifyParent() : update the status to upstream if any
025     */
026    public abstract class SubmitTransitionXCommand extends TransitionXCommand<String> {
027    
028        /**
029         * The constructor for abstract class {@link SubmitTransitionXCommand}
030         *
031         * @param name the command name
032         * @param type the command type
033         * @param priority the command priority
034         */
035        public SubmitTransitionXCommand(String name, String type, int priority) {
036            super(name, type, priority);
037        }
038    
039        /**
040         * The constructor for abstract class {@link SubmitTransitionXCommand}
041         *
042         * @param name the command name
043         * @param type the command type
044         * @param priority the command priority
045         * @param dryrun true if dryrun is enable
046         */
047        public SubmitTransitionXCommand(String name, String type, int priority, boolean dryrun) {
048            super(name, type, priority, dryrun);
049        }
050    
051        /**
052         * Submit the job
053         *
054         * @return the id
055         * @throws CommandException thrown if unable to submit
056         */
057        protected abstract String submit() throws CommandException;
058    
059        /* (non-Javadoc)
060         * @see org.apache.oozie.command.TransitionXCommand#transitToNext()
061         */
062        @Override
063        public void transitToNext() {
064            if (job == null) {
065                job = this.getJob();
066            }
067            job.setStatus(Job.Status.PREP);
068            job.resetPending();
069        }
070    
071        /* (non-Javadoc)
072         * @see org.apache.oozie.command.XCommand#execute()
073         */
074        @Override
075        protected String execute() throws CommandException {
076            try {
077                transitToNext();
078                String jobId = submit();
079                return jobId;
080            }
081            finally {
082                notifyParent();
083            }
084        }
085    }