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