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.List;
019    
020    import javax.persistence.EntityManager;
021    import javax.persistence.Query;
022    
023    import org.apache.oozie.ErrorCode;
024    import org.apache.oozie.WorkflowActionBean;
025    import org.apache.oozie.util.ParamChecker;
026    
027    public class WorkflowActionsGetPendingJPAExecutor implements JPAExecutor<List<WorkflowActionBean>>{
028        private long minimumPendingAgeSecs = 0;
029    
030        public WorkflowActionsGetPendingJPAExecutor(final long minimumPendingAgeSecs) {
031            ParamChecker.notNull(minimumPendingAgeSecs, "minimumPendingAgeSecs");
032            this.minimumPendingAgeSecs = minimumPendingAgeSecs;
033        }
034    
035        /* (non-Javadoc)
036         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
037         */
038        @Override
039        public String getName() {
040            return "WorkflowActionsGetPendingJPAExecutor";
041        }
042    
043        /**
044         * Load All the actions that are pending for more than given time.
045         *
046         * @param minimumPendingAgeSecs Minimum Pending age in seconds
047         * @return List of action beans
048         * @throws JPAExecutorException
049         */
050        @Override
051        public List<WorkflowActionBean> execute(EntityManager em) throws JPAExecutorException {
052            Timestamp ts = new Timestamp(System.currentTimeMillis() - minimumPendingAgeSecs * 1000);
053            List<WorkflowActionBean> actionList = null;
054            try {
055                Query q = em.createNamedQuery("GET_PENDING_ACTIONS");
056                q.setParameter("pendingAge", ts);
057                actionList = q.getResultList();
058            }
059            catch (IllegalStateException e) {
060                throw new JPAExecutorException(ErrorCode.E0601, e.getMessage(), e);
061            }
062            return actionList;
063        }
064    }