summaryrefslogtreecommitdiffstats
path: root/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/util/HqlFactory.java
blob: 08e2e35cde15c8ad681eeefe3342c1e98e14e742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
 * Copyright 2016 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openo.commontosca.catalog.db.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * a tool class for creating Hibernate's HQL.
 * 
 */
public class HqlFactory {

  private static final Logger logger = LoggerFactory.getLogger(HqlFactory.class);

  /**
   * get update hql.
   * @param obj the object that used to be generate the hql
   * @param excludeProperties the properties that need not to be used
   * @param filter the condition after "where"
   * @return hibernate hql
   */
  public static String getUpdateHql(Object obj, String[] excludeProperties, String filter) {
    StringBuffer hql = new StringBuffer();
    String objName = obj.getClass().getSimpleName();
    hql.append("update ");
    hql.append(objName);
    hql.append(" set ");
    Field[] fields = obj.getClass().getDeclaredFields();
    if (obj.getClass().getGenericSuperclass() != null) {
      Field[] parentFields = obj.getClass().getSuperclass().getDeclaredFields();
      fields = concat(fields, parentFields);
    }
    for (Field field : fields) {
      String name = field.getName();
      Method method = null;
      Object value = null;
      if (!contain(excludeProperties, name)) {
        String upperName = name.substring(0, 1).toUpperCase() + name.substring(1);
        try {
          method = obj.getClass().getMethod("get" + upperName);
          value = method.invoke(obj);
          if (value != null) {
            if (value instanceof String) {
              hql.append(name);
              hql.append("=");
              hql.append("'");
              hql.append(value);
              hql.append("'");
              hql.append(",");
            } else {
              hql.append(name);
              hql.append("=");
              hql.append(value);
              hql.append(",");
            }
          }
        } catch (Exception e1) {
          logger.error("error while creating update hql", e1);
        }
      }
    }

    String sql = hql.toString();
    sql = sql.substring(0, sql.lastIndexOf(","));
    if (filter != null) {
      sql = sql + " where " + filter;
    }
    logger.info("update hql is : " + sql);
    return sql;
  }

  /**
   * judge wether target contains src.
   * @param src String[]
   * @param target String
   * @return boolean
   */
  public static boolean contain(String[] src, String target) {
    if (src == null || src.length == 0 || target == null) {
      return false;
    } else {
      for (String str : src) {
        if(str.equals(target)){
          return true;
        }
      }
    }
    return false;
  }

  /**
   * concat two array.
   * @param first first array
   * @param second second array
   * @return T
   */
  public static <T> T[] concat(T[] first, T[] second) {
    T[] result = Arrays.copyOf(first, first.length + second.length);
    System.arraycopy(second, 0, result, first.length, second.length);
    return result;
  }

  public static String getOidFilter(String key, String value) {
    return key + "= '" + value + "'";
  }

  /**
   * get query hql.
   * @param data Object
   * @param column String
   * @return String
   */
  public static String getQueryHql(Object data, String column) {
    StringBuffer hql = new StringBuffer();
    String objName = data.getClass().getSimpleName();
    hql.append("select q.");
    hql.append(column);
    hql.append(" from ");
    hql.append(objName);
    hql.append(" as q where ");
    Field[] fields = data.getClass().getDeclaredFields();
    if (data.getClass().getGenericSuperclass() != null) {
      Field[] parentFields = data.getClass().getSuperclass().getDeclaredFields();
      fields = concat(fields, parentFields);
    }
    for (Field field : fields) {
      String name = field.getName();
      Method method = null;
      Object value = null;
      String upperName = name.substring(0, 1).toUpperCase() + name.substring(1);
      try {
        method = data.getClass().getMethod("get" + upperName);
        value = method.invoke(data);
        if (value != null) {
          if (value instanceof String) {
            hql.append("q." + name);
            hql.append("=");
            hql.append("'");
            hql.append(value);
            hql.append("'");
            hql.append(" and ");
          } else {
            hql.append("q." + name);
            hql.append("=");
            hql.append(value);
            hql.append("and ");
          }
        }
      } catch (Exception e1) {
        logger.error("error while creating update hql", e1);
      }
    }
    String sql = hql.toString();
    sql = sql.substring(0, sql.lastIndexOf("and"));

    logger.info("query hql is : " + sql);
    return sql.trim();
  }

  public static String getQueryHqlByFilter(Class mainObject, Object filterData, String foreignKey) {
    StringBuffer hql = new StringBuffer();
    String objName = mainObject.getSimpleName();
    // hql.append("select queryTable.");
    hql.append(" from ");
    hql.append(objName);
    hql.append(" as a where ");
    String filterHql = getQueryHql(filterData, foreignKey);
    hql.append("a." + foreignKey);
    hql.append(" in(");
    hql.append(filterHql);
    hql.append(")");
    logger.info("QueryHqlByFilter  is : " + hql);
    return hql.toString();
  }

  public static String getDeleteHqlByFilter(Class mainObject, Object filterData, String foreignKey) {
    StringBuffer hql = new StringBuffer();
    String objName = mainObject.getSimpleName();
    hql.append("delete from ");
    hql.append(objName);
    hql.append(" as b where ");
    String filterHql = getQueryHql(filterData, foreignKey);
    hql.append("b." + foreignKey);
    hql.append(" in(");
    hql.append(filterHql);
    hql.append(")");
    logger.info("DeleteHqlByFilter  is : " + hql);
    return hql.toString();
  }
}