Fix in rule management the sonar detected error
[holmes/rule-management.git] / rulemgt / src / main / java / org / openo / holmes / rulemgt / db / CorrelationRuleQueryDao.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 package org.openo.holmes.rulemgt.db;\r
17 \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.openo.holmes.common.api.entity.CorrelationRule;\r
30 import org.openo.holmes.common.exception.CorrelationException;\r
31 import org.openo.holmes.common.utils.DbDaoUtil;\r
32 import org.openo.holmes.common.utils.I18nProxy;\r
33 import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition;\r
34 import org.openo.holmes.rulemgt.constant.RuleMgtConstant;\r
35 import org.skife.jdbi.v2.Handle;\r
36 import org.skife.jdbi.v2.Query;\r
37 \r
38 @Service\r
39 @Slf4j\r
40 public class CorrelationRuleQueryDao {\r
41 \r
42     @Inject\r
43     private DbDaoUtil dbDaoUtil;\r
44 \r
45     public List<CorrelationRule> getCorrelationRulesByCondition(RuleQueryCondition ruleQueryCondition)\r
46             throws CorrelationException {\r
47         List<CorrelationRule> correlationRules = new ArrayList<CorrelationRule>();\r
48         Handle handle = null;\r
49         String whereStr = getWhereStrByRequestEntity(ruleQueryCondition);\r
50         try {\r
51             StringBuilder querySql = new StringBuilder("SELECT * FROM APLUS_RULE ").append(whereStr);\r
52             handle = dbDaoUtil.getHandle();\r
53             Query query = handle.createQuery(querySql.toString());\r
54             for (Object value : query.list()) {\r
55                 CorrelationRule correlationRule = getCorrelationRule((Map) value);\r
56                 correlationRules.add(correlationRule);\r
57             }\r
58             return correlationRules;\r
59         } catch (Exception e) {\r
60             log.warn("Query rule: rule id =" + ruleQueryCondition.getRid() + " failed");\r
61             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED, e);\r
62         } finally {\r
63             dbDaoUtil.close(handle);\r
64         }\r
65     }\r
66 \r
67     private CorrelationRule getCorrelationRule(Map value) {\r
68         CorrelationRule correlationRule = new CorrelationRule();\r
69         correlationRule.setName((String) value.get("name"));\r
70         correlationRule.setRid((String) value.get("rid"));\r
71         correlationRule.setDescription((String) value.get("description"));\r
72         correlationRule.setEnabled((Integer) value.get("enable"));\r
73         correlationRule.setTemplateID((Integer) value.get("templateID"));\r
74         correlationRule.setEngineID((String) value.get("engineID"));\r
75         correlationRule.setEngineType((String) value.get("engineType"));\r
76         correlationRule.setCreator((String) value.get("creator"));\r
77         correlationRule.setCreateTime((Date) value.get("createTime"));\r
78         correlationRule.setModifier((String) value.get("updator"));\r
79         correlationRule.setUpdateTime((Date) value.get("updateTime"));\r
80         correlationRule.setParams((Properties) value.get("params"));\r
81         correlationRule.setContent((String) value.get("content"));\r
82         correlationRule.setVendor((String) value.get("vendor"));\r
83         correlationRule.setPackageName((String) value.get("package"));\r
84         return correlationRule;\r
85     }\r
86 \r
87     private String getWhereStrByRequestEntity(RuleQueryCondition ruleQueryCondition) throws CorrelationException {\r
88         try {\r
89             Class clazz = ruleQueryCondition.getClass();\r
90             Field[] fields = clazz.getDeclaredFields();\r
91             String whereSql = " WHERE ";\r
92 \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
97                     continue;\r
98                 }\r
99                 PropertyDescriptor pd = new PropertyDescriptor(field.getName(),\r
100                         clazz);\r
101                 Method getMethod = pd.getReadMethod();\r
102                 Object o = getMethod.invoke(ruleQueryCondition);\r
103                 if (o != null) {\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
110                         }\r
111                     } else if ("name".equals(tempName)) {\r
112                         if (!"".equals(o.toString().trim())) {\r
113                             whereSql = whereSql + field.getName() + "  like '%" + o + "%'  AND ";\r
114                         }\r
115                     } else if (!"".equals(o.toString().trim())) {\r
116                         whereSql = whereSql + field.getName() + "='" + o + "'  AND ";\r
117                     }\r
118                 }\r
119             }\r
120             whereSql = whereSql.trim();\r
121             return whereSql.substring(0, whereSql.length() - "AND".length());\r
122         } catch (Exception e) {\r
123             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED, e);\r
124         }\r
125     }\r
126 }\r