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 void filter(Map<String, List<String>> filter, List<String> orArray, List<String> colArray, List<String> valArray, StringBuilder sb, String seletStr, String countStr) {
032            boolean isStatus = false;
033            boolean isGroup = false;
034            boolean isAppName = false;
035            boolean isUser = false;
036            boolean isEnabled = false;
037    
038            int index = 0;
039    
040            for (Map.Entry<String, List<String>> entry : filter.entrySet()) {
041                String colName = null;
042                String colVar = null;
043                if (entry.getKey().equals(OozieClient.FILTER_GROUP)) {
044                    List<String> values = filter.get(OozieClient.FILTER_GROUP);
045                    colName = "group";
046                    for (int i = 0; i < values.size(); i++) {
047                        colVar = "group";
048                        colVar = colVar + index;
049                        if (!isEnabled && !isGroup) {
050                            sb.append(seletStr).append(" where w.group IN (:group" + index);
051                            isGroup = true;
052                            isEnabled = true;
053                        }
054                        else {
055                            if (isEnabled && !isGroup) {
056                                sb.append(" and w.group IN (:group" + index);
057                                isGroup = true;
058                            }
059                            else {
060                                if (isGroup) {
061                                    sb.append(", :group" + index);
062                                }
063                            }
064                        }
065                        if (i == values.size() - 1) {
066                            sb.append(")");
067                        }
068                        index++;
069                        valArray.add(values.get(i));
070                        orArray.add(colName);
071                        colArray.add(colVar);
072                    }
073                }
074                else {
075                    if (entry.getKey().equals(OozieClient.FILTER_STATUS)) {
076                        List<String> values = filter.get(OozieClient.FILTER_STATUS);
077                        colName = "status";
078                        for (int i = 0; i < values.size(); i++) {
079                            colVar = "status";
080                            colVar = colVar + index;
081                            if (!isEnabled && !isStatus) {
082                                sb.append(seletStr).append(" where w.status IN (:status" + index);
083                                isStatus = true;
084                                isEnabled = true;
085                            }
086                            else {
087                                if (isEnabled && !isStatus) {
088                                    sb.append(" and w.status IN (:status" + index);
089                                    isStatus = true;
090                                }
091                                else {
092                                    if (isStatus) {
093                                        sb.append(", :status" + index);
094                                    }
095                                }
096                            }
097                            if (i == values.size() - 1) {
098                                sb.append(")");
099                            }
100                            index++;
101                            valArray.add(values.get(i));
102                            orArray.add(colName);
103                            colArray.add(colVar);
104                        }
105                    }
106                    else {
107                        if (entry.getKey().equals(OozieClient.FILTER_NAME)) {
108                            List<String> values = filter.get(OozieClient.FILTER_NAME);
109                            colName = "appName";
110                            for (int i = 0; i < values.size(); i++) {
111                                colVar = "appName";
112                                colVar = colVar + index;
113                                if (!isEnabled && !isAppName) {
114                                    sb.append(seletStr).append(" where w.appName IN (:appName" + index);
115                                    isAppName = true;
116                                    isEnabled = true;
117                                }
118                                else {
119                                    if (isEnabled && !isAppName) {
120                                        sb.append(" and w.appName IN (:appName" + index);
121                                        isAppName = true;
122                                    }
123                                    else {
124                                        if (isAppName) {
125                                            sb.append(", :appName" + index);
126                                        }
127                                    }
128                                }
129                                if (i == values.size() - 1) {
130                                    sb.append(")");
131                                }
132                                index++;
133                                valArray.add(values.get(i));
134                                orArray.add(colName);
135                                colArray.add(colVar);
136                            }
137                        }
138                        else {
139                            if (entry.getKey().equals(OozieClient.FILTER_USER)) {
140                                List<String> values = filter.get(OozieClient.FILTER_USER);
141                                colName = "user";
142                                for (int i = 0; i < values.size(); i++) {
143                                    colVar = "user";
144                                    colVar = colVar + index;
145                                    if (!isEnabled && !isUser) {
146                                        sb.append(seletStr).append(" where w.user IN (:user" + index);
147                                        isUser = true;
148                                        isEnabled = true;
149                                    }
150                                    else {
151                                        if (isEnabled && !isUser) {
152                                            sb.append(" and w.user IN (:user" + index);
153                                            isUser = true;
154                                        }
155                                        else {
156                                            if (isUser) {
157                                                sb.append(", :user" + index);
158                                            }
159                                        }
160                                    }
161                                    if (i == values.size() - 1) {
162                                        sb.append(")");
163                                    }
164                                    index++;
165                                    valArray.add(values.get(i));
166                                    orArray.add(colName);
167                                    colArray.add(colVar);
168                                }
169                            }
170                        }
171                    }
172                }
173            }
174        }
175    }