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