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;
022
023import javax.persistence.EntityManager;
024import javax.persistence.Query;
025
026import org.apache.oozie.ErrorCode;
027
028/**
029 * Count the number of Coordinator children of a parent bundle that are not ready to be purged
030 */
031public class CoordJobsCountNotForPurgeFromParentIdJPAExecutor implements JPAExecutor<Long> {
032
033    private static final long DAY_IN_MS = 24 * 60 * 60 * 1000;
034    private long olderThanDays;
035    private String parentId;
036
037    public CoordJobsCountNotForPurgeFromParentIdJPAExecutor(long olderThanDays, String parentId) {
038        this.olderThanDays = olderThanDays;
039        this.parentId = parentId;
040    }
041
042    /* (non-Javadoc)
043     * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
044     */
045    @Override
046    public String getName() {
047        return "CoordJobsCountNotForPurgeFromParentIdJPAExecutor";
048    }
049
050    /* (non-Javadoc)
051     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
052     */
053    @Override
054    @SuppressWarnings("unchecked")
055    public Long execute(EntityManager em) throws JPAExecutorException {
056        Long count = 0L;
057        try {
058            Timestamp lastModTm = new Timestamp(System.currentTimeMillis() - (olderThanDays * DAY_IN_MS));
059            Query jobQ = em.createNamedQuery("GET_COORD_COUNT_WITH_PARENT_ID_NOT_READY_FOR_PURGE");
060            jobQ.setParameter("parentId", parentId);
061            jobQ.setParameter("lastModTime", lastModTm);
062            count = (Long) jobQ.getSingleResult();
063        }
064        catch (Exception e) {
065            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
066        }
067        return count;
068    }
069
070}