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