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.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;
33 import java.util.ArrayList;
34 import java.util.List;
37 public class ExpectationServiceImpl implements ExpectationService {
39 private static Logger LOGGER = LoggerFactory.getLogger(ExpectationServiceImpl.class);
41 private ExpectationMapper expectationMapper;
44 private StateService stateService;
47 private ExpectationService expectationService;
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());
56 int res = expectationMapper.insertExpectationList(expectationList, intentId);
58 String msg = "Create expectation to database failed.";
60 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
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);
70 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
72 for (Expectation expectation : expectationList) {
73 List<State> stateList = stateService.getStateListByExpectationId(expectation.getExpectationId());
74 expectation.setStateList(stateList);
76 return expectationList;
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);
85 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
87 int res = expectationMapper.deleteExpectationByIntentId(intentId);
89 String msg = "Delete expectation in database failed.";
91 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
93 for (Expectation expectation : expectationList) {
94 stateService.deleteStateListByExpectationId(expectation.getExpectationId());
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);
104 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
106 List<String> expectationDBIdList = new ArrayList<>();
107 for (Expectation expectationDB : expectationDBList) {
108 expectationDBIdList.add(expectationDB.getExpectationId());
111 for (Expectation expectation : expectationList) {
112 if (expectationDBIdList.contains(expectation.getExpectationId())) {
113 stateService.updateStateListByExpectationId(expectation.getStateList(), expectation.getExpectationId());
114 int res = expectationMapper.updateExpectation(expectation);
116 String msg = "Update expectation in database failed.";
118 throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
120 expectationDBIdList.remove(expectation.getExpectationId());
122 expectationService.insertExpectation(expectation, intentId);
125 for (String expectationDBId : expectationDBIdList) {
126 expectationService.deleteExpectationById(expectationDBId);
128 LOGGER.info("Expectations are successfully updated.");
132 public void insertExpectation(Expectation expectation, String intentId) {
133 int res = expectationMapper.insertExpectation(expectation, intentId);
135 String msg = "Create expectation to database failed.";
137 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
142 public void deleteExpectationById(String expectationId) {
143 int res = expectationMapper.deleteExpectationById(expectationId);
145 String msg = "Delete expectation in database failed.";
147 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);