Updated to Java 11
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / 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.onap.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.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
36 \r
37 @Service\r
38 @Slf4j\r
39 public class CorrelationRuleQueryDao {\r
40 \r
41     @Inject\r
42     private DbDaoUtil dbDaoUtil;\r
43 \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
49         try {\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
56             }\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
61         } finally {\r
62             dbDaoUtil.close(handle);\r
63         }\r
64     }\r
65 \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         correlationRule.setEngineInstance((String) value.get("engineInstance"));\r
85         return correlationRule;\r
86     }\r
87 \r
88     private String getWhereStrByRequestEntity(RuleQueryCondition ruleQueryCondition) throws CorrelationException {\r
89         try {\r
90             Class clazz = ruleQueryCondition.getClass();\r
91             Field[] fields = clazz.getDeclaredFields();\r
92             String whereSql = " WHERE ";\r
93 \r
94             for (Field field : fields) {\r
95                 // Jacoco will cause an exception when calculating the coverage of the UT\r
96                 // Remove this if jacoco solves this problem in the future\r
97                 if (field.getName().contains("jacoco")) {\r
98                     continue;\r
99                 }\r
100                 PropertyDescriptor pd = new PropertyDescriptor((String)field.getName(),\r
101                         clazz);\r
102                 Method getMethod = pd.getReadMethod();\r
103                 Object o = getMethod.invoke(ruleQueryCondition);\r
104                 if (o != null) {\r
105                     String tempName = field.getName();\r
106                     if ("enabled".equals(tempName)) {\r
107                         int enabled = (int) o;\r
108                         if (enabled != RuleMgtConstant.STATUS_RULE_ALL) {\r
109                             whereSql = whereSql + "enable =" + enabled;\r
110                             whereSql += " AND ";\r
111                         }\r
112                     } else if ("name".equals(tempName)) {\r
113                         if (!"".equals(o.toString().trim())) {\r
114                             whereSql = whereSql + field.getName() + "  like '%" + o + "%'  AND ";\r
115                         }\r
116                     } else if (!"".equals(o.toString().trim())) {\r
117                         whereSql = whereSql + field.getName() + "='" + o + "'  AND ";\r
118                     }\r
119                 }\r
120             }\r
121             whereSql = whereSql.trim();\r
122             if(!"WHERE".equals(whereSql)){\r
123                 return whereSql.substring(0, whereSql.length() - "AND".length());\r
124             }\r
125             return "";\r
126         } catch (Exception e) {\r
127             throw new CorrelationException("An error occurred while building the query SQL.", e);\r
128         }\r
129     }\r
130 }\r