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.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     * Load the list of completed BundleJob for purge ready.
028     */
029    public class BundleJobsGetForPurgeJPAExecutor implements JPAExecutor<List<BundleJobBean>> {
030    
031        private static final long DAY_IN_MS = 24 * 60 * 60 * 1000;
032        private long olderThanDays;
033        private int limit;
034    
035        public BundleJobsGetForPurgeJPAExecutor(long olderThanDays, int limit) {
036            this.olderThanDays = olderThanDays;
037            this.limit = limit;
038        }
039    
040        /* (non-Javadoc)
041         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
042         */
043        @Override
044        public String getName() {
045            return "BundleJobsGetForPurgeJPAExecutor";
046        }
047    
048        /* (non-Javadoc)
049         * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
050         */
051        @Override
052        @SuppressWarnings("unchecked")
053        public List<BundleJobBean> execute(EntityManager em) throws JPAExecutorException {
054            List<BundleJobBean> bundleJobs = null;
055            try {
056                Timestamp lastModTm = new Timestamp(System.currentTimeMillis() - (olderThanDays * DAY_IN_MS));
057                Query jobQ = em.createNamedQuery("GET_COMPLETED_BUNDLE_JOBS_OLDER_THAN");
058                jobQ.setParameter("lastModTime", lastModTm);
059                jobQ.setMaxResults(limit);
060                bundleJobs = jobQ.getResultList();
061            }
062            catch (Exception e) {
063                throw new JPAExecutorException(ErrorCode.E0603, e);
064            }
065            return bundleJobs;
066        }
067    
068    }