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.enums.ContextParentType;
27 import org.onap.usecaseui.intentanalysis.bean.models.Expectation;
28 import org.onap.usecaseui.intentanalysis.bean.models.ExpectationTarget;
29 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
30 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
31 import org.onap.usecaseui.intentanalysis.mapper.ExpectationMapper;
36 public class ExpectationServiceImpl implements ExpectationService {
39 private ExpectationMapper expectationMapper;
42 private ExpectationService expectationService;
45 private ExpectationObjectService expectationObjectService;
48 private ExpectationTargetService expectationTargetService;
51 private ContextService contextService;
55 private FulfilmentInfoService fulfilmentInfoService;
57 private ContextParentType contextParentType;
60 public void createIntentExpectations(List<Expectation> intentExpectations, String intentId) {
61 for (Expectation expectation : intentExpectations) {
62 if (null != expectation) {
63 expectationObjectService.createObject(expectation.getExpectationObject(),
64 expectation.getExpectationId());
65 expectationTargetService.createTargets(expectation.getExpectationTargets(),
66 expectation.getExpectationId());
67 contextService.createContextList(expectation.getExpectationContexts(),
68 contextParentType.EXPECTATION,
69 expectation.getExpectationId());
70 fulfilmentInfoService.createFulfilmentInfo(expectation.getExpectationFulfilmentInfo(),
71 expectation.getExpectationId());
74 int res = expectationMapper.insertIntentExpectations(intentExpectations, intentId);
76 String msg = "Create expectation to database failed.";
78 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
83 public void insertIntentExpectation(Expectation expectation, String intentId) {
84 int res = expectationMapper.insertIntentExpectation(expectation, intentId);
86 String msg = "Create expectation to database failed.";
88 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
93 public List<Expectation> getIntentExpectationsByIntentId(String intentId) {
94 List<Expectation> expectationList = expectationMapper.selectIntentExpectationsByIntentId(intentId);
95 if (expectationList == null) {
96 String msg = String.format("Intent id %s doesn't exist in database.", intentId);
98 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
100 int res = expectationMapper.deleteIntentExpectationsByIntentId(intentId);
102 String msg = "Delete expectation in database failed.";
104 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
106 return expectationList;
110 public void updateIntentExpectationsByIntentId(List<Expectation> intentExpectations, String intentId) {
111 List<Expectation> expectationDBList = expectationMapper.selectIntentExpectationsByIntentId(intentId);
112 if (expectationDBList == null) {
113 String msg = String.format("Intent id %s doesn't exist in database.", intentId);
115 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
117 List<String> expectationDBIdList = new ArrayList<>();
118 for (Expectation expectationDB : expectationDBList) {
119 expectationDBIdList.add(expectationDB.getExpectationId());
122 for (Expectation expectation : intentExpectations) {
123 if (expectationDBIdList.contains(expectation.getExpectationId())) {
124 int res = expectationMapper.updateIntentExpectation(expectation);
126 String msg = "Update expectation in database failed.";
128 throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
130 expectationDBIdList.remove(expectation.getExpectationId());
132 expectationService.insertIntentExpectation(expectation, intentId);
135 for (String expectationDBId : expectationDBIdList) {
136 expectationService.deleteIntentExpectationById(expectationDBId);
138 log.info("Expectations are successfully updated.");
142 public void deleteIntentExpectationById(String expectationId) {
143 int res = expectationMapper.deleteIntentExpectationById(expectationId);
145 String msg = "Delete expectation in database failed.";
147 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
152 public void deleteIntentExpectationsByIntentId(String intentId) {
153 List<Expectation> expectationList = expectationMapper.selectIntentExpectationsByIntentId(intentId);
154 if (expectationList == null) {
155 String msg = String.format("Intent id %s doesn't exist in database.", intentId);
157 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
159 int res = expectationMapper.deleteIntentExpectationsByIntentId(intentId);
161 String msg = "Delete expectation in database failed.";
163 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);