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.BundleActionBean;
023    import org.apache.oozie.ErrorCode;
024    import org.apache.oozie.util.ParamChecker;
025    
026    /**
027     * Load the BundleAction into a Bean and return it.
028     */
029    public class BundleActionGetJPAExecutor implements JPAExecutor<BundleActionBean> {
030    
031        private String bundleActionId = null;
032    
033        /**
034         * The constructor for class {@link BundleActionGetJPAExecutor}
035         *
036         * @param bundleId bundle job id
037         * @param coordName coordinator name
038         */
039        public BundleActionGetJPAExecutor(String bundleId, String coordName) {
040            ParamChecker.notNull(bundleId, "bundleId");
041            ParamChecker.notNull(coordName, "coordName");
042            this.bundleActionId = bundleId + "_" + coordName;
043        }
044    
045        /* (non-Javadoc)
046         * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
047         */
048        @Override
049        public String getName() {
050            return "BundleActionGetJPAExecutor";
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 BundleActionBean execute(EntityManager em) throws JPAExecutorException {
059            List<BundleActionBean> baBeans;
060            try {
061                Query q = em.createNamedQuery("GET_BUNDLE_ACTION");
062                q.setParameter("bundleActionId", bundleActionId);
063                baBeans = q.getResultList();
064            }
065            catch (Exception e) {
066                throw new JPAExecutorException(ErrorCode.E0603, e);
067            }
068    
069            BundleActionBean bean = null;
070            if (baBeans != null && baBeans.size() > 0) {
071                bean = baBeans.get(0);
072                return bean;
073            }
074            else {
075                throw new JPAExecutorException(ErrorCode.E0605, bundleActionId);
076            }
077        }
078    }