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