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