d3e914b96bb13cd877378d70cca44c9a9eec1abe
[usecase-ui/intent-analysis.git] /
1 /*
2  * Copyright (C) 2023 CMCC, Inc. and others. All rights reserved.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.usecaseui.intentanalysis.service.impl;
18
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;
34
35 import java.util.Collections;
36 import java.util.List;
37
38 @Service
39 @Slf4j
40 public class IntentReportServiceImpl implements IntentReportService {
41
42     @Autowired
43     private FulfillmentInfoService fulfillmentInfoService;
44
45     @Autowired
46     private ObjectInstanceMapper objectInstanceMapper;
47
48     @Autowired
49     private IntentReportFulfillmentInfoMapper intentReportFulfillmentInfoMapper;
50
51     @Autowired
52     private IntentReportMapper intentReportMapper;
53
54     @Override
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());
64
65         saveIntentReport(intentReport, fulfillmentInfo);
66         return intentReport;
67     }
68
69     @Override
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");
75             return;
76         }
77         IntentReport intentReport = new IntentReport();
78         intentReport.setIntentReportId(CommonUtil.getUUid());
79         intentReport.setIntentReference("intentReference");
80         intentReport.setReportTime(CommonUtil.getTime());
81         saveIntentReport(intentReport, fulfillmentInfo);
82     }
83
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);
91         }
92         return fulfillmentInfo;
93     }
94
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);
101         }
102         return objectInstances;
103     }
104
105     private void saveIntentReport(IntentReport intentReport, FulfillmentInfo fulfillmentInfo) {
106         int num = intentReportMapper.insertIntentReport(intentReport);
107         if (num < 1) {
108             String msg = "Failed to insert intent report to database.";
109             log.error(msg);
110             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
111         }
112         int fulfillmentNum = intentReportFulfillmentInfoMapper.insertIntentReportFulfillment(fulfillmentInfo, intentReport.getIntentReportId());
113         if (fulfillmentNum < 1) {
114             String msg = "Failed to insert fulfillmentInfo to database.";
115             log.error(msg);
116             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
117         }
118     }
119 }