Migrate from DW to Springboot
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / db / CorrelationRuleQueryService.java
1 /**\r
2  * Copyright 2017-2021 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.onap.holmes.rulemgt.db;\r
17 \r
18 import lombok.extern.slf4j.Slf4j;\r
19 import org.jdbi.v3.core.Handle;\r
20 import org.jdbi.v3.core.Jdbi;\r
21 import org.jdbi.v3.core.statement.Query;\r
22 import org.onap.holmes.common.api.entity.CorrelationRule;\r
23 import org.onap.holmes.common.exception.CorrelationException;\r
24 import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;\r
25 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
26 import org.springframework.beans.factory.annotation.Autowired;\r
27 import org.springframework.stereotype.Service;\r
28 \r
29 import java.beans.PropertyDescriptor;\r
30 import java.lang.reflect.Field;\r
31 import java.lang.reflect.Method;\r
32 import java.util.*;\r
33 \r
34 @Service\r
35 @Slf4j\r
36 public class CorrelationRuleQueryService {\r
37 \r
38     @Autowired\r
39     private Jdbi jdbi;\r
40 \r
41     public List<CorrelationRule> getCorrelationRulesByCondition(RuleQueryCondition ruleQueryCondition)\r
42             throws CorrelationException {\r
43         List<CorrelationRule> correlationRules = new ArrayList<CorrelationRule>();\r
44         Handle handle = null;\r
45         String whereStr = getWhereStrByRequestEntity(ruleQueryCondition);\r
46         try {\r
47             StringBuilder querySql = new StringBuilder("SELECT * FROM APLUS_RULE ").append(whereStr);\r
48             handle = jdbi.open();\r
49             Query query = handle.createQuery(querySql.toString());\r
50             for (Object value : query.mapToMap().list()) {\r
51                 CorrelationRule correlationRule = getCorrelationRule((Map) value);\r
52                 correlationRules.add(correlationRule);\r
53             }\r
54             return correlationRules;\r
55         } catch (Exception e) {\r
56             log.warn("Failed to query the rule: id =" + ruleQueryCondition.getRid() + ".");\r
57             throw new CorrelationException("Failed to query the rule.", e);\r
58         } finally {\r
59             handle.close();\r
60         }\r
61     }\r
62 \r
63     private CorrelationRule getCorrelationRule(Map value) {\r
64         CorrelationRule correlationRule = new CorrelationRule();\r
65         correlationRule.setName((String) value.get("name"));\r
66         correlationRule.setRid((String) value.get("rid"));\r
67         correlationRule.setDescription((String) value.get("description"));\r
68         correlationRule.setEnabled((Integer) value.get("enable"));\r
69         correlationRule.setTemplateID((Long) value.get("templateid"));\r
70         correlationRule.setEngineID((String) value.get("engineid"));\r
71         correlationRule.setEngineType((String) value.get("enginetype"));\r
72         correlationRule.setCreator((String) value.get("creator"));\r
73         correlationRule.setCreateTime((Date) value.get("createtime"));\r
74         correlationRule.setModifier((String) value.get("updator"));\r
75         correlationRule.setUpdateTime((Date) value.get("updatetime"));\r
76         correlationRule.setParams((Properties) value.get("params"));\r
77         correlationRule.setContent((String) value.get("content"));\r
78         correlationRule.setVendor((String) value.get("vendor"));\r
79         correlationRule.setPackageName((String) value.get("package"));\r
80         correlationRule.setClosedControlLoopName((String) value.get("ctrlloop"));\r
81         correlationRule.setEngineInstance((String) value.get("engineinstance"));\r
82         return correlationRule;\r
83     }\r
84 \r
85     private String getWhereStrByRequestEntity(RuleQueryCondition ruleQueryCondition) throws CorrelationException {\r
86         try {\r
87             Class clazz = ruleQueryCondition.getClass();\r
88             Field[] fields = clazz.getDeclaredFields();\r
89             String whereSql = " WHERE ";\r
90 \r
91             for (Field field : fields) {\r
92                 // Jacoco will cause an exception when calculating the coverage of the UT\r
93                 // Remove this if jacoco solves this problem in the future\r
94                 if (field.getName().contains("jacoco")) {\r
95                     continue;\r
96                 }\r
97                 PropertyDescriptor pd = new PropertyDescriptor((String)field.getName(),\r
98                         clazz);\r
99                 Method getMethod = pd.getReadMethod();\r
100                 Object o = getMethod.invoke(ruleQueryCondition);\r
101                 if (o != null) {\r
102                     String tempName = field.getName();\r
103                     if ("enabled".equals(tempName)) {\r
104                         int enabled = (int) o;\r
105                         if (enabled != RuleMgtConstant.STATUS_RULE_ALL) {\r
106                             whereSql = whereSql + "enable =" + enabled;\r
107                             whereSql += " AND ";\r
108                         }\r
109                     } else if ("name".equals(tempName)) {\r
110                         if (!"".equals(o.toString().trim())) {\r
111                             whereSql = whereSql + field.getName() + "  like '%" + o + "%'  AND ";\r
112                         }\r
113                     } else if (!"".equals(o.toString().trim())) {\r
114                         whereSql = whereSql + field.getName() + "='" + o + "'  AND ";\r
115                     }\r
116                 }\r
117             }\r
118             whereSql = whereSql.trim();\r
119             if(!"WHERE".equals(whereSql)){\r
120                 return whereSql.substring(0, whereSql.length() - "AND".length());\r
121             }\r
122             return "";\r
123         } catch (Exception e) {\r
124             throw new CorrelationException("An error occurred while building the query SQL.", e);\r
125         }\r
126     }\r
127 }\r