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    package org.apache.oozie.command;
019    
020    import org.apache.oozie.client.Job;
021    
022    /**
023     * Base class for submit transition command. The derived class has to override these following functions:
024     * <p/>
025     * loadState() : load the job's and/or actions' state
026     * submit() : submit the job
027     * notifyParent() : update the status to upstream if any
028     */
029    public abstract class SubmitTransitionXCommand extends TransitionXCommand<String> {
030    
031        /**
032         * The constructor for abstract class {@link SubmitTransitionXCommand}
033         *
034         * @param name the command name
035         * @param type the command type
036         * @param priority the command priority
037         */
038        public SubmitTransitionXCommand(String name, String type, int priority) {
039            super(name, type, priority);
040        }
041    
042        /**
043         * The constructor for abstract class {@link SubmitTransitionXCommand}
044         *
045         * @param name the command name
046         * @param type the command type
047         * @param priority the command priority
048         * @param dryrun true if dryrun is enable
049         */
050        public SubmitTransitionXCommand(String name, String type, int priority, boolean dryrun) {
051            super(name, type, priority, dryrun);
052        }
053    
054        /**
055         * Submit the job
056         *
057         * @return the id
058         * @throws CommandException thrown if unable to submit
059         */
060        protected abstract String submit() throws CommandException;
061    
062        /* (non-Javadoc)
063         * @see org.apache.oozie.command.TransitionXCommand#transitToNext()
064         */
065        @Override
066        public void transitToNext() {
067            if (job == null) {
068                job = this.getJob();
069            }
070            job.setStatus(Job.Status.PREP);
071            job.resetPending();
072        }
073    
074        /* (non-Javadoc)
075         * @see org.apache.oozie.command.XCommand#execute()
076         */
077        @Override
078        protected String execute() throws CommandException {
079            try {
080                transitToNext();
081                String jobId = submit();
082                return jobId;
083            }
084            finally {
085                notifyParent();
086            }
087        }
088    }