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