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 */
018package org.apache.oozie.executor.jpa;
019
020import java.sql.Timestamp;
021import java.util.List;
022
023import javax.persistence.EntityManager;
024import javax.persistence.Query;
025
026import org.apache.oozie.ErrorCode;
027
028/**
029 * Load the list of completed BundleJob for purge ready.
030 */
031public class BundleJobsGetForPurgeJPAExecutor implements JPAExecutor<List<String>> {
032
033    private static final long DAY_IN_MS = 24 * 60 * 60 * 1000;
034    private long olderThanDays;
035    private int limit;
036    private int offset;
037
038    public BundleJobsGetForPurgeJPAExecutor(long olderThanDays, int limit) {
039        this(olderThanDays, 0, limit);
040    }
041
042    public BundleJobsGetForPurgeJPAExecutor(long olderThanDays, int offset, int limit) {
043        this.olderThanDays = olderThanDays;
044        this.offset = offset;
045        this.limit = limit;
046    }
047
048    /* (non-Javadoc)
049     * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
050     */
051    @Override
052    public String getName() {
053        return "BundleJobsGetForPurgeJPAExecutor";
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<String> execute(EntityManager em) throws JPAExecutorException {
062        List<String> bundleJobs = null;
063        try {
064            Timestamp lastModTm = new Timestamp(System.currentTimeMillis() - (olderThanDays * DAY_IN_MS));
065            Query jobQ = em.createNamedQuery("GET_COMPLETED_BUNDLE_JOBS_OLDER_THAN");
066            jobQ.setParameter("lastModTime", lastModTm);
067            jobQ.setMaxResults(limit);
068            jobQ.setFirstResult(offset);
069            bundleJobs = jobQ.getResultList();
070        }
071        catch (Exception e) {
072            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
073        }
074        return bundleJobs;
075    }
076
077}