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