Fixed a logging problem
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / db / AlarmInfoDaoService.java
1 /**
2  * Copyright 2021-2022 ZTE Corporation.
3  * <p>
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  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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 package org.onap.holmes.engine.db;
17
18 import org.onap.holmes.common.api.entity.AlarmInfo;
19 import org.onap.holmes.common.exception.AlarmInfoException;
20 import org.onap.holmes.engine.db.jdbi.AlarmInfoDao;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.stereotype.Service;
23
24 import java.util.List;
25
26 @Service
27
28 public class AlarmInfoDaoService {
29
30     @Autowired
31     private AlarmInfoDao alarmInfoDao;
32
33     public AlarmInfo saveAlarm(AlarmInfo alarmInfo) throws AlarmInfoException {
34         try {
35             alarmInfoDao.addAlarm(alarmInfo);
36             return alarmInfo;
37         } catch (Exception e) {
38             throw new AlarmInfoException("Can not access the database. Please contact the administrator for help.", e);
39         }
40     }
41
42     public List<AlarmInfo> queryAllAlarm() throws AlarmInfoException {
43         try {
44             return alarmInfoDao.queryAlarm();
45         } catch (Exception e) {
46             throw new AlarmInfoException("Can not access the database. Please contact the administrator for help.", e);
47         }
48     }
49
50     public void deleteAlarm(AlarmInfo alarmInfo) {
51         if (alarmInfo.getAlarmIsCleared() != 1) {
52             return;
53         }
54
55         String sourceId = alarmInfo.getSourceId();
56         String sourceName = alarmInfo.getSourceName();
57         String eventName = alarmInfo.getEventName();
58         eventName = eventName.substring(0, eventName.lastIndexOf("Cleared"));
59
60         alarmInfoDao.deleteAlarmByAlarmIsCleared(eventName, sourceId, sourceName);
61     }
62 }