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 WorkflowJobsBasicInfoFromWorkflowParentIdJPAExecutor implements JPAExecutor<List<WorkflowJobBean>> {
033
034    private String parentId;
035    private int limit;
036    private int offset;
037
038    public WorkflowJobsBasicInfoFromWorkflowParentIdJPAExecutor(String parentId, int limit) {
039        this(parentId, 0, limit);
040    }
041
042    public WorkflowJobsBasicInfoFromWorkflowParentIdJPAExecutor(String parentId, int offset, int limit) {
043        this.parentId = parentId;
044        this.offset = offset;
045        this.limit = limit;
046    }
047
048    @Override
049    public String getName() {
050        return "WorkflowJobsBasicInfoFromWorkflowParentIdJPAExecutor";
051    }
052
053    @Override
054    @SuppressWarnings("unchecked")
055    public List<WorkflowJobBean> execute(EntityManager em) throws JPAExecutorException {
056        try {
057            Query jobQ = em.createNamedQuery("GET_WORKFLOWS_BASIC_INFO_BY_PARENT_ID");
058            jobQ.setParameter("parentId", parentId);
059            jobQ.setMaxResults(limit);
060            jobQ.setFirstResult(offset);
061            return getBeanFromArray(jobQ.getResultList());
062        }
063        catch (Exception e) {
064            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
065        }
066    }
067
068    private List<WorkflowJobBean> getBeanFromArray(List resultList) {
069        List<WorkflowJobBean> wfActionBeanList = new ArrayList<WorkflowJobBean>();
070        for (Object element : resultList) {
071            WorkflowJobBean wfBean = new WorkflowJobBean();
072            Object[] arr = (Object[])element;
073            if(arr[0] != null) {
074                wfBean.setId((String) arr[0]);
075            }
076            if(arr[1] != null) {
077                wfBean.setStatus(WorkflowJob.Status.valueOf((String) arr[1]));
078            }
079            if(arr[2] != null) {
080                wfBean.setEndTime(DateUtils.toDate((Timestamp) arr[2]));
081            }
082            wfActionBeanList.add(wfBean);
083        }
084
085        return wfActionBeanList;
086    }
087}