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.WorkflowActionBean;
026    
027    /**
028     * JPA Executor to get running workflow actions
029     */
030    public class WorkflowActionsRunningGetJPAExecutor implements JPAExecutor<List<WorkflowActionBean>> {
031    
032        private final long checkAgeSecs;
033    
034        public WorkflowActionsRunningGetJPAExecutor(long checkAgeSecs) {
035            this.checkAgeSecs = checkAgeSecs;
036        }
037    
038        /* (non-Javadoc)
039         * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
040         */
041        @Override
042        @SuppressWarnings("unchecked")
043        public List<WorkflowActionBean> execute(EntityManager em) throws JPAExecutorException {
044            List<WorkflowActionBean> actions;
045            List<WorkflowActionBean> actionList = new ArrayList<WorkflowActionBean>();
046            try {
047                Timestamp ts = new Timestamp(System.currentTimeMillis() - checkAgeSecs * 1000);
048                Query q = em.createNamedQuery("GET_RUNNING_ACTIONS");
049                q.setParameter("lastCheckTime", ts);
050                actions = q.getResultList();
051                for (WorkflowActionBean a : actions) {
052                    WorkflowActionBean aa = getBeanForRunningAction(a);
053                    actionList.add(aa);
054                }
055            }
056            catch (Exception e) {
057                throw new JPAExecutorException(ErrorCode.E0605, e);
058            }
059            return actionList;
060        }
061    
062        /* (non-Javadoc)
063         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
064         */
065        @Override
066        public String getName() {
067            return "WorkflowActionsRunningGetJPAExecutor";
068        }
069    
070        /**
071         * Re-create workflow action bean
072         *
073         * @param bean
074         * @return workflow action bean
075         */
076        private WorkflowActionBean getBeanForRunningAction(WorkflowActionBean bean){
077            if (bean != null) {
078                WorkflowActionBean action = new WorkflowActionBean();
079                action.setId(bean.getId());
080                action.setConf(bean.getConf());
081                action.setConsoleUrl(bean.getConsoleUrl());
082                action.setData(bean.getData());
083                action.setErrorInfo(bean.getErrorCode(), bean.getErrorMessage());
084                action.setExternalId(bean.getExternalId());
085                action.setExternalStatus(bean.getExternalStatus());
086                action.setName(bean.getName());
087                action.setCred(bean.getCred());
088                action.setRetries(bean.getRetries());
089                action.setTrackerUri(bean.getTrackerUri());
090                action.setTransition(bean.getTransition());
091                action.setType(bean.getType());
092                action.setEndTime(bean.getEndTime());
093                action.setExecutionPath(bean.getExecutionPath());
094                action.setLastCheckTime(bean.getLastCheckTime());
095                action.setLogToken(bean.getLogToken());
096                if (bean.getPending() == true) {
097                    action.setPending();
098                }
099                action.setPendingAge(bean.getPendingAge());
100                action.setSignalValue(bean.getSignalValue());
101                action.setSlaXml(bean.getSlaXml());
102                action.setStartTime(bean.getStartTime());
103                action.setStatus(bean.getStatus());
104                action.setJobId(bean.getWfId());
105                return action;
106            }
107            return null;
108        }
109    }