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