2 * Copyright 2022 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.usecaseui.intentanalysis.service.impl;
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;
31 import java.util.ArrayList;
32 import java.util.List;
35 public class ExpectationServiceImpl implements ExpectationService {
37 private static Logger LOGGER = LoggerFactory.getLogger(ExpectationServiceImpl.class);
39 private ExpectationMapper expectationMapper;
42 private StateService stateService;
45 private ExpectationService expectationService;
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());
54 expectationMapper.insertExpectationList(expectationList, intentId);
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);
64 return expectationList;
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());
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.");
83 List<String> expectationDBIdList = new ArrayList<>();
84 for (Expectation expectationDB : expectationDBList) {
85 expectationDBIdList.add(expectationDB.getExpectationId());
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());
94 expectationService.insertExpectation(expectation, intentId);
97 for (String expectationDBId : expectationDBIdList) {
98 expectationService.deleteExpectationById(expectationDBId);
100 LOGGER.info("Expectations are successfully updated.");
104 public void insertExpectation(Expectation expectation, String intentId) {
105 expectationMapper.insertExpectation(expectation, intentId);
109 public void deleteExpectationById(String expectationId) {
110 expectationMapper.deleteExpectationById(expectationId);