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.sql.Timestamp;
018    import java.util.ArrayList;
019    import java.util.Date;
020    import java.util.List;
021    
022    import javax.persistence.EntityManager;
023    import javax.persistence.Query;
024    
025    import org.apache.oozie.BundleJobBean;
026    import org.apache.oozie.ErrorCode;
027    
028    /**
029     * Get a list of bundle Jobs that need to be started;
030     */
031    public class BundleJobsGetNeedStartJPAExecutor implements JPAExecutor<List<BundleJobBean>> {
032        private Date date;
033    
034        public BundleJobsGetNeedStartJPAExecutor(Date d) {
035            this.date = d;
036        }
037    
038        @Override
039        public String getName() {
040            return "BundleJobsGetNeedStartJPAExecutor";
041        }
042    
043        @Override
044        @SuppressWarnings("unchecked")
045        public List<BundleJobBean> execute(EntityManager em) throws JPAExecutorException {
046            List<BundleJobBean> bjBeans;
047            List<BundleJobBean> jobList = new ArrayList<BundleJobBean>();
048            try {
049                Query q = em.createNamedQuery("GET_BUNDLE_JOBS_NEED_START");
050                q.setParameter("currentTime", new Timestamp(date.getTime()));
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    }