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.IntentInstanceService;
31 import org.onap.usecaseui.intentanalysis.service.IntentReportService;
32 import org.onap.usecaseui.intentanalysis.util.CommonUtil;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Service;
35 import org.springframework.transaction.annotation.Transactional;
36 import org.springframework.util.CollectionUtils;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.stream.Collectors;
42 import static org.onap.usecaseui.intentanalysis.common.ResponseConsts.RSEPONSE_SUCCESS;
46 public class IntentReportServiceImpl implements IntentReportService {
49 private FulfillmentInfoService fulfillmentInfoService;
52 private ObjectInstanceMapper objectInstanceMapper;
55 private IntentReportFulfillmentInfoMapper intentReportFulfillmentInfoMapper;
58 private IntentReportMapper intentReportMapper;
61 private IntentInstanceService intentInstanceService;
64 @Transactional(rollbackFor = DataBaseException.class)
65 public ServiceResult getIntentReportByIntentId(String intentId) {
66 FulfillmentInfo fulfillmentInfo = getFulfillmentInfo(intentId);
68 if (fulfillmentInfo == null) {
69 return new ServiceResult(new ResultHeader(RSEPONSE_SUCCESS, "The intent has not fulfillmentInfo"),
72 fulfillmentInfo.setObjectInstances(getInstances(intentId));
73 IntentReport intentReport = new IntentReport();
74 intentReport.setIntentReportId(CommonUtil.getUUid());
75 intentReport.setIntentReference(intentInstanceService.queryIntentInstanceId(intentId));
76 intentReport.setFulfillmentInfos(Collections.singletonList(fulfillmentInfo));
77 intentReport.setReportTime(CommonUtil.getTime());
79 saveIntentReport(intentReport, fulfillmentInfo);
80 return new ServiceResult(new ResultHeader(RSEPONSE_SUCCESS, "Get report success"),
85 * Generate intention reports on a regular basis and save them in the database
87 * @param intentId intentId
90 @Transactional(rollbackFor = DataBaseException.class)
91 public void saveIntentReportByIntentId(String intentId) {
92 FulfillmentInfo fulfillmentInfo = fulfillmentInfoService.getFulfillmentInfo(intentId);
93 if (fulfillmentInfo == null) {
94 log.error("The fulfillmentInfo is null");
97 IntentReport intentReport = new IntentReport();
98 intentReport.setIntentReportId(CommonUtil.getUUid());
99 intentReport.setIntentReference(intentInstanceService.queryIntentInstanceId(intentId));
100 intentReport.setReportTime(CommonUtil.getTime());
101 saveIntentReport(intentReport, fulfillmentInfo);
105 public List<String> getIntentReportIds(String intentReference) {
106 List<String> intentReportIds = intentReportMapper.getIntentReportIds(intentReference);
107 if (CollectionUtils.isEmpty(intentReportIds)) {
108 log.error("get intentReportId is empty,intentReference is {}", intentReference);
110 return intentReportIds;
113 private FulfillmentInfo getFulfillmentInfo(String intentId) {
114 FulfillmentInfo fulfillmentInfo = fulfillmentInfoService.getFulfillmentInfo(intentId);
115 log.info("fulfillmentInfo is {}", fulfillmentInfo);
116 if (fulfillmentInfo == null) {
117 log.error("get fulfillmentInfo is failed,intentId is {}", intentId);
119 return fulfillmentInfo;
122 private List<String> getInstances(String intentId) {
123 List<String> objectInstances = objectInstanceMapper.getObjectInstances(intentId);
124 if (CollectionUtils.isEmpty(objectInstances)) {
125 log.error("get objectInstance is failed,intentId is {}", intentId);
127 return objectInstances.stream().distinct().collect(Collectors.toList());
130 private void saveIntentReport(IntentReport intentReport, FulfillmentInfo fulfillmentInfo) {
131 int num = intentReportMapper.insertIntentReport(intentReport);
133 String msg = "Failed to insert intent report to database.";
135 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
137 int fulfillmentNum = intentReportFulfillmentInfoMapper.insertIntentReportFulfillment(fulfillmentInfo, intentReport.getIntentReportId());
138 if (fulfillmentNum < 1) {
139 String msg = "Failed to insert fulfillmentInfo to database.";
141 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);