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.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.stereotype.Service;
25 import org.onap.usecaseui.intentanalysis.bean.enums.ContextParentType;
26 import org.onap.usecaseui.intentanalysis.bean.models.Expectation;
27 import org.onap.usecaseui.intentanalysis.bean.models.ExpectationTarget;
28 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
29 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
30 import org.onap.usecaseui.intentanalysis.mapper.ExpectationMapper;
31 import org.onap.usecaseui.intentanalysis.service.ContextService;
32 import org.onap.usecaseui.intentanalysis.service.ExpectationService;
33 import org.onap.usecaseui.intentanalysis.service.ExpectationObjectService;
34 import org.onap.usecaseui.intentanalysis.service.ExpectationTargetService;
35 import org.onap.usecaseui.intentanalysis.service.FulfilmentInfoService;
40 public class ExpectationServiceImpl implements ExpectationService {
43 private ExpectationMapper expectationMapper;
46 private ExpectationService expectationService;
49 private ExpectationObjectService expectationObjectService;
52 private ExpectationTargetService expectationTargetService;
55 private ContextService contextService;
58 private FulfilmentInfoService fulfilmentInfoService;
60 private ContextParentType contextParentType;
63 public void createIntentExpectations(List<Expectation> intentExpectations, String intentId) {
64 for (Expectation expectation : intentExpectations) {
65 if (null != expectation) {
66 expectationObjectService.createObject(expectation.getExpectationObject(),
67 expectation.getExpectationId());
68 expectationTargetService.createTargets(expectation.getExpectationTargets(),
69 expectation.getExpectationId());
70 contextService.createContextList(expectation.getExpectationContexts(),
71 contextParentType.EXPECTATION,
72 expectation.getExpectationId());
73 fulfilmentInfoService.createFulfilmentInfo(expectation.getExpectationFulfilmentInfo(),
74 expectation.getExpectationId());
77 int res = expectationMapper.insertIntentExpectations(intentExpectations, intentId);
79 String msg = "Create expectation to database failed.";
81 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
86 public void insertIntentExpectation(Expectation expectation, String intentId) {
87 int res = expectationMapper.insertIntentExpectation(expectation, intentId);
89 String msg = "Create expectation to database failed.";
91 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
96 public List<Expectation> getIntentExpectationsByIntentId(String intentId) {
97 List<Expectation> expectationList = expectationMapper.selectIntentExpectationsByIntentId(intentId);
98 if (expectationList == null) {
99 String msg = String.format("Intent id %s doesn't exist in database.", intentId);
101 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
103 int res = expectationMapper.deleteIntentExpectationsByIntentId(intentId);
105 String msg = "Delete expectation in database failed.";
107 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
109 return expectationList;
113 public void updateIntentExpectationsByIntentId(List<Expectation> intentExpectations, String intentId) {
114 List<Expectation> expectationDBList = expectationMapper.selectIntentExpectationsByIntentId(intentId);
115 if (expectationDBList == null) {
116 String msg = String.format("Intent id %s doesn't exist in database.", intentId);
118 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
120 List<String> expectationDBIdList = new ArrayList<>();
121 for (Expectation expectationDB : expectationDBList) {
122 expectationDBIdList.add(expectationDB.getExpectationId());
125 for (Expectation expectation : intentExpectations) {
126 if (expectationDBIdList.contains(expectation.getExpectationId())) {
127 int res = expectationMapper.updateIntentExpectation(expectation);
129 String msg = "Update expectation in database failed.";
131 throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
133 expectationDBIdList.remove(expectation.getExpectationId());
135 expectationService.insertIntentExpectation(expectation, intentId);
138 for (String expectationDBId : expectationDBIdList) {
139 expectationService.deleteIntentExpectationById(expectationDBId);
141 log.info("Expectations are successfully updated.");
145 public void deleteIntentExpectationById(String expectationId) {
146 int res = expectationMapper.deleteIntentExpectationById(expectationId);
148 String msg = "Delete expectation in database failed.";
150 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
155 public void deleteIntentExpectationsByIntentId(String intentId) {
156 List<Expectation> expectationList = expectationMapper.selectIntentExpectationsByIntentId(intentId);
157 if (expectationList == null) {
158 String msg = String.format("Intent id %s doesn't exist in database.", intentId);
160 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
162 int res = expectationMapper.deleteIntentExpectationsByIntentId(intentId);
164 String msg = "Delete expectation in database failed.";
166 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);