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 if (expectationObjectService.getExpectationObject(expectationId) != null) {
55 String msg = String.format("It already exists an object for the expectation %s, update might work.", expectationId);
57 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
59 contextService.createContextList(expectationObject.getObjectContexts(),
60 expectationObjectMapper.selectExpectationObjectId(expectationId));
61 if (expectationObjectMapper.insertExpectationObject(expectationObject, expectationId) < 1) {
62 String msg = "Failed to create expectation object to database.";
64 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
66 log.info("Successfully created expectation object to database.");
70 public ExpectationObject getExpectationObject(String expectationId) {
71 ExpectationObject expectationObject = expectationObjectMapper.selectExpectationObject(expectationId);
72 String expectationObjectId = expectationObjectMapper.selectExpectationObjectId(expectationId);
73 List<Context> contextList = contextService.getContextList(expectationObjectId);
74 if (!CollectionUtils.isEmpty(contextList)) {
75 expectationObject.setObjectContexts(contextService.getContextList(expectationObjectId));
77 log.info(String.format("Expectation object is null, expectationObjectId = %s", expectationObjectId));
79 return expectationObject;
83 public void updateExpectationObject(ExpectationObject expectationObject, String expectationId) {
84 ExpectationObject expectationObjectFromDB = expectationObjectService.getExpectationObject(expectationId);
85 if (expectationObject == null && expectationObjectFromDB != null) {
86 expectationObjectService.deleteExpectationObject(expectationId);
87 } else if (expectationObject != null && expectationObjectFromDB == null) {
88 expectationObjectService.createExpectationObject(expectationObject, expectationId);
89 } else if (expectationObject != null) {
90 contextService.updateContextList(expectationObject.getObjectContexts(),
91 expectationObjectMapper.selectExpectationObjectId(expectationId));
92 if (expectationObjectMapper.updateExpectationObject(expectationObject, expectationId) < 1) {
93 String msg = "Failed to update expectation object to database.";
95 throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
97 log.info("Successfully updated expectation object to database.");
103 public void deleteExpectationObject(String expectationId) {
104 ExpectationObject expectationObject = expectationObjectService.getExpectationObject(expectationId);
105 if (expectationObject != null) {
106 contextService.deleteContextList(expectationObjectMapper.selectExpectationObjectId(expectationId));
107 if (expectationObjectMapper.deleteExpectationObject(expectationId) < 1) {
108 String msg = "Failed to update expectation object to database.";
110 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
112 log.info("Successfully deleted expectation object to database.");