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.ErrorCode;
018    import org.apache.oozie.client.Job;
019    
020    /**
021     * Transition command for unpause the job. The derived class has to override these following functions:
022     * <p/>
023     * updateJob() : update job status and attributes
024     * unpauseChildren() : submit or queue commands to unpause children
025     * notifyParent() : update the status to upstream if any
026     *
027     * @param <T>
028     */
029    public abstract class UnpauseTransitionXCommand extends TransitionXCommand<Void> {
030        /**
031         * The constructor for abstract class {@link UnpauseTransitionXCommand}
032         *
033         * @param name the command name
034         * @param type the command type
035         * @param priority the command priority
036         */
037        public UnpauseTransitionXCommand(String name, String type, int priority) {
038            super(name, type, priority);
039        }
040    
041        /**
042         * Unpause actions associated with the job
043         *
044         * @throws CommandException thrown if failed to unpause actions
045         */
046        public abstract void unpauseChildren() throws CommandException;
047    
048        /* (non-Javadoc)
049         * @see org.apache.oozie.command.TransitionXCommand#transitToNext()
050         */
051        @Override
052        public final void transitToNext() throws CommandException {
053            if (job == null) {
054                job = this.getJob();
055            }
056    
057            if (job.getStatus() == Job.Status.PAUSED) {
058                job.setStatus(Job.Status.RUNNING);
059            }
060            else if (job.getStatus() == Job.Status.PAUSEDWITHERROR) {
061                job.setStatus(Job.Status.RUNNINGWITHERROR);
062            }
063            else if (job.getStatus() == Job.Status.PREPPAUSED) {
064                job.setStatus(Job.Status.PREP);
065            }
066            else {
067                throw new CommandException(ErrorCode.E1316, job.getId());
068            }
069    
070            //TODO: to be revisited;
071            //job.setPending();
072        }
073    
074        /* (non-Javadoc)
075         * @see org.apache.oozie.command.TransitionXCommand#execute()
076         */
077        @Override
078        protected Void execute() throws CommandException {
079            try {
080                transitToNext();
081                updateJob();
082                unpauseChildren();
083            }
084            finally {
085                notifyParent();
086            }
087            return null;
088        }
089    }