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