001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.store;
016    
017    import java.util.List;
018    import java.util.Map;
019    
020    import org.apache.oozie.client.OozieClient;
021    
022    public class StoreStatusFilter {
023        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";
024    
025        public static final String coordCountStr = "Select count(w) from CoordinatorJobBean w";
026    
027        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";
028    
029        public static final String wfCountStr = "Select count(w) from WorkflowJobBean w";
030    
031        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";
032    
033        public static final String bundleCountStr = "Select count(w) from BundleJobBean w";
034    
035        public static void filter(Map<String, List<String>> filter, List<String> orArray, List<String> colArray, List<String> valArray, StringBuilder sb, String seletStr, String countStr) {
036            boolean isStatus = false;
037            boolean isGroup = false;
038            boolean isAppName = false;
039            boolean isUser = false;
040            boolean isEnabled = false;
041    
042            int index = 0;
043    
044            for (Map.Entry<String, List<String>> entry : filter.entrySet()) {
045                String colName = null;
046                String colVar = null;
047                if (entry.getKey().equals(OozieClient.FILTER_GROUP)) {
048                    List<String> values = filter.get(OozieClient.FILTER_GROUP);
049                    colName = "group";
050                    for (int i = 0; i < values.size(); i++) {
051                        colVar = "group";
052                        colVar = colVar + index;
053                        if (!isEnabled && !isGroup) {
054                            sb.append(seletStr).append(" where w.group IN (:group" + index);
055                            isGroup = true;
056                            isEnabled = true;
057                        }
058                        else {
059                            if (isEnabled && !isGroup) {
060                                sb.append(" and w.group IN (:group" + index);
061                                isGroup = true;
062                            }
063                            else {
064                                if (isGroup) {
065                                    sb.append(", :group" + index);
066                                }
067                            }
068                        }
069                        if (i == values.size() - 1) {
070                            sb.append(")");
071                        }
072                        index++;
073                        valArray.add(values.get(i));
074                        orArray.add(colName);
075                        colArray.add(colVar);
076                    }
077                }
078                else {
079                    if (entry.getKey().equals(OozieClient.FILTER_STATUS)) {
080                        List<String> values = filter.get(OozieClient.FILTER_STATUS);
081                        colName = "status";
082                        for (int i = 0; i < values.size(); i++) {
083                            colVar = "status";
084                            colVar = colVar + index;
085                            if (!isEnabled && !isStatus) {
086                                sb.append(seletStr).append(" where w.status IN (:status" + index);
087                                isStatus = true;
088                                isEnabled = true;
089                            }
090                            else {
091                                if (isEnabled && !isStatus) {
092                                    sb.append(" and w.status IN (:status" + index);
093                                    isStatus = true;
094                                }
095                                else {
096                                    if (isStatus) {
097                                        sb.append(", :status" + index);
098                                    }
099                                }
100                            }
101                            if (i == values.size() - 1) {
102                                sb.append(")");
103                            }
104                            index++;
105                            valArray.add(values.get(i));
106                            orArray.add(colName);
107                            colArray.add(colVar);
108                        }
109                    }
110                    else {
111                        if (entry.getKey().equals(OozieClient.FILTER_NAME)) {
112                            List<String> values = filter.get(OozieClient.FILTER_NAME);
113                            colName = "appName";
114                            for (int i = 0; i < values.size(); i++) {
115                                colVar = "appName";
116                                colVar = colVar + index;
117                                if (!isEnabled && !isAppName) {
118                                    sb.append(seletStr).append(" where w.appName IN (:appName" + index);
119                                    isAppName = true;
120                                    isEnabled = true;
121                                }
122                                else {
123                                    if (isEnabled && !isAppName) {
124                                        sb.append(" and w.appName IN (:appName" + index);
125                                        isAppName = true;
126                                    }
127                                    else {
128                                        if (isAppName) {
129                                            sb.append(", :appName" + index);
130                                        }
131                                    }
132                                }
133                                if (i == values.size() - 1) {
134                                    sb.append(")");
135                                }
136                                index++;
137                                valArray.add(values.get(i));
138                                orArray.add(colName);
139                                colArray.add(colVar);
140                            }
141                        }
142                        else {
143                            if (entry.getKey().equals(OozieClient.FILTER_USER)) {
144                                List<String> values = filter.get(OozieClient.FILTER_USER);
145                                colName = "user";
146                                for (int i = 0; i < values.size(); i++) {
147                                    colVar = "user";
148                                    colVar = colVar + index;
149                                    if (!isEnabled && !isUser) {
150                                        sb.append(seletStr).append(" where w.user IN (:user" + index);
151                                        isUser = true;
152                                        isEnabled = true;
153                                    }
154                                    else {
155                                        if (isEnabled && !isUser) {
156                                            sb.append(" and w.user IN (:user" + index);
157                                            isUser = true;
158                                        }
159                                        else {
160                                            if (isUser) {
161                                                sb.append(", :user" + index);
162                                            }
163                                        }
164                                    }
165                                    if (i == values.size() - 1) {
166                                        sb.append(")");
167                                    }
168                                    index++;
169                                    valArray.add(values.get(i));
170                                    orArray.add(colName);
171                                    colArray.add(colVar);
172                                }
173                            }
174                        }
175                    }
176                }
177            }
178        }
179    }