5a792132d11f114ad748b962d8de693af5e4678d
[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.mapper.ExpectationMapper;
23 import org.onap.usecaseui.intentanalysis.service.ExpectationService;
24 import org.onap.usecaseui.intentanalysis.service.StateService;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.stereotype.Service;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 @Service
35 public class ExpectationServiceImpl implements ExpectationService {
36
37     private static Logger LOGGER = LoggerFactory.getLogger(ExpectationServiceImpl.class);
38     @Autowired
39     private ExpectationMapper expectationMapper;
40
41     @Autowired
42     private StateService stateService;
43
44     @Autowired
45     private ExpectationService expectationService;
46
47     @Override
48     public void createExpectationList(List<Expectation> expectationList, String intentId) {
49         for (Expectation expectation : expectationList) {
50             if (null != expectation) {
51                 stateService.createStateList(expectation.getStateList(), expectation.getExpectationId());
52             }
53         }
54         expectationMapper.insertExpectationList(expectationList, intentId);
55     }
56
57     @Override
58     public List<Expectation> getExpectationListByIntentId(String intentId) {
59         List<Expectation> expectationList = expectationMapper.selectExpectationByIntentId(intentId);
60         for (Expectation expectation : expectationList) {
61             List<State> stateList = stateService.getStateListByExpectationId(expectation.getExpectationId());
62             expectation.setStateList(stateList);
63         }
64         return expectationList;
65     }
66
67     @Override
68     public void deleteExpectationListById(String intentId) {
69         List<Expectation> expectationList = expectationMapper.selectExpectationByIntentId(intentId);
70         expectationMapper.deleteExpectationByIntentId(intentId);
71         for (Expectation expectation : expectationList) {
72             stateService.deleteStateListByExpectationId(expectation.getExpectationId());
73         }
74     }
75
76     @Override
77     public void updateExpectationListById(List<Expectation> expectationList, String intentId) {
78         List<Expectation> expectationDBList = expectationMapper.selectExpectationByIntentId(intentId);
79         if (expectationDBList == null) {
80             LOGGER.error("Intent ID {} doesn't exist in database.", intentId);
81             throw new IllegalArgumentException("This intent ID doesn't exist in database.");
82         }
83         List<String> expectationDBIdList = new ArrayList<>();
84         for (Expectation expectationDB : expectationDBList) {
85             expectationDBIdList.add(expectationDB.getExpectationId());
86         }
87
88         for (Expectation expectation : expectationList) {
89             if (expectationDBIdList.contains(expectation.getExpectationId())) {
90                 stateService.updateStateListByExpectationId(expectation.getStateList(), expectation.getExpectationId());
91                 expectationMapper.updateExpectation(expectation);
92                 expectationDBIdList.remove(expectation.getExpectationId());
93             } else {
94                 expectationService.insertExpectation(expectation, intentId);
95             }
96         }
97         for (String expectationDBId : expectationDBIdList) {
98             expectationService.deleteExpectationById(expectationDBId);
99         }
100         LOGGER.info("Expectations are successfully updated.");
101     }
102
103     @Override
104     public void insertExpectation(Expectation expectation, String intentId) {
105         expectationMapper.insertExpectation(expectation, intentId);
106     }
107
108     @Override
109     public void deleteExpectationById(String expectationId) {
110         expectationMapper.deleteExpectationById(expectationId);
111     }
112 }