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.onap.usecaseui.intentanalysis.common.ResponseConsts;
25 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.stereotype.Service;
28 import org.onap.usecaseui.intentanalysis.bean.enums.ContextParentType;
29 import org.onap.usecaseui.intentanalysis.bean.models.ExpectationObject;
30 import org.onap.usecaseui.intentanalysis.bean.models.Context;
31 import org.onap.usecaseui.intentanalysis.mapper.ExpectationObjectMapper;
32 import org.onap.usecaseui.intentanalysis.service.ContextService;
33 import org.onap.usecaseui.intentanalysis.service.ExpectationObjectService;
34 import org.springframework.util.CollectionUtils;
39 public class ExpectationObjectServiceImpl implements ExpectationObjectService {
41 private ContextParentType contextParentType;
44 private ExpectationObjectMapper expectationObjectMapper;
47 private ExpectationObjectService expectationObjectService;
50 private ContextService contextService;
53 public void createExpectationObject(ExpectationObject expectationObject, String expectationId) {
54 contextService.createContextList(expectationObject.getObjectContexts(),
55 expectationObjectMapper.selectExpectationObjectId(expectationId));
56 if (expectationObjectMapper.insertExpectationObject(expectationObject, expectationId) < 1) {
57 String msg = "Failed to create expectation object to database.";
59 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
61 log.info("Successfully created expectation object to database.");
65 public ExpectationObject getExpectationObject(String expectationId) {
66 ExpectationObject expectationObject = expectationObjectMapper.selectExpectationObject(expectationId);
67 String expectationObjectId = expectationObjectMapper.selectExpectationObjectId(expectationId);
68 List<Context> contextList = contextService.getContextList(expectationObjectId);
69 if (!CollectionUtils.isEmpty(contextList)) {
70 expectationObject.setObjectContexts(contextService.getContextList(expectationObjectId));
72 log.info(String.format("Expectation object is null, expectationObjectId = %s", expectationObjectId));
74 return expectationObject;
78 public void updateExpectationObject(ExpectationObject expectationObject, String expectationId) {
79 ExpectationObject expectationObjectFromDB = expectationObjectService.getExpectationObject(expectationId);
80 if (expectationObject == null && expectationObjectFromDB != null) {
81 expectationObjectService.deleteExpectationObject(expectationId);
82 } else if (expectationObject != null && expectationObjectFromDB == null) {
83 expectationObjectService.createExpectationObject(expectationObject, expectationId);
84 } else if (expectationObject != null) {
85 contextService.updateContextList(expectationObject.getObjectContexts(),
86 expectationObjectMapper.selectExpectationObjectId(expectationId));
87 if (expectationObjectMapper.updateExpectationObject(expectationObject, expectationId) < 1) {
88 String msg = "Failed to update expectation object to database.";
90 throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
92 log.info("Successfully updated expectation object to database.");
98 public void deleteExpectationObject(String expectationId) {
99 ExpectationObject expectationObject = expectationObjectService.getExpectationObject(expectationId);
100 if (expectationObject != null) {
101 contextService.deleteContextList(expectationObjectMapper.selectExpectationObjectId(expectationId));
102 if (expectationObjectMapper.deleteExpectationObject(expectationId) < 1) {
103 String msg = "Failed to update expectation object to database.";
105 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
107 log.info("Successfully deleted expectation object to database.");