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 java.util.ArrayList;
021    import java.util.List;
022    
023    import org.apache.oozie.client.Job;
024    import org.apache.oozie.client.rest.JsonBean;
025    import org.apache.oozie.util.ParamChecker;
026    
027    /**
028     * This is the base commands for all the jobs related commands . This will drive the statuses for all the jobs and all
029     * the jobs will follow the same state machine.
030     *
031     * @param <T>
032     */
033    public abstract class TransitionXCommand<T> extends XCommand<T> {
034    
035        protected Job job;
036        protected List<JsonBean> updateList = new ArrayList<JsonBean>();
037        protected List<JsonBean> insertList = new ArrayList<JsonBean>();
038    
039        public TransitionXCommand(String name, String type, int priority) {
040            super(name, type, priority);
041        }
042    
043        public TransitionXCommand(String name, String type, int priority, boolean dryrun) {
044            super(name, type, priority, dryrun);
045        }
046    
047        /**
048         * Transit to the next status based on the result of the Job.
049         *
050         * @throws CommandException
051         */
052        public abstract void transitToNext() throws CommandException;
053    
054        /**
055         * Update the parent job.
056         *
057         * @throws CommandException
058         */
059        public abstract void updateJob() throws CommandException;
060    
061        /**
062         * This will be used to notify the parent about the status of that perticular job.
063         *
064         * @throws CommandException
065         */
066        public abstract void notifyParent() throws CommandException;
067    
068        /**
069         * This will be used to perform atomically all the writes within this command.
070         *
071         * @throws CommandException
072         */
073        public abstract void performWrites() throws CommandException;
074    
075        /* (non-Javadoc)
076         * @see org.apache.oozie.command.XCommand#execute()
077         */
078        @Override
079        protected T execute() throws CommandException {
080            transitToNext();
081            updateJob();
082            notifyParent();
083            return null;
084        }
085    
086        /**
087         * Get the Job for the command.
088         *
089         * @return the job
090         */
091        public Job getJob() {
092            return job;
093        }
094    
095        /**
096         * Set the Job for the command.
097         *
098         * @param job the job
099         */
100        public void setJob(Job job) {
101            this.job = ParamChecker.notNull(job, "job");
102        }
103    
104    }