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.sql.Timestamp; 022import java.util.ArrayList; 023import java.util.List; 024import java.util.Map; 025 026import javax.persistence.EntityManager; 027import javax.persistence.Query; 028 029import org.apache.oozie.BundleJobBean; 030import org.apache.oozie.BundleJobInfo; 031import org.apache.oozie.StringBlob; 032import org.apache.oozie.client.Job; 033import org.apache.oozie.client.BundleJob.Timeunit; 034import org.apache.oozie.store.StoreStatusFilter; 035import org.apache.oozie.util.ParamChecker; 036import org.apache.openjpa.persistence.OpenJPAPersistence; 037import org.apache.openjpa.persistence.OpenJPAQuery; 038import org.apache.openjpa.persistence.jdbc.FetchDirection; 039import org.apache.openjpa.persistence.jdbc.JDBCFetchPlan; 040import org.apache.openjpa.persistence.jdbc.LRSSizeAlgorithm; 041import org.apache.openjpa.persistence.jdbc.ResultSetType; 042 043/** 044 * Load the BundleInfo and return it. 045 */ 046public class BundleJobInfoGetJPAExecutor implements JPAExecutor<BundleJobInfo> { 047 048 public static final String DEFAULT_ORDER_BY = " order by w.createdTimestamp desc "; 049 private Map<String, List<String>> filter; 050 private int start = 1; 051 private int len = 50; 052 053 /** 054 * The constructor for BundleJobInfoGetJPAExecutor 055 * 056 * @param filter the filter string 057 * @param start start location for paging 058 * @param len total length to get 059 */ 060 public BundleJobInfoGetJPAExecutor(Map<String, List<String>> filter, int start, int len) { 061 ParamChecker.notNull(filter, "filter"); 062 this.filter = filter; 063 this.start = start; 064 this.len = len; 065 } 066 067 /* (non-Javadoc) 068 * @see org.apache.oozie.executor.jpa.JPAExecutor#getName() 069 */ 070 @Override 071 public String getName() { 072 return "BundleJobInfoGetJPAExecutor"; 073 } 074 075 /* (non-Javadoc) 076 * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager) 077 */ 078 @Override 079 @SuppressWarnings("unchecked") 080 public BundleJobInfo execute(EntityManager em) throws JPAExecutorException { 081 List<String> orArray = new ArrayList<String>(); 082 List<String> colArray = new ArrayList<String>(); 083 List<Object> valArray = new ArrayList<Object>(); 084 StringBuilder sb = new StringBuilder(""); 085 String orderBy = DEFAULT_ORDER_BY; 086 087 StoreStatusFilter.filter(filter, orArray, colArray, valArray, sb, StoreStatusFilter.bundleSeletStr, 088 StoreStatusFilter.bundleCountStr); 089 orderBy = StoreStatusFilter.getSortBy(filter, orderBy); 090 091 int realLen = 0; 092 093 Query q = null; 094 Query qTotal = null; 095 if (orArray.size() == 0 && orderBy.equals(DEFAULT_ORDER_BY)) { 096 q = em.createNamedQuery("GET_BUNDLE_JOBS_COLUMNS"); 097 q.setFirstResult(start - 1); 098 q.setMaxResults(len); 099 qTotal = em.createNamedQuery("GET_BUNDLE_JOBS_COUNT"); 100 } 101 else { 102 sb = sb.toString().trim().length() == 0 ? sb.append(StoreStatusFilter.bundleSeletStr) : sb; 103 String sbTotal = sb.toString(); 104 sb.append(orderBy); 105 q = em.createQuery(sb.toString()); 106 q.setFirstResult(start - 1); 107 q.setMaxResults(len); 108 qTotal = em.createQuery(sbTotal.replace(StoreStatusFilter.bundleSeletStr, 109 StoreStatusFilter.bundleCountStr)); 110 } 111 112 for (int i = 0; i < orArray.size(); i++) { 113 q.setParameter(colArray.get(i), valArray.get(i)); 114 qTotal.setParameter(colArray.get(i), valArray.get(i)); 115 } 116 117 OpenJPAQuery kq = OpenJPAPersistence.cast(q); 118 JDBCFetchPlan fetch = (JDBCFetchPlan) kq.getFetchPlan(); 119 fetch.setFetchBatchSize(20); 120 fetch.setResultSetType(ResultSetType.SCROLL_INSENSITIVE); 121 fetch.setFetchDirection(FetchDirection.FORWARD); 122 fetch.setLRSSizeAlgorithm(LRSSizeAlgorithm.LAST); 123 List<?> resultList = q.getResultList(); 124 List<Object[]> objectArrList = (List<Object[]>) resultList; 125 List<BundleJobBean> bundleBeansList = new ArrayList<BundleJobBean>(); 126 127 for (Object[] arr : objectArrList) { 128 BundleJobBean bean = getBeanForBundleJobFromArray(arr); 129 bundleBeansList.add(bean); 130 } 131 132 realLen = ((Long) qTotal.getSingleResult()).intValue(); 133 134 return new BundleJobInfo(bundleBeansList, start, len, realLen); 135 } 136 137 private BundleJobBean getBeanForBundleJobFromArray(Object[] arr) { 138 139 BundleJobBean bean = new BundleJobBean(); 140 bean.setId((String) arr[0]); 141 if (arr[1] != null) { 142 bean.setAppName((String) arr[1]); 143 } 144 if (arr[2] != null) { 145 bean.setAppPath((String) arr[2]); 146 } 147 if (arr[3] != null) { 148 bean.setConfBlob((StringBlob) arr[3]); 149 } 150 if (arr[4] != null) { 151 bean.setStatus(Job.Status.valueOf((String) arr[4])); 152 } 153 if (arr[5] != null) { 154 bean.setKickoffTime((Timestamp) arr[5]); 155 } 156 if (arr[6] != null) { 157 bean.setStartTime((Timestamp) arr[6]); 158 } 159 if (arr[7] != null) { 160 bean.setEndTime((Timestamp) arr[7]); 161 } 162 if (arr[8] != null) { 163 bean.setPauseTime((Timestamp) arr[8]); 164 } 165 if (arr[9] != null) { 166 bean.setCreatedTime((Timestamp) arr[9]); 167 } 168 if (arr[10] != null) { 169 bean.setUser((String) arr[10]); 170 } 171 if (arr[11] != null) { 172 bean.setGroup((String) arr[11]); 173 } 174 if (arr[12] != null) { 175 bean.setTimeUnit(Timeunit.valueOf((String) arr[12])); 176 } 177 if (arr[13] != null) { 178 bean.setTimeOut((Integer) arr[13]); 179 } 180 181 return bean; 182 } 183}