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 javax.persistence.EntityManager;
022import javax.persistence.Query;
023
024import org.apache.oozie.coord.CoordUtils;
025import org.apache.oozie.CoordinatorEngine.FILTER_COMPARATORS;
026import org.apache.oozie.ErrorCode;
027import org.apache.oozie.util.Pair;
028import org.apache.oozie.util.ParamChecker;
029
030import java.util.List;
031import java.util.Map;
032
033/**
034 * Load the number of running actions for a coordinator job.
035 */
036public class CoordActionsCountForJobIdJPAExecutor implements JPAExecutor<Integer> {
037
038    private String coordJobId = null;
039    private Map<Pair<String, FILTER_COMPARATORS>, List<Object>> filterMap;
040
041    public CoordActionsCountForJobIdJPAExecutor(String coordJobId, Map<Pair<String, FILTER_COMPARATORS>, List<Object>> filterMap) {
042        ParamChecker.notNull(coordJobId, "coordJobId");
043        this.coordJobId = coordJobId;
044        this.filterMap = filterMap;
045    }
046
047    @Override
048    public String getName() {
049        return "CoordActionsCountForJobIdJPAExecutor";
050    }
051
052    @Override
053    public Integer execute(EntityManager em) throws JPAExecutorException {
054        try {
055            Query q = em.createNamedQuery("GET_COORD_ACTIONS_COUNT_BY_JOBID");
056            q = setQueryParameters(q, em);
057            Long count = (Long) q.getSingleResult();
058            return Integer.valueOf(count.intValue());
059        }
060        catch (Exception e) {
061            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
062        }
063    }
064
065    private Query setQueryParameters(Query q, EntityManager em){
066        Map<String, Object> params = null;
067        if (filterMap != null) {
068            // Add the filter clause
069            String query = q.toString();
070            StringBuilder sbTotal = new StringBuilder(query);
071            // Get the 'where' clause for status filters
072            StringBuilder statusClause = new StringBuilder();
073            params = CoordUtils.getWhereClause(statusClause, filterMap);
074            sbTotal.insert(sbTotal.length(), statusClause);
075            q = em.createQuery(sbTotal.toString());
076        }
077        if (params != null) {
078            for (String pname : params.keySet()) {
079                q.setParameter(pname, params.get(pname));
080            }
081        }
082        q.setParameter("jobId", coordJobId);
083        return q;
084    }
085
086}