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;
030import org.apache.openjpa.persistence.OpenJPAPersistence;
031
032/**
033 * JPA Command to get subset of workflow actions for a particular workflow.
034 */
035public class WorkflowActionSubsetGetJPAExecutor implements JPAExecutor<List<WorkflowActionBean>> {
036
037    private final String wfId;
038    private final int start;
039    private final int length;
040
041    /**
042     * This Constructor creates the WorkflowActionSubsetGetJPAExecutor object Which gets the List of wrokflow action
043     * bean.
044     *
045     * @param wfId
046     * @param start
047     * @param length
048     */
049    public WorkflowActionSubsetGetJPAExecutor(String wfId, int start, int length) {
050        ParamChecker.notNull(wfId, "wfJobId");
051        this.wfId = wfId;
052        this.start = start;
053        this.length = length;
054    }
055
056    /* (non-Javadoc)
057     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
058     */
059    @Override
060    @SuppressWarnings("unchecked")
061    public List<WorkflowActionBean> execute(EntityManager em) throws JPAExecutorException {
062        List<WorkflowActionBean> actions;
063        List<WorkflowActionBean> actionList = new ArrayList<WorkflowActionBean>();
064        try {
065            Query q = em.createNamedQuery("GET_ACTIONS_FOR_WORKFLOW");
066            OpenJPAPersistence.cast(q);
067            q.setParameter("wfId", wfId);
068            q.setFirstResult(start - 1);
069            q.setMaxResults(length);
070            actions = q.getResultList();
071            for (WorkflowActionBean a : actions) {
072                WorkflowActionBean aa = getBeanForRunningAction(a);
073                actionList.add(aa);
074            }
075        }
076        catch (Exception e) {
077            throw new JPAExecutorException(ErrorCode.E0605, "null", e);
078        }
079        return actionList;
080    }
081
082    /* (non-Javadoc)
083     * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
084     */
085    @Override
086    public String getName() {
087        return "WorkflowActionSubsetGetJPAExecutor";
088    }
089
090    private WorkflowActionBean getBeanForRunningAction(WorkflowActionBean a) throws SQLException {
091        if (a != null) {
092            WorkflowActionBean action = new WorkflowActionBean();
093            action.setId(a.getId());
094            action.setConf(a.getConf());
095            action.setConsoleUrl(a.getConsoleUrl());
096            action.setData(a.getData());
097            action.setStats(a.getStats());
098            action.setExternalChildIDs(a.getExternalChildIDs());
099            action.setErrorInfo(a.getErrorCode(), a.getErrorMessage());
100            action.setExternalId(a.getExternalId());
101            action.setExternalStatus(a.getExternalStatus());
102            action.setName(a.getName());
103            action.setCred(a.getCred());
104            action.setRetries(a.getRetries());
105            action.setTrackerUri(a.getTrackerUri());
106            action.setTransition(a.getTransition());
107            action.setType(a.getType());
108            action.setEndTime(a.getEndTime());
109            action.setExecutionPath(a.getExecutionPath());
110            action.setLastCheckTime(a.getLastCheckTime());
111            action.setLogToken(a.getLogToken());
112            if (a.getPending() == true) {
113                action.setPending();
114            }
115            action.setPendingAge(a.getPendingAge());
116            action.setSignalValue(a.getSignalValue());
117            action.setSlaXml(a.getSlaXml());
118            action.setStartTime(a.getStartTime());
119            action.setStatus(a.getStatus());
120            action.setJobId(a.getWfId());
121            return action;
122        }
123        return null;
124    }
125}