6a72aea8f0c644cb5d14803eb437d668cc4bd1bb
[usecase-ui/intent-analysis.git] /
1 /*
2  * Copyright 2022 Huawei Technologies Co., Ltd.
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
20 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
21 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.stereotype.Service;
26 import org.onap.usecaseui.intentanalysis.bean.models.FulfillmentInfo;
27 import org.onap.usecaseui.intentanalysis.mapper.FulfillmentInfoMapper;
28 import org.onap.usecaseui.intentanalysis.service.FulfillmentInfoService;
29 import lombok.extern.slf4j.Slf4j;
30
31
32 @Service
33 @Slf4j
34 public class FulfillmentInfoServiceImpl implements FulfillmentInfoService {
35
36     @Autowired
37     private FulfillmentInfoMapper fulfillmentInfoMapper;
38
39     @Autowired
40     private FulfillmentInfoService fulfillmentInfoService;
41
42     @Override
43     public void createFulfillmentInfo(FulfillmentInfo fulfillmentInfo, String parentId) {
44         if (fulfillmentInfo != null) {
45             if (fulfillmentInfoMapper.insertFulfillmentInfo(fulfillmentInfo, parentId) < 1) {
46                 String msg = "Failed to create fulfillment info to database.";
47                 log.error(msg);
48                 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
49             }
50             log.info("Successfully created fulfillment info to database.");
51         }
52     }
53
54     @Override
55     public void deleteFulfillmentInfo(String parentId) {
56         if (fulfillmentInfoService.getFulfillmentInfo(parentId) != null) {
57             if (fulfillmentInfoMapper.deleteFulfillmentInfo(parentId) < 1) {
58                 String msg = "Failed to delete fulfillment info to database.";
59                 log.error(msg);
60                 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
61             }
62             log.info("Successfully deleted fulfillment info to database.");
63         }
64     }
65
66     @Override
67     public void updateFulfillmentInfo(FulfillmentInfo fulfillmentInfo, String parentId) {
68
69         FulfillmentInfo fulfillmentInfoDB = fulfillmentInfoService.getFulfillmentInfo(parentId);
70         if (fulfillmentInfoDB == null && fulfillmentInfo != null) {
71             fulfillmentInfoService.createFulfillmentInfo(fulfillmentInfo, parentId);
72         } else if (fulfillmentInfoDB != null && fulfillmentInfo == null) {
73             fulfillmentInfoService.deleteFulfillmentInfo(parentId);
74         } else if (fulfillmentInfoDB != null) {
75             if (fulfillmentInfoMapper.updateFulfillmentInfo(fulfillmentInfo, parentId) < 1) {
76                 String msg = "Failed to update fulfillment info to database.";
77                 log.error(msg);
78                 throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
79             }
80             log.info("Successfully updated fulfillment info to database.");
81         }
82     }
83
84     @Override
85     public FulfillmentInfo getFulfillmentInfo(String parentId) {
86         FulfillmentInfo fulfillmentInfo = fulfillmentInfoMapper.selectFulfillmentInfo(parentId);
87         if (fulfillmentInfo == null) {
88             log.info(String.format("FulfillmentInfo is null, parentId = %s", parentId));
89         }
90         return fulfillmentInfo;
91     }
92 }