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.CoordinatorJobBean;
024    import org.apache.oozie.ErrorCode;
025    
026    /**
027     * Load the list of completed CoordinatorJob for purge ready.
028     */
029    public class CoordJobsGetForPurgeJPAExecutor implements JPAExecutor<List<CoordinatorJobBean>> {
030    
031        private static final long DAY_IN_MS = 24 * 60 * 60 * 1000;
032        private long olderThanDays;
033        private int limit;
034    
035        public CoordJobsGetForPurgeJPAExecutor(long olderThanDays, int limit) {
036            this.olderThanDays = olderThanDays;
037            this.limit = limit;
038        }
039    
040        @Override
041        public String getName() {
042            return "CoordJobsGetForPurgeJPAExecutor";
043        }
044    
045        @Override
046        @SuppressWarnings("unchecked")
047        public List<CoordinatorJobBean> execute(EntityManager em) throws JPAExecutorException {
048            List<CoordinatorJobBean> coordJobs = null;
049            try {
050                Timestamp lastModTm = new Timestamp(System.currentTimeMillis() - (olderThanDays * DAY_IN_MS));
051                Query jobQ = em.createNamedQuery("GET_COMPLETED_COORD_JOBS_OLDER_THAN_STATUS");
052                jobQ.setParameter("lastModTime", lastModTm);
053                jobQ.setMaxResults(limit);
054                coordJobs = jobQ.getResultList();
055            }
056            catch (Exception e) {
057                throw new JPAExecutorException(ErrorCode.E0603, e);
058            }
059            return coordJobs;
060        }
061    
062    }