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.bean.models.ResultHeader;
23 import org.onap.usecaseui.intentanalysis.bean.models.ServiceResult;
24 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
25 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
26 import org.onap.usecaseui.intentanalysis.mapper.IntentReportFulfillmentInfoMapper;
27 import org.onap.usecaseui.intentanalysis.mapper.IntentReportMapper;
28 import org.onap.usecaseui.intentanalysis.mapper.ObjectInstanceMapper;
29 import org.onap.usecaseui.intentanalysis.service.FulfillmentInfoService;
30 import org.onap.usecaseui.intentanalysis.service.IntentReportService;
31 import org.onap.usecaseui.intentanalysis.util.CommonUtil;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Service;
34 import org.springframework.transaction.annotation.Transactional;
35 import org.springframework.util.CollectionUtils;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.stream.Collectors;
41 import static org.onap.usecaseui.intentanalysis.common.ResponseConsts.RSEPONSE_SUCCESS;
45 public class IntentReportServiceImpl implements IntentReportService {
48 private FulfillmentInfoService fulfillmentInfoService;
51 private ObjectInstanceMapper objectInstanceMapper;
54 private IntentReportFulfillmentInfoMapper intentReportFulfillmentInfoMapper;
57 private IntentReportMapper intentReportMapper;
60 @Transactional(rollbackFor = DataBaseException.class)
61 public ServiceResult getIntentReportByIntentId(String intentId) {
62 FulfillmentInfo fulfillmentInfo = getFulfillmentInfo(intentId);
64 if (fulfillmentInfo == null) {
65 return new ServiceResult(new ResultHeader(RSEPONSE_SUCCESS, "The intent has not fulfillmentInfo"),
68 fulfillmentInfo.setObjectInstances(getInstances(intentId));
69 IntentReport intentReport = new IntentReport();
70 intentReport.setIntentReportId(CommonUtil.getUUid());
71 intentReport.setIntentReference("intentReference");
72 intentReport.setFulfillmentInfos(Collections.singletonList(fulfillmentInfo));
73 intentReport.setReportTime(CommonUtil.getTime());
75 saveIntentReport(intentReport, fulfillmentInfo);
76 return new ServiceResult(new ResultHeader(RSEPONSE_SUCCESS, "Get report success"),
81 @Transactional(rollbackFor = DataBaseException.class)
82 public void saveIntentReportByIntentId(String intentId) {
83 FulfillmentInfo fulfillmentInfo = fulfillmentInfoService.getFulfillmentInfo(intentId);
84 if (fulfillmentInfo == null) {
85 log.error("The fulfillmentInfo is null");
88 IntentReport intentReport = new IntentReport();
89 intentReport.setIntentReportId(CommonUtil.getUUid());
90 intentReport.setIntentReference("intentReference");
91 intentReport.setReportTime(CommonUtil.getTime());
92 saveIntentReport(intentReport, fulfillmentInfo);
95 private FulfillmentInfo getFulfillmentInfo(String intentId) {
96 FulfillmentInfo fulfillmentInfo = fulfillmentInfoService.getFulfillmentInfo(intentId);
97 log.info("fulfillmentInfo is {}", fulfillmentInfo);
98 if (fulfillmentInfo == null) {
99 log.error("get fulfillmentInfo is failed,intentId is {}", intentId);
101 return fulfillmentInfo;
104 private List<String> getInstances(String intentId) {
105 List<String> objectInstances = objectInstanceMapper.getObjectInstances(intentId);
106 if (CollectionUtils.isEmpty(objectInstances)) {
107 log.error("get objectInstance is failed,intentId is {}", intentId);
109 return objectInstances.stream().distinct().collect(Collectors.toList());
112 private void saveIntentReport(IntentReport intentReport, FulfillmentInfo fulfillmentInfo) {
113 int num = intentReportMapper.insertIntentReport(intentReport);
115 String msg = "Failed to insert intent report to database.";
117 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
119 int fulfillmentNum = intentReportFulfillmentInfoMapper.insertIntentReportFulfillment(fulfillmentInfo, intentReport.getIntentReportId());
120 if (fulfillmentNum < 1) {
121 String msg = "Failed to insert fulfillmentInfo to database.";
123 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);