2 * Copyright 2016-2017 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.aai.esr.util;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import java.lang.reflect.Field;
22 import java.lang.reflect.Method;
23 import java.util.Arrays;
25 public class HqlFactory {
27 private static final Logger logger = LoggerFactory.getLogger(HqlFactory.class);
31 * @param obj the object that used to be generate the hql
32 * @param excludeProperties the properties that need not to be used
33 * @param filter the condition after "where"
34 * @return hibernate hql
36 public static String getUpdateHql(Object obj, String[] excludeProperties, String filter) {
37 StringBuffer hql = new StringBuffer();
38 String objName = obj.getClass().getSimpleName();
39 hql.append("update ");
42 Field[] fields = obj.getClass().getDeclaredFields();
43 if (obj.getClass().getGenericSuperclass() != null) {
44 Field[] parentFields = obj.getClass().getSuperclass().getDeclaredFields();
45 fields = concat(fields, parentFields);
47 for (Field field : fields) {
48 String name = field.getName();
51 if (!contain(excludeProperties, name)) {
52 String upperName = name.substring(0, 1).toUpperCase() + name.substring(1);
54 method = obj.getClass().getMethod("get" + upperName);
55 value = method.invoke(obj);
57 if (value instanceof String) {
71 } catch (Exception error) {
72 logger.error("error while creating update hql", error);
77 String sql = hql.toString();
78 sql = sql.substring(0, sql.lastIndexOf(","));
80 sql = sql + " where " + filter;
82 logger.info("update hql is : " + sql);
87 * identify whether or not to include target string in source string.
89 public static boolean contain(String[] src, String target) {
90 if (src == null || src.length == 0 || target == null) {
93 for (String str : src) {
94 if (str.equals(target)) {
105 public static <T> T[] concat(T[] first, T[] second) {
106 T[] result = Arrays.copyOf(first, first.length + second.length);
107 System.arraycopy(second, 0, result, first.length, second.length);
111 public static String getOidFilter(String key, String value) {
112 return key + "= '" + value + "'";
118 public static String getQueryHql(Object data, String column) {
119 StringBuffer hql = new StringBuffer();
120 String objName = data.getClass().getSimpleName();
121 hql.append("select q.");
123 hql.append(" from ");
125 hql.append(" as q where ");
126 Field[] fields = data.getClass().getDeclaredFields();
127 if (data.getClass().getGenericSuperclass() != null) {
128 Field[] parentFields = data.getClass().getSuperclass().getDeclaredFields();
129 fields = concat(fields, parentFields);
131 for (Field field : fields) {
132 String name = field.getName();
133 Method method = null;
135 String upperName = name.substring(0, 1).toUpperCase() + name.substring(1);
137 method = data.getClass().getMethod("get" + upperName);
138 value = method.invoke(data);
140 if (value instanceof String) {
141 hql.append("q." + name);
148 hql.append("q." + name);
154 } catch (Exception e) {
155 logger.error("error while creating update hql", e);
158 String sql = hql.toString();
159 sql = sql.substring(0, sql.lastIndexOf("and"));
161 logger.info("query hql is : " + sql);