7052ed3b1d9aa321417b3eafda1f9b94753899bd
[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
23 import lombok.extern.slf4j.Slf4j;
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.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 import org.springframework.util.CollectionUtils;
37
38
39 @Service
40 @Slf4j
41 public class ExpectationServiceImpl implements ExpectationService {
42
43     @Autowired
44     private ExpectationMapper expectationMapper;
45
46     @Autowired
47     private ExpectationService expectationService;
48
49     @Autowired
50     private ExpectationObjectService expectationObjectService;
51
52     @Autowired
53     private ExpectationTargetService expectationTargetService;
54
55     @Autowired
56     private ContextService contextService;
57
58     @Autowired
59     private FulfilmentInfoService fulfilmentInfoService;
60
61     private ContextParentType contextParentType;
62
63     @Override
64     public void createIntentExpectationList(List<Expectation> intentExpectationList, String intentId) {
65         for (Expectation expectation : intentExpectationList) {
66             if (expectation != null) {
67                 String expectationId = expectation.getExpectationId();
68                 expectationObjectService.createExpectationObject(expectation.getExpectationObject(),
69                         expectationId);
70                 expectationTargetService.createExpectationTargetList(expectation.getExpectationTargets(),
71                         expectationId);
72                 contextService.createContextList(expectation.getExpectationContexts(), expectationId);
73                 fulfilmentInfoService.createFulfilmentInfo(expectation.getExpectationFulfilmentInfo(),
74                         expectationId);
75             }
76         }
77         if (expectationMapper.insertIntentExpectationList(intentExpectationList, intentId) < 1) {
78             String msg = "Failed to create expectation list to database.";
79             log.error(msg);
80             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
81         }
82         log.info("Successfully created expectation list to database.");
83     }
84
85     @Override
86     public void createIntentExpectation(Expectation expectation, String intentId) {
87         expectationObjectService.createExpectationObject(expectation.getExpectationObject(),
88                 expectation.getExpectationId());
89         expectationTargetService.createExpectationTargetList(expectation.getExpectationTargets(),
90                 expectation.getExpectationId());
91         contextService.createContextList(expectation.getExpectationContexts(),
92                 expectation.getExpectationId());
93         fulfilmentInfoService.createFulfilmentInfo(expectation.getExpectationFulfilmentInfo(),
94                 expectation.getExpectationId());
95
96         if (expectationMapper.insertIntentExpectation(expectation, intentId) < 1) {
97             String msg = "Failed to create expectation to database.";
98             log.error(msg);
99             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
100         }
101         log.info("Successfully created expectation to database.");
102     }
103
104     @Override
105     public List<Expectation> getIntentExpectationList(String intentId) {
106         List<Expectation> expectationList = expectationMapper.selectIntentExpectationList(intentId);
107         if (!CollectionUtils.isEmpty(expectationList)) {
108             for (Expectation expectation : expectationList) {
109                 String expectationId = expectation.getExpectationId();
110                 expectation.setExpectationObject(expectationObjectService.getExpectationObject(expectationId));
111                 expectation.setExpectationTargets(expectationTargetService.getExpectationTargetList(expectationId));
112                 expectation.setExpectationContexts(contextService.getContextList(expectationId));
113                 expectation.setExpectationFulfilmentInfo(fulfilmentInfoService.getFulfilmentInfo(expectationId));
114             }
115         } else {
116             log.info(String.format("Expectation list is null, intentId = %s", intentId));
117         }
118         return expectationList;
119     }
120
121     @Override
122     public Expectation getIntentExpectation(String expectationId) {
123         Expectation expectation = expectationMapper.selectIntentExpectation(expectationId);
124         if (expectation != null) {
125             expectation.setExpectationObject(expectationObjectService.getExpectationObject(expectationId));
126             expectation.setExpectationTargets(expectationTargetService.getExpectationTargetList(expectationId));
127             expectation.setExpectationContexts(contextService.getContextList(expectationId));
128             expectation.setExpectationFulfilmentInfo(fulfilmentInfoService.getFulfilmentInfo(expectationId));
129         } else {
130             log.info(String.format("Expectation is null, expectationId = %s", expectationId));
131         }
132         return expectation;
133     }
134
135     @Override
136     public void updateIntentExpectationList(List<Expectation> intentExpectationList, String intentId) {
137         List<Expectation> expectationListFromDB = expectationService.getIntentExpectationList(intentId);
138         if (CollectionUtils.isEmpty(expectationListFromDB) && !CollectionUtils.isEmpty(intentExpectationList)) {
139             expectationService.createIntentExpectationList(intentExpectationList, intentId);
140         } else if (!CollectionUtils.isEmpty(expectationListFromDB) && CollectionUtils.isEmpty(intentExpectationList)) {
141             expectationService.deleteIntentExpectationList(intentId);
142         } else if (!CollectionUtils.isEmpty(expectationListFromDB) && !CollectionUtils.isEmpty(intentExpectationList)) {
143             List<String> expectationIdListFromDB = new ArrayList<>();
144             for (Expectation expectationDB : expectationListFromDB) {
145                 expectationIdListFromDB.add(expectationDB.getExpectationId());
146             }
147
148             for (Expectation expectation : intentExpectationList) {
149                 if (expectationIdListFromDB.contains(expectation.getExpectationId())) {
150                     expectationObjectService.updateExpectationObject(expectation.getExpectationObject(), expectation.getExpectationId());
151                     expectationTargetService.updateExpectationTargetList(expectation.getExpectationTargets(), expectation.getExpectationId());
152                     contextService.updateContextList(expectation.getExpectationContexts(), expectation.getExpectationId());
153                     fulfilmentInfoService.updateFulfilmentInfo(expectation.getExpectationFulfilmentInfo(), expectation.getExpectationId());
154                     if (expectationMapper.updateIntentExpectation(expectation) < 1) {
155                         String msg = "Failed to update expectation to database.";
156                         log.error(msg);
157                         throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
158                     }
159                     expectationIdListFromDB.remove(expectation.getExpectationId());
160                 } else {
161                     expectationService.createIntentExpectation(expectation, intentId);
162                 }
163             }
164             for (String expectationIdFromDB : expectationIdListFromDB) {
165                 expectationService.deleteIntentExpectation(expectationIdFromDB);
166             }
167             log.info("Successfully updated expectation list to database.");
168         }
169
170     }
171
172     @Override
173     public void deleteIntentExpectation(String expectationId) {
174         Expectation expectation = expectationService.getIntentExpectation(expectationId);
175         if (expectation != null) {
176             expectationObjectService.deleteExpectationObject(expectationId);
177             expectationTargetService.deleteExpectationTargetList(expectationId);
178             contextService.deleteContextList(expectationId);
179             fulfilmentInfoService.deleteFulfilmentInfo(expectationId);
180             if (expectationMapper.deleteIntentExpectation(expectationId) < 1) {
181                 String msg = "Failed to delete expectation to database.";
182                 log.error(msg);
183                 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
184             }
185             log.info("Successfully deleted expectation to database.");
186         }
187
188     }
189
190     @Override
191     public void deleteIntentExpectationList(String intentId) {
192         List<Expectation> expectationList = expectationService.getIntentExpectationList(intentId);
193         if (!CollectionUtils.isEmpty(expectationList)) {
194             for (Expectation expectation : expectationList) {
195                 String expectationId = expectation.getExpectationId();
196                 expectationObjectService.deleteExpectationObject(expectationId);
197                 expectationTargetService.deleteExpectationTargetList(expectationId);
198                 contextService.deleteContextList(expectationId);
199                 fulfilmentInfoService.deleteFulfilmentInfo(expectationId);
200             }
201             if (expectationMapper.deleteIntentExpectationList(intentId) < 1) {
202                 String msg = "Failed to delete expectation list to database.";
203                 log.error(msg);
204                 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
205             }
206             log.info("Successfully deleted expectation list to database.");
207         }
208     }
209 }