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.CoordinatorActionBean;
025    import org.apache.oozie.ErrorCode;
026    
027    /**
028     * Load the list of running CoordinatorAction and return the list.
029     */
030    public class CoordActionsRunningGetJPAExecutor implements JPAExecutor<List<CoordinatorActionBean>> {
031    
032        private final long checkAgeSecs;
033    
034        public CoordActionsRunningGetJPAExecutor(long checkAgeSecs) {
035            this.checkAgeSecs = checkAgeSecs;
036        }
037    
038        /* (non-Javadoc)
039         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
040         */
041        @Override
042        public String getName() {
043            return "CoordActionsRunningGetJPAExecutor";
044        }
045    
046        /* (non-Javadoc)
047         * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
048         */
049        @Override
050        @SuppressWarnings("unchecked")
051        public List<CoordinatorActionBean> execute(EntityManager em) throws JPAExecutorException {
052            List<CoordinatorActionBean> actions;
053            List<CoordinatorActionBean> actionList = new ArrayList<CoordinatorActionBean>();
054            try {
055                Timestamp ts = new Timestamp(System.currentTimeMillis() - checkAgeSecs * 1000);
056                Query q = em.createNamedQuery("GET_RUNNING_ACTIONS_OLDER_THAN");
057                q.setParameter("lastModifiedTime", ts);
058                actions = q.getResultList();
059                for (CoordinatorActionBean a : actions) {
060                    CoordinatorActionBean aa = getBeanForRunningCoordAction(a);
061                    actionList.add(aa);
062                }
063            }
064            catch (Exception e) {
065                throw new JPAExecutorException(ErrorCode.E0603, e);
066            }
067            return actionList;
068        }
069    
070        private CoordinatorActionBean getBeanForRunningCoordAction(CoordinatorActionBean a) {
071            if (a != null) {
072                CoordinatorActionBean action = new CoordinatorActionBean();
073                action.setId(a.getId());
074                action.setActionNumber(a.getActionNumber());
075                action.setActionXml(a.getActionXml());
076                action.setConsoleUrl(a.getConsoleUrl());
077                action.setCreatedConf(a.getCreatedConf());
078                // action.setErrorCode(a.getErrorCode());
079                // action.setErrorMessage(a.getErrorMessage());
080                action.setExternalStatus(a.getExternalStatus());
081                action.setMissingDependencies(a.getMissingDependencies());
082                action.setRunConf(a.getRunConf());
083                action.setTimeOut(a.getTimeOut());
084                action.setTrackerUri(a.getTrackerUri());
085                action.setType(a.getType());
086                action.setCreatedTime(a.getCreatedTime());
087                action.setExternalId(a.getExternalId());
088                action.setJobId(a.getJobId());
089                action.setLastModifiedTime(a.getLastModifiedTime());
090                action.setNominalTime(a.getNominalTime());
091                action.setSlaXml(a.getSlaXml());
092                action.setStatus(a.getStatus());
093                return action;
094            }
095            return null;
096        }
097    }