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