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.wf;
020
021import java.util.List;
022import java.util.Map;
023
024import org.apache.oozie.ErrorCode;
025import org.apache.oozie.WorkflowJobBean;
026import org.apache.oozie.WorkflowsInfo;
027import org.apache.oozie.command.CommandException;
028import org.apache.oozie.command.PreconditionException;
029import org.apache.oozie.executor.jpa.WorkflowsJobGetJPAExecutor;
030import org.apache.oozie.service.JPAService;
031import org.apache.oozie.service.Services;
032
033public class JobsXCommand extends WorkflowXCommand<WorkflowsInfo> {
034    private final Map<String, List<String>> filter;
035    private final int start;
036    private final int len;
037    private WorkflowsInfo workflows;
038
039    /**
040     * Constructor taking the filter information
041     *
042     * @param filter Can be name, status, user, group and combination of these
043     * @param start starting from this index in the list of workflows matching the filter are returned
044     * @param length number of workflows to be returned from the list of workflows matching the filter and starting from
045     *        index "start".
046     */
047    public JobsXCommand(Map<String, List<String>> filter, int start, int length) {
048        super("job.info", "job.info", 1, true);
049        this.filter = filter;
050        this.start = start;
051        this.len = length;
052    }
053
054    /* (non-Javadoc)
055     * @see org.apache.oozie.command.XCommand#execute()
056     */
057    @Override
058    protected WorkflowsInfo execute() throws CommandException {
059        try {
060            JPAService jpaService = Services.get().get(JPAService.class);
061            if (jpaService != null) {
062                this.workflows = jpaService.execute(new WorkflowsJobGetJPAExecutor(this.filter, this.start, this.len));
063            }
064            else {
065                throw new CommandException(ErrorCode.E0610);
066            }
067            for (WorkflowJobBean workflow : this.workflows.getWorkflows()) {
068                workflow.setConsoleUrl(JobXCommand.getJobConsoleUrl(workflow.getId()));
069            }
070            return this.workflows;
071        }
072        catch (Exception ex) {
073            throw new CommandException(ErrorCode.E0603, ex.getMessage(), ex);
074        }
075    }
076
077    /* (non-Javadoc)
078     * @see org.apache.oozie.command.XCommand#getEntityKey()
079     */
080    @Override
081    public String getEntityKey() {
082        return null;
083    }
084
085    /* (non-Javadoc)
086     * @see org.apache.oozie.command.XCommand#isLockRequired()
087     */
088    @Override
089    protected boolean isLockRequired() {
090        return false;
091    }
092
093    /* (non-Javadoc)
094     * @see org.apache.oozie.command.XCommand#loadState()
095     */
096    @Override
097    protected void loadState() throws CommandException {
098    }
099
100    /* (non-Javadoc)
101     * @see org.apache.oozie.command.XCommand#verifyPrecondition()
102     */
103    @Override
104    protected void verifyPrecondition() throws CommandException, PreconditionException {
105    }
106}