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