001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.executor.jpa;
016    
017    import java.sql.Timestamp;
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import javax.persistence.EntityManager;
022    import javax.persistence.Query;
023    
024    import org.apache.oozie.ErrorCode;
025    import org.apache.oozie.util.ParamChecker;
026    
027    public class CoordActionsGetReadyGroupbyJobIDJPAExecutor implements JPAExecutor<List<String>>{
028        private long checkAgeSecs = 0;
029    
030        public CoordActionsGetReadyGroupbyJobIDJPAExecutor(final long checkAgeSecs) {
031            ParamChecker.notNull(checkAgeSecs, "checkAgeSecs");
032            this.checkAgeSecs = checkAgeSecs;
033        }
034    
035        /* (non-Javadoc)
036         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
037         */
038        @Override
039        public String getName() {
040            return "CoordActionsGetReadyGroupbyJobIDJPAExecutor";
041        }
042    
043        /* (non-Javadoc)
044         * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
045         */
046        @Override
047        public List<String> execute(EntityManager em) throws JPAExecutorException {
048            List<String> jobids = new ArrayList<String>();
049            try {
050                Query q = em.createNamedQuery("GET_READY_ACTIONS_GROUP_BY_JOBID");
051                Timestamp ts = new Timestamp(System.currentTimeMillis() - checkAgeSecs * 1000);
052                q.setParameter(1, ts);
053                List<Object[]> list = q.getResultList();
054    
055                for (Object[] arr : list) {
056                    if (arr != null && arr[0] != null) {
057                        jobids.add((String) arr[0]);
058                    }
059                }
060    
061                return jobids;
062            }
063            catch (IllegalStateException e) {
064                throw new JPAExecutorException(ErrorCode.E0601, e.getMessage(), e);
065            }
066        }
067    
068    }