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