2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.md.sal.dom.xsql;
11 import java.io.Serializable;
12 import java.lang.reflect.Method;
14 import java.util.concurrent.ConcurrentHashMap;
17 * To be removed in Nitrogen
20 public class XSQLCriteria implements Serializable {
21 private static final long serialVersionUID = 1L;
23 private static final String operators[] =
24 new String[] {" and ", " or ", ">=", "<=", "!=", "=", ">", "<", "like",
25 "is null", "not null", "skip"};
26 private static final String STRING_CHAR = "'";
28 public static final int OP_CODE_AND = 0;
29 public static final int OP_CODE_OR = 1;
30 public static final int OP_CODE_GTEQ = 2;
31 public static final int OP_CODE_LTEQ = 3;
32 public static final int OP_CODE_NOT_EQ = 4;
33 public static final int OP_CODE_EQUAL = 5;
34 public static final int OP_CODE_GT = 6;
35 public static final int OP_CODE_LT = 7;
36 public static final int OP_CODE_LIKE = 8;
37 public static final int OP_CODE_NULL = 9;
38 public static final int OP_CODE_NOT_NULL = 10;
39 public static final int OP_CODE_SKIP = 11;
41 private XSQLCriteria left = null;
42 private XSQLCriteria right = null;
44 private int operation = -1;
46 private Object leftValue = null;
47 private Object rightValue = null;
48 private String criteria = null;
50 private static final Map<Class<?>, Map<String, Method>> methodCache =
51 new ConcurrentHashMap<>();
53 public XSQLCriteria(final String data, final int parentOperation) {
55 parse(data, parentOperation);
58 private void parse(String data, int parentOperation) {
62 int index1 = data.indexOf("(");
64 String leftCondition = data.substring(0, index1).trim();
65 if (leftCondition.trim().equals("")) {
66 int index2 = data.lastIndexOf(")");
67 if (index2 < data.length() - 1) {
68 String rtValue = data.substring(index2 + 1).trim();
69 if (!rtValue.equals("")) {
71 new XSQLCriteria(data.substring(index1 + 1, index2),
73 data = data.substring(index2 + 1);
75 data = data.substring(1, index2);
78 data = data.substring(1, index2);
81 right = new XSQLCriteria(
82 data.substring(index1 + 1, data.length() - 1),
84 data = data.substring(0, index1);
88 for (int i = 0; i < operators.length; i++) {
89 index1 = data.indexOf(operators[i]);
93 left = new XSQLCriteria(data.substring(0, index1),
96 if (left.leftValue != null && left.rightValue == null
97 && left.right == null) {
98 leftValue = left.leftValue;
102 right = new XSQLCriteria(
103 data.substring(index1 + operators[i].length()),
106 if (right.leftValue != null && right.rightValue == null
107 && right.right == null) {
108 rightValue = right.leftValue;
115 if (data.startsWith("'") && data.endsWith("'")) {
116 data = data.substring(1, data.length() - 1);
119 if (parentOperation == OP_CODE_LIKE && data.startsWith("%") && data.endsWith("%")
120 && data.substring(1, data.length() - 1).indexOf("%") == -1) {
121 data = data.substring(1, data.length() - 1);
127 public static Object getValue(Object element, String propertyName) {
129 Map<String, Method> cache = methodCache.get(element.getClass());
131 cache = new ConcurrentHashMap<>();
132 methodCache.put(element.getClass(), cache);
135 Method m = cache.get(propertyName);
137 Method methods[] = element.getClass().getMethods();
138 for (Method mm : methods) {
139 if (mm.getName().equals(propertyName) || mm.getName()
140 .equals("get" + propertyName) || mm.getName()
141 .equals("is" + propertyName)) {
143 m.setAccessible(true);
144 cache.put(propertyName, m);
154 if (m.getParameterTypes() == null
155 || m.getParameterTypes().length == 0) {
156 value = m.invoke(element, null);
158 if (String.class.isAssignableFrom(m.getParameterTypes()[0])) {
161 Object arg = m.getParameterTypes()[0].newInstance();
162 value = m.invoke(element, arg);
165 } catch (Exception err) {
166 err.printStackTrace();
171 public int isObjectFitCriteria(Object element, String propertyName) {
172 Object value = getValue(element, propertyName);
174 value = value.toString();
176 return checkValue(value);
179 public int checkValue(Object value) {
180 if (leftValue != null && rightValue != null) {
183 if (leftValue.equals("?")) {
189 if (rightValue.equals("?")) {
194 if (operation != OP_CODE_SKIP && operation != OP_CODE_NULL) {
195 if (aSide == null && bSide != null) {
197 } else if (aSide != null && bSide == null) {
199 } else if (aSide == null && bSide == null) {
205 if (aSide.equals(bSide)) {
211 if (!aSide.equals(bSide)) {
217 if (aSide.toString().indexOf(bSide.toString()) != -1) {
228 case OP_CODE_NOT_NULL:
235 if (aSide == null || bSide == null) {
238 if (Double.parseDouble(aSide.toString().trim()) > Double
239 .parseDouble(bSide.toString().trim())) {
245 if (aSide == null || bSide == null) {
248 if (Double.parseDouble(aSide.toString().trim()) >= Double
249 .parseDouble(bSide.toString().trim())) {
256 if (aSide == null || bSide == null) {
259 if (Double.parseDouble(aSide.toString().trim()) < Double
260 .parseDouble(bSide.toString().trim())) {
266 if (aSide == null || bSide == null) {
269 if (Double.parseDouble(aSide.toString().trim()) <= Double
270 .parseDouble(bSide.toString().trim())) {
280 leftResult = left.checkValue(value);
285 rightResult = right.checkValue(value);
288 if (operation == OP_CODE_SKIP) {
289 if (rightResult == 0) {
296 if (operation == OP_CODE_AND) {
297 if (leftResult == 0) {
300 if (rightResult == 0) {
303 if (leftResult >= 2) {
306 if (rightResult >= 2) {
313 if (operation == OP_CODE_OR) {
314 if (leftResult == 0 && rightResult == 0) {
317 if (leftResult >= 2) {
320 if (rightResult >= 2) {
329 public String toString() {
333 public String getCriteriaForProperty(XSQLColumn col) {
334 StringBuffer result = new StringBuffer();
335 if (criteria == null) {
339 if (leftValue != null && rightValue != null) {
340 if (leftValue.toString().toLowerCase().equals(col.getName().toLowerCase()) ||
341 leftValue.toString().toLowerCase().equals(col.toString().toLowerCase()) /*||
342 /*col.getName().toLowerCase().indexOf(leftValue.toString().toLowerCase()) != -1*/) {
343 result.append("? ").append(operators[operation]).append(" ").append(rightValue);
345 if (rightValue.toString().toLowerCase().equals(col.getName().toLowerCase()) ||
346 rightValue.toString().toLowerCase().equals(col.toString().toLowerCase()) /*||
347 col.getName().toLowerCase().indexOf(rightValue.toString().toLowerCase()) != -1*/) {
348 result.append("? ").append(operators[operation]).append(" ").append(leftValue);
350 return result.toString();
351 } else if (left != null && right != null) {
352 String leftString = left.getCriteriaForProperty(col);
353 String rightString = right.getCriteriaForProperty(col);
354 if (!leftString.equals("") && !rightString.equals("")) {
355 return leftString + " " + operators[operation] + " "
357 } else if (!leftString.equals("")) {
359 } else if (!rightString.equals("")) {
363 } else if (leftValue != null && leftValue.toString().toLowerCase()
364 .equals(col.toString().toLowerCase()) && right != null) {
365 return "? " + operators[operation] + " (" + right
366 .getCriteriaForProperty(col) + ")";
367 } else if (rightValue != null && rightValue.toString().toLowerCase()
368 .equals(col.toString().toLowerCase()) && left != null) {
369 return "(" + left.getCriteriaForProperty(col) + ") "
370 + operators[operation] + " ?";
375 public static void main(String args[]) {
377 new XSQLCriteria("ip like '%101%' or (354>=0 or 456>=3)", -1);
378 System.out.println(p.checkValue("192.268.4.4"));
379 p = new XSQLCriteria("? like '%267%'", -1);
380 System.out.println(p.checkValue("192.268.4.4"));