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