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.List;
024
025import javax.persistence.EntityManager;
026import javax.persistence.Query;
027
028import org.apache.oozie.ErrorCode;
029import org.apache.oozie.util.ParamChecker;
030
031public class CoordActionsGetReadyGroupbyJobIDJPAExecutor implements JPAExecutor<List<String>>{
032    private long checkAgeSecs = 0;
033
034    public CoordActionsGetReadyGroupbyJobIDJPAExecutor(final long checkAgeSecs) {
035        ParamChecker.notNull(checkAgeSecs, "checkAgeSecs");
036        this.checkAgeSecs = checkAgeSecs;
037    }
038
039    /* (non-Javadoc)
040     * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
041     */
042    @Override
043    public String getName() {
044        return "CoordActionsGetReadyGroupbyJobIDJPAExecutor";
045    }
046
047    /* (non-Javadoc)
048     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
049     */
050    @Override
051    public List<String> execute(EntityManager em) throws JPAExecutorException {
052        List<String> jobids = new ArrayList<String>();
053        try {
054            Query q = em.createNamedQuery("GET_READY_ACTIONS_GROUP_BY_JOBID");
055            Timestamp ts = new Timestamp(System.currentTimeMillis() - checkAgeSecs * 1000);
056            q.setParameter("lastModifiedTime", ts);
057            List<Object[]> list = q.getResultList();
058
059            for (Object[] arr : list) {
060                if (arr != null && arr[0] != null) {
061                    jobids.add((String) arr[0]);
062                }
063            }
064
065            return jobids;
066        }
067        catch (IllegalStateException e) {
068            throw new JPAExecutorException(ErrorCode.E0601, e.getMessage(), e);
069        }
070    }
071
072}