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.coord;
016    
017    import java.util.List;
018    
019    import org.apache.oozie.CoordinatorActionBean;
020    import org.apache.oozie.CoordinatorJobBean;
021    import org.apache.oozie.ErrorCode;
022    import org.apache.oozie.XException;
023    import org.apache.oozie.command.CommandException;
024    import org.apache.oozie.command.PreconditionException;
025    import org.apache.oozie.executor.jpa.CoordActionsSubsetGetForJobJPAExecutor;
026    import org.apache.oozie.executor.jpa.CoordJobGetJPAExecutor;
027    import org.apache.oozie.service.JPAService;
028    import org.apache.oozie.service.Services;
029    import org.apache.oozie.util.ParamChecker;
030    
031    /**
032     * Command for loading a coordinator job information
033     */
034    public class CoordJobXCommand extends CoordinatorXCommand<CoordinatorJobBean> {
035        private final String id;
036        private final boolean getActionInfo;
037        private int start = 1;
038        private int len = Integer.MAX_VALUE;
039    
040        /**
041         * Constructor for loading a coordinator job information
042         *
043         * @param id coord jobId
044         */
045        public CoordJobXCommand(String id) {
046            this(id, 1, Integer.MAX_VALUE);
047        }
048    
049        /**
050         * Constructor for loading a coordinator job information
051         *
052         * @param id coord jobId
053         * @param start starting index in the list of actions belonging to the job
054         * @param length number of actions to be returned
055         */
056        public CoordJobXCommand(String id, int start, int length) {
057            super("job.info", "job.info", 1);
058            this.id = ParamChecker.notEmpty(id, "id");
059            this.getActionInfo = true;
060            this.start = start;
061            this.len = length;
062        }
063    
064        /**
065         * Constructor for loading a coordinator job information
066         *
067         * @param id coord jobId
068         * @param getActionInfo false to ignore loading actions for the job
069         */
070        public CoordJobXCommand(String id, boolean getActionInfo) {
071            super("job.info", "job.info", 1);
072            this.id = ParamChecker.notEmpty(id, "id");
073            this.getActionInfo = getActionInfo;
074        }
075    
076        /* (non-Javadoc)
077         * @see org.apache.oozie.command.XCommand#isLockRequired()
078         */
079        @Override
080        protected boolean isLockRequired() {
081            return false;
082        }
083    
084        /* (non-Javadoc)
085         * @see org.apache.oozie.command.XCommand#getEntityKey()
086         */
087        @Override
088        protected String getEntityKey() {
089            return this.id;
090        }
091    
092        /* (non-Javadoc)
093         * @see org.apache.oozie.command.XCommand#loadState()
094         */
095        @Override
096        protected void loadState() throws CommandException {
097        }
098    
099        /* (non-Javadoc)
100         * @see org.apache.oozie.command.XCommand#verifyPrecondition()
101         */
102        @Override
103        protected void verifyPrecondition() throws CommandException, PreconditionException {
104        }
105    
106        /* (non-Javadoc)
107         * @see org.apache.oozie.command.XCommand#execute()
108         */
109        @Override
110        protected CoordinatorJobBean execute() throws CommandException {
111            try {
112                JPAService jpaService = Services.get().get(JPAService.class);
113                CoordinatorJobBean coordJob = null;
114                if (jpaService != null) {
115                    coordJob = jpaService.execute(new CoordJobGetJPAExecutor(id));
116                    if (getActionInfo) {
117                        List<CoordinatorActionBean> coordActions = jpaService
118                                .execute(new CoordActionsSubsetGetForJobJPAExecutor(id, start, len));
119                        coordJob.setActions(coordActions);
120                    }
121                }
122                else {
123                    LOG.error(ErrorCode.E0610);
124                }
125                return coordJob;
126            }
127            catch (XException ex) {
128                throw new CommandException(ex);
129            }
130        }
131    
132    }