2e7db097de420ba2b31a199eaaf6d11c36a71ada
[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         if (expectationObjectService.getExpectationObject(expectationId) != null) {
55             String msg = String.format("It already exists an object for the expectation %s, update might work.", expectationId);
56             log.error(msg);
57             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
58         }
59         contextService.createContextList(expectationObject.getObjectContexts(),
60                 expectationObjectMapper.selectExpectationObjectId(expectationId));
61         if (expectationObjectMapper.insertExpectationObject(expectationObject, expectationId) < 1) {
62             String msg = "Failed to create expectation object to database.";
63             log.error(msg);
64             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
65         }
66         log.info("Successfully created expectation object to database.");
67     }
68
69     @Override
70     public ExpectationObject getExpectationObject(String expectationId) {
71         ExpectationObject expectationObject = expectationObjectMapper.selectExpectationObject(expectationId);
72         String expectationObjectId = expectationObjectMapper.selectExpectationObjectId(expectationId);
73         List<Context> contextList = contextService.getContextList(expectationObjectId);
74         if (!CollectionUtils.isEmpty(contextList)) {
75             expectationObject.setObjectContexts(contextService.getContextList(expectationObjectId));
76         } else {
77             log.info(String.format("Expectation object is null, expectationObjectId = %s", expectationObjectId));
78         }
79         return expectationObject;
80     }
81
82     @Override
83     public void updateExpectationObject(ExpectationObject expectationObject, String expectationId) {
84         ExpectationObject expectationObjectFromDB = expectationObjectService.getExpectationObject(expectationId);
85         if (expectationObject == null && expectationObjectFromDB != null) {
86             expectationObjectService.deleteExpectationObject(expectationId);
87         } else if (expectationObject != null && expectationObjectFromDB == null) {
88             expectationObjectService.createExpectationObject(expectationObject, expectationId);
89         } else if (expectationObject != null) {
90             contextService.updateContextList(expectationObject.getObjectContexts(),
91                     expectationObjectMapper.selectExpectationObjectId(expectationId));
92             if (expectationObjectMapper.updateExpectationObject(expectationObject, expectationId) < 1) {
93                 String msg = "Failed to update expectation object to database.";
94                 log.error(msg);
95                 throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL);
96             }
97             log.info("Successfully updated expectation object to database.");
98         }
99
100     }
101
102     @Override
103     public void deleteExpectationObject(String expectationId) {
104         ExpectationObject expectationObject = expectationObjectService.getExpectationObject(expectationId);
105         if (expectationObject != null) {
106             contextService.deleteContextList(expectationObjectMapper.selectExpectationObjectId(expectationId));
107             if (expectationObjectMapper.deleteExpectationObject(expectationId) < 1) {
108                 String msg = "Failed to update expectation object to database.";
109                 log.error(msg);
110                 throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL);
111             }
112             log.info("Successfully deleted expectation object to database.");
113         }
114     }
115 }