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.executor.jpa;
019    
020    import java.sql.SQLException;
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import javax.persistence.EntityManager;
025    import javax.persistence.Query;
026    
027    import org.apache.oozie.ErrorCode;
028    import org.apache.oozie.WorkflowActionBean;
029    import org.apache.oozie.util.ParamChecker;
030    import org.apache.openjpa.persistence.OpenJPAPersistence;
031    
032    /**
033     * JPA Command to get subset of workflow actions for a particular workflow.
034     */
035    public class WorkflowActionSubsetGetJPAExecutor implements JPAExecutor<List<WorkflowActionBean>> {
036    
037        private final String wfId;
038        private final int start;
039        private final int length;
040    
041        /**
042         * This Constructor creates the WorkflowActionSubsetGetJPAExecutor object Which gets the List of wrokflow action
043         * bean.
044         *
045         * @param wfId
046         * @param start
047         * @param length
048         */
049        public WorkflowActionSubsetGetJPAExecutor(String wfId, int start, int length) {
050            ParamChecker.notNull(wfId, "wfJobId");
051            this.wfId = wfId;
052            this.start = start;
053            this.length = length;
054        }
055    
056        /* (non-Javadoc)
057         * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
058         */
059        @Override
060        @SuppressWarnings("unchecked")
061        public List<WorkflowActionBean> execute(EntityManager em) throws JPAExecutorException {
062            List<WorkflowActionBean> actions;
063            List<WorkflowActionBean> actionList = new ArrayList<WorkflowActionBean>();
064            try {
065                Query q = em.createNamedQuery("GET_ACTIONS_FOR_WORKFLOW");
066                OpenJPAPersistence.cast(q);
067                q.setParameter("wfId", wfId);
068                q.setFirstResult(start - 1);
069                q.setMaxResults(length);
070                actions = q.getResultList();
071            }
072            catch (Exception e) {
073                throw new JPAExecutorException(ErrorCode.E0605, "null", e);
074            }
075            return actions;
076        }
077    
078        /* (non-Javadoc)
079         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
080         */
081        @Override
082        public String getName() {
083            return "WorkflowActionSubsetGetJPAExecutor";
084        }
085    }