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