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 */
018package org.apache.oozie.executor.jpa;
019
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import javax.persistence.EntityManager;
025import javax.persistence.Query;
026import org.apache.oozie.ErrorCode;
027import org.apache.oozie.SLAEventBean;
028import org.apache.oozie.client.OozieClient;
029
030/**
031 * Load the list of SLAEventBean for a seqId and return the list.
032 */
033@Deprecated
034public class SLAEventsGetForFilterJPAExecutor implements JPAExecutor<List<SLAEventBean>> {
035
036    private static final String selectStr = "SELECT OBJECT(w) FROM SLAEventBean w WHERE w.event_id > :seqid";
037    private long seqId = -1;
038    private int len;
039    private long[] lastSeqId;
040    private Map<String, List<String>> filter;
041    private StringBuilder sb;
042
043    public SLAEventsGetForFilterJPAExecutor(long seqId, int len, Map<String, List<String>> filter, long[] lastSeqId) {
044        this.seqId = seqId;
045        this.len = len;
046        this.filter = filter;
047        this.lastSeqId = lastSeqId;
048        this.lastSeqId[0] = seqId;
049    }
050
051    @Override
052    public String getName() {
053        return "SLAEventsGetForJobIdJPAExecutor";
054    }
055
056    @Override
057    @SuppressWarnings("unchecked")
058    public List<SLAEventBean> execute(EntityManager em) throws JPAExecutorException {
059
060        List<SLAEventBean> seBeans;
061        StringBuilder sb = new StringBuilder(selectStr);
062        Map<String, String> keyVal = new HashMap<String, String>();
063
064        for (Map.Entry<String, List<String>> entry : filter.entrySet()) {
065
066            if (entry.getKey().equals(OozieClient.FILTER_JOBID) || entry.getKey().equals(OozieClient.FILTER_APPNAME)) {
067                sb.append(" AND ");
068            }
069
070            if (entry.getKey().equals(OozieClient.FILTER_JOBID)) {
071                List<String> vals = entry.getValue();
072                if (vals.size() == 1) {
073                    sb.append("w.slaId = :jobid");
074                    keyVal.put("jobid", vals.get(0));
075                }
076                else {
077                    for (int i = 0; i < vals.size(); i++) {
078                        String val = vals.get(i);
079                        keyVal.put("jobid" + i, val);
080                        if (i == 0) {
081                            sb.append("w.slaId IN (:jobid" + i);
082                        }
083                        else {
084                            sb.append(",:jobid" + i);
085                        }
086                    }
087                    sb.append(")");
088                }
089            }
090            else if (entry.getKey().equals(OozieClient.FILTER_APPNAME)) {
091                List<String> vals = entry.getValue();
092                if (vals.size() == 1) {
093                    sb.append("w.appName = :appname");
094                    keyVal.put("appname", vals.get(0));
095                }
096                else {
097                    for (int i = 0; i < vals.size(); i++) {
098                        String val = vals.get(i);
099                        keyVal.put("appname" + i, val);
100                        if (i == 0) {
101                            sb.append("w.appName IN (:appname" + i);
102                        }
103                        else {
104                            sb.append(",:appname" + i);
105                        }
106                    }
107                    sb.append(")");
108                }
109            }
110        }
111        sb.append(" ORDER BY w.event_id ");
112
113        try {
114            Query q = em.createQuery(sb.toString());
115            q.setMaxResults(len);
116            q.setParameter("seqid", seqId);
117            for (Map.Entry<String, String> entry : keyVal.entrySet()) {
118                q.setParameter(entry.getKey(), entry.getValue());
119            }
120            seBeans = q.getResultList();
121            for (SLAEventBean j : seBeans) {
122                lastSeqId[0] = Math.max(lastSeqId[0], j.getEvent_id());
123            }
124
125        }
126        catch (Exception e) {
127            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
128        }
129        return seBeans;
130    }
131
132}