af7a2e9a1243166fc1f8954a02cf0b9d041444f2
[usecase-ui/intent-analysis.git] /
1 /*
2  * Copyright 2022 Huawei Technologies Co., Ltd.
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.usecaseui.intentanalysis.service.impl;
18
19
20 import org.onap.usecaseui.intentanalysis.bean.models.Expectation;
21 import org.onap.usecaseui.intentanalysis.bean.models.State;
22 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
23 import org.onap.usecaseui.intentanalysis.mapper.ExpectationMapper;
24 import org.onap.usecaseui.intentanalysis.service.ExpectationService;
25 import org.onap.usecaseui.intentanalysis.service.StateService;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.stereotype.Service;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
31
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36 @Service
37 public class ExpectationServiceImpl implements ExpectationService {
38
39     private static Logger LOGGER = LoggerFactory.getLogger(ExpectationServiceImpl.class);
40     @Autowired
41     private ExpectationMapper expectationMapper;
42
43     @Autowired
44     private StateService stateService;
45
46     @Autowired
47     private ExpectationService expectationService;
48
49     @Override
50     public void createExpectationList(List<Expectation> expectationList, String intentId) {
51         for (Expectation expectation : expectationList) {
52             if (null != expectation) {
53                 stateService.createStateList(expectation.getStateList(), expectation.getExpectationId());
54             }
55         }
56         int res = expectationMapper.insertExpectationList(expectationList, intentId);
57         if (res < 1) {
58             String msg = "Create expectation to database failed.";
59             LOGGER.error(msg);
60             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
61         }
62     }
63
64     @Override
65     public List<Expectation> getExpectationListByIntentId(String intentId) {
66         List<Expectation> expectationList = expectationMapper.selectExpectationByIntentId(intentId);
67         if (expectationList == null) {
68             String msg = String.format("Intent id %s doesn't exist in database.", intentId);
69             LOGGER.error(msg);
70             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
71         }
72         for (Expectation expectation : expectationList) {
73             List<State> stateList = stateService.getStateListByExpectationId(expectation.getExpectationId());
74             expectation.setStateList(stateList);
75         }
76         return expectationList;
77     }
78
79     @Override
80     public void deleteExpectationListById(String intentId) {
81         List<Expectation> expectationList = expectationMapper.selectExpectationByIntentId(intentId);
82         if (expectationList == null) {
83             String msg = String.format("Intent id %s doesn't exist in database.", intentId);
84             LOGGER.error(msg);
85             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
86         }
87         int res = expectationMapper.deleteExpectationByIntentId(intentId);
88         if (res < 1) {
89             String msg = "Delete expectation in database failed.";
90             LOGGER.error(msg);
91             throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
92         }
93         for (Expectation expectation : expectationList) {
94             stateService.deleteStateListByExpectationId(expectation.getExpectationId());
95         }
96     }
97
98     @Override
99     public void updateExpectationListById(List<Expectation> expectationList, String intentId) {
100         List<Expectation> expectationDBList = expectationMapper.selectExpectationByIntentId(intentId);
101         if (expectationDBList == null) {
102             String msg = String.format("Intent id %s doesn't exist in database.", intentId);
103             LOGGER.error(msg);
104             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
105         }
106         List<String> expectationDBIdList = new ArrayList<>();
107         for (Expectation expectationDB : expectationDBList) {
108             expectationDBIdList.add(expectationDB.getExpectationId());
109         }
110
111         for (Expectation expectation : expectationList) {
112             if (expectationDBIdList.contains(expectation.getExpectationId())) {
113                 stateService.updateStateListByExpectationId(expectation.getStateList(), expectation.getExpectationId());
114                 int res = expectationMapper.updateExpectation(expectation);
115                 if (res < 1) {
116                     String msg = "Update expectation in database failed.";
117                     LOGGER.error(msg);
118                     throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
119                 }
120                 expectationDBIdList.remove(expectation.getExpectationId());
121             } else {
122                 expectationService.insertExpectation(expectation, intentId);
123             }
124         }
125         for (String expectationDBId : expectationDBIdList) {
126             expectationService.deleteExpectationById(expectationDBId);
127         }
128         LOGGER.info("Expectations are successfully updated.");
129     }
130
131     @Override
132     public void insertExpectation(Expectation expectation, String intentId) {
133         int res = expectationMapper.insertExpectation(expectation, intentId);
134         if (res < 1) {
135             String msg = "Create expectation to database failed.";
136             LOGGER.error(msg);
137             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
138         }
139     }
140
141     @Override
142     public void deleteExpectationById(String expectationId) {
143         int res = expectationMapper.deleteExpectationById(expectationId);
144         if (res < 1) {
145             String msg = "Delete expectation in database failed.";
146             LOGGER.error(msg);
147             throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
148         }
149     }
150 }