d5db0ca1024aa92acfd66639859d5b31185cd09c
[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.enums.ContextParentType;
27 import org.onap.usecaseui.intentanalysis.bean.models.Expectation;
28 import org.onap.usecaseui.intentanalysis.bean.models.ExpectationTarget;
29 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
30 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
31 import org.onap.usecaseui.intentanalysis.mapper.ExpectationMapper;
32
33
34 @Service
35 @Slf4j
36 public class ExpectationServiceImpl implements ExpectationService {
37
38     @Autowired
39     private ExpectationMapper expectationMapper;
40
41     @Autowired
42     private ExpectationService expectationService;
43
44     @Autowired
45     private ExpectationObjectService expectationObjectService;
46
47     @Autowired
48     private ExpectationTargetService expectationTargetService;
49
50     @Autowired
51     private ContextService contextService;
52
53
54     @Autowired
55     private FulfilmentInfoService fulfilmentInfoService;
56
57     private ContextParentType contextParentType;
58
59     @Override
60     public void createIntentExpectations(List<Expectation> intentExpectations, String intentId) {
61         for (Expectation expectation : intentExpectations) {
62             if (null != expectation) {
63                 expectationObjectService.createObject(expectation.getExpectationObject(),
64                                                       expectation.getExpectationId());
65                 expectationTargetService.createTargets(expectation.getExpectationTargets(),
66                                                        expectation.getExpectationId());
67                 contextService.createContextList(expectation.getExpectationContexts(),
68                                                  contextParentType.EXPECTATION,
69                                                  expectation.getExpectationId());
70                 fulfilmentInfoService.createFulfilmentInfo(expectation.getExpectationFulfilmentInfo(),
71                                                            expectation.getExpectationId());
72             }
73         }
74         int res = expectationMapper.insertIntentExpectations(intentExpectations, intentId);
75         if (res < 1) {
76             String msg = "Create expectation to database failed.";
77             log.error(msg);
78             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
79         }
80     }
81
82     @Override
83     public void insertIntentExpectation(Expectation expectation, String intentId) {
84         int res = expectationMapper.insertIntentExpectation(expectation, intentId);
85         if (res < 1) {
86             String msg = "Create expectation to database failed.";
87             log.error(msg);
88             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
89         }
90     }
91
92     @Override
93     public List<Expectation> getIntentExpectationsByIntentId(String intentId) {
94         List<Expectation> expectationList = expectationMapper.selectIntentExpectationsByIntentId(intentId);
95         if (expectationList == null) {
96             String msg = String.format("Intent id %s doesn't exist in database.", intentId);
97             log.error(msg);
98             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
99         }
100         int res = expectationMapper.deleteIntentExpectationsByIntentId(intentId);
101         if (res < 1) {
102             String msg = "Delete expectation in database failed.";
103             log.error(msg);
104             throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
105         }
106         return expectationList;
107     }
108
109     @Override
110     public void updateIntentExpectationsByIntentId(List<Expectation> intentExpectations, String intentId) {
111         List<Expectation> expectationDBList = expectationMapper.selectIntentExpectationsByIntentId(intentId);
112         if (expectationDBList == null) {
113             String msg = String.format("Intent id %s doesn't exist in database.", intentId);
114             log.error(msg);
115             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
116         }
117         List<String> expectationDBIdList = new ArrayList<>();
118         for (Expectation expectationDB : expectationDBList) {
119             expectationDBIdList.add(expectationDB.getExpectationId());
120         }
121
122         for (Expectation expectation : intentExpectations) {
123             if (expectationDBIdList.contains(expectation.getExpectationId())) {
124                 int res = expectationMapper.updateIntentExpectation(expectation);
125                 if (res < 1) {
126                     String msg = "Update expectation in database failed.";
127                     log.error(msg);
128                     throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
129                 }
130                 expectationDBIdList.remove(expectation.getExpectationId());
131             } else {
132                 expectationService.insertIntentExpectation(expectation, intentId);
133             }
134         }
135         for (String expectationDBId : expectationDBIdList) {
136             expectationService.deleteIntentExpectationById(expectationDBId);
137         }
138         log.info("Expectations are successfully updated.");
139     }
140
141     @Override
142     public void deleteIntentExpectationById(String expectationId) {
143         int res = expectationMapper.deleteIntentExpectationById(expectationId);
144         if (res < 1) {
145             String msg = "Delete expectation in database failed.";
146             log.error(msg);
147             throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
148         }
149     }
150
151     @Override
152     public void deleteIntentExpectationsByIntentId(String intentId) {
153         List<Expectation> expectationList = expectationMapper.selectIntentExpectationsByIntentId(intentId);
154         if (expectationList == null) {
155             String msg = String.format("Intent id %s doesn't exist in database.", intentId);
156             log.error(msg);
157             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
158         }
159         int res = expectationMapper.deleteIntentExpectationsByIntentId(intentId);
160         if (res < 1) {
161             String msg = "Delete expectation in database failed.";
162             log.error(msg);
163             throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
164         }
165     }
166 }