Fixed MSB Invocation Issues
[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 \r
20 import org.jvnet.hk2.annotations.Service;\r
21 import org.onap.holmes.common.api.entity.CorrelationRule;\r
22 import org.onap.holmes.common.exception.CorrelationException;\r
23 import org.onap.holmes.common.utils.CorrelationRuleMapper;\r
24 import org.skife.jdbi.v2.sqlobject.Bind;\r
25 import org.skife.jdbi.v2.sqlobject.BindBean;\r
26 import org.skife.jdbi.v2.sqlobject.GetGeneratedKeys;\r
27 import org.skife.jdbi.v2.sqlobject.SqlQuery;\r
28 import org.skife.jdbi.v2.sqlobject.SqlUpdate;\r
29 import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;\r
30 \r
31 @Service\r
32 @RegisterMapper(CorrelationRuleMapper.class)\r
33 public abstract class CorrelationRuleDao {\r
34 \r
35     @GetGeneratedKeys\r
36     @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
37     protected abstract String addRule(@BindBean CorrelationRule correlationRule);\r
38 \r
39     @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
40     protected abstract int updateRuleByRid(@BindBean CorrelationRule correlationRule);\r
41 \r
42     @SqlUpdate("DELETE FROM APLUS_RULE WHERE RID=:rid")\r
43     protected abstract int deleteRuleByRid(@Bind("rid") String rid);\r
44 \r
45     @SqlUpdate("DELETE FROM APLUS_RULE WHERE RID=:rid AND NAME=:name")\r
46     protected abstract int deleteRuleByRidAndName(@Bind("rid") String rid, @Bind("name") String name);\r
47 \r
48     @SqlQuery("SELECT * FROM APLUS_RULE")\r
49     protected abstract List<CorrelationRule> queryAllRules();\r
50 \r
51     @SqlQuery("SELECT * FROM APLUS_RULE WHERE RID=:rid")\r
52     protected abstract CorrelationRule queryRuleById(@Bind("rid") String rid);\r
53 \r
54     @SqlQuery("SELECT * FROM APLUS_RULE WHERE NAME=:name")\r
55     protected abstract CorrelationRule queryRuleByName(@Bind("name") String name);\r
56 \r
57     @SqlQuery("SELECT * FROM APLUS_RULE WHERE enable=:enable")\r
58     public abstract List<CorrelationRule> queryRuleByEnable(@Bind("enable") int enable);\r
59 \r
60     @SqlQuery("SELECT * FROM APLUS_RULE WHERE engineinstance=:engineinstance")\r
61     public abstract List<CorrelationRule> queryRuleByEngineInstance(@Bind("engineinstance") String engineinstance);\r
62 \r
63     public List<CorrelationRule> queryRuleByRuleEngineInstance(String enginetype) {\r
64         return queryRuleByEngineInstance(enginetype);\r
65     }\r
66 \r
67     public List<CorrelationRule> queryRuleByRuleEnable(int enable) {\r
68         return queryRuleByEnable(enable);\r
69     }\r
70 \r
71 \r
72     private void deleteRule2DbInner(CorrelationRule correlationRule) {\r
73         String name = correlationRule.getName() != null ? correlationRule.getName().trim() : "";\r
74         String rid = correlationRule.getRid() != null ? correlationRule.getRid().trim() : "";\r
75         if (!"".equals(name) && !"".equals(rid)) {\r
76             deleteRuleByRidAndName(rid, name);\r
77         } else if (!"".equals(rid)) {\r
78             deleteRuleByRid(rid);\r
79         }\r
80     }\r
81 \r
82     public CorrelationRule saveRule(CorrelationRule correlationRule) throws CorrelationException {\r
83         try {\r
84             addRule(correlationRule);\r
85             return correlationRule;\r
86         } catch (Exception e) {\r
87             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
88         }\r
89     }\r
90 \r
91     public void updateRule(CorrelationRule correlationRule) throws CorrelationException {\r
92         try {\r
93             updateRuleByRid(correlationRule);\r
94         } catch (Exception e) {\r
95             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
96         }\r
97     }\r
98 \r
99     public void deleteRule(CorrelationRule correlationRule) throws CorrelationException {\r
100         try {\r
101             deleteRule2DbInner(correlationRule);\r
102         } catch (Exception e) {\r
103             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
104         }\r
105     }\r
106 \r
107 \r
108     public CorrelationRule queryRuleByRid(String rid) throws CorrelationException {\r
109         try {\r
110             return queryRuleById(rid);\r
111         } catch (Exception e) {\r
112             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
113         }\r
114     }\r
115 \r
116     public CorrelationRule queryRuleByRuleName(String name) throws CorrelationException {\r
117         try {\r
118             return queryRuleByName(name);\r
119         } catch (Exception e) {\r
120             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
121         }\r
122     }\r
123 }\r
124 \r