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 CoordinatorJob for purge ready.
030 */
031public class CoordJobsGetForPurgeJPAExecutor 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 CoordJobsGetForPurgeJPAExecutor(long olderThanDays, int limit) {
039        this(olderThanDays, 0, limit);
040    }
041
042    public CoordJobsGetForPurgeJPAExecutor(long olderThanDays, int offset, int limit) {
043        this.olderThanDays = olderThanDays;
044        this.offset = offset;
045        this.limit = limit;
046    }
047
048    @Override
049    public String getName() {
050        return "CoordJobsGetForPurgeJPAExecutor";
051    }
052
053    @Override
054    @SuppressWarnings("unchecked")
055    public List<String> execute(EntityManager em) throws JPAExecutorException {
056        List<String> coordJobs = null;
057        try {
058            Timestamp lastModTm = new Timestamp(System.currentTimeMillis() - (olderThanDays * DAY_IN_MS));
059            Query jobQ = em.createNamedQuery("GET_COMPLETED_COORD_JOBS_WITH_NO_PARENT_OLDER_THAN_STATUS");
060            jobQ.setParameter("lastModTime", lastModTm);
061            jobQ.setMaxResults(limit);
062            jobQ.setFirstResult(offset);
063            coordJobs = jobQ.getResultList();
064        }
065        catch (Exception e) {
066            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
067        }
068        return coordJobs;
069    }
070
071}