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.store;
020
021import java.util.List;
022import java.util.Map;
023
024import org.apache.oozie.client.OozieClient;
025import org.apache.oozie.util.XLog;
026
027public class StoreStatusFilter {
028    public static final String coordSeletStr = "Select w.id, w.appName, w.statusStr, w.user, w.group, w.startTimestamp, w.endTimestamp, w.appPath, w.concurrency, w.frequency, w.lastActionTimestamp, w.nextMaterializedTimestamp, w.createdTimestamp, w.timeUnitStr, w.timeZone, w.timeOut from CoordinatorJobBean w";
029
030    public static final String coordCountStr = "Select count(w) from CoordinatorJobBean w";
031
032    public static final String wfSeletStr = "Select w.id, w.appName, w.statusStr, w.run, w.user, w.group, w.createdTimestamp, w.startTimestamp, w.lastModifiedTimestamp, w.endTimestamp from WorkflowJobBean w";
033
034    public static final String wfCountStr = "Select count(w) from WorkflowJobBean w";
035
036    public static final String bundleSeletStr = "Select w.id, w.appName, w.appPath, w.conf, w.statusStr, w.kickoffTimestamp, w.startTimestamp, w.endTimestamp, w.pauseTimestamp, w.createdTimestamp, w.user, w.group, w.timeUnitStr, w.timeOut from BundleJobBean w";
037
038    public static final String bundleCountStr = "Select count(w) from BundleJobBean w";
039
040    public static void filter(Map<String, List<String>> filter, List<String> orArray, List<String> colArray,
041            List<String> valArray, StringBuilder sb, String seletStr, String countStr) {
042        boolean isStatus = false;
043        boolean isAppName = false;
044        boolean isUser = false;
045        boolean isEnabled = false;
046        boolean isFrequency = false;
047        boolean isId = false;
048        boolean isUnit = false;
049
050        int index = 0;
051
052        for (Map.Entry<String, List<String>> entry : filter.entrySet()) {
053            String colName = null;
054            String colVar = null;
055            if (entry.getKey().equals(OozieClient.FILTER_GROUP)) {
056                XLog.getLog(StoreStatusFilter.class).warn("Filter by 'group' is not supported anymore");
057            }
058            else {
059                if (entry.getKey().equals(OozieClient.FILTER_STATUS)) {
060                    List<String> values = filter.get(OozieClient.FILTER_STATUS);
061                    colName = "status";
062                    for (int i = 0; i < values.size(); i++) {
063                        colVar = "status";
064                        colVar = colVar + index;
065                        if (!isEnabled && !isStatus) {
066                            sb.append(seletStr).append(" where w.statusStr IN (:status" + index);
067                            isStatus = true;
068                            isEnabled = true;
069                        }
070                        else {
071                            if (isEnabled && !isStatus) {
072                                sb.append(" and w.statusStr IN (:status" + index);
073                                isStatus = true;
074                            }
075                            else {
076                                if (isStatus) {
077                                    sb.append(", :status" + index);
078                                }
079                            }
080                        }
081                        if (i == values.size() - 1) {
082                            sb.append(")");
083                        }
084                        index++;
085                        valArray.add(values.get(i));
086                        orArray.add(colName);
087                        colArray.add(colVar);
088                    }
089                }
090                else {
091                    if (entry.getKey().equals(OozieClient.FILTER_NAME)) {
092                        List<String> values = filter.get(OozieClient.FILTER_NAME);
093                        colName = "appName";
094                        for (int i = 0; i < values.size(); i++) {
095                            colVar = "appName";
096                            colVar = colVar + index;
097                            if (!isEnabled && !isAppName) {
098                                sb.append(seletStr).append(" where w.appName IN (:appName" + index);
099                                isAppName = true;
100                                isEnabled = true;
101                            }
102                            else {
103                                if (isEnabled && !isAppName) {
104                                    sb.append(" and w.appName IN (:appName" + index);
105                                    isAppName = true;
106                                }
107                                else {
108                                    if (isAppName) {
109                                        sb.append(", :appName" + index);
110                                    }
111                                }
112                            }
113                            if (i == values.size() - 1) {
114                                sb.append(")");
115                            }
116                            index++;
117                            valArray.add(values.get(i));
118                            orArray.add(colName);
119                            colArray.add(colVar);
120                        }
121                    }
122                    else {
123                        if (entry.getKey().equals(OozieClient.FILTER_USER)) {
124                            List<String> values = filter.get(OozieClient.FILTER_USER);
125                            colName = "user";
126                            for (int i = 0; i < values.size(); i++) {
127                                colVar = "user";
128                                colVar = colVar + index;
129                                if (!isEnabled && !isUser) {
130                                    sb.append(seletStr).append(" where w.user IN (:user" + index);
131                                    isUser = true;
132                                    isEnabled = true;
133                                }
134                                else {
135                                    if (isEnabled && !isUser) {
136                                        sb.append(" and w.user IN (:user" + index);
137                                        isUser = true;
138                                    }
139                                    else {
140                                        if (isUser) {
141                                            sb.append(", :user" + index);
142                                        }
143                                    }
144                                }
145                                if (i == values.size() - 1) {
146                                    sb.append(")");
147                                }
148                                index++;
149                                valArray.add(values.get(i));
150                                orArray.add(colName);
151                                colArray.add(colVar);
152                            }
153                        }
154                        else if (entry.getKey().equals(OozieClient.FILTER_FREQUENCY)) {
155                            List<String> values = filter.get(OozieClient.FILTER_FREQUENCY);
156                            colName = "frequency";
157                            for (int i = 0; i < values.size(); i++) {
158                                colVar = "frequency";
159                                colVar = colVar + index;
160                                if (!isEnabled && !isFrequency) {
161                                    sb.append(seletStr).append(" where w.frequency IN (:frequency" + index);
162                                    isFrequency = true;
163                                    isEnabled = true;
164                                }
165                                else {
166                                    if (isEnabled && !isFrequency) {
167                                        sb.append(" and w.frequency IN (:frequency" + index);
168                                        isFrequency = true;
169                                    }
170                                    else {
171                                        if (isFrequency) {
172                                            sb.append(", :frequency" + index);
173                                        }
174                                    }
175                                }
176                                if (i == values.size() - 1) {
177                                    sb.append(")");
178                                }
179                                index++;
180                                valArray.add(values.get(i));
181                                orArray.add(colName);
182                                colArray.add(colVar);
183                            }
184                        }
185                        else if (entry.getKey().equals(OozieClient.FILTER_ID)) {
186                            List<String> values = filter.get(OozieClient.FILTER_ID);
187                            colName = "id";
188                            for (int i = 0; i < values.size(); i++) {
189                                colVar = "id";
190                                colVar = colVar + index;
191                                if (!isEnabled && !isId) {
192                                    sb.append(seletStr).append(" where w.id IN (:id" + index);
193                                    isId = true;
194                                    isEnabled = true;
195                                }
196                                else {
197                                    if (isEnabled && !isId) {
198                                        sb.append(" and w.id IN (:id" + index);
199                                        isId = true;
200                                    }
201                                    else {
202                                        if (isId) {
203                                            sb.append(", :id" + index);
204                                        }
205                                    }
206                                }
207                                if (i == values.size() - 1) {
208                                    sb.append(")");
209                                }
210                                index++;
211                                valArray.add(values.get(i));
212                                orArray.add(colName);
213                                colArray.add(colVar);
214                            }
215                        }
216                        // Filter map has time unit filter specified
217                        else if (entry.getKey().equals(OozieClient.FILTER_UNIT)) {
218                            List<String> values = filter.get(OozieClient.FILTER_UNIT);
219                            colName = "timeUnitStr";
220                            for (int i = 0; i < values.size(); ++i) {
221                                colVar = colName + index;
222                                // This unit filter value is the first condition to be added to the where clause of
223                                // query
224                                if (!isEnabled && !isUnit) {
225                                    sb.append(seletStr).append(" where w.timeUnitStr IN (:timeUnitStr" + index);
226                                    isUnit = true;
227                                    isEnabled = true;
228                                } else {
229                                    // Unit filter is neither the first nor the last condition to be added to the where
230                                    // clause of query
231                                    if (isEnabled && !isUnit) {
232                                        sb.append(" and w.timeUnitStr IN (:timeUnitStr" + index);
233                                        isUnit = true;
234                                    } else {
235                                        if (isUnit) {
236                                            sb.append(", :timeUnitStr" + index);
237                                        }
238                                    }
239                                }
240                                // This unit filter value is the last condition to be added to the where clause of query
241                                if (i == values.size() - 1) {
242                                    sb.append(")");
243                                }
244                                ++index;
245                                valArray.add(values.get(i));
246                                orArray.add(colName);
247                                colArray.add(colVar);
248                            }
249                        }
250                    }
251                }
252            }
253        }
254    }
255}