64dcea23eea240ec39b76ad0f739c57ff511fb2c
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / db / CorrelationRuleDao.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.util.List;\r
19 import org.onap.holmes.common.api.entity.CorrelationRule;\r
20 import org.onap.holmes.common.exception.CorrelationException;\r
21 import org.onap.holmes.common.utils.CorrelationRuleMapper;\r
22 import org.skife.jdbi.v2.sqlobject.Bind;\r
23 import org.skife.jdbi.v2.sqlobject.BindBean;\r
24 import org.skife.jdbi.v2.sqlobject.GetGeneratedKeys;\r
25 import org.skife.jdbi.v2.sqlobject.SqlQuery;\r
26 import org.skife.jdbi.v2.sqlobject.SqlUpdate;\r
27 import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;\r
28 \r
29 @RegisterMapper(CorrelationRuleMapper.class)\r
30 public abstract class CorrelationRuleDao {\r
31 \r
32     @GetGeneratedKeys\r
33     @SqlUpdate("INSERT INTO APLUS_RULE  (NAME,CTRLLOOP,DESCRIPTION,ENABLE,TEMPLATEID,ENGINETYPE,CREATOR,UPDATOR,PARAMS,CONTENT ,VENDOR,CREATETIME,UPDATETIME,ENGINEID,PACKAGE,RID, ENGINEINSTANCE) VALUES (:name,:closedControlLoopName,:description,:enabled,:templateID,:engineType,:creator,:modifier,:params,:content,:vendor,:createTime,:updateTime,:engineID,:packageName,:rid,:engineInstance)")\r
34     protected abstract String addRule(@BindBean CorrelationRule correlationRule);\r
35 \r
36     @SqlUpdate("UPDATE APLUS_RULE SET CTRLLOOP=:closedControlLoopName,DESCRIPTION=:description,ENABLE=:enabled,CONTENT=:content,UPDATOR=:modifier,UPDATETIME=:updateTime, PACKAGE=:packageName, ENGINEINSTANCE=:engineInstance WHERE RID=:rid")\r
37     protected abstract int updateRuleByRid(@BindBean CorrelationRule correlationRule);\r
38 \r
39     @SqlUpdate("DELETE FROM APLUS_RULE WHERE RID=:rid")\r
40     protected abstract int deleteRuleByRid(@Bind("rid") String rid);\r
41 \r
42     @SqlUpdate("DELETE FROM APLUS_RULE WHERE RID=:rid AND NAME=:name")\r
43     protected abstract int deleteRuleByRidAndName(@Bind("rid") String rid, @Bind("name") String name);\r
44 \r
45     @SqlQuery("SELECT * FROM APLUS_RULE")\r
46     protected abstract List<CorrelationRule> queryAllRules();\r
47 \r
48     @SqlQuery("SELECT * FROM APLUS_RULE WHERE RID=:rid")\r
49     protected abstract CorrelationRule queryRuleById(@Bind("rid") String rid);\r
50 \r
51     @SqlQuery("SELECT * FROM APLUS_RULE WHERE NAME=:name")\r
52     protected abstract CorrelationRule queryRuleByName(@Bind("name") String name);\r
53 \r
54     @SqlQuery("SELECT * FROM APLUS_RULE WHERE enable=:enable")\r
55     public abstract List<CorrelationRule> queryRuleByEnable(@Bind("enable") int enable);\r
56 \r
57     @SqlQuery("SELECT * FROM APLUS_RULE WHERE engineinstance=:engineinstance")\r
58     public abstract List<CorrelationRule> queryRuleByEngineInstance(@Bind("engineinstance") String engineinstance);\r
59 \r
60     public List<CorrelationRule> queryRuleByRuleEngineInstance(String enginetype) {\r
61         return queryRuleByEngineInstance(enginetype);\r
62     }\r
63 \r
64     public List<CorrelationRule> queryRuleByRuleEnable(int enable) {\r
65         return queryRuleByEnable(enable);\r
66     }\r
67 \r
68 \r
69     private void deleteRule2DbInner(CorrelationRule correlationRule) {\r
70         String name = correlationRule.getName() != null ? correlationRule.getName().trim() : "";\r
71         String rid = correlationRule.getRid() != null ? correlationRule.getRid().trim() : "";\r
72         if (!"".equals(name) && !"".equals(rid)) {\r
73             deleteRuleByRidAndName(rid, name);\r
74         } else if (!"".equals(rid)) {\r
75             deleteRuleByRid(rid);\r
76         }\r
77     }\r
78 \r
79     public CorrelationRule saveRule(CorrelationRule correlationRule) throws CorrelationException {\r
80         try {\r
81             addRule(correlationRule);\r
82             return correlationRule;\r
83         } catch (Exception e) {\r
84             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
85         }\r
86     }\r
87 \r
88     public void updateRule(CorrelationRule correlationRule) throws CorrelationException {\r
89         try {\r
90             updateRuleByRid(correlationRule);\r
91         } catch (Exception e) {\r
92             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
93         }\r
94     }\r
95 \r
96     public void deleteRule(CorrelationRule correlationRule) throws CorrelationException {\r
97         try {\r
98             deleteRule2DbInner(correlationRule);\r
99         } catch (Exception e) {\r
100             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
101         }\r
102     }\r
103 \r
104 \r
105     public CorrelationRule queryRuleByRid(String rid) throws CorrelationException {\r
106         try {\r
107             return queryRuleById(rid);\r
108         } catch (Exception e) {\r
109             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
110         }\r
111     }\r
112 \r
113     public CorrelationRule queryRuleByRuleName(String name) throws CorrelationException {\r
114         try {\r
115             return queryRuleByName(name);\r
116         } catch (Exception e) {\r
117             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
118         }\r
119     }\r
120 }\r
121 \r