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.ArrayList;
018    import java.util.List;
019    
020    import javax.persistence.EntityManager;
021    import javax.persistence.Query;
022    
023    import org.apache.oozie.BundleJobBean;
024    import org.apache.oozie.ErrorCode;
025    
026    /**
027     * Get a list of unpaused bundle Jobs;
028     */
029    public class BundleJobsGetUnpausedJPAExecutor implements JPAExecutor<List<BundleJobBean>> {
030        private int limit;
031    
032        public BundleJobsGetUnpausedJPAExecutor(int limit) {
033            this.limit = limit;
034        }
035    
036        @Override
037        public String getName() {
038            return "BundleJobsGetUnpausedJPAExecutor";
039        }
040    
041        @Override
042        @SuppressWarnings("unchecked")
043        public List<BundleJobBean> execute(EntityManager em) throws JPAExecutorException {
044            List<BundleJobBean> bjBeans;
045            List<BundleJobBean> jobList = new ArrayList<BundleJobBean>();
046            try {
047                Query q = em.createNamedQuery("GET_BUNDLE_JOBS_UNPAUSED");
048                if (limit > 0) {
049                    q.setMaxResults(limit);
050                }
051                bjBeans = q.getResultList();
052                for (BundleJobBean j : bjBeans) {
053                    jobList.add(j);
054                }
055            }
056            catch (Exception e) {
057                throw new JPAExecutorException(ErrorCode.E0603, e);
058            }
059            return jobList;
060        }
061    }