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 org.apache.oozie.ErrorCode;
022import org.apache.oozie.WorkflowJobBean;
023import org.apache.oozie.client.WorkflowJob;
024import org.apache.oozie.util.DateUtils;
025
026import javax.persistence.EntityManager;
027import javax.persistence.Query;
028import java.sql.Timestamp;
029import java.util.ArrayList;
030import java.util.List;
031
032public class WorkflowJobsBasicInfoFromCoordParentIdJPAExecutor  implements JPAExecutor<List<WorkflowJobBean>> {
033    private String parentId;
034    private int limit;
035    private int offset;
036
037    public WorkflowJobsBasicInfoFromCoordParentIdJPAExecutor(String parentId, int limit) {
038        this(parentId, 0, limit);
039    }
040
041    public WorkflowJobsBasicInfoFromCoordParentIdJPAExecutor(String parentId, int offset, int limit) {
042        this.parentId = parentId;
043        this.offset = offset;
044        this.limit = limit;
045    }
046
047    @Override
048    public String getName() {
049        return "WorkflowJobsBasicInfoFromCoordParentIdJPAExecutor";
050    }
051
052    @Override
053    @SuppressWarnings("unchecked")
054    public List<WorkflowJobBean> execute(EntityManager em) throws JPAExecutorException {
055        try {
056            Query jobQ = em.createNamedQuery("GET_WORKFLOWS_BASIC_INFO_BY_COORD_PARENT_ID");
057            jobQ.setParameter("parentId", parentId + "%");  // The '%' is the wildcard
058            jobQ.setMaxResults(limit);
059            jobQ.setFirstResult(offset);
060            return getBeanFromArray(jobQ.getResultList());
061        }
062        catch (Exception e) {
063            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
064        }
065    }
066
067    private List<WorkflowJobBean> getBeanFromArray(List resultList) {
068        List<WorkflowJobBean> wfActionBeanList = new ArrayList<WorkflowJobBean>();
069        for (Object element : resultList) {
070            WorkflowJobBean wfBean = new WorkflowJobBean();
071            Object[] arr = (Object[])element;
072            if(arr[0] != null) {
073                wfBean.setId((String) arr[0]);
074            }
075            if(arr[1] != null) {
076                wfBean.setStatus(WorkflowJob.Status.valueOf((String) arr[1]));
077            }
078            if(arr[2] != null) {
079                wfBean.setEndTime(DateUtils.toDate((Timestamp) arr[2]));
080            }
081            wfActionBeanList.add(wfBean);
082        }
083        return wfActionBeanList;
084    }
085}