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