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 java.util.ArrayList;
21 import java.util.List;
22 import lombok.extern.slf4j.Slf4j;
23 import org.onap.usecaseui.intentanalysis.service.*;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.stereotype.Service;
26 import org.onap.usecaseui.intentanalysis.bean.models.Expectation;
27 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
28 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
29 import org.onap.usecaseui.intentanalysis.mapper.ExpectationMapper;
34 public class ExpectationServiceImpl implements ExpectationService {
37 private ExpectationMapper expectationMapper;
40 private ExpectationService expectationService;
43 private ExpectationObjectService expectationObjectService;
46 private ExpectationTargetService expectationTargetService;
49 private ContextService contextService;
53 private FulfilmentInfoService fulfilmentInfoService;
56 public void createIntentExpectationList(List<Expectation> intentExpectationList, String intentId) {
57 if (expectationMapper.insertIntentExpectationList(intentExpectationList, intentId) < 1) {
58 String msg = "Create expectation to database failed.";
60 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
62 for (Expectation expectation : intentExpectationList) {
63 String expectationId = expectation.getExpectationId();
64 expectationObjectService.createExpectationObject(expectation.getExpectationObject(),
66 expectationTargetService.createExpectationTargetList(expectation.getExpectationTargets(),
68 contextService.createContextList(expectation.getExpectationContexts(), expectationId);
69 fulfilmentInfoService.createFulfilmentInfo(expectation.getExpectationFulfilmentInfo(),
75 public void createIntentExpectation(Expectation expectation, String intentId) {
76 if (expectationMapper.insertIntentExpectation(expectation, intentId) < 1) {
77 String msg = "Create expectation to database failed.";
79 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
84 public List<Expectation> getIntentExpectationListByIntentId(String intentId) {
85 List<Expectation> expectationList = expectationMapper.selectIntentExpectationListByIntentId(intentId);
86 if (expectationList == null) {
87 String msg = String.format("Expectation: Intent id %s doesn't exist in database.", intentId);
89 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
91 for (Expectation expectation : expectationList) {
92 if (expectation != null) {
93 String expectationId = expectation.getExpectationId();
94 expectation.setExpectationObject(expectationObjectService.getExpectationObjectByExpectationId(expectationId));
95 expectation.setExpectationTargets(expectationTargetService.getExpectationTargetListByExpectationId(expectationId));
96 expectation.setExpectationContexts(contextService.getContextListByParentId(expectationId));
97 expectation.setExpectationFulfilmentInfo(fulfilmentInfoService.getFulfilmentInfoByParentId(expectationId));
100 return expectationList;
104 public void updateIntentExpectationListByIntentId(List<Expectation> intentExpectationList, String intentId) {
105 List<Expectation> expectationList = expectationMapper.selectIntentExpectationListByIntentId(intentId);
106 if (expectationList == null) {
107 String msg = String.format("Intent id %s doesn't exist in database.", intentId);
109 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
111 List<String> expectationIdList = new ArrayList<>();
112 for (Expectation expectationDB : expectationList) {
113 expectationIdList.add(expectationDB.getExpectationId());
116 for (Expectation expectation : intentExpectationList) {
117 if (expectationIdList.contains(expectation.getExpectationId())) {
118 if (expectationMapper.updateIntentExpectation(expectation) < 1) {
119 String msg = "Update expectation in database failed.";
121 throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
123 expectationIdList.remove(expectation.getExpectationId());
125 expectationService.createIntentExpectation(expectation, intentId);
128 for (String expectationDBId : expectationIdList) {
129 expectationService.deleteIntentExpectationById(expectationDBId);
131 log.info("Expectations are successfully updated.");
135 public void deleteIntentExpectationById(String expectationId) {
136 if (expectationMapper.deleteIntentExpectationById(expectationId) < 1) {
137 String msg = "Delete expectation in database failed.";
139 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
144 public void deleteIntentExpectationListByIntentId(String intentId) {
145 List<Expectation> expectationList = expectationMapper.selectIntentExpectationListByIntentId(intentId);
146 if (expectationList == null) {
147 String msg = String.format("Intent id %s doesn't exist in database.", intentId);
149 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
151 if (expectationMapper.deleteIntentExpectationListByIntentId(intentId) < 1) {
152 String msg = "Delete expectation in database failed.";
154 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);