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 */
018package org.apache.oozie.executor.jpa;
019
020import java.util.ArrayList;
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.client.Job;
029import org.apache.oozie.util.ParamChecker;
030
031/**
032 * Load the list of BundleAction return it.
033 */
034public class BundleActionsGetStatusPendingJPAExecutor implements JPAExecutor<List<BundleActionBean>> {
035
036    private String bundleId = null;
037
038    /**
039     * The constructor for class {@link BundleActionsGetStatusPendingJPAExecutor}
040     *
041     * @param bundleId bundle job id
042     */
043    public BundleActionsGetStatusPendingJPAExecutor(String bundleId) {
044        ParamChecker.notNull(bundleId, "bundleId");
045        this.bundleId = bundleId;
046    }
047
048    /* (non-Javadoc)
049     * @see org.apache.oozie.executor.jpa.JPAExecutor#getName()
050     */
051    @Override
052    public String getName() {
053        return "BundleActionsGetStatusPendingJPAExecutor";
054    }
055
056    /* (non-Javadoc)
057     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
058     */
059    @Override
060    @SuppressWarnings("unchecked")
061    public List<BundleActionBean> execute(EntityManager em) throws JPAExecutorException {
062        List<BundleActionBean> baBeans = new ArrayList<BundleActionBean>();
063        try {
064            Query q = em.createNamedQuery("GET_BUNDLE_ACTION_STATUS_PENDING_FOR_BUNDLE");
065            q.setParameter("bundleId", bundleId);
066            List<Object[]> bundleActionList = q.getResultList();
067            for (Object[] a : bundleActionList) {
068                BundleActionBean bab = createBeanFromBundle(a);
069                baBeans.add(bab);
070            }
071        }
072        catch (Exception e) {
073            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
074        }
075
076        return baBeans;
077    }
078
079    private BundleActionBean createBeanFromBundle(Object[] arr) {
080        BundleActionBean bab = new BundleActionBean();
081        if (arr[0] != null) {
082            bab.setCoordId((String) arr[0]);
083        }
084        if (arr[1] != null) {
085            bab.setStatus(Job.Status.valueOf((String) arr[1]));
086        }
087        if (arr[2] != null) {
088            bab.setPending((Integer) arr[2]);
089        }
090
091        return bab;
092    }
093
094}