Upload the ESR server seed code.
[aai/esr-server.git] / esr-core / esr-mgr / src / main / java / org / onap / aai / esr / util / HqlFactory.java
1 /**
2  * Copyright 2016 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.aai.esr.util;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.lang.reflect.Field;
22 import java.lang.reflect.Method;
23 import java.util.Arrays;
24
25 public class HqlFactory {
26
27   private static final Logger logger = LoggerFactory.getLogger(HqlFactory.class);
28
29   /**
30    * get update hql.
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
35    */
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 ");
40     hql.append(objName);
41     hql.append(" set ");
42     Field[] fields = obj.getClass().getDeclaredFields();
43     if (obj.getClass().getGenericSuperclass() != null) {
44       Field[] parentFields = obj.getClass().getSuperclass().getDeclaredFields();
45       fields = concat(fields, parentFields);
46     }
47     for (Field field : fields) {
48       String name = field.getName();
49       Method method = null;
50       Object value = null;
51       if (!contain(excludeProperties, name)) {
52         String upperName = name.substring(0, 1).toUpperCase() + name.substring(1);
53         try {
54           method = obj.getClass().getMethod("get" + upperName);
55           value = method.invoke(obj);
56           if (value != null) {
57             if (value instanceof String) {
58               hql.append(name);
59               hql.append("=");
60               hql.append("'");
61               hql.append(value);
62               hql.append("'");
63               hql.append(",");
64             } else {
65               hql.append(name);
66               hql.append("=");
67               hql.append(value);
68               hql.append(",");
69             }
70           }
71         } catch (Exception error) {
72           logger.error("error while creating update hql", error);
73         }
74       }
75     }
76
77     String sql = hql.toString();
78     sql = sql.substring(0, sql.lastIndexOf(","));
79     if (filter != null) {
80       sql = sql + " where " + filter;
81     }
82     logger.info("update hql is : " + sql);
83     return sql;
84   }
85
86   /**
87    * identify whether or not to include target string in source string.
88    */
89   public static boolean contain(String[] src, String target) {
90     if (src == null || src.length == 0 || target == null) {
91       return false;
92     } else {
93       for (String str : src) {
94         if (str.equals(target)) {
95           return true;
96         }
97       }
98     }
99     return false;
100   }
101
102   /**
103    * concat str.
104    */
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);
108     return result;
109   }
110
111   public static String getOidFilter(String key, String value) {
112     return key + "= '" + value + "'";
113   }
114
115   /**
116    * get query hql.
117    */
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.");
122     hql.append(column);
123     hql.append(" from ");
124     hql.append(objName);
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);
130     }
131     for (Field field : fields) {
132       String name = field.getName();
133       Method method = null;
134       Object value = null;
135       String upperName = name.substring(0, 1).toUpperCase() + name.substring(1);
136       try {
137         method = data.getClass().getMethod("get" + upperName);
138         value = method.invoke(data);
139         if (value != null) {
140           if (value instanceof String) {
141             hql.append("q." + name);
142             hql.append("=");
143             hql.append("'");
144             hql.append(value);
145             hql.append("'");
146             hql.append(" and ");
147           } else {
148             hql.append("q." + name);
149             hql.append("=");
150             hql.append(value);
151             hql.append("and ");
152           }
153         }
154       } catch (Exception e) {
155         logger.error("error while creating update hql", e);
156       }
157     }
158     String sql = hql.toString();
159     sql = sql.substring(0, sql.lastIndexOf("and"));
160
161     logger.info("query hql is : " + sql);
162     return sql.trim();
163   }
164 }