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