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.ArrayList;
023import java.util.Date;
024import java.util.List;
025
026import javax.persistence.EntityManager;
027import javax.persistence.Query;
028
029import org.apache.oozie.CoordinatorJobBean;
030import org.apache.oozie.ErrorCode;
031import org.apache.oozie.util.ParamChecker;
032
033/**
034 * JPA command to get coordinator jobs which are qualify for Materialization.
035 */
036public class CoordJobsToBeMaterializedJPAExecutor implements JPAExecutor<List<CoordinatorJobBean>> {
037
038    private Date dateInput;
039    private int limit;
040
041    /**
042     * @param date
043     * @param limit
044     */
045    public CoordJobsToBeMaterializedJPAExecutor(Date date, int limit) {
046        ParamChecker.notNull(date, "Coord Job Materialization Date");
047        this.dateInput = date;
048        this.limit = limit;
049    }
050
051    /* (non-Javadoc)
052     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
053     */
054    @SuppressWarnings("unchecked")
055    @Override
056    public List<CoordinatorJobBean> execute(EntityManager em) throws JPAExecutorException {
057        List<CoordinatorJobBean> cjBeans;
058        try {
059            Query q = em.createNamedQuery("GET_COORD_JOBS_OLDER_FOR_MATERIALIZATION");
060            q.setParameter("matTime", new Timestamp(this.dateInput.getTime()));
061            if (limit > 0) {
062                q.setMaxResults(limit);
063            }
064
065           cjBeans = q.getResultList();
066        }
067        catch (IllegalStateException e) {
068            throw new JPAExecutorException(ErrorCode.E0601, e.getMessage(), e);
069        }
070        return cjBeans;
071    }
072
073    @Override
074    public String getName() {
075        return "CoordJobsToBeMaterializedJPAExecutor";
076    }
077
078    /**
079     * @return the dateInput
080     */
081    public Date getDateInput() {
082        return dateInput;
083    }
084
085    /**
086     * @param dateInput the dateInput to set
087     */
088    public void setDateInput(Date dateInput) {
089        this.dateInput = dateInput;
090    }
091
092    /**
093     * @return the limit
094     */
095    public int getLimit() {
096        return limit;
097    }
098
099    /**
100     * @param limit the limit to set
101     */
102    public void setLimit(int limit) {
103        this.limit = limit;
104    }
105}