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.util.List;
021    import javax.persistence.EntityManager;
022    import javax.persistence.Query;
023    import org.apache.oozie.ErrorCode;
024    import org.apache.oozie.SLAEventBean;
025    
026    /**
027     * Load the list of SLAEventBean for a seqId and return the list.
028     */
029    public class SLAEventsGetForSeqIdJPAExecutor implements JPAExecutor<List<SLAEventBean>> {
030    
031        private long seqId = 0;
032        private int limitLen = 100; // Default
033        private long[] lastSeqId;
034    
035        public SLAEventsGetForSeqIdJPAExecutor(long seqId, int limitLen, long[] lastSeqId) {
036            this.seqId = seqId;
037            this.limitLen = limitLen;
038            this.lastSeqId = lastSeqId;
039            this.lastSeqId[0] = seqId;
040        }
041    
042        @Override
043        public String getName() {
044            return "SLAEventsGetForSeqIdJPAExecutor";
045        }
046    
047        @Override
048        @SuppressWarnings("unchecked")
049        public List<SLAEventBean> execute(EntityManager em) throws JPAExecutorException {
050            List<SLAEventBean> seBeans;
051            try {
052                Query q = em.createNamedQuery("GET_SLA_EVENT_NEWER_SEQ_LIMITED");
053                q.setParameter("id", seqId);
054                q.setMaxResults(limitLen);
055                seBeans = q.getResultList();
056                for (SLAEventBean j : seBeans) {
057                    lastSeqId[0] = Math.max(lastSeqId[0], j.getEvent_id());
058                }
059            }
060            catch (Exception e) {
061                throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
062            }
063            return seBeans;
064        }
065    }