Migrate from DW to Springboot
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / db / CorrelationRuleService.java
1 /**
2  * Copyright 2021-2022 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.holmes.rulemgt.db;
18
19 import org.apache.commons.lang3.StringUtils;
20 import org.onap.holmes.common.api.entity.CorrelationRule;
21 import org.onap.holmes.common.exception.CorrelationException;
22 import org.onap.holmes.rulemgt.db.jdbi.CorrelationRuleDao;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.stereotype.Service;
25
26 import java.util.List;
27
28 @Service
29 public class CorrelationRuleService {
30
31     @Autowired
32     private CorrelationRuleDao correlationRuleDao;
33
34     public List<CorrelationRule> queryRuleByRuleEngineInstance(String enginetype) {
35         return correlationRuleDao.queryRuleByEngineInstance(enginetype);
36     }
37
38     public List<CorrelationRule> queryRuleByRuleEnable(int enable) {
39         return correlationRuleDao.queryRuleByEnable(enable);
40     }
41
42
43     private void deleteRule2DbInner(CorrelationRule correlationRule) {
44         String name = correlationRule.getName() != null ? correlationRule.getName().trim() : "";
45         String rid = correlationRule.getRid() != null ? correlationRule.getRid().trim() : "";
46         if (!StringUtils.EMPTY.equals(name) && !StringUtils.EMPTY.equals(rid)) {
47             correlationRuleDao.deleteRuleByRidAndName(rid, name);
48         } else if (!"".equals(rid)) {
49             correlationRuleDao.deleteRuleByRid(rid);
50         }
51     }
52
53     public CorrelationRule saveRule(CorrelationRule correlationRule) throws CorrelationException {
54         try {
55             correlationRuleDao.addRule(correlationRule);
56             return correlationRule;
57         } catch (Exception e) {
58             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);
59         }
60     }
61
62     public void updateRule(CorrelationRule correlationRule) throws CorrelationException {
63         try {
64             correlationRuleDao.updateRuleByRid(correlationRule);
65         } catch (Exception e) {
66             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);
67         }
68     }
69
70     public void deleteRule(CorrelationRule correlationRule) throws CorrelationException {
71         try {
72             deleteRule2DbInner(correlationRule);
73         } catch (Exception e) {
74             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);
75         }
76     }
77
78
79     public CorrelationRule queryRuleByRid(String rid) throws CorrelationException {
80         try {
81             return correlationRuleDao.queryRuleById(rid);
82         } catch (Exception e) {
83             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);
84         }
85     }
86
87     public CorrelationRule queryRuleByRuleName(String name) throws CorrelationException {
88         try {
89             return correlationRuleDao.queryRuleByName(name);
90         } catch (Exception e) {
91             throw new CorrelationException("Can not access the database. Please contact the administrator for help.", e);
92         }
93     }
94 }