Change the groupid from openo to onap
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / db / CorrelationRuleDao.java
diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleDao.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleDao.java
new file mode 100644 (file)
index 0000000..2a422c4
--- /dev/null
@@ -0,0 +1,106 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.onap.holmes.rulemgt.db;\r
+\r
+import java.util.List;\r
+import org.onap.holmes.common.api.entity.CorrelationRule;\r
+import org.onap.holmes.common.exception.CorrelationException;\r
+import org.onap.holmes.rulemgt.db.mapper.CorrelationRuleMapper;\r
+import org.skife.jdbi.v2.sqlobject.Bind;\r
+import org.skife.jdbi.v2.sqlobject.BindBean;\r
+import org.skife.jdbi.v2.sqlobject.GetGeneratedKeys;\r
+import org.skife.jdbi.v2.sqlobject.SqlQuery;\r
+import org.skife.jdbi.v2.sqlobject.SqlUpdate;\r
+import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;\r
+\r
+@RegisterMapper(CorrelationRuleMapper.class)\r
+public abstract class CorrelationRuleDao {\r
+\r
+    @GetGeneratedKeys\r
+    @SqlUpdate("INSERT INTO APLUS_RULE  (NAME,DESCRIPTION,ENABLE,TEMPLATEID,ENGINETYPE,CREATOR,UPDATOR,PARAMS,CONTENT ,VENDOR,CREATETIME,UPDATETIME,ENGINEID,PACKAGE,RID) VALUES (:name,:description,:enabled,:templateID,:engineType,:creator,:modifier,:params,:content,:vendor,:createTime,:updateTime,:engineID,:packageName,:rid)")\r
+    protected abstract int addRule(@BindBean CorrelationRule correlationRule);\r
+\r
+    @SqlUpdate("UPDATE APLUS_RULE SET DESCRIPTION=:description,ENABLE=:enabled,CONTENT=:content,UPDATOR=:modifier,UPDATETIME=:updateTime WHERE RID=:rid")\r
+    protected abstract int updateRuleByRid(@BindBean CorrelationRule correlationRule);\r
+\r
+    @SqlUpdate("DELETE FROM APLUS_RULE WHERE RID=:rid")\r
+    protected abstract int deleteRuleByRid(@Bind("rid") String rid);\r
+\r
+    @SqlUpdate("DELETE FROM APLUS_RULE WHERE RID=:rid AND NAME=:name")\r
+    protected abstract int deleteRuleByRidAndName(@Bind("rid") String rid, @Bind("name") String name);\r
+\r
+    @SqlQuery("SELECT * FROM APLUS_RULE")\r
+    protected abstract List<CorrelationRule> queryAllRules();\r
+\r
+    @SqlQuery("SELECT * FROM APLUS_RULE WHERE RID=:rid")\r
+    protected abstract CorrelationRule queryRuleById(@Bind("rid") String rid);\r
+\r
+    @SqlQuery("SELECT * FROM APLUS_RULE WHERE NAME=:name")\r
+    protected abstract CorrelationRule queryRuleByName(@Bind("name") String name);\r
+\r
+    private void deleteRule2DbInner(CorrelationRule correlationRule) {\r
+        String name = correlationRule.getName() != null ? correlationRule.getName().trim() : "";\r
+        String rid = correlationRule.getRid() != null ? correlationRule.getRid().trim() : "";\r
+        if (!"".equals(name) && !"".equals(rid)) {\r
+            deleteRuleByRidAndName(rid, name);\r
+        } else if (!"".equals(rid)) {\r
+            deleteRuleByRid(rid);\r
+        }\r
+    }\r
+\r
+    public CorrelationRule saveRule(CorrelationRule correlationRule) throws CorrelationException {\r
+        try {\r
+            addRule(correlationRule);\r
+            return correlationRule;\r
+        } catch (Exception e) {\r
+            throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
+        }\r
+    }\r
+\r
+    public void updateRule(CorrelationRule correlationRule) throws CorrelationException {\r
+        try {\r
+            updateRuleByRid(correlationRule);\r
+        } catch (Exception e) {\r
+            throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
+        }\r
+    }\r
+\r
+    public void deleteRule(CorrelationRule correlationRule) throws CorrelationException {\r
+        try {\r
+            deleteRule2DbInner(correlationRule);\r
+        } catch (Exception e) {\r
+            throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
+        }\r
+    }\r
+\r
+\r
+    public CorrelationRule queryRuleByRid(String rid) throws CorrelationException {\r
+        try {\r
+            return queryRuleById(rid);\r
+        } catch (Exception e) {\r
+            throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
+        }\r
+    }\r
+\r
+    public CorrelationRule queryRuleByRuleName(String name) throws CorrelationException {\r
+        try {\r
+            return queryRuleByName(name);\r
+        } catch (Exception e) {\r
+            throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);\r
+        }\r
+    }\r
+}\r
+\r