1d60ca5647be8d4d37744cc24e5fb27d282b4197
[usecase-ui/intent-analysis.git] /
1 /*
2  * Copyright (C) 2023 CMCC, Inc. and others. All rights reserved.
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 import lombok.extern.slf4j.Slf4j;
20 import org.onap.usecaseui.intentanalysis.bean.models.FulfillmentInfo;
21 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
22 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
23 import org.onap.usecaseui.intentanalysis.mapper.ObjectInstanceMapper;
24 import org.onap.usecaseui.intentanalysis.service.ObjectInstanceService;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.stereotype.Service;
27 import org.springframework.util.CollectionUtils;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 @Slf4j
33 @Service
34 public class ObjectInstanceServiceImpl implements ObjectInstanceService {
35     @Autowired
36     private ObjectInstanceMapper objectInstanceMapper;
37
38     @Override
39     public void saveObjectInstances(String intentId, FulfillmentInfo eventModel) {
40         List<String> instances = new ArrayList<>(eventModel.getObjectInstances());
41         List<String> objectInstancesDb = objectInstanceMapper.getObjectInstances(intentId);
42         if (!CollectionUtils.isEmpty(objectInstancesDb)) {
43             instances.removeAll(objectInstancesDb);
44             if (CollectionUtils.isEmpty(instances)) {
45                 log.info("The objectInstances already exist in the database");
46                 return;
47             }
48         }
49         int objectInstanceNum = objectInstanceMapper.insertObjectInstanceList(instances, intentId);
50         if (objectInstanceNum < 1) {
51             String msg = "Failed to insert objectInstances to database.";
52             log.error(msg);
53             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
54         }
55     }
56 }