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