0b2c20d160fa83bb0e5b16cf89fafc04d2c3916f
[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.FulfilmentInfo;
27 import org.onap.usecaseui.intentanalysis.mapper.FulfilmentInfoMapper;
28 import org.onap.usecaseui.intentanalysis.service.FulfilmentInfoService;
29 import lombok.extern.slf4j.Slf4j;
30
31
32 @Service
33 @Slf4j
34 public class FulfilmentInfoServiceImpl implements FulfilmentInfoService {
35
36     @Autowired
37     private FulfilmentInfoMapper fulfilmentInfoMapper;
38
39     @Autowired
40     private FulfilmentInfoService fulfilmentInfoService;
41
42     @Override
43     public void createFulfilmentInfo(FulfilmentInfo fulfilmentInfo, String parentId) {
44         if (fulfilmentInfo != null) {
45             if (fulfilmentInfoMapper.insertFulfilmentInfo(fulfilmentInfo, parentId) < 1) {
46                 String msg = "Failed to create fulfilment info to database.";
47                 log.error(msg);
48                 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
49             }
50             log.info("Successfully created fulfilment info to database.");
51         }
52     }
53
54     @Override
55     public void deleteFulfilmentInfo(String parentId) {
56         if (fulfilmentInfoService.getFulfilmentInfo(parentId) != null) {
57             if (fulfilmentInfoMapper.deleteFulfilmentInfo(parentId) < 1) {
58                 String msg = "Failed to delete fulfilment info to database.";
59                 log.error(msg);
60                 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
61             }
62             log.info("Successfully deleted fulfilment info to database.");
63         }
64     }
65
66     @Override
67     public void updateFulfilmentInfo(FulfilmentInfo fulfilmentInfo, String parentId) {
68
69         FulfilmentInfo fulfillmentInfoDB = fulfilmentInfoService.getFulfilmentInfo(parentId);
70         if (fulfillmentInfoDB == null && fulfilmentInfo != null) {
71             fulfilmentInfoService.createFulfilmentInfo(fulfilmentInfo, parentId);
72         } else if (fulfillmentInfoDB != null && fulfilmentInfo == null) {
73             fulfilmentInfoService.deleteFulfilmentInfo(parentId);
74         } else if (fulfillmentInfoDB != null) {
75             if (fulfilmentInfoMapper.updateFulfilmentInfo(fulfilmentInfo, parentId) < 1) {
76                 String msg = "Failed to update fulfilment info to database.";
77                 log.error(msg);
78                 throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
79             }
80             log.info("Successfully updated fulfilment info to database.");
81         }
82     }
83
84     @Override
85     public FulfilmentInfo getFulfilmentInfo(String parentId) {
86         FulfilmentInfo fulfilmentInfo = fulfilmentInfoMapper.selectFulfilmentInfo(parentId);
87         if (fulfilmentInfo == null) {
88             log.info(String.format("FulfilmentInfo is null, parentId = %s", parentId));
89         }
90         return fulfilmentInfo;
91     }
92 }