76911e90fb3d534880d75b3425dafe42a3be69e6
[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 java.util.ArrayList;
21 import java.util.List;
22 import lombok.extern.slf4j.Slf4j;
23 import org.onap.usecaseui.intentanalysis.service.*;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.stereotype.Service;
26 import org.onap.usecaseui.intentanalysis.bean.models.Expectation;
27 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
28 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
29 import org.onap.usecaseui.intentanalysis.mapper.ExpectationMapper;
30
31
32 @Service
33 @Slf4j
34 public class ExpectationServiceImpl implements ExpectationService {
35
36     @Autowired
37     private ExpectationMapper expectationMapper;
38
39     @Autowired
40     private ExpectationService expectationService;
41
42     @Autowired
43     private ExpectationObjectService expectationObjectService;
44
45     @Autowired
46     private ExpectationTargetService expectationTargetService;
47
48     @Autowired
49     private ContextService contextService;
50
51
52     @Autowired
53     private FulfilmentInfoService fulfilmentInfoService;
54
55     @Override
56     public void createIntentExpectationList(List<Expectation> intentExpectationList, String intentId) {
57         if (expectationMapper.insertIntentExpectationList(intentExpectationList, intentId) < 1) {
58             String msg = "Create expectation to database failed.";
59             log.error(msg);
60             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
61         }
62         for (Expectation expectation : intentExpectationList) {
63             String expectationId = expectation.getExpectationId();
64             expectationObjectService.createExpectationObject(expectation.getExpectationObject(),
65                 expectationId);
66             expectationTargetService.createExpectationTargetList(expectation.getExpectationTargets(),
67                 expectationId);
68             contextService.createContextList(expectation.getExpectationContexts(), expectationId);
69             fulfilmentInfoService.createFulfilmentInfo(expectation.getExpectationFulfilmentInfo(),
70                 expectationId);
71         }
72     }
73
74     @Override
75     public void createIntentExpectation(Expectation expectation, String intentId) {
76         if (expectationMapper.insertIntentExpectation(expectation, intentId) < 1) {
77             String msg = "Create expectation to database failed.";
78             log.error(msg);
79             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
80         }
81     }
82
83     @Override
84     public List<Expectation> getIntentExpectationListByIntentId(String intentId) {
85         List<Expectation> expectationList = expectationMapper.selectIntentExpectationListByIntentId(intentId);
86         if (expectationList == null) {
87             String msg = String.format("Expectation: Intent id %s doesn't exist in database.", intentId);
88             log.error(msg);
89             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
90         }
91         for (Expectation expectation : expectationList) {
92             if (expectation != null) {
93                 String expectationId = expectation.getExpectationId();
94                 expectation.setExpectationObject(expectationObjectService.getExpectationObjectByExpectationId(expectationId));
95                 expectation.setExpectationTargets(expectationTargetService.getExpectationTargetListByExpectationId(expectationId));
96                 expectation.setExpectationContexts(contextService.getContextListByParentId(expectationId));
97                 expectation.setExpectationFulfilmentInfo(fulfilmentInfoService.getFulfilmentInfoByParentId(expectationId));
98             }
99         }
100         return expectationList;
101     }
102
103     @Override
104     public void updateIntentExpectationListByIntentId(List<Expectation> intentExpectationList, String intentId) {
105         List<Expectation> expectationList = expectationMapper.selectIntentExpectationListByIntentId(intentId);
106         if (expectationList == null) {
107             String msg = String.format("Intent id %s doesn't exist in database.", intentId);
108             log.error(msg);
109             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
110         }
111         List<String> expectationIdList = new ArrayList<>();
112         for (Expectation expectationDB : expectationList) {
113             expectationIdList.add(expectationDB.getExpectationId());
114         }
115
116         for (Expectation expectation : intentExpectationList) {
117             if (expectationIdList.contains(expectation.getExpectationId())) {
118                 if (expectationMapper.updateIntentExpectation(expectation) < 1) {
119                     String msg = "Update expectation in database failed.";
120                     log.error(msg);
121                     throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
122                 }
123                 expectationIdList.remove(expectation.getExpectationId());
124             } else {
125                 expectationService.createIntentExpectation(expectation, intentId);
126             }
127         }
128         for (String expectationDBId : expectationIdList) {
129             expectationService.deleteIntentExpectationById(expectationDBId);
130         }
131         log.info("Expectations are successfully updated.");
132     }
133
134     @Override
135     public void deleteIntentExpectationById(String expectationId) {
136         if (expectationMapper.deleteIntentExpectationById(expectationId) < 1) {
137             String msg = "Delete expectation in database failed.";
138             log.error(msg);
139             throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
140         }
141     }
142
143     @Override
144     public void deleteIntentExpectationListByIntentId(String intentId) {
145         List<Expectation> expectationList = expectationMapper.selectIntentExpectationListByIntentId(intentId);
146         if (expectationList == null) {
147             String msg = String.format("Intent id %s doesn't exist in database.", intentId);
148             log.error(msg);
149             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
150         }
151         if (expectationMapper.deleteIntentExpectationListByIntentId(intentId) < 1) {
152             String msg = "Delete expectation in database failed.";
153             log.error(msg);
154             throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
155         }
156     }
157 }