2 * Copyright (C) 2023 CMCC, Inc. and others. All rights reserved.
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;
19 import lombok.extern.slf4j.Slf4j;
20 import org.onap.usecaseui.intentanalysis.bean.models.FulfillmentInfo;
21 import org.onap.usecaseui.intentanalysis.bean.models.IntentReport;
22 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
23 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
24 import org.onap.usecaseui.intentanalysis.mapper.IntentReportFulfillmentInfoMapper;
25 import org.onap.usecaseui.intentanalysis.mapper.IntentReportMapper;
26 import org.onap.usecaseui.intentanalysis.mapper.ObjectInstanceMapper;
27 import org.onap.usecaseui.intentanalysis.service.FulfillmentInfoService;
28 import org.onap.usecaseui.intentanalysis.service.IntentReportService;
29 import org.onap.usecaseui.intentanalysis.util.CommonUtil;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Service;
32 import org.springframework.transaction.annotation.Transactional;
33 import org.springframework.util.CollectionUtils;
35 import java.util.Collections;
36 import java.util.List;
40 public class IntentReportServiceImpl implements IntentReportService {
43 private FulfillmentInfoService fulfillmentInfoService;
46 private ObjectInstanceMapper objectInstanceMapper;
49 private IntentReportFulfillmentInfoMapper intentReportFulfillmentInfoMapper;
52 private IntentReportMapper intentReportMapper;
55 @Transactional(rollbackFor = DataBaseException.class)
56 public IntentReport getIntentReportByIntentId(String intentId) {
57 FulfillmentInfo fulfillmentInfo = getFulfillmentInfo(intentId);
58 fulfillmentInfo.setObjectInstances(getInstances(intentId));
59 IntentReport intentReport = new IntentReport();
60 intentReport.setIntentReportId(CommonUtil.getUUid());
61 intentReport.setIntentReference("intentReference");
62 intentReport.setFulfillmentInfos(Collections.singletonList(fulfillmentInfo));
63 intentReport.setReportTime(CommonUtil.getTime());
65 saveIntentReport(intentReport, fulfillmentInfo);
70 @Transactional(rollbackFor = DataBaseException.class)
71 public void saveIntentReportByIntentId(String intentId) {
72 FulfillmentInfo fulfillmentInfo = fulfillmentInfoService.getFulfillmentInfo(intentId);
73 if (fulfillmentInfo == null) {
74 log.error("The fulfillmentInfo is null");
77 IntentReport intentReport = new IntentReport();
78 intentReport.setIntentReportId(CommonUtil.getUUid());
79 intentReport.setIntentReference("intentReference");
80 intentReport.setReportTime(CommonUtil.getTime());
81 saveIntentReport(intentReport, fulfillmentInfo);
84 private FulfillmentInfo getFulfillmentInfo(String intentId) {
85 FulfillmentInfo fulfillmentInfo = fulfillmentInfoService.getFulfillmentInfo(intentId);
86 log.info("fulfillmentInfo is {}", fulfillmentInfo);
87 if (fulfillmentInfo == null) {
88 log.error("get fulfillmentInfo is failed,intentId is {}", intentId);
89 String msg = "get fulfillmentInfo is empty, please enter the right intentId";
90 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
92 return fulfillmentInfo;
95 private List<String> getInstances(String intentId) {
96 List<String> objectInstances = objectInstanceMapper.getObjectInstances(intentId);
97 if (CollectionUtils.isEmpty(objectInstances)) {
98 log.error("get objectInstance is failed,intentId is {}", intentId);
99 String msg = "get objectInstance is failed";
100 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
102 return objectInstances;
105 private void saveIntentReport(IntentReport intentReport, FulfillmentInfo fulfillmentInfo) {
106 int num = intentReportMapper.insertIntentReport(intentReport);
108 String msg = "Failed to insert intent report to database.";
110 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
112 int fulfillmentNum = intentReportFulfillmentInfoMapper.insertIntentReportFulfillment(fulfillmentInfo, intentReport.getIntentReportId());
113 if (fulfillmentNum < 1) {
114 String msg = "Failed to insert fulfillmentInfo to database.";
116 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);