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