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.CoordinatorActionBean;
024    import org.apache.oozie.CoordinatorJobBean;
025    import org.apache.oozie.client.Job;
026    import org.apache.oozie.client.rest.JsonBean;
027    import org.apache.oozie.command.coord.CoordinatorXCommand;
028    import org.apache.oozie.util.ParamChecker;
029    
030    /**
031     * This is the base commands for all the jobs related commands . This will drive the statuses for all the jobs and all
032     * the jobs will follow the same state machine.
033     *
034     * @param <T>
035     */
036    public abstract class TransitionXCommand<T> extends XCommand<T> {
037    
038        protected Job job;
039        protected List<JsonBean> updateList = new ArrayList<JsonBean>();
040        protected List<JsonBean> insertList = new ArrayList<JsonBean>();
041    
042        public TransitionXCommand(String name, String type, int priority) {
043            super(name, type, priority);
044        }
045    
046        public TransitionXCommand(String name, String type, int priority, boolean dryrun) {
047            super(name, type, priority, dryrun);
048        }
049    
050        /**
051         * Transit to the next status based on the result of the Job.
052         *
053         * @throws CommandException
054         */
055        public abstract void transitToNext() throws CommandException;
056    
057        /**
058         * Update the parent job.
059         *
060         * @throws CommandException
061         */
062        public abstract void updateJob() throws CommandException;
063    
064        /**
065         * This will be used to notify the parent about the status of that perticular job.
066         *
067         * @throws CommandException
068         */
069        public abstract void notifyParent() throws CommandException;
070    
071        /**
072         * This will be used to generate Job Notification events on status changes
073         *
074         * @param user
075         * @param appName
076         * @param em
077         * @throws CommandException
078         */
079        public void generateEvents(CoordinatorJobBean coordJob) throws CommandException {
080            for (JsonBean actionBean : updateList) {
081                if (actionBean instanceof CoordinatorActionBean) {
082                    CoordinatorActionBean caBean = (CoordinatorActionBean) actionBean;
083                    caBean.setJobId(coordJob.getId());
084                    CoordinatorXCommand.generateEvent(caBean, coordJob.getUser(), coordJob.getAppName(), null);
085                }
086                // TODO generate Coord Job event
087            }
088        }
089    
090        /**
091         * This will be used to perform atomically all the writes within this command.
092         *
093         * @throws CommandException
094         */
095        public abstract void performWrites() throws CommandException;
096    
097        /* (non-Javadoc)
098         * @see org.apache.oozie.command.XCommand#execute()
099         */
100        @Override
101        protected T execute() throws CommandException {
102            transitToNext();
103            updateJob();
104            notifyParent();
105            return null;
106        }
107    
108        /**
109         * Get the Job for the command.
110         *
111         * @return the job
112         */
113        public Job getJob() {
114            return job;
115        }
116    
117        /**
118         * Set the Job for the command.
119         *
120         * @param job the job
121         */
122        public void setJob(Job job) {
123            this.job = ParamChecker.notNull(job, "job");
124        }
125    
126    }