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;
23 import lombok.extern.slf4j.Slf4j;
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.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;
36 import org.springframework.util.CollectionUtils;
41 public class ExpectationServiceImpl implements ExpectationService {
44 private ExpectationMapper expectationMapper;
47 private ExpectationService expectationService;
50 private ExpectationObjectService expectationObjectService;
53 private ExpectationTargetService expectationTargetService;
56 private ContextService contextService;
59 private FulfilmentInfoService fulfilmentInfoService;
61 private ContextParentType contextParentType;
64 public void createIntentExpectationList(List<Expectation> intentExpectationList, String intentId) {
65 for (Expectation expectation : intentExpectationList) {
66 if (expectation != null) {
67 String expectationId = expectation.getExpectationId();
68 expectationObjectService.createExpectationObject(expectation.getExpectationObject(),
70 expectationTargetService.createExpectationTargetList(expectation.getExpectationTargets(),
72 contextService.createContextList(expectation.getExpectationContexts(), expectationId);
73 fulfilmentInfoService.createFulfilmentInfo(expectation.getExpectationFulfilmentInfo(),
77 if (expectationMapper.insertIntentExpectationList(intentExpectationList, intentId) < 1) {
78 String msg = "Failed to create expectation list to database.";
80 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
82 log.info("Successfully created expectation list to database.");
86 public void createIntentExpectation(Expectation expectation, String intentId) {
87 expectationObjectService.createExpectationObject(expectation.getExpectationObject(),
88 expectation.getExpectationId());
89 expectationTargetService.createExpectationTargetList(expectation.getExpectationTargets(),
90 expectation.getExpectationId());
91 contextService.createContextList(expectation.getExpectationContexts(),
92 expectation.getExpectationId());
93 fulfilmentInfoService.createFulfilmentInfo(expectation.getExpectationFulfilmentInfo(),
94 expectation.getExpectationId());
96 if (expectationMapper.insertIntentExpectation(expectation, intentId) < 1) {
97 String msg = "Failed to create expectation to database.";
99 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
101 log.info("Successfully created expectation to database.");
105 public List<Expectation> getIntentExpectationList(String intentId) {
106 List<Expectation> expectationList = expectationMapper.selectIntentExpectationList(intentId);
107 if (!CollectionUtils.isEmpty(expectationList)) {
108 for (Expectation expectation : expectationList) {
109 String expectationId = expectation.getExpectationId();
110 expectation.setExpectationObject(expectationObjectService.getExpectationObject(expectationId));
111 expectation.setExpectationTargets(expectationTargetService.getExpectationTargetList(expectationId));
112 expectation.setExpectationContexts(contextService.getContextList(expectationId));
113 expectation.setExpectationFulfilmentInfo(fulfilmentInfoService.getFulfilmentInfo(expectationId));
116 log.info(String.format("Expectation list is null, intentId = %s", intentId));
118 return expectationList;
122 public Expectation getIntentExpectation(String expectationId) {
123 Expectation expectation = expectationMapper.selectIntentExpectation(expectationId);
124 if (expectation != null) {
125 expectation.setExpectationObject(expectationObjectService.getExpectationObject(expectationId));
126 expectation.setExpectationTargets(expectationTargetService.getExpectationTargetList(expectationId));
127 expectation.setExpectationContexts(contextService.getContextList(expectationId));
128 expectation.setExpectationFulfilmentInfo(fulfilmentInfoService.getFulfilmentInfo(expectationId));
130 log.info(String.format("Expectation is null, expectationId = %s", expectationId));
136 public void updateIntentExpectationList(List<Expectation> intentExpectationList, String intentId) {
137 List<Expectation> expectationListFromDB = expectationService.getIntentExpectationList(intentId);
138 if (CollectionUtils.isEmpty(expectationListFromDB) && !CollectionUtils.isEmpty(intentExpectationList)) {
139 expectationService.createIntentExpectationList(intentExpectationList, intentId);
140 } else if (!CollectionUtils.isEmpty(expectationListFromDB) && CollectionUtils.isEmpty(intentExpectationList)) {
141 expectationService.deleteIntentExpectationList(intentId);
142 } else if (!CollectionUtils.isEmpty(expectationListFromDB) && !CollectionUtils.isEmpty(intentExpectationList)) {
143 List<String> expectationIdListFromDB = new ArrayList<>();
144 for (Expectation expectationDB : expectationListFromDB) {
145 expectationIdListFromDB.add(expectationDB.getExpectationId());
148 for (Expectation expectation : intentExpectationList) {
149 if (expectationIdListFromDB.contains(expectation.getExpectationId())) {
150 expectationObjectService.updateExpectationObject(expectation.getExpectationObject(), expectation.getExpectationId());
151 expectationTargetService.updateExpectationTargetList(expectation.getExpectationTargets(), expectation.getExpectationId());
152 contextService.updateContextList(expectation.getExpectationContexts(), expectation.getExpectationId());
153 fulfilmentInfoService.updateFulfilmentInfo(expectation.getExpectationFulfilmentInfo(), expectation.getExpectationId());
154 if (expectationMapper.updateIntentExpectation(expectation) < 1) {
155 String msg = "Failed to update expectation to database.";
157 throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
159 expectationIdListFromDB.remove(expectation.getExpectationId());
161 expectationService.createIntentExpectation(expectation, intentId);
164 for (String expectationIdFromDB : expectationIdListFromDB) {
165 expectationService.deleteIntentExpectation(expectationIdFromDB);
167 log.info("Successfully updated expectation list to database.");
173 public void deleteIntentExpectation(String expectationId) {
174 Expectation expectation = expectationService.getIntentExpectation(expectationId);
175 if (expectation != null) {
176 expectationObjectService.deleteExpectationObject(expectationId);
177 expectationTargetService.deleteExpectationTargetList(expectationId);
178 contextService.deleteContextList(expectationId);
179 fulfilmentInfoService.deleteFulfilmentInfo(expectationId);
180 if (expectationMapper.deleteIntentExpectation(expectationId) < 1) {
181 String msg = "Failed to delete expectation to database.";
183 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
185 log.info("Successfully deleted expectation to database.");
191 public void deleteIntentExpectationList(String intentId) {
192 List<Expectation> expectationList = expectationService.getIntentExpectationList(intentId);
193 if (!CollectionUtils.isEmpty(expectationList)) {
194 for (Expectation expectation : expectationList) {
195 String expectationId = expectation.getExpectationId();
196 expectationObjectService.deleteExpectationObject(expectationId);
197 expectationTargetService.deleteExpectationTargetList(expectationId);
198 contextService.deleteContextList(expectationId);
199 fulfilmentInfoService.deleteFulfilmentInfo(expectationId);
201 if (expectationMapper.deleteIntentExpectationList(intentId) < 1) {
202 String msg = "Failed to delete expectation list to database.";
204 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
206 log.info("Successfully deleted expectation list to database.");