001 /** 002 * Copyright (c) 2010 Yahoo! Inc. All rights reserved. 003 * Licensed under the Apache License, Version 2.0 (the "License"); 004 * you may not use this file except in compliance with the License. 005 * You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software 010 * distributed under the License is distributed on an "AS IS" BASIS, 011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 * See the License for the specific language governing permissions and 013 * limitations under the License. See accompanying LICENSE file. 014 */ 015 package org.apache.oozie.executor.jpa; 016 017 import java.sql.Timestamp; 018 import java.util.ArrayList; 019 import java.util.List; 020 import java.util.Map; 021 022 import javax.persistence.EntityManager; 023 import javax.persistence.Query; 024 025 import org.apache.oozie.CoordinatorJobBean; 026 import org.apache.oozie.CoordinatorJobInfo; 027 import org.apache.oozie.client.Job.Status; 028 import org.apache.oozie.client.CoordinatorJob.Timeunit; 029 import org.apache.oozie.store.StoreStatusFilter; 030 import org.apache.oozie.util.ParamChecker; 031 import org.apache.openjpa.persistence.OpenJPAPersistence; 032 import org.apache.openjpa.persistence.OpenJPAQuery; 033 import org.apache.openjpa.persistence.jdbc.FetchDirection; 034 import org.apache.openjpa.persistence.jdbc.JDBCFetchPlan; 035 import org.apache.openjpa.persistence.jdbc.LRSSizeAlgorithm; 036 import org.apache.openjpa.persistence.jdbc.ResultSetType; 037 038 /** 039 * Load the CoordinatorInfo and return it. 040 */ 041 public class CoordJobInfoGetJPAExecutor implements JPAExecutor<CoordinatorJobInfo> { 042 043 private Map<String, List<String>> filter; 044 private int start = 1; 045 private int len = 50; 046 047 public CoordJobInfoGetJPAExecutor(Map<String, List<String>> filter, int start, int len) { 048 ParamChecker.notNull(filter, "filter"); 049 this.filter = filter; 050 this.start = start; 051 this.len = len; 052 } 053 054 @Override 055 public String getName() { 056 return "CoordInfoGetJPAExecutor"; 057 } 058 059 @Override 060 @SuppressWarnings("unchecked") 061 public CoordinatorJobInfo execute(EntityManager em) throws JPAExecutorException { 062 List<String> orArray = new ArrayList<String>(); 063 List<String> colArray = new ArrayList<String>(); 064 List<String> valArray = new ArrayList<String>(); 065 StringBuilder sb = new StringBuilder(""); 066 067 StoreStatusFilter.filter(filter, orArray, colArray, valArray, sb, StoreStatusFilter.coordSeletStr, 068 StoreStatusFilter.coordCountStr); 069 070 int realLen = 0; 071 072 Query q = null; 073 Query qTotal = null; 074 if (orArray.size() == 0) { 075 q = em.createNamedQuery("GET_COORD_JOBS_COLUMNS"); 076 q.setFirstResult(start - 1); 077 q.setMaxResults(len); 078 qTotal = em.createNamedQuery("GET_COORD_JOBS_COUNT"); 079 } 080 else { 081 StringBuilder sbTotal = new StringBuilder(sb); 082 sb.append(" order by w.createdTimestamp desc "); 083 q = em.createQuery(sb.toString()); 084 q.setFirstResult(start - 1); 085 q.setMaxResults(len); 086 qTotal = em.createQuery(sbTotal.toString().replace(StoreStatusFilter.coordSeletStr, 087 StoreStatusFilter.coordCountStr)); 088 } 089 090 for (int i = 0; i < orArray.size(); i++) { 091 q.setParameter(colArray.get(i), valArray.get(i)); 092 qTotal.setParameter(colArray.get(i), valArray.get(i)); 093 } 094 095 OpenJPAQuery kq = OpenJPAPersistence.cast(q); 096 JDBCFetchPlan fetch = (JDBCFetchPlan) kq.getFetchPlan(); 097 fetch.setFetchBatchSize(20); 098 fetch.setResultSetType(ResultSetType.SCROLL_INSENSITIVE); 099 fetch.setFetchDirection(FetchDirection.FORWARD); 100 fetch.setLRSSizeAlgorithm(LRSSizeAlgorithm.LAST); 101 List<?> resultList = q.getResultList(); 102 List<Object[]> objectArrList = (List<Object[]>) resultList; 103 List<CoordinatorJobBean> coordBeansList = new ArrayList<CoordinatorJobBean>(); 104 105 for (Object[] arr : objectArrList) { 106 CoordinatorJobBean ww = getBeanForCoordinatorJobFromArray(arr); 107 coordBeansList.add(ww); 108 } 109 110 realLen = ((Long) qTotal.getSingleResult()).intValue(); 111 112 return new CoordinatorJobInfo(coordBeansList, start, len, realLen); 113 } 114 115 private CoordinatorJobBean getBeanForCoordinatorJobFromArray(Object[] arr) { 116 CoordinatorJobBean bean = new CoordinatorJobBean(); 117 bean.setId((String) arr[0]); 118 if (arr[1] != null) { 119 bean.setAppName((String) arr[1]); 120 } 121 if (arr[2] != null) { 122 bean.setStatus(Status.valueOf((String) arr[2])); 123 } 124 if (arr[3] != null) { 125 bean.setUser((String) arr[3]); 126 } 127 if (arr[4] != null) { 128 bean.setGroup((String) arr[4]); 129 } 130 if (arr[5] != null) { 131 bean.setStartTime((Timestamp) arr[5]); 132 } 133 if (arr[6] != null) { 134 bean.setEndTime((Timestamp) arr[6]); 135 } 136 if (arr[7] != null) { 137 bean.setAppPath((String) arr[7]); 138 } 139 if (arr[8] != null) { 140 bean.setConcurrency(((Integer) arr[8]).intValue()); 141 } 142 if (arr[9] != null) { 143 bean.setFrequency(((Integer) arr[9]).intValue()); 144 } 145 if (arr[10] != null) { 146 bean.setLastActionTime((Timestamp) arr[10]); 147 } 148 if (arr[11] != null) { 149 bean.setNextMaterializedTime((Timestamp) arr[11]); 150 } 151 if (arr[12] != null) { 152 bean.setCreatedTime((Timestamp) arr[12]); 153 } 154 if (arr[13] != null) { 155 bean.setTimeUnit(Timeunit.valueOf((String) arr[13])); 156 } 157 if (arr[14] != null) { 158 bean.setTimeZone((String) arr[14]); 159 } 160 if (arr[15] != null) { 161 bean.setTimeout((Integer) arr[15]); 162 } 163 return bean; 164 } 165 }