2 * Copyright 2017 ZTE Corporation.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
16 package org.onap.holmes.rulemgt.db;
\r
18 import java.beans.PropertyDescriptor;
\r
19 import java.lang.reflect.Field;
\r
20 import java.lang.reflect.Method;
\r
21 import java.util.ArrayList;
\r
22 import java.util.Date;
\r
23 import java.util.List;
\r
24 import java.util.Map;
\r
25 import java.util.Properties;
\r
26 import javax.inject.Inject;
\r
27 import lombok.extern.slf4j.Slf4j;
\r
28 import org.jvnet.hk2.annotations.Service;
\r
29 import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;
\r
30 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;
\r
31 import org.onap.holmes.common.api.entity.CorrelationRule;
\r
32 import org.onap.holmes.common.exception.CorrelationException;
\r
33 import org.onap.holmes.common.utils.DbDaoUtil;
\r
34 import org.skife.jdbi.v2.Handle;
\r
35 import org.skife.jdbi.v2.Query;
\r
39 public class CorrelationRuleQueryDao {
\r
42 private DbDaoUtil dbDaoUtil;
\r
44 public List<CorrelationRule> getCorrelationRulesByCondition(RuleQueryCondition ruleQueryCondition)
\r
45 throws CorrelationException {
\r
46 List<CorrelationRule> correlationRules = new ArrayList<CorrelationRule>();
\r
47 Handle handle = null;
\r
48 String whereStr = getWhereStrByRequestEntity(ruleQueryCondition);
\r
50 StringBuilder querySql = new StringBuilder("SELECT * FROM APLUS_RULE ").append(whereStr);
\r
51 handle = dbDaoUtil.getHandle();
\r
52 Query query = handle.createQuery(querySql.toString());
\r
53 for (Object value : query.list()) {
\r
54 CorrelationRule correlationRule = getCorrelationRule((Map) value);
\r
55 correlationRules.add(correlationRule);
\r
57 return correlationRules;
\r
58 } catch (Exception e) {
\r
59 log.warn("Failed to query the rule: id =" + ruleQueryCondition.getRid() + ".");
\r
60 throw new CorrelationException("Failed to query the rule.", e);
\r
62 dbDaoUtil.close(handle);
\r
66 private CorrelationRule getCorrelationRule(Map value) {
\r
67 CorrelationRule correlationRule = new CorrelationRule();
\r
68 correlationRule.setName((String) value.get("name"));
\r
69 correlationRule.setRid((String) value.get("rid"));
\r
70 correlationRule.setDescription((String) value.get("description"));
\r
71 correlationRule.setEnabled((Integer) value.get("enable"));
\r
72 correlationRule.setTemplateID((Long) value.get("templateID"));
\r
73 correlationRule.setEngineID((String) value.get("engineID"));
\r
74 correlationRule.setEngineType((String) value.get("engineType"));
\r
75 correlationRule.setCreator((String) value.get("creator"));
\r
76 correlationRule.setCreateTime((Date) value.get("createTime"));
\r
77 correlationRule.setModifier((String) value.get("updator"));
\r
78 correlationRule.setUpdateTime((Date) value.get("updateTime"));
\r
79 correlationRule.setParams((Properties) value.get("params"));
\r
80 correlationRule.setContent((String) value.get("content"));
\r
81 correlationRule.setVendor((String) value.get("vendor"));
\r
82 correlationRule.setPackageName((String) value.get("package"));
\r
83 correlationRule.setClosedControlLoopName((String) value.get("ctrlloop"));
\r
84 return correlationRule;
\r
87 private String getWhereStrByRequestEntity(RuleQueryCondition ruleQueryCondition) throws CorrelationException {
\r
89 Class clazz = ruleQueryCondition.getClass();
\r
90 Field[] fields = clazz.getDeclaredFields();
\r
91 String whereSql = " WHERE ";
\r
93 for (Field field : fields) {
\r
94 // Jacoco will cause an exception when calculating the coverage of the UT
\r
95 // Remove this if jacoco solves this problem in the future
\r
96 if (field.getName().contains("jacoco")) {
\r
99 PropertyDescriptor pd = new PropertyDescriptor(field.getName(),
\r
101 Method getMethod = pd.getReadMethod();
\r
102 Object o = getMethod.invoke(ruleQueryCondition);
\r
104 String tempName = field.getName();
\r
105 if ("enabled".equals(tempName)) {
\r
106 int enabled = (int) o;
\r
107 if (enabled != RuleMgtConstant.STATUS_RULE_ALL) {
\r
108 whereSql = whereSql + "enable =" + enabled;
\r
109 whereSql += " AND ";
\r
111 } else if ("name".equals(tempName)) {
\r
112 if (!"".equals(o.toString().trim())) {
\r
113 whereSql = whereSql + field.getName() + " like '%" + o + "%' AND ";
\r
115 } else if (!"".equals(o.toString().trim())) {
\r
116 whereSql = whereSql + field.getName() + "='" + o + "' AND ";
\r
120 whereSql = whereSql.trim();
\r
121 if(!"WHERE".equals(whereSql)){
\r
122 return whereSql.substring(0, whereSql.length() - "AND".length());
\r
125 } catch (Exception e) {
\r
126 throw new CorrelationException("An error occurred while building the query SQL.", e);
\r