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 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;
32 import java.util.ArrayList;
33 import java.util.List;
37 public class ExpectationServiceImpl implements ExpectationService {
40 private ExpectationMapper expectationMapper;
43 private StateService stateService;
46 private ExpectationService expectationService;
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());
55 int res = expectationMapper.insertExpectationList(expectationList, intentId);
57 String msg = "Create expectation to database failed.";
59 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
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);
69 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
71 for (Expectation expectation : expectationList) {
72 List<State> stateList = stateService.getStateListByExpectationId(expectation.getExpectationId());
73 expectation.setStateList(stateList);
75 return expectationList;
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);
84 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
86 int res = expectationMapper.deleteExpectationByIntentId(intentId);
88 String msg = "Delete expectation in database failed.";
90 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
92 for (Expectation expectation : expectationList) {
93 stateService.deleteStateListByExpectationId(expectation.getExpectationId());
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);
103 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
105 List<String> expectationDBIdList = new ArrayList<>();
106 for (Expectation expectationDB : expectationDBList) {
107 expectationDBIdList.add(expectationDB.getExpectationId());
110 for (Expectation expectation : expectationList) {
111 if (expectationDBIdList.contains(expectation.getExpectationId())) {
112 stateService.updateStateListByExpectationId(expectation.getStateList(), expectation.getExpectationId());
113 int res = expectationMapper.updateExpectation(expectation);
115 String msg = "Update expectation in database failed.";
117 throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
119 expectationDBIdList.remove(expectation.getExpectationId());
121 expectationService.insertExpectation(expectation, intentId);
124 for (String expectationDBId : expectationDBIdList) {
125 expectationService.deleteExpectationById(expectationDBId);
127 log.info("Expectations are successfully updated.");
131 public void insertExpectation(Expectation expectation, String intentId) {
132 int res = expectationMapper.insertExpectation(expectation, intentId);
134 String msg = "Create expectation to database failed.";
136 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
141 public void deleteExpectationById(String expectationId) {
142 int res = expectationMapper.deleteExpectationById(expectationId);
144 String msg = "Delete expectation in database failed.";
146 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);