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