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.SQLException;
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    import org.apache.oozie.util.ParamChecker;
030    
031    /**
032     * Load workflow actions for a workflow job.
033     */
034    public class WorkflowJobGetActionsJPAExecutor implements JPAExecutor<List<WorkflowActionBean>> {
035    
036        private String wfJobId = null;
037    
038        public WorkflowJobGetActionsJPAExecutor(String wfJobId) {
039            ParamChecker.notNull(wfJobId, "wfJobId");
040            this.wfJobId = wfJobId;
041        }
042    
043        @Override
044        public String getName() {
045            return "WorkflowJobGetActionsJPAExecutor";
046        }
047    
048        @Override
049        @SuppressWarnings("unchecked")
050        public List<WorkflowActionBean> execute(EntityManager em) throws JPAExecutorException {
051            List<WorkflowActionBean> actions;
052            List<WorkflowActionBean> actionList = new ArrayList<WorkflowActionBean>();
053            try {
054                Query q = em.createNamedQuery("GET_ACTIONS_FOR_WORKFLOW");
055                q.setParameter("wfId", wfJobId);
056                actions = q.getResultList();
057                for (WorkflowActionBean a : actions) {
058                    WorkflowActionBean aa = getBeanForRunningAction(a);
059                    actionList.add(aa);
060                }
061            }
062            catch (SQLException e) {
063                throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
064            }
065            return actionList;
066        }
067    
068        private WorkflowActionBean getBeanForRunningAction(WorkflowActionBean a) throws SQLException {
069            if (a != null) {
070                WorkflowActionBean action = new WorkflowActionBean();
071                action.setId(a.getId());
072                action.setConf(a.getConf());
073                action.setConsoleUrl(a.getConsoleUrl());
074                action.setData(a.getData());
075                action.setStats(a.getStats());
076                action.setExternalChildIDs(a.getExternalChildIDs());
077                action.setErrorInfo(a.getErrorCode(), a.getErrorMessage());
078                action.setExternalId(a.getExternalId());
079                action.setExternalStatus(a.getExternalStatus());
080                action.setName(a.getName());
081                action.setCred(a.getCred());
082                action.setRetries(a.getRetries());
083                action.setTrackerUri(a.getTrackerUri());
084                action.setTransition(a.getTransition());
085                action.setType(a.getType());
086                action.setEndTime(a.getEndTime());
087                action.setExecutionPath(a.getExecutionPath());
088                action.setLastCheckTime(a.getLastCheckTime());
089                action.setLogToken(a.getLogToken());
090                if (a.getPending() == true) {
091                    action.setPending();
092                }
093                action.setPendingAge(a.getPendingAge());
094                action.setSignalValue(a.getSignalValue());
095                action.setSlaXml(a.getSlaXml());
096                action.setStartTime(a.getStartTime());
097                action.setStatus(a.getStatus());
098                action.setJobId(a.getWfId());
099                action.setUserRetryCount(a.getUserRetryCount());
100                action.setUserRetryInterval(a.getUserRetryInterval());
101                action.setUserRetryMax(a.getUserRetryMax());
102                return action;
103            }
104            return null;
105        }
106    
107    }