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