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.util.List;
018    
019    import javax.persistence.EntityManager;
020    import javax.persistence.Query;
021    
022    import org.apache.oozie.CoordinatorActionBean;
023    import org.apache.oozie.ErrorCode;
024    import org.apache.oozie.util.ParamChecker;
025    
026    /**
027     * Load the CoordinatorJob into a Bean and return it.
028     */
029    public class CoordJobGetActionByActionNumberJPAExecutor implements JPAExecutor<CoordinatorActionBean> {
030    
031        private String coordJobId = null;
032        private final int actionNumber;
033    
034        public CoordJobGetActionByActionNumberJPAExecutor(String coordJobId, int actionNumber) {
035            ParamChecker.notNull(coordJobId, "coordJobId");
036            this.coordJobId = coordJobId;
037            this.actionNumber = actionNumber;
038        }
039    
040        /* (non-Javadoc)
041         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
042         */
043        @Override
044        public String getName() {
045            return "CoordJobGetActionByActionNumberJPAExecutor";
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 CoordinatorActionBean execute(EntityManager em) throws JPAExecutorException {
054            try {
055                CoordinatorActionBean caBean = null;
056                Query q = em.createNamedQuery("GET_COORD_ACTION_FOR_COORD_JOB_BY_ACTION_NUMBER");
057                q.setParameter("jobId", coordJobId);
058                q.setParameter("actionNumber", actionNumber);
059    
060                List<CoordinatorActionBean> actionList = q.getResultList();
061                if (actionList.size() > 0) {
062                    caBean = actionList.get(0);
063                }
064                return caBean;
065            }
066            catch (Exception e) {
067                throw new JPAExecutorException(ErrorCode.E0603, e);
068            }
069        }
070    }