d3bfd340fe5fbb9af23a90de2254fc0deafa0ec7
[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.onap.usecaseui.intentanalysis.common.ResponseConsts;
25 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.stereotype.Service;
28 import org.onap.usecaseui.intentanalysis.bean.enums.ContextParentType;
29 import org.onap.usecaseui.intentanalysis.bean.models.ExpectationObject;
30 import org.onap.usecaseui.intentanalysis.bean.models.Context;
31 import org.onap.usecaseui.intentanalysis.mapper.ExpectationObjectMapper;
32 import org.onap.usecaseui.intentanalysis.service.ContextService;
33 import org.onap.usecaseui.intentanalysis.service.ExpectationObjectService;
34 import org.springframework.util.CollectionUtils;
35
36
37 @Service
38 @Slf4j
39 public class ExpectationObjectServiceImpl implements ExpectationObjectService {
40
41     private ContextParentType contextParentType;
42
43     @Autowired
44     private ExpectationObjectMapper expectationObjectMapper;
45
46     @Autowired
47     private ExpectationObjectService expectationObjectService;
48
49     @Autowired
50     private ContextService contextService;
51
52     @Override
53     public void createExpectationObject(ExpectationObject expectationObject, String expectationId) {
54         contextService.createContextList(expectationObject.getObjectContexts(),
55                 expectationObjectMapper.selectExpectationObjectId(expectationId));
56         if (expectationObjectMapper.insertExpectationObject(expectationObject, expectationId) < 1) {
57             String msg = "Failed to create expectation object to database.";
58             log.error(msg);
59             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
60         }
61         log.info("Successfully created expectation object to database.");
62     }
63
64     @Override
65     public ExpectationObject getExpectationObject(String expectationId) {
66         ExpectationObject expectationObject = expectationObjectMapper.selectExpectationObject(expectationId);
67         String expectationObjectId = expectationObjectMapper.selectExpectationObjectId(expectationId);
68         List<Context> contextList = contextService.getContextList(expectationObjectId);
69         if (!CollectionUtils.isEmpty(contextList)) {
70             expectationObject.setObjectContexts(contextService.getContextList(expectationObjectId));
71         } else {
72             log.info(String.format("Expectation object is null, expectationObjectId = %s", expectationObjectId));
73         }
74         return expectationObject;
75     }
76
77     @Override
78     public void updateExpectationObject(ExpectationObject expectationObject, String expectationId) {
79         ExpectationObject expectationObjectFromDB = expectationObjectService.getExpectationObject(expectationId);
80         if (expectationObject == null && expectationObjectFromDB != null) {
81             expectationObjectService.deleteExpectationObject(expectationId);
82         } else if (expectationObject != null && expectationObjectFromDB == null) {
83             expectationObjectService.createExpectationObject(expectationObject, expectationId);
84         } else if (expectationObject != null) {
85             contextService.updateContextList(expectationObject.getObjectContexts(),
86                     expectationObjectMapper.selectExpectationObjectId(expectationId));
87             if (expectationObjectMapper.updateExpectationObject(expectationObject, expectationId) < 1) {
88                 String msg = "Failed to update expectation object to database.";
89                 log.error(msg);
90                 throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
91             }
92             log.info("Successfully updated expectation object to database.");
93         }
94
95     }
96
97     @Override
98     public void deleteExpectationObject(String expectationId) {
99         ExpectationObject expectationObject = expectationObjectService.getExpectationObject(expectationId);
100         if (expectationObject != null) {
101             contextService.deleteContextList(expectationObjectMapper.selectExpectationObjectId(expectationId));
102             if (expectationObjectMapper.deleteExpectationObject(expectationId) < 1) {
103                 String msg = "Failed to update expectation object to database.";
104                 log.error(msg);
105                 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
106             }
107             log.info("Successfully deleted expectation object to database.");
108         }
109     }
110 }