6ea3e065e1f4289377b8744e5e1033a53eaf0bf4
[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.util.CollectionUtils;
33
34 import java.util.Collections;
35 import java.util.List;
36
37 @Service
38 @Slf4j
39 public class IntentReportServiceImpl implements IntentReportService {
40
41     @Autowired
42     private FulfillmentInfoService fulfillmentInfoService;
43
44     @Autowired
45     private ObjectInstanceMapper objectInstanceMapper;
46
47     @Autowired
48     private IntentReportFulfillmentInfoMapper intentReportFulfillmentInfoMapper;
49
50     @Autowired
51     private IntentReportMapper intentReportMapper;
52
53     @Override
54     public IntentReport getIntentReportByIntentId(String intentId) {
55         FulfillmentInfo fulfillmentInfo = fulfillmentInfoService.getFulfillmentInfo(intentId);
56         System.out.println(fulfillmentInfo);
57         if (fulfillmentInfo == null) {
58             log.error("get fulfillmentInfo is failed,intentId is {}", intentId);
59             String msg = "get fulfillmentInfo is failed";
60             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
61         }
62         fulfillmentInfo.setFulfillmentId(intentId);
63         List<String> objectInstances = objectInstanceMapper.getObjectInstances(intentId);
64         if (CollectionUtils.isEmpty(objectInstances)) {
65             log.error("get objectInstance is failed,intentId is {}", intentId);
66             String msg = "get objectInstance is failed";
67             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
68         }
69         String uUid = CommonUtil.getUUid();
70         fulfillmentInfo.setObjectInstances(objectInstances);
71         IntentReport intentReport = new IntentReport();
72         intentReport.setIntentReportId(uUid);
73         intentReport.setIntentReference("intentReference");
74         intentReport.setFulfillmentInfos(Collections.singletonList(fulfillmentInfo));
75         intentReport.setReportTime(CommonUtil.getTime());
76
77         int num = intentReportMapper.insertIntentReport(intentReport);
78         if (num < 1) {
79             String msg = "Failed to insert intent report to database.";
80             log.error(msg);
81             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
82         }
83         int fulfillmentNum = intentReportFulfillmentInfoMapper.insertIntentReportFulfillment(fulfillmentInfo, uUid);
84         if (fulfillmentNum < 1) {
85             String msg = "Failed to insert fulfillmentInfo to database.";
86             log.error(msg);
87             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
88         }
89         return intentReport;
90     }
91 }