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.sla;
019
020import java.sql.Timestamp;
021import java.util.Date;
022import java.util.LinkedHashMap;
023import java.util.List;
024import java.util.Map;
025import javax.persistence.EntityManager;
026import javax.persistence.Query;
027
028import org.apache.oozie.ErrorCode;
029import org.apache.oozie.executor.jpa.JPAExecutor;
030import org.apache.oozie.executor.jpa.JPAExecutorException;
031import org.apache.oozie.sla.SLASummaryBean;
032
033/**
034 * Load the list of SLASummaryBean (for dashboard) and return the list.
035 */
036public class SLASummaryGetForFilterJPAExecutor implements JPAExecutor<List<SLASummaryBean>> {
037
038    private static final String selectStr = "SELECT OBJECT(s) FROM SLASummaryBean s WHERE ";
039
040    private SLASummaryFilter filter;
041    private int numMaxResults;
042
043
044    public SLASummaryGetForFilterJPAExecutor(SLASummaryFilter filter, int numMaxResults) {
045        this.filter = filter;
046        this.numMaxResults = numMaxResults;
047    }
048
049    @Override
050    public String getName() {
051        return "SLASummaryGetForFilterJPAExecutor";
052    }
053
054    @SuppressWarnings("unchecked")
055    @Override
056    public List<SLASummaryBean> execute(EntityManager em) throws JPAExecutorException {
057        List<SLASummaryBean> ssBean;
058        StringBuilder sb = new StringBuilder(selectStr);
059        Map<String, Object> queryParams = new LinkedHashMap<String, Object>();
060        boolean firstCondition = true;
061        if (filter.getJobId() != null) {
062            firstCondition = false;
063            if (filter.getParentId() != null) {
064                sb.append("(s.jobId = :jobId OR s.parentId = :parentId)");
065                queryParams.put("jobId", filter.getJobId());
066                queryParams.put("parentId", filter.getParentId());
067            }
068            else {
069                sb.append("s.jobId = :jobId");
070                queryParams.put("jobId", filter.getJobId());
071            }
072        }
073        if (filter.getParentId() != null && filter.getJobId() == null) {
074            firstCondition = false;
075            sb.append("s.parentId = :parentId");
076            queryParams.put("parentId", filter.getParentId());
077        }
078        if (filter.getAppName() != null && filter.getJobId() == null && filter.getParentId() == null) {
079            firstCondition = false;
080            sb.append("s.appName = :appName");
081            queryParams.put("appName", filter.getAppName());
082        }
083        if (filter.getNominalStart() != null) {
084            if (firstCondition) {
085                firstCondition = false;
086            }
087            else {
088                sb.append(" AND ");
089            }
090            sb.append("s.nominalTimeTS >= :nominalTimeStart");
091            queryParams.put("nominalTimeStart", new Timestamp(filter.getNominalStart().getTime()));
092        }
093
094        if (filter.getNominalEnd() != null) {
095            if (firstCondition) {
096                firstCondition = false;
097            }
098            else {
099                sb.append(" AND ");
100            }
101            sb.append("s.nominalTimeTS <= :nominalTimeEnd");
102            queryParams.put("nominalTimeEnd", new Timestamp(filter.getNominalEnd().getTime()));
103        }
104
105        sb.append(" ORDER BY s.nominalTimeTS");
106        try {
107            Query q = em.createQuery(sb.toString());
108            for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
109                q.setParameter(entry.getKey(), entry.getValue());
110            }
111            q.setMaxResults(numMaxResults);
112            ssBean = (List<SLASummaryBean>) q.getResultList();
113        }
114        catch (Exception e) {
115            throw new JPAExecutorException(ErrorCode.E0603, e.getMessage(), e);
116        }
117        return ssBean;
118    }
119
120    public static class SLASummaryFilter {
121
122        private String appName;
123        private String jobId;
124        private String parentId;
125        private Date nominalStart;
126        private Date nominalEnd;
127
128        public SLASummaryFilter() {
129        }
130
131        public String getAppName() {
132            return appName;
133        }
134
135        public void setAppName(String appName) {
136            this.appName = appName;
137        }
138
139        public String getJobId() {
140            return jobId;
141        }
142
143        public void setJobId(String jobId) {
144            this.jobId = jobId;
145        }
146
147        public String getParentId() {
148            return parentId;
149        }
150
151        public void setParentId(String parentId) {
152            this.parentId = parentId;
153        }
154
155        public Date getNominalStart() {
156            return nominalStart;
157        }
158
159        public void setNominalStart(Date nominalStart) {
160            this.nominalStart = nominalStart;
161        }
162
163        public Date getNominalEnd() {
164            return nominalEnd;
165        }
166
167        public void setNominalEnd(Date nominalEnd) {
168            this.nominalEnd = nominalEnd;
169        }
170
171    }
172
173}