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