001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.executor.jpa;
016    
017    import java.util.List;
018    
019    import javax.persistence.EntityManager;
020    
021    import org.apache.oozie.ErrorCode;
022    import org.apache.oozie.WorkflowActionBean;
023    import org.apache.oozie.WorkflowJobBean;
024    import org.apache.oozie.command.CommandException;
025    import org.apache.oozie.service.JPAService;
026    import org.apache.oozie.service.Services;
027    import org.apache.oozie.util.ParamChecker;
028    
029    /**
030     * This JPA Executor is responsible for getting the Workflow job with actions in certain range.
031     */
032    public class WorkflowInfoWithActionsSubsetGetJPAExecutor implements JPAExecutor<WorkflowJobBean> {
033    
034        private String wfJobId = null;
035        private WorkflowJobBean workflow;
036        private final int start;
037        private final int len;
038    
039        /**
040         * This will create the WorkflowInfoWithActionsSubsetGetJPAExecutor object. which is responsible for getting the
041         * Workflow job with actions in certain range.
042         * 
043         * @param wfJobId
044         * @param start
045         * @param len
046         */
047        public WorkflowInfoWithActionsSubsetGetJPAExecutor(String wfJobId, int start, int len) {
048            ParamChecker.notNull(wfJobId, "wfJobId");
049            this.wfJobId = wfJobId;
050            this.start = start;
051            this.len = len;
052        }
053    
054        /* (non-Javadoc)
055         * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
056         */
057        @Override
058        public WorkflowJobBean execute(EntityManager em) throws JPAExecutorException {
059            try {
060                JPAService jpaService = Services.get().get(JPAService.class);
061                if (jpaService != null) {
062                    this.workflow = jpaService.execute(new WorkflowJobGetJPAExecutor(this.wfJobId));
063                }
064                else {
065                    throw new JPAExecutorException(ErrorCode.E0610, this.wfJobId);
066                }
067            }
068            catch (Exception ex) {
069                if (ex instanceof JPAExecutorException) {
070                    throw (JPAExecutorException) ex;
071                }
072                else {
073                    throw new JPAExecutorException(ErrorCode.E0603, ex);
074                }
075            }
076    
077            if (this.workflow != null) {
078                JPAService jpaService = Services.get().get(JPAService.class);
079                List<WorkflowActionBean> actionList;
080                if (jpaService != null) {
081                    actionList = jpaService.execute(new WorkflowActionSubsetGetJPAExecutor(this.wfJobId, start, len));
082                }
083                else {
084                    throw new JPAExecutorException(ErrorCode.E0610, this.wfJobId);
085                }
086                this.workflow.setActions(actionList);
087            }
088            else {
089                throw new JPAExecutorException(ErrorCode.E0604, wfJobId);
090            }
091    
092            return this.workflow;
093        }
094    
095        /* (non-Javadoc)
096         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
097         */
098        @Override
099        public String getName() {
100            return "WorkflowInfoWithActionsSubsetGetJPAExecutor";
101        }
102    }