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 WorkflowJob for purge ready.
031 */
032public class WorkflowJobsGetForPurgeJPAExecutor 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 WorkflowJobsGetForPurgeJPAExecutor(long olderThanDays, int limit) {
040        this(olderThanDays, 0, limit);
041    }
042
043    public WorkflowJobsGetForPurgeJPAExecutor(long olderThanDays, int offset, int limit) {
044        this.olderThanDays = olderThanDays;
045        this.offset = offset;
046        this.limit = limit;
047    }
048
049    /* (non-Javadoc)
050     * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
051     */
052    @Override
053    public String getName() {
054        return "WorkflowJobsGetForPurgeJPAExecutor";
055    }
056
057    /* (non-Javadoc)
058     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
059     */
060    @Override
061    @SuppressWarnings("unchecked")
062    public List<String> execute(EntityManager em) throws JPAExecutorException {
063        List<String> workflows = null;
064        try {
065            Timestamp maxEndTime = new Timestamp(System.currentTimeMillis() - (olderThanDays * DAY_IN_MS));
066            Query jobQ = em.createNamedQuery("GET_COMPLETED_WORKFLOWS_WITH_NO_PARENT_OLDER_THAN");
067            jobQ.setParameter("endTime", maxEndTime);
068            jobQ.setMaxResults(limit);
069            jobQ.setFirstResult(offset);
070            workflows = jobQ.getResultList();
071        }
072        catch (Exception e) {
073            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
074        }
075        return workflows;
076    }
077
078}