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